Class: Lesli::RoleService

Inherits:
ApplicationLesliService show all
Defined in:
app/services/lesli/role_service.rb

Instance Method Summary collapse

Methods inherited from ApplicationLesliService

#cache_key_for_account, #cache_key_for_user, #delete, #error, #errors, #errors_as_sentence, #found?, #initialize, #result, #successful?

Constructor Details

This class inherits a constructor from Lesli::ApplicationLesliService

Instance Method Details

#create(params) ⇒ Object

Parameters:

  • Hash (params)

    of the permitted attributes for a role

Returns:

  • (Object)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/services/lesli/role_service.rb', line 111

def create params

    role = current_user..roles.new(params)

    unless current_user.can_work_with_role?(role)
        self.error(I18n.t("core.roles.messages_danger_creating_role_object_level_permission_too_high"))
    end

    # check if user can work with that object level permission
    if role.object_level_permission.to_f >= current_user.roles.map(&:object_level_permission).max()
        self.error(I18n.t("core.roles.messages_danger_creating_role_object_level_permission_too_high"))
    end

    # do not create if errors found
    return self unless self.successful?

    # Try to save role and logs if it went OK
    if role.save
        self.resource = role
        #Role::Activity.log_create(current_user, self.resource)
    else
        self.error(role.errors.full_messages.to_sentence)
    end

    self
end

#destroyObject

Returns:

  • (Object)


161
162
163
164
165
166
167
168
169
170
171
# File 'app/services/lesli/role_service.rb', line 161

def destroy
    unless self.resource.destroy
        self.error(self.resource.errors.full_messages.to_sentence)
    end

    if self.successful?
        LesliSecurity::Role::Activity.log_destroy(current_user, self.resource)
    end

    self
end

#find(id) ⇒ Object



36
37
38
39
# File 'app/services/lesli/role_service.rb', line 36

def find id 
    self.resource = current_user..roles.find_by_id(id)
    self
end

#indexArray

Returns Paginated index of users.

Returns:

  • (Array)

    Paginated index of users.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/services/lesli/role_service.rb', line 53

def index 

    users_subquery = <<-SQL
        left join (
            select
                count(1) users,
                role_id
            from lesli_shield_user_roles
            inner join lesli_users as u
                on u.id = lesli_shield_user_roles.user_id
                and u.deleted_at is null
            where lesli_shield_user_roles.deleted_at is null
            group by (role_id)
        ) users on users.role_id = lesli_roles.id
    SQL

    actions_subquery = <<-SQL
        left join (
            select
                count(1) actions,
                role_id
            from lesli_shield_role_actions
            group by role_id
        ) actions on actions.role_id = lesli_roles.id
    SQL

    current_user..roles
    .where.not(:name => ['owner'])
    .where("lesli_roles.permission_level <= ?", current_user.max_level_permission)
    .joins(users_subquery)
    .joins(actions_subquery)
    .select(
        :id, 
        :name, 
        :active, 
        :isolated, 
        :description,
        :path_default, 
        :permission_level, 
        "users.users",
        "actions.actions"
    )
    .page(query[:pagination][:page])
    .per(query[:pagination][:perPage])
    .order(permission_level: :desc, name: :asc)
end

#list(params) ⇒ Object

Return a list of roles that the user is able to work with according to object level permission



43
44
45
46
47
48
# File 'app/services/lesli/role_service.rb', line 43

def list(params)
    current_user..roles
    .where("permission_level <= ?", current_user.max_level_permission)
    .order(permission_level: :desc, name: :asc)
    .select(:id, :name, :permission_level)
end

#optionsObject



173
174
175
176
177
# File 'app/services/lesli/role_service.rb', line 173

def options 
    { 
        :object_level_permissions => self.roles_with_object_level_permission
    }
end

#showObject

Returns:

  • (Object)


103
104
105
# File 'app/services/lesli/role_service.rb', line 103

def show
    self.resource
end

#update(params) ⇒ Object

Parameters:

  • Hash (params)

    of the permitted attributes for a role

Returns:

  • (Object)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/services/lesli/role_service.rb', line 142

def update params
    old_attributes = self.resource.attributes

    unless self.resource.update(params)
        self.error(self.resource.errors.full_messages.to_sentence)
    end

    if self.successful?
        new_attributes = self.resource.attributes

        #LesliSecurity::Role::Activity.log_update(current_user, role, old_attributes, new_attributes)
    end

    self
end