Class: Marty::User

Inherits:
Base show all
Defined in:
app/models/marty/user.rb

Constant Summary collapse

MARTY_IMPORT_UNIQUENESS =
[:login]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

get_final_attrs, get_struct_attrs, make_hash, make_openstruct, mcfly_pt

Methods inherited from ActiveRecord::Base

joins, old_joins

Class Method Details

.authenticate_with?(login, password) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/marty/user.rb', line 76

def self.authenticate_with?(, password)
  cf = Rails.configuration.marty

  auth_source = cf.auth_source.to_s

  if auth_source == 'local'
    ok = password == cf.local_password
  elsif auth_source == 'ldap'
    ok = (, password)
  else
    raise "bad auth_source: #{auth_source.inspect}"
  end

  () if ok
end

.currentObject



96
97
98
# File 'app/models/marty/user.rb', line 96

def self.current
  Mcfly.whodunnit
end

.current=(user) ⇒ Object



92
93
94
# File 'app/models/marty/user.rb', line 92

def self.current=(user)
  Mcfly.whodunnit = user
end

.has_role(role) ⇒ Object



100
101
102
103
# File 'app/models/marty/user.rb', line 100

def self.has_role(role)
   mr = Mcfly.whodunnit.user_roles rescue []
   mr.any? { |ur| ur.role == role }
end

.ldap_login(login, password) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/marty/user.rb', line 59

def self.(, password)
  # IMPORTANT NOTE: if server allows anonymous LDAP access, empty
  # passwords will succeed!  i.e. if a valid user with empty
  # password is sent in, ldap.bind will return OK.
  cf = Rails.configuration.marty.ldap
  ldap = Net::LDAP.new(host: cf.host,
                       port: cf.port,
                       base: cf.base_dn,
                       encryption: cf.encryption,
                       auth: {
                         method: :simple,
                         username: cf.domain + '\\' + ,
                         password: password,
                       })
  ldap.bind
end

.try_to_autologin(key) ⇒ Object

Returns the user who matches the given autologin key or nil



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/marty/user.rb', line 30

def self.try_to_autologin(key)
  tokens = Marty::Token.find_all_by_action_and_value('autologin', key.to_s)
  # Make sure there's only 1 token that matches the key
  if tokens.size == 1
    token = tokens.first
    autologin = Rails.configuration.marty.autologin || 0

    if (token.created_on > autologin.to_i.day.ago) &&
        token.user && token.user.active?
      token.user
    end
  end
end

.try_to_login(login, password) ⇒ Object

Returns the user that matches provided login and password, or nil



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/marty/user.rb', line 45

def self.(, password)
   = .to_s
  password = password.to_s

  # Make sure no one can sign in with an empty password
  return nil if password.empty?

  user = ()

  return nil if !user || !user.active?

  authenticate_with?(, password) || nil
end

Instance Method Details

#nameObject



17
18
19
# File 'app/models/marty/user.rb', line 17

def name
  "#{firstname} #{lastname}"
end

#rolesObject



25
26
27
# File 'app/models/marty/user.rb', line 25

def roles
  user_roles.map(&:role)
end

#to_sObject



21
22
23
# File 'app/models/marty/user.rb', line 21

def to_s
  name
end