Class: EgovUtils::User

Inherits:
Principal show all
Defined in:
app/models/egov_utils/user.rb

Constant Summary collapse

DEFAULT_ROLE =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Principal

#auth_source, #ldap?, #ldap_domain, #organization_by_domain, #organization_id, #organization_id_by_key, #organization_key, #organization_with_suborganizations_ids, #organization_with_suborganizations_keys, #reload

Class Method Details

.anonymousObject



79
80
81
# File 'app/models/egov_utils/user.rb', line 79

def self.anonymous
  self.new
end

.authenticate(login, password, active_only = true) ⇒ Object



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
69
70
71
72
73
74
75
76
77
# File 'app/models/egov_utils/user.rb', line 41

def self.authenticate(, password, active_only=true)
   = .to_s
  password = password.to_s

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

  # Fail over to case-insensitive if none was found
  user = find_by(login: ) || where( arel_table[:login].lower.eq(.downcase) ).first

  if user
    # user is already in local database
    return nil unless user.password_check?(password)
    return nil if active_only && !user.active?
  else
    # user is not yet registered, try to authenticate with available sources
    attrs = EgovUtils::AuthSource.authenticate(, password)
    if attrs
      user = new(attrs.merge(active: true))
      if user.ldap_register_allowed? && user.save
        user.reload
        logger.info("User '#{user.}' created from external auth source: #{user.provider}") if logger && user.auth_source
      end
    end
  end

  if user && !user.new_record?
    # Check last login
    if user.days_before_inactive && (user. || user.created_at) < (Time.now - user.days_before_inactive.days)
      user.update_column(:active, false)
    end

    user.update_column(:last_login_at, Time.now) if user.active?
  end

  user
end

.currentObject



87
88
89
# File 'app/models/egov_utils/user.rb', line 87

def self.current
  RequestLocals.fetch(:current_user) { User.anonymous }
end

.current=(user) ⇒ Object



83
84
85
# File 'app/models/egov_utils/user.rb', line 83

def self.current=(user)
  RequestLocals.store[:current_user] = user || anonymous
end

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/egov_utils/user.rb', line 127

def admin?
  has_role?('admin')
end

#all_role_namesObject



135
136
137
138
139
# File 'app/models/egov_utils/user.rb', line 135

def all_role_names
  @all_role_names ||= groups.map(&:roles).flatten + roles
  @all_role_names << DEFAULT_ROLE if DEFAULT_ROLE && !@all_role_names.any?
  @all_role_names
end

#all_rolesObject



141
142
143
# File 'app/models/egov_utils/user.rb', line 141

def all_roles
  all_role_names.map{|rn| EgovUtils::UserUtils::Role.find(rn) }.compact.collect{|cls| cls.new }
end

#fullnameObject



123
124
125
# File 'app/models/egov_utils/user.rb', line 123

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

#generate_reset_password_tokenObject



164
165
166
167
# File 'app/models/egov_utils/user.rb', line 164

def generate_reset_password_token
  self.confirmation_code = nil
  generate_confirmation_code
end

#has_role?(role_name) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/egov_utils/user.rb', line 131

def has_role?(role_name)
  all_role_names.include?(role_name)
end

#ldap_dnObject



145
146
147
148
149
150
# File 'app/models/egov_utils/user.rb', line 145

def ldap_dn
  @ldap_dn ||= begin
    dn = auth_source.send(:get_user_dn, )
    dn[:dn] if dn
  end
end

#ldap_groupsObject



152
153
154
# File 'app/models/egov_utils/user.rb', line 152

def ldap_groups
  groups.where.not(ldap_uid: nil)
end

#ldap_register_allowed?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/egov_utils/user.rb', line 99

def ldap_register_allowed?
  auth_source && auth_source.register_members_only? && ldap_groups.any?
end

#locked?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/egov_utils/user.rb', line 119

def locked?
  false
end

#logged?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/egov_utils/user.rb', line 115

def logged?
  persisted?
end

#must_change_password?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'app/models/egov_utils/user.rb', line 156

def must_change_password?
  (super || password_expired?) && !provider?
end

#password_change_possible?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/egov_utils/user.rb', line 111

def password_change_possible?
  !provider.present?
end

#password_check?(password) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
# File 'app/models/egov_utils/user.rb', line 103

def password_check?(password)
  if provider.present?
    auth_source.authenticate(, password)
  else
    authenticate(password)
  end
end

#password_expired?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'app/models/egov_utils/user.rb', line 160

def password_expired?
  false
end

#rolesObject



95
96
97
# File 'app/models/egov_utils/user.rb', line 95

def roles
  logged? ? super : ['anonymous']
end

#to_sObject



91
92
93
# File 'app/models/egov_utils/user.rb', line 91

def to_s
  fullname
end