Class: Group

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/group.rb

Overview

Security Group

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(description) ⇒ Object



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

def self.add(description)
  Group.create(:description => description)
end

Instance Method Details

#add_party(a_party) ⇒ Object

add party to group group lives on TO side of relationship



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/group.rb', line 110

def add_party(a_party)
  # check and see if party is already a member of this group
  rel = get_relationship(a_party).first
  unless rel.nil?
    # if so, return relationship
    return rel 
  else
    # if not then build party_relationship
    rt = RelationshipType.find_by_internal_identifier('group_membership')
    pr = PartyRelationship.new
    pr.description = rt.description
    pr.relationship_type = rt
    pr.from_role = RoleType.find_by_internal_identifier('group_member')
    pr.to_role = RoleType.find_by_internal_identifier('group')
    pr.from_party = a_party
    pr.to_party = self.party
    pr.save
    return pr
  end
end

#add_role(role) ⇒ Object



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

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

#add_user(user) ⇒ Object

add user to group



94
95
96
# File 'app/models/group.rb', line 94

def add_user(user)
  add_party(user.party)
end

#all_class_capabilitiesObject



150
151
152
153
154
155
156
157
158
# File 'app/models/group.rb', line 150

def all_class_capabilities
  scope_type = ScopeType.find_by_internal_identifier('class')
  Capability.joins(:capability_type).joins(:capability_accessors).
        where("(capability_accessors.capability_accessor_record_type = 'Group' AND
                capability_accessor_record_id = (#{self.id})) OR
               (capability_accessors.capability_accessor_record_type = 'SecurityRole' AND
                capability_accessor_record_id IN (#{roles.select('security_roles.id').to_sql}))").
        where(:scope_type_id => scope_type.id)
end

#all_uniq_class_capabilitiesObject



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

def all_uniq_class_capabilities
  all_class_capabilities.all.uniq
end

#class_capabilities_to_hashObject



164
165
166
167
168
169
170
# File 'app/models/group.rb', line 164

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

#create_partyObject



45
46
47
48
49
50
51
52
# File 'app/models/group.rb', line 45

def create_party
  pty = Party.new
  pty.description = self.description
  pty.business_party = self
  
  pty.save
  self.save
end

#destroy_partyObject



59
60
61
62
63
# File 'app/models/group.rb', line 59

def destroy_party
  if self.party
    self.party.destroy
  end
end

#destroy_party_relationshipsObject



65
66
67
# File 'app/models/group.rb', line 65

def destroy_party_relationships
  party_relationships.destroy_all
end

#get_relationship(a_party) ⇒ Object



103
104
105
106
# File 'app/models/group.rb', line 103

def get_relationship(a_party)
  role_type = RoleType.find_by_internal_identifier('group')
  PartyRelationship.where(:party_id_to => self.party.id, :party_id_from => a_party.id, :role_type_id_to => role_type.id)
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'app/models/group.rb', line 28

def has_role?(role)
  role = role.is_a?(SecurityRole) ? role : SecurityRole.find_by_internal_identifier(role.to_s)
  roles.include?(role)
end

#join_party_relationshipsObject



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

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

#membersObject



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

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

#party_relationshipsObject

group lives on TO side of relationship



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

def party_relationships
  PartyRelationship.where(:party_id_to => self.party.id)
end

#remove_all_rolesObject



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

def remove_all_roles
  party.remove_all_roles
end

#remove_party(a_party) ⇒ Object

remove party from group group lives on TO side of relationship



133
134
135
136
137
138
139
140
# File 'app/models/group.rb', line 133

def remove_party(a_party)
  begin
    get_relationship(a_party).first.destroy
  rescue Exception => e
    Rails.logger.error e.message
    return nil
  end
end

#remove_role(role) ⇒ Object



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

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

#remove_user(user) ⇒ Object

remove user from group



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

def remove_user(user)
  remove_party(user.party)
end

#role_class_capabilitiesObject



142
143
144
145
146
147
148
# File 'app/models/group.rb', line 142

def role_class_capabilities
  scope_type = ScopeType.find_by_internal_identifier('class')
  Capability.joins(:capability_type).joins(:capability_accessors).
        where(:capability_accessors => { :capability_accessor_record_type => "SecurityRole" }).
        where("capability_accessor_record_id IN (#{roles.select('security_roles.id').to_sql})").
        where(:scope_type_id => scope_type.id)
end

#rolesObject

roles this group has



24
25
26
# File 'app/models/group.rb', line 24

def roles
  party.security_roles
end

#roles_notObject

roles this group does NOT have



19
20
21
# File 'app/models/group.rb', line 19

def roles_not
  party.roles_not
end

#save_partyObject



54
55
56
57
# File 'app/models/group.rb', line 54

def save_party
  self.party.description = self.description
  self.party.save
end

#usersObject

get users in this group



84
85
86
# File 'app/models/group.rb', line 84

def users
  User.joins(:party).joins("JOIN #{join_party_relationships}")
end

#users_notObject

get users not in this group



89
90
91
# File 'app/models/group.rb', line 89

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