From 3e36c6c77401b8b616462e1cd882139d6fb61b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Wed, 15 Jul 2020 19:12:11 +0300 Subject: [PATCH] added bulk permission detachment --- app/Traits/Permissions.php | 78 ++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/app/Traits/Permissions.php b/app/Traits/Permissions.php index 197bb56b5..269ccd9fc 100644 --- a/app/Traits/Permissions.php +++ b/app/Traits/Permissions.php @@ -43,34 +43,51 @@ trait Permissions public function attachPermissionsToAllRoles($permissions, $require = 'read-admin-panel') { - $this->getRoles($require)->each(function ($role) use ($permissions) { - foreach ($permissions as $id => $permission) { - if ($this->isActionList($permission)) { - $this->attachPermissionsByAction($role, $id, $permission); - - continue; - } - - $this->attachPermission($role, $permission); - } - }); + $this->applyPermissionsToAllRoles('attach', $permissions, $require); } public function detachPermissionsByRoleNames($roles) { foreach ($roles as $role_name => $permissions) { - $role = Role::where('name', $role_name)->first(); - - if (empty($role)) { - continue; - } - foreach ($permissions as $permission_name) { - $this->detachPermission($role, $permission_name); + $this->detachPermission($role_name, $permission_name); } } } + public function detachPermissionsFromAdminRoles($permissions) + { + $this->detachPermissionsFromAllRoles($permissions, 'read-admin-panel'); + } + + public function detachPermissionsFromPortalRoles($permissions) + { + $this->detachPermissionsFromAllRoles($permissions, 'read-client-portal'); + } + + public function detachPermissionsFromAllRoles($permissions, $require = 'read-admin-panel') + { + $this->applyPermissionsToAllRoles('detach', $permissions, $require); + } + + public function applyPermissionsToAllRoles($apply, $permissions, $require = 'read-admin-panel') + { + $this->getRoles($require)->each(function ($role) use ($apply, $permissions) { + $f1 = $apply . 'PermissionsByAction'; + $f2 = $apply . 'Permission'; + + foreach ($permissions as $id => $permission) { + if ($this->isActionList($permission)) { + $this->$f1($role, $id, $permission); + + continue; + } + + $this->$f2($role, $permission); + } + }); + } + public function updatePermissionNames($permissions) { $actions = $this->getActionsMap(); @@ -255,7 +272,7 @@ trait Permissions $role->attachPermission($permission); } - public function detachPermission($role, $permission) + public function detachPermission($role, $permission, $delete = true) { if (is_string($role)) { $role = Role::where('name', $role)->first(); @@ -273,7 +290,14 @@ trait Permissions return; } - $role->detachPermission($permission); + if ($role->hasPermission($permission->name)) { + $role->detachPermission($permission); + } + + if ($delete === false) { + return; + } + $permission->delete(); } @@ -289,6 +313,18 @@ trait Permissions public function attachPermissionsByAction($role, $page, $action_list) { + $this->applyPermissionsByAction('attach', $role, $page, $action_list); + } + + public function detachPermissionsByAction($role, $page, $action_list) + { + $this->applyPermissionsByAction('detach', $role, $page, $action_list); + } + + public function applyPermissionsByAction($apply, $role, $page, $action_list) + { + $function = $apply . 'Permission'; + $actions_map = collect($this->getActionsMap()); $actions = explode(',', $action_list); @@ -298,7 +334,7 @@ trait Permissions $name = $action . '-' . $page; - $this->attachPermission($role, $name); + $this->$function($role, $name); } }