Skip to content

Commit b13d79e

Browse files
authored
feat(permission): add endpoint for retrieving all and single permission (#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]>
1 parent 91d2457 commit b13d79e

File tree

5 files changed

+374
-0
lines changed

5 files changed

+374
-0
lines changed

.github/assets/swagger.yml

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,111 @@ paths:
657657
security:
658658
- BearerAuth: []
659659

660+
/permissions:
661+
get:
662+
tags:
663+
- Permission
664+
summary: Retrieves all permissions
665+
operationId: getPermissions
666+
parameters:
667+
- name: sort
668+
in: query
669+
schema:
670+
type: string
671+
enum:
672+
- id
673+
- id:asc
674+
- id:desc
675+
- name
676+
- name:asc
677+
- name:desc
678+
default: id
679+
- name: per_page
680+
in: query
681+
schema:
682+
type: integer
683+
default: 25
684+
maximum: 50
685+
- name: page
686+
in: query
687+
schema:
688+
type: integer
689+
default: 1
690+
minimum: 0
691+
responses:
692+
"200":
693+
description: Successful operation
694+
content:
695+
application/json:
696+
schema:
697+
type: array
698+
items:
699+
$ref: "#/components/schemas/Permission"
700+
"403":
701+
description:
702+
"Forbidden - The server understood the request, but is refusing\
703+
\ to fulfill it."
704+
content:
705+
application/json:
706+
schema:
707+
$ref: "#/components/schemas/Error"
708+
"500":
709+
description:
710+
"Internal Server Error - A generic error message, given when\
711+
\ an unexpected condition was encountered and no more specific message\
712+
\ is suitable."
713+
content:
714+
application/json:
715+
schema:
716+
$ref: "#/components/schemas/Error"
717+
security:
718+
- BearerAuth: []
719+
720+
/permissions/{id}:
721+
get:
722+
tags:
723+
- Permission
724+
summary: Retrieves a permission
725+
operationId: getPermission
726+
parameters:
727+
- name: id
728+
in: path
729+
required: true
730+
schema:
731+
type: integer
732+
responses:
733+
"200":
734+
description: Successful operation
735+
content:
736+
application/json:
737+
schema:
738+
$ref: "#/components/schemas/Permission"
739+
"403":
740+
description:
741+
"Forbidden - The server understood the request, but is refusing\
742+
\ to fulfill it."
743+
content:
744+
application/json:
745+
schema:
746+
$ref: "#/components/schemas/Error"
747+
"404":
748+
description: Not Found - The server cannot find the requested resource.
749+
content:
750+
application/json:
751+
schema:
752+
$ref: "#/components/schemas/Error"
753+
"500":
754+
description:
755+
"Internal Server Error - A generic error message, given when\
756+
\ an unexpected condition was encountered and no more specific message\
757+
\ is suitable."
758+
content:
759+
application/json:
760+
schema:
761+
$ref: "#/components/schemas/Error"
762+
security:
763+
- BearerAuth: []
764+
660765
/roles:
661766
get:
662767
tags:
@@ -1187,6 +1292,25 @@ components:
11871292
role:
11881293
type: integer
11891294
nullable: true
1295+
1296+
Permission:
1297+
type: object
1298+
required:
1299+
- id
1300+
- name
1301+
- guard
1302+
- created_at
1303+
- updated_at
1304+
properties:
1305+
id:
1306+
type: integer
1307+
readOnly: true
1308+
name:
1309+
type: string
1310+
readOnly: true
1311+
guard:
1312+
type: string
1313+
readOnly: true
11901314
created_at:
11911315
type: string
11921316
format: date-time
@@ -1195,6 +1319,7 @@ components:
11951319
type: string
11961320
format: date-time
11971321
readOnly: true
1322+
11981323
Role:
11991324
required:
12001325
- id
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Responses\ApiSuccessResponse;
6+
use Illuminate\Http\Request;
7+
use Spatie\Permission\Models\Permission;
8+
9+
class PermissionController extends Controller
10+
{
11+
12+
/**
13+
* Display a paginated list of permissions.
14+
*
15+
* @param \Illuminate\Http\Request $request
16+
* @return \Illuminate\Pagination\LengthAwarePaginator
17+
*/
18+
public function index(Request $request): \Illuminate\Pagination\LengthAwarePaginator
19+
{
20+
$request->validate([
21+
'sort' => 'string|in:id,id:asc,id:desc,name,name:asc,name:desc',
22+
'per_page' => 'integer|between:1,50',
23+
]);
24+
25+
$perms = Permission::paginate($request->per_page ?? 25);
26+
27+
if ($request->sort) {
28+
// Sort can be a string like 'id' or 'name:desc'
29+
$sort = explode(':', $request->sort);
30+
$perms = Permission::orderBy($sort[0], $sort[1] ?? 'asc')->paginate($request->per_page ?? 25);
31+
} else {
32+
$perms = Permission::orderBy('id')->paginate($request->per_page ?? 25);
33+
}
34+
35+
return $perms;
36+
}
37+
38+
/**
39+
* Display the specified resource.
40+
*
41+
* @param \Spatie\Permission\Models\Permission $permission The permission to be displayed
42+
* @return \App\Http\Responses\ApiSuccessResponse The success response containing the permission
43+
*/
44+
public function show(Permission $permission): ApiSuccessResponse
45+
{
46+
return new ApiSuccessResponse($permission);
47+
}
48+
}

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
require __DIR__ . '/api/v1/auth.php';
2525
require __DIR__ . '/api/v1/user.php';
2626
require __DIR__ . '/api/v1/role.php';
27+
require __DIR__ . '/api/v1/permission.php';
2728
});

routes/api/v1/permission.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use App\Http\Controllers\PermissionController;
4+
use Illuminate\Support\Facades\Route;
5+
6+
Route::group(['middleware' => 'auth:sanctum'], function () {
7+
Route::get('/permissions', [PermissionController::class, 'index'])->name('api.v1.permissions.index');
8+
Route::get('/permissions/{permission}', [PermissionController::class, 'show'])->name('api.v1.permissions.show');
9+
});

0 commit comments

Comments
 (0)