-
Notifications
You must be signed in to change notification settings - Fork 0
[DRAFT] feat: RBAC Phase 4 - Role Management CRUD API #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+732
β0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
app/Http/Controllers/Api/V1/RoleManagementController.php
This file contains hidden or 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,147 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace App\Http\Controllers\Api\V1; | ||
|
|
||
| use App\Http\Controllers\Controller; | ||
| use App\Http\Requests\Api\V1\CreateRoleRequest; | ||
| use App\Http\Requests\Api\V1\UpdateRoleRequest; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Response; | ||
| use Illuminate\Support\Facades\Gate; | ||
| use Spatie\Permission\Models\Role; | ||
|
|
||
| class RoleManagementController extends Controller | ||
| { | ||
| /** | ||
| * List all roles with permission count and user count. | ||
| */ | ||
| public function index(): JsonResponse | ||
| { | ||
| Gate::authorize('viewAny', Role::class); | ||
|
|
||
| $roles = Role::withCount(['permissions', 'users']) | ||
| ->orderBy('name') | ||
| ->get(); | ||
|
|
||
| return response()->json([ | ||
| 'data' => $roles->map(fn (Role $role) => [ | ||
| 'id' => $role->id, | ||
| 'name' => $role->name, | ||
| 'permissions_count' => $role->permissions_count, | ||
| 'users_count' => $role->users_count, | ||
| 'created_at' => $role->created_at?->toIso8601String() ?? '', | ||
| 'updated_at' => $role->updated_at?->toIso8601String() ?? '', | ||
| ]), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new role with permissions. | ||
| */ | ||
| public function store(CreateRoleRequest $request): JsonResponse | ||
| { | ||
| Gate::authorize('create', Role::class); | ||
|
|
||
| /** @var string $name */ | ||
| $name = $request->input('name'); | ||
| $role = Role::create(['name' => $name]); | ||
|
|
||
| /** @var array<int, string> $permissions */ | ||
| $permissions = $request->input('permissions', []); | ||
| if (! empty($permissions)) { | ||
| $role->syncPermissions($permissions); | ||
| } | ||
|
|
||
| return response()->json([ | ||
| 'data' => [ | ||
| 'id' => $role->id, | ||
| 'name' => $role->name, | ||
| 'permissions' => $role->fresh()?->permissions->pluck('name') ?? [], | ||
| 'created_at' => $role->created_at?->toIso8601String() ?? '', | ||
| 'updated_at' => $role->updated_at?->toIso8601String() ?? '', | ||
| ], | ||
| ], 201); | ||
| } | ||
|
|
||
| /** | ||
| * Get role details with assigned permissions. | ||
| */ | ||
| public function show(int $id): JsonResponse | ||
| { | ||
| $role = Role::with('permissions')->findOrFail($id); | ||
| Gate::authorize('view', $role); | ||
|
|
||
| return response()->json([ | ||
| 'data' => [ | ||
| 'id' => $role->id, | ||
| 'name' => $role->name, | ||
| 'permissions' => $role->permissions->pluck('name'), | ||
| 'users_count' => $role->users()->count(), | ||
| 'created_at' => $role->created_at?->toIso8601String() ?? '', | ||
| 'updated_at' => $role->updated_at?->toIso8601String() ?? '', | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Update role name and/or permissions. | ||
| */ | ||
| public function update(UpdateRoleRequest $request, int $id): JsonResponse | ||
| { | ||
| $role = Role::findOrFail($id); | ||
| Gate::authorize('update', $role); | ||
|
|
||
| if ($request->filled('name')) { | ||
| /** @var string $name */ | ||
| $name = $request->input('name'); | ||
| $role->name = $name; | ||
| $role->save(); | ||
| } | ||
|
|
||
| if ($request->has('permissions')) { | ||
| /** @var array<int, string> $permissions */ | ||
| $permissions = $request->input('permissions', []); | ||
| $role->syncPermissions($permissions); | ||
| } | ||
|
|
||
| $freshRole = $role->fresh(); | ||
|
|
||
| return response()->json([ | ||
| 'data' => [ | ||
| 'id' => $role->id, | ||
| 'name' => $role->name, | ||
| 'permissions' => $freshRole?->permissions->pluck('name') ?? [], | ||
| 'created_at' => $role->created_at?->toIso8601String() ?? '', | ||
| 'updated_at' => $role->updated_at?->toIso8601String() ?? '', | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Delete role (only if not assigned to any users). | ||
| */ | ||
| public function destroy(int $id): Response|JsonResponse | ||
| { | ||
| $role = Role::findOrFail($id); | ||
| Gate::authorize('delete', $role); | ||
|
|
||
| $usersCount = $role->users()->count(); | ||
|
|
||
| if ($usersCount > 0) { | ||
| return response()->json([ | ||
| 'message' => 'Cannot delete role while assigned to users', | ||
| 'assigned_to' => $usersCount, | ||
| ], 422); | ||
| } | ||
|
|
||
| $role->delete(); | ||
|
|
||
| return response()->noContent(); | ||
| } | ||
| } | ||
This file contains hidden or 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,64 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace App\Http\Requests\Api\V1; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use Spatie\Permission\Models\Permission; | ||
|
|
||
| class CreateRoleRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return true; // Authorization handled by Gate in controller | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, array<int, mixed>> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| 'name' => ['required', 'string', 'max:255', 'unique:roles,name'], | ||
| 'permissions' => ['nullable', 'array'], | ||
| 'permissions.*' => [ | ||
| 'required', | ||
| 'string', | ||
| function (string $attribute, mixed $value, \Closure $fail): void { | ||
| if (! is_string($value)) { | ||
| $fail('The permission must be a valid string.'); | ||
|
|
||
| return; | ||
| } | ||
| if (! Permission::where('name', $value)->exists()) { | ||
| $fail("The permission {$value} does not exist."); | ||
| } | ||
| }, | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get custom error messages for validation rules. | ||
| * | ||
| * @return array<string, string> | ||
| */ | ||
| public function messages(): array | ||
| { | ||
| return [ | ||
| 'name.required' => 'Role name is required.', | ||
| 'name.unique' => 'A role with this name already exists.', | ||
| 'permissions.*.required' => 'Each permission must be a valid string.', | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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,72 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace App\Http\Requests\Api\V1; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use Illuminate\Validation\Rule; | ||
| use Spatie\Permission\Models\Permission; | ||
|
|
||
| class UpdateRoleRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return true; // Authorization handled by Gate in controller | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, array<int, mixed>> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| $roleId = $this->route('id'); | ||
|
|
||
| return [ | ||
| 'name' => [ | ||
| 'sometimes', | ||
| 'required', | ||
| 'string', | ||
| 'max:255', | ||
| Rule::unique('roles', 'name')->ignore($roleId), | ||
| ], | ||
| 'permissions' => ['nullable', 'array'], | ||
| 'permissions.*' => [ | ||
| 'required', | ||
| 'string', | ||
| function (string $attribute, mixed $value, \Closure $fail): void { | ||
| if (! is_string($value)) { | ||
| $fail('The permission must be a valid string.'); | ||
|
|
||
| return; | ||
| } | ||
| if (! Permission::where('name', $value)->exists()) { | ||
| $fail("The permission {$value} does not exist."); | ||
| } | ||
| }, | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get custom error messages for validation rules. | ||
| * | ||
| * @return array<string, string> | ||
| */ | ||
| public function messages(): array | ||
| { | ||
| return [ | ||
| 'name.unique' => 'A role with this name already exists.', | ||
| 'permissions.*.required' => 'Each permission must be a valid string.', | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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,55 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace App\Policies; | ||
|
|
||
| use App\Models\User; | ||
| use Spatie\Permission\Models\Role; | ||
|
|
||
| class RoleManagementPolicy | ||
| { | ||
| /** | ||
| * Determine whether the user can view any models. | ||
| */ | ||
| public function viewAny(User $user): bool | ||
| { | ||
| return $user->can('roles.read'); | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can view the model. | ||
| */ | ||
| public function view(User $user, Role $role): bool | ||
| { | ||
| return $user->can('roles.read'); | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can create models. | ||
| */ | ||
| public function create(User $user): bool | ||
| { | ||
| return $user->can('roles.create'); | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can update the model. | ||
| */ | ||
| public function update(User $user, Role $role): bool | ||
| { | ||
| return $user->can('roles.update'); | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can delete the model. | ||
| */ | ||
| public function delete(User $user, Role $role): bool | ||
| { | ||
| return $user->can('roles.delete'); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.