Class: Marty::User

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

Constant Summary collapse

MARTY_IMPORT_UNIQUENESS =
[:login]

Constants inherited from Base

Base::COUNT_SIG, Base::DISTINCT_SIG, Base::FIRST_SIG, Base::GROUP_SIG, Base::JOINS_SIG, Base::LAST_SIG, Base::LIMIT_SIG, Base::MCFLY_PT_SIG, Base::NOT_SIG, Base::ORDER_SIG, Base::PLUCK_SIG, Base::SELECT_SIG, Base::WHERE_SIG

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

mcfly_pt

Methods inherited from ActiveRecord::Base

joins, old_joins

Class Method Details

.authenticate_with?(login, password) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/marty/user.rb', line 56

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"
    # 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,
                         })
    ok = ldap.bind
  else
    raise "bad auth_source: #{auth_source.inspect}"
  end

  () if ok
end

.currentObject



89
90
91
# File 'app/models/marty/user.rb', line 89

def self.current
  Mcfly.whodunnit
end

.current=(user) ⇒ Object



85
86
87
# File 'app/models/marty/user.rb', line 85

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

.has_role(role) ⇒ Object



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

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

.try_to_autologin(key) ⇒ Object

Returns the user who matches the given autologin key or nil



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/marty/user.rb', line 27

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/marty/user.rb', line 42

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



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

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

#to_sObject



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

def to_s
  name
end