Class: LesliGuard::RoleService
- Inherits:
-
Lesli::ApplicationLesliService
- Object
- Lesli::ApplicationLesliService
- LesliGuard::RoleService
- Defined in:
- app/services/lesli_guard/role_service.rb
Instance Method Summary collapse
- #create(params) ⇒ Object
- #destroy ⇒ Object
- #find(id) ⇒ Object
-
#index ⇒ Array
Paginated index of users.
- #options ⇒ Object
- #show ⇒ Object
- #update(params) ⇒ Object
Instance Method Details
#create(params) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/services/lesli_guard/role_service.rb', line 88 def create params role = current_user.account.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..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..to_sentence) end self end |
#destroy ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/services/lesli_guard/role_service.rb', line 138 def destroy unless self.resource.destroy self.error(self.resource.errors..to_sentence) end if self.successful? LesliGuard::Role::Activity.log_destroy(current_user, self.resource) end self end |
#find(id) ⇒ Object
37 38 39 40 |
# File 'app/services/lesli_guard/role_service.rb', line 37 def find id self.resource = current_user.account.roles.find_by_id(id) self end |
#index ⇒ Array
Returns Paginated index of users.
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 73 74 75 |
# File 'app/services/lesli_guard/role_service.rb', line 45 def index current_user.account.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.) .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 |
#options ⇒ Object
150 151 152 153 154 |
# File 'app/services/lesli_guard/role_service.rb', line 150 def { :object_level_permissions => self. } end |
#show ⇒ Object
80 81 82 |
# File 'app/services/lesli_guard/role_service.rb', line 80 def show self.resource end |
#update(params) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/services/lesli_guard/role_service.rb', line 119 def update params old_attributes = self.resource.attributes unless self.resource.update(params) self.error(self.resource.errors..to_sentence) end if self.successful? new_attributes = self.resource.attributes LesliGuard::Role::Activity.log_update(current_user, role, old_attributes, new_attributes) end self end |