Method: LesliSecurity::RoleDescriptorService#privileges

Defined in:
app/services/lesli_security/role_descriptor_service.rb

#privileges(role) ⇒ Object



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
# File 'app/services/lesli_security/role_descriptor_service.rb', line 29

def privileges role

    # Inner join the role power table with the descriptors
    # so we get only the descriptors that are assigned to the specific role
    sanitized_role_power_join = ActiveRecord::Base.sanitize_sql([%(
        inner join lesli_role_powers 
        on lesli_role_powers.descriptor_id = lesli_descriptors.id 
        and lesli_role_powers.deleted_at is null
        and lesli_role_powers.role_id = ?
    ), role.id])

    current_user..descriptors
    .where.not(:name => "owner")
    .joins(sanitized_role_power_join)
    .select(
        "coalesce(lesli_role_powers.descriptor_id, lesli_descriptors.id) as id", 
        "lesli_descriptors.name as name", 
        "lesli_role_powers.plist",
        "lesli_role_powers.pindex",
        "lesli_role_powers.pshow",
        "lesli_role_powers.pcreate",
        "lesli_role_powers.pupdate",
        "lesli_role_powers.pdestroy",
        Lesli::Descriptor::Privilege.joins(action: :system_controller).where("lesli_descriptor_privileges.descriptor_id = lesli_descriptors.id and lesli_system_controller_actions.name = 'list'").arel.exists.as("has_list"),
        Lesli::Descriptor::Privilege.joins(action: :system_controller).where("lesli_descriptor_privileges.descriptor_id = lesli_descriptors.id and lesli_system_controller_actions.name = 'index'").arel.exists.as("has_index"),
        Lesli::Descriptor::Privilege.joins(action: :system_controller).where("lesli_descriptor_privileges.descriptor_id = lesli_descriptors.id and lesli_system_controller_actions.name = 'show'").arel.exists.as("has_show"),
        Lesli::Descriptor::Privilege.joins(action: :system_controller).where("lesli_descriptor_privileges.descriptor_id = lesli_descriptors.id and lesli_system_controller_actions.name = 'create'").arel.exists.as("has_create"),
        Lesli::Descriptor::Privilege.joins(action: :system_controller).where("lesli_descriptor_privileges.descriptor_id = lesli_descriptors.id and lesli_system_controller_actions.name = 'update'").arel.exists.as("has_update"),
        Lesli::Descriptor::Privilege.joins(action: :system_controller).where("lesli_descriptor_privileges.descriptor_id = lesli_descriptors.id and lesli_system_controller_actions.name = 'destroy'").arel.exists.as("has_destroy")
    )
end