-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(permission): add endpoint for retrieving all and single permissi…
…on (#109) This adds new endpoints to retrieve all permissions and a single permission. The implementation enhances the system's capability for managing permissions. --------- Signed-off-by: Valentin Sickert <[email protected]> Signed-off-by: Lapotor <[email protected]>
- Loading branch information
Showing
5 changed files
with
374 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Responses\ApiSuccessResponse; | ||
use Illuminate\Http\Request; | ||
use Spatie\Permission\Models\Permission; | ||
|
||
class PermissionController extends Controller | ||
{ | ||
|
||
/** | ||
* Display a paginated list of permissions. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Pagination\LengthAwarePaginator | ||
*/ | ||
public function index(Request $request): \Illuminate\Pagination\LengthAwarePaginator | ||
{ | ||
$request->validate([ | ||
'sort' => 'string|in:id,id:asc,id:desc,name,name:asc,name:desc', | ||
'per_page' => 'integer|between:1,50', | ||
]); | ||
|
||
$perms = Permission::paginate($request->per_page ?? 25); | ||
|
||
if ($request->sort) { | ||
// Sort can be a string like 'id' or 'name:desc' | ||
$sort = explode(':', $request->sort); | ||
$perms = Permission::orderBy($sort[0], $sort[1] ?? 'asc')->paginate($request->per_page ?? 25); | ||
} else { | ||
$perms = Permission::orderBy('id')->paginate($request->per_page ?? 25); | ||
} | ||
|
||
return $perms; | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
* | ||
* @param \Spatie\Permission\Models\Permission $permission The permission to be displayed | ||
* @return \App\Http\Responses\ApiSuccessResponse The success response containing the permission | ||
*/ | ||
public function show(Permission $permission): ApiSuccessResponse | ||
{ | ||
return new ApiSuccessResponse($permission); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
use App\Http\Controllers\PermissionController; | ||
use Illuminate\Support\Facades\Route; | ||
|
||
Route::group(['middleware' => 'auth:sanctum'], function () { | ||
Route::get('/permissions', [PermissionController::class, 'index'])->name('api.v1.permissions.index'); | ||
Route::get('/permissions/{permission}', [PermissionController::class, 'show'])->name('api.v1.permissions.show'); | ||
}); |
Oops, something went wrong.