Class: LesliShield::RoleActionService

Inherits:
Lesli::ApplicationLesliService
  • Object
show all
Defined in:
app/services/lesli_shield/role_action_service.rb

Instance Method Summary collapse

Instance Method Details

#add_guest_actions(role) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/services/lesli_shield/role_action_service.rb', line 74

def add_guest_actions role

    # Adding default system actions for profile descriptor
    [
        { controller: "lesli/users", actions: ["update"] },        # enable user edition
        { controller: "lesli/abouts", actions: ["show"] },         # system status
        { controller: "lesli/user/sessions", actions: ["index"] }, # session management
        { controller: "lesli_admin/profiles", actions: ["show"] }  # enable profile view
    ].each do |controller_action|

        controller_action[:actions].each do |action_name|

            system_controller_action = Lesli::Resource.actions.joins(:parent)
            .where("parents_lesli_resources.route = ?", controller_action[:controller])
            .where("lesli_resources.action = ?", action_name)

            role.actions.find_or_create_by(
                action: system_controller_action.first
            )
        end
    end
end

#add_owner_actions(role) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/services/lesli_shield/role_action_service.rb', line 97

def add_owner_actions role

    now = Time.current

    # Adding default system actions for profile descriptor
    actions = Lesli::Resource.actions

    records = actions.map do |action|
        {
            role_id: role.id,
            action_id: action.id,
            created_at: now,
            updated_at: now
        }
    end

    role.actions.upsert_all(records,
        unique_by: :index_role_actions_on_role_and_action
    )
end

#clean(action) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'app/services/lesli_shield/role_action_service.rb', line 10

def clean action 
    {
        :id => action.id,
        :role_id => action.role_id,
        :action_id => action.action_id,
        :deleted_at => action.deleted_at,
        :active => action.active
    }
end

#find(id) ⇒ Object



4
5
6
# File 'app/services/lesli_shield/role_action_service.rb', line 4

def find id
    super(Role::Action.with_deleted.find(id))
end

#index(role_id) ⇒ Object



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_shield/role_action_service.rb', line 8

def index role_id

    def clean action 
        {
            :id => action.id,
            :role_id => action.role_id,
            :action_id => action.action_id,
            :deleted_at => action.deleted_at,
            :active => action.active
        }
    end

    role_actions = {}

    Role::Action.with_deleted.joins(action: :parent)
    .where(:role_id => role_id)
    .select(
        :id,
        :role_id,
        :deleted_at,
        "parents_lesli_resources.id as controller_id",
        "parents_lesli_resources.label as controller_name",
        "lesli_resources.action as action_name",
        "lesli_resources.id as action_id",
        "case when lesli_shield_role_actions.deleted_at is null then TRUE else FALSE end active"
    ).each do |action|

        unless role_actions.has_key?(action[:controller_name])
            role_actions[action[:controller_name]] = {
                list:nil,
                index: nil,
                show:nil,
                create:nil,
                update:nil,
                destroy:nil
            }
        end

        if  action[:action_name] == "list"
            role_actions[action[:controller_name]][:list] = clean(action)
        end

        if  action[:action_name] == "index"
            role_actions[action[:controller_name]][:index] = clean(action)
        end

        if  action[:action_name] == "show"
            role_actions[action[:controller_name]][:show] = clean(action)
        end

        if  action[:action_name] == "create"
            role_actions[action[:controller_name]][:create] = clean(action)
        end

        if  action[:action_name] == "update"
            role_actions[action[:controller_name]][:update] = clean(action)
        end

        if  action[:action_name] == "destroy"
            role_actions[action[:controller_name]][:destroy] = clean(action)
        end
    end

    role_actions
end