Class: AuthenticationManager

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/authentication_manager.rb

Class Method Summary collapse

Class Method Details

.authenticate(username, password) ⇒ Object

Attempt to authenticate user' with the provided password’. Return a User object if successful, nil otherwise



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
62
63
64
65
66
67
68
# File 'backend/app/model/authentication_manager.rb', line 20

def self.authenticate(username, password)
  authentication_sources.each do |source|
    begin
      user = User.find(:username => username)

      # System users are only authenticated locally.
      next if (user && user.is_system_user == 1 && source != DBAuth)

      #ANW-97: check if user is inactive
      next if (user && user.is_active_user != 1)

      jsonmodel_user = source.authenticate(username, password)

      if !jsonmodel_user
        next
      end

      # Force their admin status based on what they already had
      jsonmodel_user.is_admin = (user && user.can?(:administer_system))

      if user
        begin
          user.update_from_json(jsonmodel_user,
                                :source => source.name,
                                :lock_version => user.lock_version)
        rescue Sequel::NoExistingObject => e
          # We'll swallow these because they only really mean that the user
          # logged in twice simultaneously.  As long as one of the updates
          # succeeded it doesn't really matter.
          user = User.find(:username => username)
        end
      else
        DB.attempt {
          user = User.create_from_json(jsonmodel_user, :source => source.name)
        }.and_if_constraint_fails {
          return authenticate(username, password)
        }
      end

      return user
    rescue
      Log.error("Error communicating with authentication source #{source.inspect}: #{$!}")
      Log.exception($!)
      next
    end
  end

  nil
end

.authentication_sourcesObject



13
14
15
# File 'backend/app/model/authentication_manager.rb', line 13

def self.authentication_sources
  prepare_sources(AppConfig[:authentication_sources]) + [DBAuth]
end

.matching_usernames(query) ⇒ Object



71
72
73
74
75
# File 'backend/app/model/authentication_manager.rb', line 71

def self.matching_usernames(query)
  authentication_sources.map {|source|
    source.matching_usernames(query)
  }.flatten(1).sort.uniq
end

.prepare_sources(sources) ⇒ Object



5
6
7
8
9
10
11
# File 'backend/app/model/authentication_manager.rb', line 5

def self.prepare_sources(sources)
  sources.map { |source|
    model = Kernel.const_get(source[:model].intern)

    model.new(source)
  }
end