Method: LesliSecurity::RoleDescriptorService#index

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

#index(role) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/lesli_security/role_descriptor_service.rb', line 4

def index role

    # Left join to the role power table, so we can get the records
    # from the assigned descriptors and the available descriptors
    sanitized_role_power_join = ActiveRecord::Base.sanitize_sql([%(
        left join lesli_role_powers 
        on lesli_role_powers.descriptor_id = lesli_descriptors.id 
        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_descriptors.description as description",
        # we take a descriptor as active if it is already in the role power table
        # to validate this we use the following logic:
        #   if the role power is not deleted (deleted_at column must be null)
        #   and the descriptor_id is not null in the role power table
        "case when lesli_role_powers.deleted_at is null and lesli_role_powers.id is not null then true else false end as active"
    )
end