Skip to content

Commit a1aa608

Browse files
authored
feat(user): add user permissions and tests (#78)
Introduce user permissions to the `UserController` with corresponding tests. Define permissions for each controller function as follows: - `index()`: `list-users` - `store()`: `create-users` - `show()`: - For viewing own user: `show-users` - For viewing all users: `list-users` - `update()`: - For updating all users: `update-users` - For updating own user: `update-users-self` - `destroy()`: `delete-users` This update ensures that user controller functions are now restricted and accessible based on the specified permissions. The associated tests validate the correct implementation of these permissions. Signed-off-by: Valentin Sickert <[email protected]>
1 parent d460257 commit a1aa608

File tree

4 files changed

+625
-16
lines changed

4 files changed

+625
-16
lines changed

app/Http/Controllers/UserController.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,36 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Http\Responses\ApiErrorResponse;
56
use App\Http\Responses\ApiSuccessResponse;
67
use App\Models\User;
8+
use App\Permissions\UsersPermissions;
79
use Illuminate\Http\Request;
810
use Illuminate\Http\Response;
911

1012
class UserController extends Controller
1113
{
14+
15+
/**
16+
* UserController constructor.
17+
*/
18+
public function __construct()
19+
{
20+
/**
21+
* Permissions:
22+
* - index: list-users
23+
* - store: create-users
24+
* - show: show-users || list-users
25+
* - update-users || update-users-self
26+
* - destroy: delete-users
27+
*/
28+
$this->middleware('permission:'.UsersPermissions::CAN_LIST_USERS)->only('index');
29+
$this->middleware('permission:'.UsersPermissions::CAN_LIST_USERS.'|'.UsersPermissions::CAN_SHOW_USERS)->only('show');
30+
$this->middleware('permission:'.UsersPermissions::CAN_CREATE_USERS)->only('store');
31+
$this->middleware('permission:'.UsersPermissions::CAN_UPDATE_USERS.'|'.UsersPermissions::CAN_UPDATE_USERS_SELF)->only('update');
32+
$this->middleware('permission:'.UsersPermissions::CAN_DELETE_USERS)->only('destroy');
33+
}
34+
1235
/**
1336
* Display a listing of the resource.
1437
*/
@@ -42,6 +65,12 @@ public function store(Request $request)
4265
*/
4366
public function show(User $user)
4467
{
68+
/** @var User $authUser */
69+
$authUser = auth()->user();
70+
71+
if(!$authUser->checkPermissionTo(UsersPermissions::CAN_LIST_USERS) && !$authUser->is($user)) {
72+
return new ApiErrorResponse("You can only view your own user.", status: Response::HTTP_FORBIDDEN);
73+
}
4574
return new ApiSuccessResponse($user);
4675
}
4776

@@ -56,6 +85,13 @@ public function update(Request $request, User $user)
5685
'password' => 'sometimes|required|min:8|confirmed',
5786
]);
5887

88+
/** @var User $authUser */
89+
$authUser = auth()->user();
90+
91+
if($authUser->checkPermissionTo(UsersPermissions::CAN_UPDATE_USERS_SELF) && !$authUser->is($user)) {
92+
return new ApiErrorResponse("You can only update your own user.", status: Response::HTTP_FORBIDDEN);
93+
}
94+
5995
$user->update($validated);
6096

6197
return new ApiSuccessResponse($user);

app/Permissions/UsersPermissions.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Permissions;
4+
5+
/**
6+
* Class UsersPermissions
7+
*
8+
* This class defines the permissions related to users.
9+
*/
10+
class UsersPermissions
11+
{
12+
/** Permission for listing and view all users. */
13+
public const CAN_LIST_USERS = 'list-users';
14+
15+
/** Permission for showing users itself. */
16+
public const CAN_SHOW_USERS = 'show-users';
17+
18+
/** Permission for creating users. */
19+
public const CAN_CREATE_USERS = 'create-users';
20+
21+
/** Permission for updating users. */
22+
public const CAN_UPDATE_USERS = 'update-users';
23+
24+
/** Permission for updating users itself. */
25+
public const CAN_UPDATE_USERS_SELF = 'update-users-self';
26+
27+
/** Permission for deleting users. */
28+
public const CAN_DELETE_USERS = 'delete-users';
29+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
use App\Permissions\UsersPermissions;
4+
use Carbon\Carbon;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Support\Facades\DB;
7+
8+
return new class extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*/
13+
public function up(): void
14+
{
15+
DB::table('permissions')->insert(
16+
[
17+
[
18+
'name' => UsersPermissions::CAN_LIST_USERS,
19+
'guard_name' => 'web',
20+
'created_at' => Carbon::now(),
21+
'updated_at' => Carbon::now(),
22+
],
23+
[
24+
'name' => UsersPermissions::CAN_SHOW_USERS,
25+
'guard_name' => 'web',
26+
'created_at' => Carbon::now(),
27+
'updated_at' => Carbon::now(),
28+
],
29+
[
30+
'name' => UsersPermissions::CAN_CREATE_USERS,
31+
'guard_name' => 'web',
32+
'created_at' => Carbon::now(),
33+
'updated_at' => Carbon::now(),
34+
],
35+
[
36+
'name' => UsersPermissions::CAN_UPDATE_USERS,
37+
'guard_name' => 'web',
38+
'created_at' => Carbon::now(),
39+
'updated_at' => Carbon::now(),
40+
],
41+
[
42+
'name' => UsersPermissions::CAN_UPDATE_USERS_SELF,
43+
'guard_name' => 'web',
44+
'created_at' => Carbon::now(),
45+
'updated_at' => Carbon::now(),
46+
],
47+
[
48+
'name' => UsersPermissions::CAN_DELETE_USERS,
49+
'guard_name' => 'web',
50+
'created_at' => Carbon::now(),
51+
'updated_at' => Carbon::now(),
52+
]
53+
]
54+
);
55+
}
56+
57+
/**
58+
* Reverse the migrations.
59+
*/
60+
public function down(): void
61+
{
62+
DB::table('permissions')->whereIn('name', [
63+
UsersPermissions::CAN_LIST_USERS,
64+
UsersPermissions::CAN_SHOW_USERS,
65+
UsersPermissions::CAN_CREATE_USERS,
66+
UsersPermissions::CAN_UPDATE_USERS,
67+
UsersPermissions::CAN_UPDATE_USERS_SELF,
68+
UsersPermissions::CAN_DELETE_USERS,
69+
])->delete();
70+
}
71+
};

0 commit comments

Comments
 (0)