Class: LesliVault::RoleService

Inherits:
Lesli::ApplicationLesliService
  • Object
show all
Defined in:
app/services/lesli_vault/role_service.rb

Instance Method Summary collapse

Instance Method Details

#create(params) ⇒ Object

Parameters:

  • Hash (params)

    of the permitted attributes for a role

Returns:

  • (Object)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/services/lesli_vault/role_service.rb', line 83

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)


133
134
135
136
137
138
139
140
141
142
143
# File 'app/services/lesli_vault/role_service.rb', line 133

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

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

    self
end

#find(id) ⇒ Object

Returns:

  • (Object)


148
149
150
151
# File 'app/services/lesli_vault/role_service.rb', line 148

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

#indexArray

Returns Paginated index of users.

Returns:

  • (Array)

    Paginated index of users.



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
# File 'app/services/lesli_vault/role_service.rb', line 40

def index 

    current_user..roles.where.not(
        :name => ["owner"]
    ).joins("
        left join (
            select
                count(1) users,
                role_id
            from lesli_user_powers
            inner join lesli_users as u
                on u.id = lesli_user_powers.user_id
                and u.deleted_at is null
            where lesli_user_powers.deleted_at is null
            group by (role_id)
        ) users on users.role_id = lesli_roles.id
    ").where("lesli_roles.object_level_permission <= ?", current_user.max_object_level_permission)
    .select(
        :id, 
        :name, 
        :active, 
        :isolated, 
        :description,
        :path_default, 
        :object_level_permission, 
        "users.users"
    )
    .page(query[:pagination][:page])
    .per(query[:pagination][:perPage])
    .order(object_level_permission: :desc, name: :asc)
end

#optionsObject



153
154
155
156
157
# File 'app/services/lesli_vault/role_service.rb', line 153

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

#showObject

Returns:

  • (Object)


75
76
77
# File 'app/services/lesli_vault/role_service.rb', line 75

def show
    self.resource
end

#update(params) ⇒ Object

Parameters:

  • Hash (params)

    of the permitted attributes for a role

Returns:

  • (Object)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/services/lesli_vault/role_service.rb', line 114

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

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

    self
end