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



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

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



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

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



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

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

#add_user(user) ⇒ Object

add user to group



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

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

#all_class_capabilitiesObject



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

def all_class_capabilities
  scope_type = ScopeType.find_by_internal_identifier('class')
  Capability.includes(:capability_type).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



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

def all_uniq_class_capabilities
  all_class_capabilities.all.uniq
end

#class_capabilities_to_hashObject



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

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

#create_partyObject



52
53
54
55
56
57
58
59
# File 'app/models/group.rb', line 52

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

  pty.save
  self.save
end

#destroy_partyObject



66
67
68
69
70
# File 'app/models/group.rb', line 66

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

#destroy_party_relationshipsObject



72
73
74
# File 'app/models/group.rb', line 72

def destroy_party_relationships
  party_relationships.destroy_all
end

#get_relationship(a_party) ⇒ Object



105
106
107
108
# File 'app/models/group.rb', line 105

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)


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

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

#membersObject



81
82
83
# File 'app/models/group.rb', line 81

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

#party_relationshipsObject

group lives on TO side of relationship



77
78
79
# File 'app/models/group.rb', line 77

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

#remove_all_rolesObject



48
49
50
# File 'app/models/group.rb', line 48

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



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

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

#remove_role(role) ⇒ Object



44
45
46
# File 'app/models/group.rb', line 44

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

#remove_user(user) ⇒ Object

remove user from group



101
102
103
# File 'app/models/group.rb', line 101

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

#role_class_capabilitiesObject



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

def role_class_capabilities
  scope_type = ScopeType.find_by_internal_identifier('class')
  Capability.includes(:capability_type).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



31
32
33
# File 'app/models/group.rb', line 31

def roles
  party.security_roles
end

#roles_notObject

roles this group does NOT have



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

def roles_not
  party.roles_not
end

#save_partyObject



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

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

#to_data_hashObject



174
175
176
# File 'app/models/group.rb', line 174

def to_data_hash
  self.to_hash(only: [:id, :description, :created_at, :updated_at])
end

#usersObject

get users in this group



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

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

#users_notObject

get users not in this group



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

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