From 9f9554107ecc233bd5dad43419791f5d6372bf1c Mon Sep 17 00:00:00 2001 From: Mohammad Hafijul Islam Date: Sun, 1 Oct 2023 17:06:51 +0600 Subject: [PATCH 1/2] LP-26 feat: permission pagination search added --- config/auth.php | 3 +- ..._09_28_224955_create_permission_tables.php | 19 ++++++++++-- .../2023_09_28_230630_create_teams_table.php | 4 +-- routes/api.php | 2 +- src/Http/Controllers/PermissionController.php | 31 +++++++------------ src/Http/Requests/StorePermissionRequest.php | 28 ++--------------- src/Models/Permission.php | 4 +-- src/Models/Role.php | 4 +-- src/Models/Team.php | 4 +-- src/Models/User.php | 4 +-- .../Eloquent/PermissionRepository.php | 9 +++++- 11 files changed, 52 insertions(+), 60 deletions(-) diff --git a/config/auth.php b/config/auth.php index 08c3b39..b0cdce2 100644 --- a/config/auth.php +++ b/config/auth.php @@ -139,5 +139,6 @@ | | This value will be used to across system where model is needed */ - 'middleware' => ['auth:sanctum'], +// 'middleware' => ['auth:sanctum'], + 'middleware' => [], ]; diff --git a/database/migrations/2023_09_28_224955_create_permission_tables.php b/database/migrations/2023_09_28_224955_create_permission_tables.php index 29a1d3b..98c987f 100644 --- a/database/migrations/2023_09_28_224955_create_permission_tables.php +++ b/database/migrations/2023_09_28_224955_create_permission_tables.php @@ -28,23 +28,34 @@ public function up() } Schema::create($tableNames['permissions'], function (Blueprint $table) { - $table->bigIncrements('id'); // permission id + $table->id(); // permission id $table->string('name'); // For MySQL 8.0 use string('name', 125); $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); + $table->foreignId('creator_id')->nullable(); + $table->foreignId('editor_id')->nullable(); + $table->foreignId('destroyer_id')->nullable(); + $table->foreignId('restorer_id')->nullable(); $table->timestamps(); - + $table->softDeletes(); + $table->timestamp('restored_at')->nullable(); $table->unique(['name', 'guard_name']); }); Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) { - $table->bigIncrements('id'); // role id + $table->id(); // role id if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing $table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable(); $table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index'); } $table->string('name'); // For MySQL 8.0 use string('name', 125); $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); + $table->foreignId('creator_id')->nullable(); + $table->foreignId('editor_id')->nullable(); + $table->foreignId('destroyer_id')->nullable(); + $table->foreignId('restorer_id')->nullable(); $table->timestamps(); + $table->softDeletes(); + $table->timestamp('restored_at')->nullable(); if ($teams || config('permission.testing')) { $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']); } else { @@ -57,6 +68,7 @@ public function up() $table->string('model_type'); $table->unsignedBigInteger($columnNames['model_morph_key']); + $table->timestamps(); $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index'); $table->foreign(PermissionRegistrar::$pivotPermission) @@ -102,6 +114,7 @@ public function up() Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { $table->unsignedBigInteger(PermissionRegistrar::$pivotPermission); $table->unsignedBigInteger(PermissionRegistrar::$pivotRole); + $table->timestamps(); $table->foreign(PermissionRegistrar::$pivotPermission) ->references('id') // permission id diff --git a/database/migrations/2023_09_28_230630_create_teams_table.php b/database/migrations/2023_09_28_230630_create_teams_table.php index 1bfaa89..08d12ec 100644 --- a/database/migrations/2023_09_28_230630_create_teams_table.php +++ b/database/migrations/2023_09_28_230630_create_teams_table.php @@ -31,8 +31,8 @@ public function up(): void Schema::create('teams', function (Blueprint $table) { $table->id(); - $table->json('name')->nullable(); - $table->json('teams_data')->nullable(); + $table->string('name'); + $table->text('description')->nullable(); $table->foreignId('creator_id')->nullable(); $table->foreignId('editor_id')->nullable(); $table->foreignId('destroyer_id')->nullable(); diff --git a/routes/api.php b/routes/api.php index 1d92acc..e477383 100644 --- a/routes/api.php +++ b/routes/api.php @@ -51,7 +51,7 @@ Route::middleware(config('fintech.auth.middleware'))->group(function () { Route::apiResource('users', \Fintech\Auth\Http\Controllers\UserController::class); // Route::apiResource('roles', \Fintech\Auth\Http\Controllers\RoleController::class); - // Route::apiResource('permissions', \Fintech\Auth\Http\Resources\PermissionCollection::class); + Route::apiResource('permissions', \Fintech\Auth\Http\Controllers\PermissionController::class); // Route::apiResource('teams', \Fintech\Auth\Http\Controllers\TeamController::class); Route::apiSingleton('users.profile', \Fintech\Auth\Http\Controllers\ProfileController::class); }); diff --git a/src/Http/Controllers/PermissionController.php b/src/Http/Controllers/PermissionController.php index 7b4f263..9b3658b 100644 --- a/src/Http/Controllers/PermissionController.php +++ b/src/Http/Controllers/PermissionController.php @@ -2,6 +2,7 @@ namespace Fintech\Auth\Http\Controllers; +use Fintech\Auth\Facades\Auth; use Fintech\Auth\Http\Requests\ImportPermissionRequest; use Fintech\Auth\Http\Requests\IndexPermissionRequest; use Fintech\Auth\Http\Requests\StorePermissionRequest; @@ -30,14 +31,6 @@ class PermissionController extends Controller { use ApiResponseTrait; - /** - * PermissionController constructor. - */ - public function __construct() - { - - } - /** * @lrd:start * Return a listing of the permission resource as collection. @@ -51,7 +44,7 @@ public function index(IndexPermissionRequest $request): PermissionCollection|Jso try { $inputs = $request->validated(); - $permissionPaginate = \Auth::permission()->list($inputs); + $permissionPaginate = Auth::permission()->list($inputs); return new PermissionCollection($permissionPaginate); @@ -74,7 +67,7 @@ public function store(StorePermissionRequest $request): JsonResponse try { $inputs = $request->validated(); - $permission = \Auth::permission()->create($inputs); + $permission = Auth::permission()->create($inputs); if (! $permission) { throw new StoreOperationException(); @@ -103,7 +96,7 @@ public function show(string|int $id): PermissionResource|JsonResponse { try { - $permission = \Auth::permission()->read($id); + $permission = Auth::permission()->read($id); if (! $permission) { throw new ResourceNotFoundException(__('core::messages.resource.notfound', ['model' => 'Permission', 'id' => strval($id)])); @@ -134,7 +127,7 @@ public function update(UpdatePermissionRequest $request, string|int $id): JsonRe { try { - $permission = \Auth::permission()->read($id); + $permission = Auth::permission()->read($id); if (! $permission) { throw new ResourceNotFoundException(__('core::messages.resource.notfound', ['model' => 'Permission', 'id' => strval($id)])); @@ -142,7 +135,7 @@ public function update(UpdatePermissionRequest $request, string|int $id): JsonRe $inputs = $request->validated(); - if (! \Auth::permission()->update($id, $inputs)) { + if (! Auth::permission()->update($id, $inputs)) { throw new UpdateOperationException(); } @@ -174,13 +167,13 @@ public function destroy(string|int $id) { try { - $permission = \Auth::permission()->read($id); + $permission = Auth::permission()->read($id); if (! $permission) { throw new ResourceNotFoundException(__('core::messages.resource.notfound', ['model' => 'Permission', 'id' => strval($id)])); } - if (! \Auth::permission()->destroy($id)) { + if (! Auth::permission()->destroy($id)) { throw new DeleteOperationException(); } @@ -210,13 +203,13 @@ public function restore(string|int $id) { try { - $permission = \Auth::permission()->read($id, true); + $permission = Auth::permission()->read($id, true); if (! $permission) { throw new ResourceNotFoundException(__('core::messages.resource.notfound', ['model' => 'Permission', 'id' => strval($id)])); } - if (! \Auth::permission()->restore($id)) { + if (! Auth::permission()->restore($id)) { throw new RestoreOperationException(); } @@ -245,7 +238,7 @@ public function export(IndexPermissionRequest $request): JsonResponse try { $inputs = $request->validated(); - $permissionPaginate = \Auth::permission()->export($inputs); + $permissionPaginate = Auth::permission()->export($inputs); return $this->exported(__('core::messages.resource.exported', ['model' => 'Permission'])); @@ -269,7 +262,7 @@ public function import(ImportPermissionRequest $request): JsonResponse try { $inputs = $request->validated(); - $permissionPaginate = \Auth::permission()->list($inputs); + $permissionPaginate = Auth::permission()->list($inputs); return new PermissionCollection($permissionPaginate); diff --git a/src/Http/Requests/StorePermissionRequest.php b/src/Http/Requests/StorePermissionRequest.php index 5d4e3f6..cd5b563 100644 --- a/src/Http/Requests/StorePermissionRequest.php +++ b/src/Http/Requests/StorePermissionRequest.php @@ -3,6 +3,7 @@ namespace Fintech\Auth\Http\Requests; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Rule; class StorePermissionRequest extends FormRequest { @@ -22,31 +23,8 @@ public function authorize(): bool public function rules(): array { return [ - // - ]; - } - - /** - * Get the validation attributes that apply to the request. - * - * @return array - */ - public function attributes() - { - return [ - // - ]; - } - - /** - * Get the validation messages that apply to the request. - * - * @return array - */ - public function messages() - { - return [ - // + 'name' => ['required', 'string', 'min:5', 'max:255'], + 'guard_name' => ['required', 'string', Rule::in(array_keys(config('auth.guards', ['web', 'api'])))], ]; } } diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 1d33b37..6bad2cd 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -21,12 +21,12 @@ class Permission extends Model implements Auditable protected $primaryKey = 'id'; - protected $fillable = []; - protected $guarded = ['id']; protected $casts = []; + protected $hidden = ["creator_id", "editor_id", "destroyer_id", "restorer_id", "deleted_at", "restored_at"]; + /* |-------------------------------------------------------------------------- | FUNCTIONS diff --git a/src/Models/Role.php b/src/Models/Role.php index eefac52..8f73c66 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -21,12 +21,12 @@ class Role extends Model implements Auditable protected $primaryKey = 'id'; - protected $fillable = []; - protected $guarded = ['id']; protected $casts = []; + protected $hidden = ["creator_id", "editor_id", "destroyer_id", "restorer_id", "deleted_at", "restored_at"]; + /* |-------------------------------------------------------------------------- | FUNCTIONS diff --git a/src/Models/Team.php b/src/Models/Team.php index 8d22149..15a1764 100644 --- a/src/Models/Team.php +++ b/src/Models/Team.php @@ -21,12 +21,12 @@ class Team extends Model implements Auditable protected $primaryKey = 'id'; - protected $fillable = []; - protected $guarded = ['id']; protected $casts = []; + protected $hidden = ["creator_id", "editor_id", "destroyer_id", "restorer_id", "deleted_at", "restored_at"]; + /* |-------------------------------------------------------------------------- | FUNCTIONS diff --git a/src/Models/User.php b/src/Models/User.php index 14d4449..06a634c 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -25,8 +25,6 @@ class User extends Authenticatable implements Auditable protected $primaryKey = 'id'; - protected $fillable = []; - protected $guarded = ['id']; protected $casts = [ @@ -40,6 +38,8 @@ class User extends Authenticatable implements Auditable 'wrong_pin' => 0, ]; + protected $hidden = ["creator_id", "editor_id", "destroyer_id", "restorer_id", "deleted_at", "restored_at"]; + /* |-------------------------------------------------------------------------- | FUNCTIONS diff --git a/src/Repositories/Eloquent/PermissionRepository.php b/src/Repositories/Eloquent/PermissionRepository.php index 7acc334..78d08f4 100644 --- a/src/Repositories/Eloquent/PermissionRepository.php +++ b/src/Repositories/Eloquent/PermissionRepository.php @@ -4,7 +4,9 @@ use Fintech\Auth\Exceptions\PermissionRepositoryException; use Fintech\Auth\Interfaces\PermissionRepository as InterfacesPermissionRepository; +use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use InvalidArgumentException; @@ -29,13 +31,18 @@ public function __construct() /** * return a list or pagination of items from * filtered options - * + * @param array $filters * @return LengthAwarePaginator|Builder[]|Collection */ public function list(array $filters = []) { $query = $this->model->newQuery(); + if(isset($filters['search']) && !empty($filters['search'])) { + $query->where('name', 'like', "%{$filters['search']}%") + ->orWhere('guard_name', 'like', "%{$filters['search']}%"); + } + //Handle Sorting $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); From cc823cb0f74947e75714dfcb2b73d3a704f759dc Mon Sep 17 00:00:00 2001 From: hafijul233 Date: Sun, 1 Oct 2023 11:07:51 +0000 Subject: [PATCH 2/2] Fix styling --- config/auth.php | 2 +- src/Models/Permission.php | 2 +- src/Models/Role.php | 2 +- src/Models/Team.php | 2 +- src/Models/User.php | 2 +- src/Repositories/Eloquent/PermissionRepository.php | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/config/auth.php b/config/auth.php index b0cdce2..886e2f4 100644 --- a/config/auth.php +++ b/config/auth.php @@ -139,6 +139,6 @@ | | This value will be used to across system where model is needed */ -// 'middleware' => ['auth:sanctum'], + // 'middleware' => ['auth:sanctum'], 'middleware' => [], ]; diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 6bad2cd..18469b4 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -25,7 +25,7 @@ class Permission extends Model implements Auditable protected $casts = []; - protected $hidden = ["creator_id", "editor_id", "destroyer_id", "restorer_id", "deleted_at", "restored_at"]; + protected $hidden = ['creator_id', 'editor_id', 'destroyer_id', 'restorer_id', 'deleted_at', 'restored_at']; /* |-------------------------------------------------------------------------- diff --git a/src/Models/Role.php b/src/Models/Role.php index 8f73c66..c01358f 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -25,7 +25,7 @@ class Role extends Model implements Auditable protected $casts = []; - protected $hidden = ["creator_id", "editor_id", "destroyer_id", "restorer_id", "deleted_at", "restored_at"]; + protected $hidden = ['creator_id', 'editor_id', 'destroyer_id', 'restorer_id', 'deleted_at', 'restored_at']; /* |-------------------------------------------------------------------------- diff --git a/src/Models/Team.php b/src/Models/Team.php index 15a1764..54e6093 100644 --- a/src/Models/Team.php +++ b/src/Models/Team.php @@ -25,7 +25,7 @@ class Team extends Model implements Auditable protected $casts = []; - protected $hidden = ["creator_id", "editor_id", "destroyer_id", "restorer_id", "deleted_at", "restored_at"]; + protected $hidden = ['creator_id', 'editor_id', 'destroyer_id', 'restorer_id', 'deleted_at', 'restored_at']; /* |-------------------------------------------------------------------------- diff --git a/src/Models/User.php b/src/Models/User.php index 06a634c..78dc272 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -38,7 +38,7 @@ class User extends Authenticatable implements Auditable 'wrong_pin' => 0, ]; - protected $hidden = ["creator_id", "editor_id", "destroyer_id", "restorer_id", "deleted_at", "restored_at"]; + protected $hidden = ['creator_id', 'editor_id', 'destroyer_id', 'restorer_id', 'deleted_at', 'restored_at']; /* |-------------------------------------------------------------------------- diff --git a/src/Repositories/Eloquent/PermissionRepository.php b/src/Repositories/Eloquent/PermissionRepository.php index 78d08f4..8e09792 100644 --- a/src/Repositories/Eloquent/PermissionRepository.php +++ b/src/Repositories/Eloquent/PermissionRepository.php @@ -31,14 +31,14 @@ public function __construct() /** * return a list or pagination of items from * filtered options - * @param array $filters + * * @return LengthAwarePaginator|Builder[]|Collection */ public function list(array $filters = []) { $query = $this->model->newQuery(); - if(isset($filters['search']) && !empty($filters['search'])) { + if (isset($filters['search']) && ! empty($filters['search'])) { $query->where('name', 'like', "%{$filters['search']}%") ->orWhere('guard_name', 'like', "%{$filters['search']}%"); }