permessions added to user

This commit is contained in:
2023-12-14 19:57:31 +05:00
parent 3d273055e8
commit 4ad386a734
8 changed files with 87 additions and 28 deletions

View File

@@ -3,7 +3,16 @@
namespace App\Models\System\Roles; namespace App\Models\System\Roles;
use Spatie\Permission\Models\Permission as SpatiePermission; use Spatie\Permission\Models\Permission as SpatiePermission;
use Spatie\Translatable\HasTranslations;
class Permission extends SpatiePermission class Permission extends SpatiePermission
{ {
use HasTranslations;
/**
* Translatable fields
*
* @var array<string>
*/
public $translatable = ['display_name'];
} }

View File

@@ -48,15 +48,15 @@ class CardOrderFieldsForIndex
Text::make(__('Phone'), 'phone'), Text::make(__('Phone'), 'phone'),
// Badge::make(__('Status'), 'status') Badge::make(__('Status'), 'status')
// ->map(OrderRepo::statusClasses()) ->map(OrderRepo::statusClasses())
// ->addTypes([ ->addTypes([
// 'primary' => 'dark:bg-gray-900 bg-gray-600 text-white', 'primary' => 'dark:bg-gray-900 bg-gray-600 text-white',
// ]) ])
// ->labels(OrderRepo::statusValues()) ->labels(OrderRepo::statusValues())
// ->withIcons() ->withIcons()
// ->icons(OrderRepo::statusIcons()) ->icons(OrderRepo::statusIcons())
// ->sortable(), ->sortable(),
]; ];
} }
} }

View File

@@ -4,6 +4,7 @@ namespace App\Nova\Resources\System\Roles;
use App\Models\System\Roles\Permission as PermissionModel; use App\Models\System\Roles\Permission as PermissionModel;
use App\Nova\Resource; use App\Nova\Resource;
use App\Repos\System\Nova\NovaRepo;
use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text; use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest; use Laravel\Nova\Http\Requests\NovaRequest;
@@ -22,7 +23,7 @@ class Permission extends Resource
* *
* @var string * @var string
*/ */
public static $title = 'name'; public static $title = 'display_name';
/** /**
* The columns that should be searched. * The columns that should be searched.
@@ -57,8 +58,13 @@ class Permission extends Resource
return [ return [
ID::make()->sortable(), ID::make()->sortable(),
Text::make(__('Name'), 'name') Text::make(__('Code'), 'name')
->rules('required', 'string', 'max:255', 'unique:permissions,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') Text::make(__('Guard name'), 'guard_name')
->rules('required', 'string', 'max:255'), ->rules('required', 'string', 'max:255'),

View File

@@ -2,7 +2,9 @@
namespace App\Nova\Resources\System\Roles; namespace App\Nova\Resources\System\Roles;
use App\Models\System\Roles\Role as RoleModel;
use App\Nova\Resource; use App\Nova\Resource;
use App\Repos\System\Nova\NovaRepo;
use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text; use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest; use Laravel\Nova\Http\Requests\NovaRequest;
@@ -12,16 +14,16 @@ class Role extends Resource
/** /**
* The model the resource corresponds to. * The model the resource corresponds to.
* *
* @var class-string<\App\Models\Resources\System\Roles\Role> * @var class-string<RoleModel>
*/ */
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. * The single value that should be used to represent the resource when being displayed.
* *
* @var string * @var string
*/ */
public static $title = 'name'; public static $title = 'display_name';
/** /**
* The columns that should be searched. * The columns that should be searched.
@@ -69,12 +71,16 @@ class Role extends Resource
return [ return [
ID::make()->sortable(), ID::make()->sortable(),
Text::make(__('Name'), 'name') Text::make(__('Code'), 'name')
->rules('required', 'string', 'max:255', 'unique:roles,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') ->rules('required', 'string', 'max:255')
->translatable(), ->translatable(),
Text::make(__('Guard name'), 'guard_name')
->rules('required', 'string', 'max:255'),
]; ];
} }

View File

@@ -48,15 +48,6 @@ class RolePolicy
*/ */
public function update(User $user, Role $role): bool public function update(User $user, Role $role): bool
{ {
if (in_array($role->name, [
'king',
'superadmin',
'admin',
'operator',
])) {
return false;
}
if ($user->isAdmin()) { if ($user->isAdmin()) {
return true; return true;
} }

View File

@@ -83,6 +83,14 @@ class NovaRepo
return fn () => Gate::allows('isMe', auth()->user()); 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 * Depends on region
*/ */

View File

@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$tableNames = config('permission.table_names');
Schema::table($tableNames['permissions'], function (Blueprint $table) {
$table->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');
});
}
};

View File

@@ -28,7 +28,6 @@ class UsersTableSeeder extends Seeder
'superadmin', 'superadmin',
'admin', 'admin',
'operator', 'operator',
'user',
])->each(fn ($role) => Role::create(['name' => $role])); ])->each(fn ($role) => Role::create(['name' => $role]));
} }