From 9fd64ad696268f13362cf45973386d0911d84596 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Thu, 6 Feb 2020 12:33:47 +0300 Subject: [PATCH] permissions trait --- app/Listeners/Update/V20/Version200.php | 86 +------------------ app/Traits/Permissions.php | 106 ++++++++++++++++++++++++ database/seeds/Roles.php | 64 +------------- 3 files changed, 113 insertions(+), 143 deletions(-) create mode 100644 app/Traits/Permissions.php diff --git a/app/Listeners/Update/V20/Version200.php b/app/Listeners/Update/V20/Version200.php index 1063b3356..05cd55efa 100644 --- a/app/Listeners/Update/V20/Version200.php +++ b/app/Listeners/Update/V20/Version200.php @@ -5,21 +5,21 @@ namespace App\Listeners\Update\V20; use App\Abstracts\Listeners\Update as Listener; use App\Events\Install\UpdateFinished as Event; use App\Models\Auth\User; -use App\Models\Auth\Role; -use App\Models\Auth\Permission; use App\Models\Common\Company; use App\Models\Common\EmailTemplate; use App\Models\Common\Report; +use App\Traits\Permissions; use App\Utilities\Installer; use App\Utilities\Overrider; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Schema; -use Illuminate\Support\Str; class Version200 extends Listener { + use Permissions; + const ALIAS = 'core'; const VERSION = '2.0.0'; @@ -890,86 +890,6 @@ class Version200 extends Listener ]); } - public function attachPermissions($items) - { - $actions_map = collect([ - 'c' => 'create', - 'r' => 'read', - 'u' => 'update', - 'd' => 'delete', - ]); - - foreach ($items as $role_name => $permissions) { - $role = Role::where('name', $role_name)->first(); - - foreach ($permissions as $page => $action_list) { - $actions = explode(',', $action_list); - - foreach ($actions as $short_action) { - $action = $actions_map->get($short_action); - - $display_name = Str::title($action . ' ' . str_replace('-', ' ', $page)); - - $permission = Permission::firstOrCreate([ - 'name' => $action . '-' . $page, - ], [ - 'display_name' => $display_name, - 'description' => $display_name, - ]); - - if ($role->hasPermission($permission->name)) { - continue; - } - - $role->attachPermission($permission); - } - } - } - } - - public function detachPermissions($items) - { - foreach ($items as $role_name => $permissions) { - $role = Role::where('name', $role_name)->first(); - - foreach ($permissions as $permission_name) { - $permission = Permission::where('name', $permission_name)->first(); - - if (empty($permission)) { - continue; - } - - $role->detachPermission($permission); - $permission->delete(); - } - } - } - - public function updatePermissionNames($items) - { - $actions = [ - 'create', - 'read', - 'update', - 'delete', - ]; - - foreach ($items as $old => $new) { - foreach ($actions as $action) { - $old_name = $action . '-' . $old; - $new_name = $action . '-' . $new; - $new_display_name = Str::title(str_replace('-', ' ', $new_name)); - - DB::table('permissions') - ->where('name', $old_name) - ->update([ - 'name' => $new_name, - 'display_name' => $new_display_name, - ]); - } - } - } - public function deleteOldFiles() { $files = [ diff --git a/app/Traits/Permissions.php b/app/Traits/Permissions.php new file mode 100644 index 000000000..554ae5893 --- /dev/null +++ b/app/Traits/Permissions.php @@ -0,0 +1,106 @@ + 'create', + 'r' => 'read', + 'u' => 'update', + 'd' => 'delete', + ]; + } + + public function attachPermissions($roles) + { + $actions_map = collect($this->getActionsMap()); + + foreach ($roles as $role_name => $permissions) { + $role_display_name = Str::title($role_name); + + $role = Role::firstOrCreate([ + 'name' => $role_name, + ], [ + 'display_name' => $role_display_name, + 'description' => $role_display_name, + ]); + + foreach ($permissions as $page => $action_list) { + $actions = explode(',', $action_list); + + foreach ($actions as $short_action) { + $action = $actions_map->get($short_action); + + $display_name = Str::title($action . ' ' . str_replace('-', ' ', $page)); + + $permission = Permission::firstOrCreate([ + 'name' => $action . '-' . $page, + ], [ + 'display_name' => $display_name, + 'description' => $display_name, + ]); + + if ($role->hasPermission($permission->name)) { + continue; + } + + $role->attachPermission($permission); + } + } + } + } + + public function detachPermissions($roles) + { + foreach ($roles as $role_name => $permissions) { + $role = Role::where('name', $role_name)->first(); + + if (empty($role)) { + continue; + } + + foreach ($permissions as $permission_name) { + $permission = Permission::where('name', $permission_name)->first(); + + if (empty($permission)) { + continue; + } + + $role->detachPermission($permission); + $permission->delete(); + } + } + } + + public function updatePermissionNames($permissions) + { + $actions = $this->getActionsMap(); + + foreach ($permissions as $old => $new) { + foreach ($actions as $action) { + $old_name = $action . '-' . $old; + + $permission = Permission::where('name', $old_name)->first(); + + if (empty($permission)) { + continue; + } + + $new_name = $action . '-' . $new; + $new_display_name = Str::title(str_replace('-', ' ', $new_name)); + + $permission->update([ + 'name' => $new_name, + 'display_name' => $new_display_name, + ]); + } + } + } +} diff --git a/database/seeds/Roles.php b/database/seeds/Roles.php index e0f31f092..3283809fe 100644 --- a/database/seeds/Roles.php +++ b/database/seeds/Roles.php @@ -2,13 +2,12 @@ namespace Database\Seeds; use App\Abstracts\Model; -use App\Models\Auth\Role; -use App\Models\Auth\Permission; +use App\Traits\Permissions; use Illuminate\Database\Seeder; -use Illuminate\Support\Str; class Roles extends Seeder { + use Permissions; /** * Run the database seeds. @@ -24,7 +23,7 @@ class Roles extends Seeder Model::reguard(); } - private function roles() + private function create() { $rows = [ 'admin' => [ @@ -145,61 +144,6 @@ class Roles extends Seeder ] ]; - return $rows; - } - - private function actions() - { - return collect([ - 'c' => 'create', - 'r' => 'read', - 'u' => 'update', - 'd' => 'delete', - ]); - } - - private function create() - { - $roles = $this->roles(); - - $actions_map = $this->actions(); - - foreach ($roles as $role_name => $permissions) { - $this->command->info('Creating Role ' . Str::title($role_name)); - - $role = Role::firstOrCreate([ - 'name' => $role_name, - ], [ - 'display_name' => Str::title($role_name), - 'description' => Str::title($role_name), - ]); - - foreach ($permissions as $page => $action_list) { - $actions = explode(',', $action_list); - - foreach ($actions as $short_action) { - $action = $actions_map->get($short_action); - - $display_name = Str::title($action . ' ' . str_replace('-', ' ', $page)); - - $this->command->info('Creating Permission ' . $display_name); - - $permission = Permission::firstOrCreate([ - 'name' => $action . '-' . $page, - ], [ - 'display_name' => $display_name, - 'description' => $display_name, - ]); - - if ($role->hasPermission($permission->name)) { - $this->command->info($role_name . ': ' . $display_name . ' already exist'); - - continue; - } - - $role->attachPermission($permission); - } - } - } + $this->attachPermissions($rows); } }