Skip to content

Commit

Permalink
Merge pull request #5 from fintechbd/LP-26-Permission-Pagination
Browse files Browse the repository at this point in the history
LP-26 permission pagination
  • Loading branch information
mah-shamim authored Oct 1, 2023
2 parents 5be4b78 + cc823cb commit 7405707
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 59 deletions.
3 changes: 2 additions & 1 deletion config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,6 @@
|
| This value will be used to across system where model is needed
*/
'middleware' => ['auth:sanctum'],
// 'middleware' => ['auth:sanctum'],
'middleware' => [],
];
19 changes: 16 additions & 3 deletions database/migrations/2023_09_28_224955_create_permission_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions database/migrations/2023_09_28_230630_create_teams_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
31 changes: 12 additions & 19 deletions src/Http/Controllers/PermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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);

Expand All @@ -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();
Expand Down Expand Up @@ -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)]));
Expand Down Expand Up @@ -134,15 +127,15 @@ 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)]));
}

$inputs = $request->validated();

if (! \Auth::permission()->update($id, $inputs)) {
if (! Auth::permission()->update($id, $inputs)) {

throw new UpdateOperationException();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -210,13 +203,13 @@ public function restore(string|int $id)
{
try {

$permission = \Auth::permission()->read($id, true);
$permission = Auth::permission()->read($id, true);

Check failure on line 206 in src/Http/Controllers/PermissionController.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Fintech\Auth\Services\PermissionService::read() invoked with 2 parameters, 1 required.

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();
}
Expand Down Expand Up @@ -245,7 +238,7 @@ public function export(IndexPermissionRequest $request): JsonResponse
try {
$inputs = $request->validated();

$permissionPaginate = \Auth::permission()->export($inputs);
$permissionPaginate = Auth::permission()->export($inputs);

Check failure on line 241 in src/Http/Controllers/PermissionController.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Fintech\Auth\Services\PermissionService::export().

return $this->exported(__('core::messages.resource.exported', ['model' => 'Permission']));

Expand All @@ -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);

Check failure on line 267 in src/Http/Controllers/PermissionController.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Fintech\Auth\Http\Controllers\PermissionController::import() should return Illuminate\Http\JsonResponse but returns Fintech\Auth\Http\Resources\PermissionCollection.

Expand Down
28 changes: 3 additions & 25 deletions src/Http/Requests/StorePermissionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fintech\Auth\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StorePermissionRequest extends FormRequest
{
Expand All @@ -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'])))],
];
}
}
4 changes: 2 additions & 2 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class User extends Authenticatable implements Auditable

protected $primaryKey = 'id';

protected $fillable = [];

protected $guarded = ['id'];

protected $casts = [
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/Repositories/Eloquent/PermissionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,6 +38,11 @@ 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');

Expand Down

0 comments on commit 7405707

Please sign in to comment.