Module: UserConcerns

Extended by:
ActiveSupport::Concern
Included in:
User
Defined in:
app/models/concerns/user_concerns.rb

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/models/concerns/user_concerns.rb', line 17

def active?
  !inactive?
end

#can_impersonate?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/concerns/user_concerns.rb', line 46

def can_impersonate?
  superuser?
end

#current_impersonationObject



64
65
66
67
# File 'app/models/concerns/user_concerns.rb', line 64

def current_impersonation
  return @current_impersonation if defined?(@current_impersonation)
  @current_impersonation = UcbRailsUser::Impersonation.find_by(user_id: self.id, active: true)
end

#full_nameObject



41
42
43
44
# File 'app/models/concerns/user_concerns.rb', line 41

def full_name
  return nil unless first_name || last_name
  "#{first_name} #{last_name}".strip
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'app/models/concerns/user_concerns.rb', line 13

def has_role?(role)
  superuser? || roles.include?(role)
end

#impersonate!(target) ⇒ Object

target can be a User instance or a user id return true if Impersonation succeeded; false otherwise



52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/concerns/user_concerns.rb', line 52

def impersonate!(target)
  target_id =
    if target.respond_to?(:id)
      target.id
    else
      User.find_by(id: target)&.id
    end
  return false unless impersonation_is_valid?(target_id)
  @current_impersonation = create_impersonation(target_id)
  @current_impersonation.present?
end

#impersonating?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/concerns/user_concerns.rb', line 73

def impersonating?
  current_impersonation.present?
end

#impersonation_targetObject



69
70
71
# File 'app/models/concerns/user_concerns.rb', line 69

def impersonation_target
  current_impersonation&.target
end

#inactive!(_inactive = true) ⇒ Object



33
34
35
# File 'app/models/concerns/user_concerns.rb', line 33

def inactive!(_inactive=true)
  update_attribute(:inactive_flag, _inactive)
end

#inactive?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/models/concerns/user_concerns.rb', line 21

def inactive?
  inactive_flag
end

#ldap_entryObject



37
38
39
# File 'app/models/concerns/user_concerns.rb', line 37

def ldap_entry
  UcbRailsUser::LdapPerson::Finder.find_by_uid!(uid)
end

#recent_impersonations(max = 25) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'app/models/concerns/user_concerns.rb', line 82

def recent_impersonations(max=25)
  @recent_impersonations ||=
    impersonations
      .uniq(&:target_id)
      .reject(&:active)
      .sort_by(&:created_at)
      .reverse
      .take(max)
end

#rolesObject

Overridden by application



9
10
11
# File 'app/models/concerns/user_concerns.rb', line 9

def roles
  []
end

#stop_impersonation!Object



77
78
79
80
# File 'app/models/concerns/user_concerns.rb', line 77

def stop_impersonation!
  impersonations.update_all(active: false)
  @current_impersonation = nil
end

#superuser!(_superuser = true) ⇒ Object



25
26
27
# File 'app/models/concerns/user_concerns.rb', line 25

def superuser!(_superuser=true)
  update_attribute(:superuser_flag, _superuser)
end

#superuser?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/concerns/user_concerns.rb', line 29

def superuser?
  superuser_flag
end