From 4ad386a734d13abd29199e0fbee53db3007602d3 Mon Sep 17 00:00:00 2001 From: Nurmuhammet Allanov Date: Thu, 14 Dec 2023 19:57:31 +0500 Subject: [PATCH] permessions added to user --- app/Models/System/Roles/Permission.php | 9 +++++ .../Card/Concerns/CardOrderFieldsForIndex.php | 18 ++++----- .../Resources/System/Roles/Permission.php | 12 ++++-- app/Nova/Resources/System/Roles/Role.php | 18 ++++++--- app/Policies/System/Roles/RolePolicy.php | 9 ----- app/Repos/System/Nova/NovaRepo.php | 8 ++++ ...ay_name_to_roles_and_permessions_table.php | 40 +++++++++++++++++++ database/seeders/UsersTableSeeder.php | 1 - 8 files changed, 87 insertions(+), 28 deletions(-) create mode 100644 database/migrations/2023_12_14_194949_add_display_name_to_roles_and_permessions_table.php diff --git a/app/Models/System/Roles/Permission.php b/app/Models/System/Roles/Permission.php index 1a1b1fc..58988b4 100644 --- a/app/Models/System/Roles/Permission.php +++ b/app/Models/System/Roles/Permission.php @@ -3,7 +3,16 @@ namespace App\Models\System\Roles; use Spatie\Permission\Models\Permission as SpatiePermission; +use Spatie\Translatable\HasTranslations; class Permission extends SpatiePermission { + use HasTranslations; + + /** + * Translatable fields + * + * @var array + */ + public $translatable = ['display_name']; } diff --git a/app/Nova/Resources/Order/Card/Concerns/CardOrderFieldsForIndex.php b/app/Nova/Resources/Order/Card/Concerns/CardOrderFieldsForIndex.php index 06a5f06..2e6b8c9 100644 --- a/app/Nova/Resources/Order/Card/Concerns/CardOrderFieldsForIndex.php +++ b/app/Nova/Resources/Order/Card/Concerns/CardOrderFieldsForIndex.php @@ -48,15 +48,15 @@ class CardOrderFieldsForIndex Text::make(__('Phone'), 'phone'), - // Badge::make(__('Status'), 'status') - // ->map(OrderRepo::statusClasses()) - // ->addTypes([ - // 'primary' => 'dark:bg-gray-900 bg-gray-600 text-white', - // ]) - // ->labels(OrderRepo::statusValues()) - // ->withIcons() - // ->icons(OrderRepo::statusIcons()) - // ->sortable(), + Badge::make(__('Status'), 'status') + ->map(OrderRepo::statusClasses()) + ->addTypes([ + 'primary' => 'dark:bg-gray-900 bg-gray-600 text-white', + ]) + ->labels(OrderRepo::statusValues()) + ->withIcons() + ->icons(OrderRepo::statusIcons()) + ->sortable(), ]; } } diff --git a/app/Nova/Resources/System/Roles/Permission.php b/app/Nova/Resources/System/Roles/Permission.php index 8f7f9af..23dc1f6 100644 --- a/app/Nova/Resources/System/Roles/Permission.php +++ b/app/Nova/Resources/System/Roles/Permission.php @@ -4,6 +4,7 @@ namespace App\Nova\Resources\System\Roles; use App\Models\System\Roles\Permission as PermissionModel; use App\Nova\Resource; +use App\Repos\System\Nova\NovaRepo; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use Laravel\Nova\Http\Requests\NovaRequest; @@ -22,7 +23,7 @@ class Permission extends Resource * * @var string */ - public static $title = 'name'; + public static $title = 'display_name'; /** * The columns that should be searched. @@ -57,8 +58,13 @@ class Permission extends Resource return [ ID::make()->sortable(), - Text::make(__('Name'), 'name') - ->rules('required', 'string', 'max:255', 'unique:permissions,name'), + Text::make(__('Code'), 'name') + ->rules('required', 'string', 'max:255', 'unique:permissions,name') + ->readonly(NovaRepo::readonlyOnUpdate()), + + Text::make(__('Name'), 'display_name') + ->rules('required', 'string', 'max:255') + ->translatable(), Text::make(__('Guard name'), 'guard_name') ->rules('required', 'string', 'max:255'), diff --git a/app/Nova/Resources/System/Roles/Role.php b/app/Nova/Resources/System/Roles/Role.php index 5a234a1..ecc1272 100644 --- a/app/Nova/Resources/System/Roles/Role.php +++ b/app/Nova/Resources/System/Roles/Role.php @@ -2,7 +2,9 @@ namespace App\Nova\Resources\System\Roles; +use App\Models\System\Roles\Role as RoleModel; use App\Nova\Resource; +use App\Repos\System\Nova\NovaRepo; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use Laravel\Nova\Http\Requests\NovaRequest; @@ -12,16 +14,16 @@ class Role extends Resource /** * The model the resource corresponds to. * - * @var class-string<\App\Models\Resources\System\Roles\Role> + * @var class-string */ - public static $model = \App\Models\System\Roles\Role::class; + public static $model = RoleModel::class; /** * The single value that should be used to represent the resource when being displayed. * * @var string */ - public static $title = 'name'; + public static $title = 'display_name'; /** * The columns that should be searched. @@ -69,12 +71,16 @@ class Role extends Resource return [ ID::make()->sortable(), - Text::make(__('Name'), 'name') - ->rules('required', 'string', 'max:255', 'unique:roles,name'), + Text::make(__('Code'), 'name') + ->rules('required', 'string', 'max:255', 'unique:roles,name') + ->readonly(NovaRepo::readonlyOnUpdate()), - Text::make(__('Guard name'), 'guard_name') + Text::make(__('Name'), 'display_name') ->rules('required', 'string', 'max:255') ->translatable(), + + Text::make(__('Guard name'), 'guard_name') + ->rules('required', 'string', 'max:255'), ]; } diff --git a/app/Policies/System/Roles/RolePolicy.php b/app/Policies/System/Roles/RolePolicy.php index ac4aef1..29718b8 100644 --- a/app/Policies/System/Roles/RolePolicy.php +++ b/app/Policies/System/Roles/RolePolicy.php @@ -48,15 +48,6 @@ class RolePolicy */ public function update(User $user, Role $role): bool { - if (in_array($role->name, [ - 'king', - 'superadmin', - 'admin', - 'operator', - ])) { - return false; - } - if ($user->isAdmin()) { return true; } diff --git a/app/Repos/System/Nova/NovaRepo.php b/app/Repos/System/Nova/NovaRepo.php index b3532a1..cd8441b 100644 --- a/app/Repos/System/Nova/NovaRepo.php +++ b/app/Repos/System/Nova/NovaRepo.php @@ -83,6 +83,14 @@ class NovaRepo return fn () => Gate::allows('isMe', auth()->user()); } + /** + * Readonly only on "update" pages + */ + public static function readonlyOnUpdate(): Closure + { + return fn ($request) => $request->isUpdateOrUpdateAttachedRequest(); + } + /** * Depends on region */ diff --git a/database/migrations/2023_12_14_194949_add_display_name_to_roles_and_permessions_table.php b/database/migrations/2023_12_14_194949_add_display_name_to_roles_and_permessions_table.php new file mode 100644 index 0000000..60e9223 --- /dev/null +++ b/database/migrations/2023_12_14_194949_add_display_name_to_roles_and_permessions_table.php @@ -0,0 +1,40 @@ +jsonb('display_name')->nullable(); + }); + + Schema::table($tableNames['roles'], function (Blueprint $table) { + $table->jsonb('display_name')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + $tableNames = config('permission.table_names'); + + Schema::table($tableNames['permissions'], function (Blueprint $table) { + $table->dropColumn('display_name'); + }); + + Schema::table($tableNames['roles'], function (Blueprint $table) { + $table->dropColumn('display_name'); + }); + } +}; diff --git a/database/seeders/UsersTableSeeder.php b/database/seeders/UsersTableSeeder.php index 1984d0c..20f80ed 100644 --- a/database/seeders/UsersTableSeeder.php +++ b/database/seeders/UsersTableSeeder.php @@ -28,7 +28,6 @@ class UsersTableSeeder extends Seeder 'superadmin', 'admin', 'operator', - 'user', ])->each(fn ($role) => Role::create(['name' => $role])); }