-
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
Conversation
🎉 Blocker Resolved - Action Required✅ Issue #125 CompleteThe blocking issue (#125 - Guard Migration) has been successfully completed and merged! All 6 sub-issues are done:
|
| Feature | Status | Recommendation |
|---|---|---|
| Role Management CRUD API | ✅ This PR | Correct |
| Permission CRUD API | ❌ Missing | New sub-issue + PR |
| Direct Permission Assignment | ❌ Missing | New sub-issue + PR |
| Predefined Roles Seeder | ❌ Missing | New sub-issue + PR |
| API Documentation | ❌ Missing | New sub-issue + PR |
I'll create sub-issues for #108 following the Epic workflow pattern from #125.
🎯 Summary
This PR is technically correct! Just needs:
- Rebase to resolve config duplicate
- Test verification (should be green now)
- DRAFT removal
Once rebased, this should be ready to merge! 🚀
References:
- Blocker [EPIC] Migrate Permission System from 'web' to 'sanctum' Guard #125: ✅ CLOSED
- Config Set sanctum as default guard in config/auth.php #134: ✅ MERGED via fix(auth): Set sanctum as default guard in config/auth.php #135
- Parent Epic RBAC Phase 4: Documentation & Final Testing #108: 🚧 Additional sub-issues coming
🔍 Test Fix Required: Missing
|
- Add RoleManagementController with 5 CRUD endpoints - Add CreateRoleRequest and UpdateRoleRequest with validation - Add RoleManagementPolicy with 5 authorization methods - Register policy in AppServiceProvider - Add 26 comprehensive PEST tests (TDD approach) - Configure sanctum guard in auth.php - Add API routes for role management Related to #108 (RBAC Phase 4) Blocked by guard architecture migration (see next issue)
0c3526c to
b6a9d08
Compare
💡 Tip: Consider Using Draft PRsBenefits of opening PRs as drafts initially:
How to convert:
This is just a friendly reminder - feel free to continue as is! 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a comprehensive CRUD API for role management, introducing new endpoints for creating, reading, updating, and deleting roles with permission assignments. The implementation follows Laravel's API resource pattern with proper authorization, validation, and test coverage.
Key changes:
- New
RoleManagementControllerimplementing CRUD operations for roles with permission management - Form request classes (
CreateRoleRequest,UpdateRoleRequest) for validation with custom error messages RoleManagementPolicydefining authorization rules for role operations- Comprehensive test suite covering authentication, authorization, validation, and business logic scenarios
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| app/Http/Controllers/Api/V1/RoleManagementController.php | New controller implementing index, store, show, update, and destroy methods for role management |
| app/Http/Requests/Api/V1/CreateRoleRequest.php | Form request with validation rules for creating roles including permission existence checks |
| app/Http/Requests/Api/V1/UpdateRoleRequest.php | Form request with validation rules for updating roles with unique name constraint ignoring current role |
| app/Policies/RoleManagementPolicy.php | Policy class defining viewAny, view, create, update, and delete authorization methods |
| app/Providers/AppServiceProvider.php | Registers RoleManagementPolicy for Spatie's Role model |
| routes/api.php | Registers five new role management endpoints under /v1/roles |
| tests/Feature/RoleManagementApiTest.php | Comprehensive feature tests covering all endpoints with authentication, authorization, and validation scenarios |
- Fix N+1 query in index() by eager loading users_count - Remove description field (not in DB schema) - Removed validation from CreateRoleRequest/UpdateRoleRequest - Removed from all controller responses - Updated test expectations Addresses: Copilot PR review comments on PR #131
💡 Tip: Consider Using Draft PRsBenefits of opening PRs as drafts initially:
How to convert:
This is just a friendly reminder - feel free to continue as is! 😊 |
🎯 Objective
Implement RESTful Role Management CRUD API as part of RBAC Phase 4 (#108).
📊 Status
This PR is in DRAFT status because tests are currently failing due to Laravel Guard architecture mismatch. See blocking issue #125 for full technical context.
🚧 Blocking Issue
#125: [EPIC] Migrate Permission System from 'web' to 'sanctum' Guard
Problem: Permissions created with default
guard_name='web', but User authenticated viaauth:sanctummiddleware. Spatie Laravel-Permission checks fail due to guard mismatch.Impact: 36 tests failing (3 in this PR's test file, 33 in existing tests)
Solution: Systematic migration to explicit
guard_name='sanctum'across all test files and User model.✅ What's Implemented
1. Controller (
app/Http/Controllers/Api/V1/RoleManagementController.php)Full RESTful CRUD implementation:
index()- List all roles with permission countsstore(CreateRoleRequest)- Create role with permissionsshow($id)- Get role detail with permissionsupdate(UpdateRoleRequest, $id)- Update role name/permissionsdestroy($id)- Delete role (with safety check for assigned users)Quality Gates:
2. Form Requests
CreateRoleRequest.php (66 lines):
RoleManagementPolicy::create()UpdateRoleRequest.php (71 lines):
RoleManagementPolicy::update()3. Policy (
app/Policies/RoleManagementPolicy.php)5 authorization methods:
viewAny()→ checksroles.readview()→ checksroles.readcreate()→ checksroles.createupdate()→ checksroles.updatedelete()→ checksroles.deleteRegistered in
AppServiceProviderviaGate::policy().4. Routes (
routes/api.php)5 RESTful endpoints under
/v1/roles:All protected with
auth:sanctummiddleware.5. Auth Config (
config/auth.php)Added sanctum guard configuration:
Purpose: Enables
$role->users()->count()to work correctly in controller.6. Tests (
tests/Feature/RoleManagementApiTest.php)26 comprehensive tests covering:
Test Status:
Failing Tests:
GET /v1/roles - List Roles → returns list of roles with permissionsGET /v1/roles/{id} - Get Role Details → returns role with permissionsPATCH /v1/roles/{id} - Update Role → updates role successfullyError Message:
🔧 Technical Implementation Details
Safety Features
Database Interactions
Role::withCount('permissions')for efficient loading$role->users()->count()before deletionsyncPermissions()(add/remove in single operation)Response Structure
{ "data": { "id": 1, "name": "Regional Manager", "description": "Manages multiple branches", "permissions": ["employees.read", "shifts.read"], "permissions_count": 2, "assigned_to": 5, "created_at": "2025-11-09T10:00:00.000000Z", "updated_at": "2025-11-09T10:00:00.000000Z" } }🔜 Next Steps
📊 Current Test Status
Before Guard Migration:
Expected After #125:
🔗 Related Issues
Implements:
Blocked by:
📝 Review Notes
This PR demonstrates TDD RED phase intentionally:
Architecture Decision: Preferred systematic migration over quick fix to avoid technical debt. This PR preserves context and demonstrates proper issue workflow.
Status: 🚧 DRAFT (waiting for #125)
Ready for Review: After #125 completed
Estimated Completion: 2-3 hours after #125 started