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)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/marty/user.rb', line 84

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

  find_by(login: ) if ok
end

.currentObject



104
105
106
# File 'app/models/marty/user.rb', line 104

def self.current
  Mcfly.whodunnit
end

.current=(user) ⇒ Object



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

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

.has_role(role) ⇒ Object



108
109
110
111
# File 'app/models/marty/user.rb', line 108

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

.ldap_login(login, password) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/marty/user.rb', line 67

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



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/marty/user.rb', line 38

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/marty/user.rb', line 53

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 = find_by(login: )

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

  authenticate_with?(, password) || nil
end

Instance Method Details

#nameObject



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

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

#rolesObject



33
34
35
# File 'app/models/marty/user.rb', line 33

def roles
  user_roles.map(&:role)
end

#to_sObject



29
30
31
# File 'app/models/marty/user.rb', line 29

def to_s
  name
end

#unread_web_notifications_countObject



125
126
127
128
129
130
# File 'app/models/marty/user.rb', line 125

def unread_web_notifications_count
  notification_deliveries.where(
    delivery_type: :web,
    state: [:sent]
  ).count
end