blob: 849fa71aa213c1d09eb820feb72e8775a05b1068 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
class Group < ActiveRecord::Base
attr_accessible :name, :active, :comment
has_many :group_memberships, :dependent => :destroy
has_many :group_permissions, :dependent => :destroy
has_many :permittances, :foreign_key => :target_group_id, :class_name => "GroupPermission", :dependent => :destroy
validates :name,
:presence => true
def to_s
self.name
end
def permission_targets(permission)
group_permissions.where(:permission => permission).pluck(:target_group_id)
end
def has_permission(target_type, target_id, permission)
target_group_ids = GroupMembership.where(:item_id => target_id, :item_type => target_type).pluck(:group_id)
return group_permissions.where(:permission => permission, :target_group_id => target_group_ids).first != nil
end
def self.union(sets=[])
group_ids = []
sets.each do |set|
group_ids = group_ids + set
end
return group_ids.uniq
end
def self.target_group_ids_by_permission(permission, *group_sets)
target_groups = []
group_sets.each do |groups|
target_groups = target_groups + Group.union(groups.collect{|g| g.permission_targets(permission)})
end
return target_groups.uniq
end
end
|