fix: add idempotency check to role assignment API #164
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.
🐛 Bug Fix: Role Assignment Idempotency
Problem
Phase 3 role assignment API (
POST /api/v1/users/{id}/roles) did not handle idempotent role assignments. Attempting to assign the same role twice to the same user caused a database unique constraint violation (500 Internal Server Error).Root Cause
RoleController::store()did not check if the role was already assigned before attempting to insert intomodel_has_rolestable, which has a unique constraint on(tenant_id, role_id, model_id, model_type).Solution
✅ Added idempotency check in
RoleController::store():200 OKwith existing assignment if role already assigned✅ Activated skipped integration test:
✅ Added edge case test:
🧪 Testing
Test Results
New Tests
role assignment is idempotent(previously skipped)idempotency returns existing role with different temporal parameters(new edge case)📝 Changes Summary
Modified Files (2)
app/Http/Controllers/RoleController.php- Added idempotency checktests/Feature/Integration/RbacIntegrationTest.php- Activated skipped test + new testLines Changed
✅ Acceptance Criteria
200 OK(not 500)🔗 Related
Fixes #163
Discovered in: PR #162 (Permission Naming Bug Fix)
📊 Impact
Severity: Medium → Fixed
Type: Bug Fix
Priority: Medium
Breaking Changes: None