permissions trait
This commit is contained in:
parent
5373e89429
commit
9fd64ad696
@ -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 = [
|
||||
|
106
app/Traits/Permissions.php
Normal file
106
app/Traits/Permissions.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Auth\Permission;
|
||||
use App\Models\Auth\Role;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait Permissions
|
||||
{
|
||||
public function getActionsMap()
|
||||
{
|
||||
return [
|
||||
'c' => '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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user