/
organization_policy.rb
62 lines (49 loc) · 1.13 KB
/
organization_policy.rb
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Manage::OrganizationPolicy < ApplicationPolicy
def index?
user.user_permissions
.where(permission_slug: [:manage_organizations])
.present?
end
def show?
user_has_any_permissions?(
[:manage_organizations, nil],
[:manage_organizations, record]
)
end
def create?
user_has_any_permissions?(:manage_organizations)
end
def new?
create?
end
def update?
user_has_any_permissions?(
[:manage_organizations, nil],
[:manage_organizations, record]
)
end
def edit?
update?
end
def destroy?
destroyable? && user_has_any_permissions?(:manage_organizations)
end
private
def destroyable?
record.facility_groups.none?
end
class Scope < Scope
attr_reader :user, :scope
def initialize(user, scope)
super
@user = user
@scope = scope
end
def resolve
return scope.none unless user.has_permission?(:manage_organizations)
organization_ids = organization_ids_for_permission(:manage_organizations)
return scope.all if organization_ids.empty?
scope.where(id: organization_ids)
end
end
end