Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::Validations, ErpTechSvcs::Utils::CompassAccessNegotiator
Defined in:
app/models/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErpTechSvcs::Utils::CompassAccessNegotiator

#has_capability?, #with_capability

Instance Attribute Details

#password_validatorObject

Returns the value of attribute password_validator.



5
6
7
# File 'app/models/user.rb', line 5

def password_validator
  @password_validator
end

#skip_activation_emailObject

Returns the value of attribute skip_activation_email.



5
6
7
# File 'app/models/user.rb', line 5

def skip_activation_email
  @skip_activation_email
end

Instance Method Details

#add_instance_attribute(k, v) ⇒ Object



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

def add_instance_attribute(k,v)
  @instance_attrs = {} if @instance_attrs.nil?
  @instance_attrs[k] = v
end

#add_role(role) ⇒ Object



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

def add_role(role)
  party.add_role(role)
end

#add_roles(*passed_roles) ⇒ Object



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

def add_roles(*passed_roles)
  party.add_roles(*passed_roles)
end

#all_capabilitiesObject



147
148
149
150
151
152
153
154
155
# File 'app/models/user.rb', line 147

def all_capabilities
  Capability.joins(:capability_type).joins(:capability_accessors).
        where("(capability_accessors.capability_accessor_record_type = 'Group' AND
                capability_accessor_record_id IN (#{groups.select('groups.id').to_sql})) OR
               (capability_accessors.capability_accessor_record_type = 'SecurityRole' AND
                capability_accessor_record_id IN (#{all_roles.select('security_roles.id').to_sql})) OR
               (capability_accessors.capability_accessor_record_type = 'User' AND
                capability_accessor_record_id = #{self.id})")
end

#all_class_capabilitiesObject



171
172
173
174
# File 'app/models/user.rb', line 171

def all_class_capabilities
  scope_type = ScopeType.find_by_internal_identifier('class')
  all_capabilities.where(:scope_type_id => scope_type.id)
end

#all_rolesObject

composite roles for this user



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

def all_roles
  SecurityRole.joins(:parties).joins("LEFT JOIN users ON parties.id=users.party_id").
    where("(parties.business_party_type='Group' AND 
            parties.business_party_id IN (#{groups.select('groups.id').to_sql})) OR 
           (users.id=#{self.id})")
end

#all_uniq_capabilitiesObject



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

def all_uniq_capabilities
  all_capabilities.all.uniq
end

#all_uniq_class_capabilitiesObject



176
177
178
# File 'app/models/user.rb', line 176

def all_uniq_class_capabilities
  all_class_capabilities.all.uniq
end

#all_uniq_rolesObject



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

def all_uniq_roles
  all_roles.all.uniq
end

#class_capabilities_to_hashObject



180
181
182
183
184
185
186
# File 'app/models/user.rb', line 180

def class_capabilities_to_hash
  all_uniq_class_capabilities.map {|capability| 
    { :capability_type_iid => capability.capability_type.internal_identifier, 
      :capability_resource_type => capability.capability_resource_type 
    }
  }.compact
end

#email_cannot_match_username_of_other_userObject



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

def email_cannot_match_username_of_other_user
  unless User.where(:username => self.email).where('id != ?',self.id).first.nil?
    errors.add(:email, "In use by another user")
  end
end

#group_capabilitiesObject



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

def group_capabilities
  Capability.joins(:capability_type).joins(:capability_accessors).
        where(:capability_accessors => { :capability_accessor_record_type => "Group" }).
        where("capability_accessor_record_id IN (#{groups.select('groups.id').to_sql})")
end

#group_class_capabilitiesObject



161
162
163
164
# File 'app/models/user.rb', line 161

def group_class_capabilities
  scope_type = ScopeType.find_by_internal_identifier('class')
  group_capabilities.where(:scope_type_id => scope_type.id)
end

#group_partiesObject

party records for the groups this user belongs to



102
103
104
# File 'app/models/user.rb', line 102

def group_parties
  Party.joins("JOIN #{join_party_relationships}")
end

#group_relationshipsObject

user lives on FROM side of relationship



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

def group_relationships
  role_type = RoleType.find_by_internal_identifier('group_member')
  PartyRelationship.where(:party_id_from => self.party.id, :role_type_id_from => role_type.id)
end

#group_rolesObject

roles assigned to the groups this user belongs to



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

def group_roles
  SecurityRole.joins(:parties).
    where(:parties => {:business_party_type => 'Group'}).
    where("parties.business_party_id IN (#{groups.select('groups.id').to_sql})")
end

#groupsObject

groups this user belongs to



107
108
109
# File 'app/models/user.rb', line 107

def groups
  Group.joins(:party).joins("JOIN #{join_party_relationships}")
end

#groups_notObject

groups this user does NOT belong to



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

def groups_not
  Group.joins(:party).joins("LEFT JOIN #{join_party_relationships}").where("party_relationships.id IS NULL")
end

#has_role?(*passed_roles) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/user.rb', line 56

def has_role?(*passed_roles)
  result = false
  passed_roles.flatten!
  passed_roles.each do |role|
    role_iid = role.is_a?(SecurityRole) ?  role.internal_identifier : role.to_s
    all_uniq_roles.each do |this_role|
      result = true if (this_role.internal_identifier == role_iid)
      break if result
    end
    break if result
  end
  result
end

#instance_attributesObject

these two methods allow us to assign instance level attributes that are not persisted. These are used for mailers



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

def instance_attributes
  @instance_attrs.nil? ? {} : @instance_attrs
end

#join_party_relationshipsObject



96
97
98
99
# File 'app/models/user.rb', line 96

def join_party_relationships
  role_type = RoleType.find_by_internal_identifier('group_member')
  "party_relationships ON party_id_from = #{self.party.id} AND party_id_to = parties.id AND role_type_id_from=#{role_type.id}"
end

#remove_all_rolesObject



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

def remove_all_roles
  party.remove_all_roles
end

#remove_role(role) ⇒ Object



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

def remove_role(role)
  party.remove_role(role)
end

#remove_roles(*passed_roles) ⇒ Object



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

def remove_roles(*passed_roles)
  party.remove_roles(*passed_roles)
end

#role_capabilitiesObject



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

def role_capabilities
  Capability.joins(:capability_type).joins(:capability_accessors).
        where(:capability_accessors => { :capability_accessor_record_type => "SecurityRole" }).
        where("capability_accessor_record_id IN (#{all_roles.select('security_roles.id').to_sql})")
end

#role_class_capabilitiesObject



166
167
168
169
# File 'app/models/user.rb', line 166

def role_class_capabilities
  scope_type = ScopeType.find_by_internal_identifier('class')
  role_capabilities.where(:scope_type_id => scope_type.id)
end

#rolesObject

roles this user has



52
53
54
# File 'app/models/user.rb', line 52

def roles
  party.security_roles
end

#roles_notObject

roles this user does NOT have



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

def roles_not
  party.roles_not
end

#send_activation_needed_email!Object

This allows the disabling of the activation email sent via the sorcery user_activation submodule



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

def send_activation_needed_email!
  super unless skip_activation_email
end