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
63
64
65
66
67
68
69
70
71
72
|
# File 'app/services/lesli_guard/descriptor_privilege_service.rb', line 4
def index params
privileges_pivot = Lesli::SystemController.joins(:actions)
.where("lesli_system_controller_actions.deleted_at IS NULL")
.group(:system_controller_id)
.select(
:system_controller_id,
"MAX(CASE WHEN lesli_system_controller_actions.name = 'list' THEN lesli_system_controller_actions.id end) AS list_action",
"MAX(CASE WHEN lesli_system_controller_actions.name = 'index' THEN lesli_system_controller_actions.id end) AS index_action",
"MAX(CASE WHEN lesli_system_controller_actions.name = 'show' THEN lesli_system_controller_actions.id end) AS show_action",
"MAX(CASE WHEN lesli_system_controller_actions.name = 'create' THEN lesli_system_controller_actions.id end) AS create_action",
"MAX(CASE WHEN lesli_system_controller_actions.name = 'update' THEN lesli_system_controller_actions.id end) AS update_action",
"MAX(CASE WHEN lesli_system_controller_actions.name = 'destroy' THEN lesli_system_controller_actions.id end) AS destroy_action"
)
Lesli::SystemController.joins("INNER JOIN (#{privileges_pivot.to_sql}) privileges ON privileges.system_controller_id = lesli_system_controllers.id")
.joins("left join lesli_descriptor_privileges dp_list on dp_list.descriptor_id = #{params[:descriptor_id]} and dp_list.action_id = privileges.list_action")
.joins("left join lesli_descriptor_privileges dp_index on dp_index.descriptor_id = #{params[:descriptor_id]} and dp_index.action_id = privileges.index_action")
.joins("left join lesli_descriptor_privileges dp_show on dp_show.descriptor_id = #{params[:descriptor_id]} and dp_show.action_id = privileges.show_action")
.joins("left join lesli_descriptor_privileges dp_create on dp_create.descriptor_id = #{params[:descriptor_id]} and dp_create.action_id = privileges.create_action")
.joins("left join lesli_descriptor_privileges dp_update on dp_update.descriptor_id = #{params[:descriptor_id]} and dp_update.action_id = privileges.update_action")
.joins("left join lesli_descriptor_privileges dp_destroy on dp_destroy.descriptor_id = #{params[:descriptor_id]} and dp_destroy.action_id = privileges.destroy_action")
.order(:name)
.select(
"name as controller",
"privileges.list_action",
"case when privileges.list_action is not null then case when dp_list.action_id is null then false else true end end as list_active",
"privileges.index_action",
"case when privileges.index_action is not null then case when dp_index.action_id is null or dp_index.deleted_at is not null then false else true end end as index_active",
"privileges.show_action",
"case when privileges.show_action is not null then case when dp_show.action_id is null or dp_show.deleted_at is not null then false else true end end as show_active",
"privileges.create_action",
"case when privileges.create_action is not null then case when dp_create.action_id is null or dp_create.deleted_at is not null then false else true end end as create_active",
"privileges.update_action",
"case when privileges.update_action is not null then case when dp_update.action_id is null or dp_update.deleted_at is not null then false else true end end as update_active",
"privileges.destroy_action",
"case when privileges.destroy_action is not null then case when dp_destroy.action_id is null or dp_update.deleted_at is not null then false else true end end as destroy_active",
).map do |privilege|
privilege.attributes.compact
end
end
|