Module: Median::Aleph::Authentication

Included in:
Median::Aleph
Defined in:
lib/median/aleph/authentication.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(username, password) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/median/aleph/authentication.rb', line 4

def authenticate(username, password)
  return false if username.blank? or password.blank?

  username.gsub!(/\s/, '')
  username.upcase!

  auth_info = post_url(Median.config.aleph_x_service_base_url,
    op: 'bor-auth',
    bor_id: username,
    verification: password,
    library: Median.config.aleph_user_library
  )
  puts auth_info

   = get_url(
    Median.config.aleph_x_service_base_url,
      op: 'bor-info',
      bor_id: username,
      verification: password,
      loans: 'N',
      cash: 'B',
      hold: 'N',
      library: Median.config.aleph_user_library
  )

  unless auth_info.at_xpath('/bor-auth/error') or .at_xpath('/bor-info/error')
    options = {}
    # user id in the ILS Alpeh
    options[:ilsid] = auth_info.at_xpath('//z303-id').try(:content)
    # name of the user
    name = auth_info.at_xpath('//z303-name').try(:content)
    options[:lastname], options[:firstname] = name.split(',').map(&:strip) if name.present?
    # email
    options[:email] = auth_info.at_xpath('//z304-email-address').try(:content)
    # map bor-status to roles
    options[:role]  = 'g' # guest by default
    status = auth_info.at_xpath('//z305-bor-status').try(:content)
    case status
    when /PS/ then options[:role] = 's'
    when /PA/ then options[:role] = 'a'
    when /PE/ then options[:role] = 'e'
    when /PZ/ then options[:role] = 'z'
    end
    # exiry date
    expiry_date = auth_info.at_xpath('//z305-expiry-date').try(:content)
    options[:expiry_date] = Date.strptime(expiry_date, '%d/%m/%Y') if expiry_date.present?
    # cash balance
    balance = .at_xpath('//balance').try(:content).to_f
    balance = balance * -1 if balance != 0 and .at_xpath('//sign').try(:content).try(:strip).try(:downcase) == 'd'
    options[:cash_balance] = balance

    # TODO: What about banned users?

    return options
  end

  false
end