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)


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

def active?
  !inactive?
end

#can_impersonate?Boolean

Returns:

  • (Boolean)


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

def can_impersonate?
  superuser?
end

#current_impersonationObject



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

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



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

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

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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)


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

def impersonating?
  current_impersonation.present?
end

#impersonation_targetObject



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

def impersonation_target
  current_impersonation&.target
end

#inactive!(_inactive = true) ⇒ Object



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

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

#inactive?Boolean

Returns:

  • (Boolean)


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

def inactive?
  inactive_flag
end

#ldap_entryObject



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

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

#recent_impersonations(max = 25) ⇒ Object



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

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



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

def roles
  []
end

#stop_impersonation!Object



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

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

#superuser!(_superuser = true) ⇒ Object



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

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

#superuser?Boolean

Returns:

  • (Boolean)


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

def superuser?
  superuser_flag
end