refactor: casl factory - users - #2815
Conversation
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- In Users.js the new tests for the admin password change endpoint use a URL with a trailing
}(e.g./api/v3/users/${userIdAdmin}/password}), which will hit the wrong route and should be corrected to/password. - The refactor now mixes
Action.AccessAnyfor guards withAction.UserRead/UserUpdateincheckUserAuthorization; consider making the admin vs non-admin semantics clearer by consistently usingAccessAnyfor admin-only endpoint checks and the user actions for self-access, to avoid confusion around which action is meant to represent admin-level access.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In Users.js the new tests for the admin password change endpoint use a URL with a trailing `}` (e.g. `/api/v3/users/${userIdAdmin}/password}`), which will hit the wrong route and should be corrected to `/password`.
- The refactor now mixes `Action.AccessAny` for guards with `Action.UserRead`/`UserUpdate` in `checkUserAuthorization`; consider making the admin vs non-admin semantics clearer by consistently using `AccessAny` for admin-only endpoint checks and the user actions for self-access, to avoid confusion around which action is meant to represent admin-level access.
## Individual Comments
### Comment 1
<location path="test/Users.js" line_range="213-215" />
<code_context>
});
- it("0050: admin should fail to change password for user when new and confirmation passwords do not match", async () => {
+ it("0050: anonymous user should not be able to access admin password change endpoint", async () => {
+ return request(appUrl)
+ .patch(`/api/v3/users/${userIdAdmin}/password}`)
+ .send({
+ newPassword: "compromisedPassword",
</code_context>
<issue_to_address>
**issue (bug_risk):** The admin password change endpoint URL in this test has a trailing `}` which likely makes the test hit the wrong route.
The controller route is `@Patch('/:id/password')`, so this URL should be `/api/v3/users/${userIdAdmin}/password` (no trailing `}`). As written, the test calls a non-existent endpoint and won’t properly cover the real admin password change route. Please fix the URL here and in the corresponding authenticated-user test below to match the controller route.
</issue_to_address>
### Comment 2
<location path="test/Users.js" line_range="224-221" />
<code_context>
+ .expect(TestData.UnauthorizedStatusCode);
+ });
+
+ it("0060: authenticated user should not be able to access admin password change endpoint", async () => {
+ return request(appUrl)
+ .patch(`/api/v3/users/${userIdAdmin}/password}`)
+ .send({
+ newPassword: "compromisedPassword",
+ confirmPassword: "compromisedPassword",
+ })
+ .set({ Authorization: `Bearer ${accessTokenUser1}` })
+ .set("Accept", "application/json")
+ .expect(TestData.UnauthorizedStatusCode);
+ });
+
</code_context>
<issue_to_address>
**question (bug_risk):** The expected status code for an authenticated non-admin hitting an admin-only endpoint may be incorrect.
For this admin-only `PATCH /users/:id/password` route, an authenticated non-admin should typically receive `403 Forbidden` (authenticated but lacks permission), not `401 Unauthorized`. The test currently expects `TestData.UnauthorizedStatusCode`. Please verify the actual response from `AuthenticatedPoliciesGuard` and `@CheckPolicies` for this endpoint and update the expected status code so the test matches the intended authorization behavior.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
d5810d1 to
a8c04ce
Compare
Code Review Report: refactor-casl-factory-users BranchOverview
Commits responsible: Code ChangesNew Files
Modified Files
Assessment
Improvement Needed
VerdictThe changes are well-executed and maintain backward compatibility while improving code structure. The refactoring follows the established pattern from the datasets refactoring (commit Security Review
Commits responsible: Test CoverageExisting Coverage
Coverage Assessment
Suggestions for Improvement
Security Examples
Testing for Security Use CasesTest: Unauthorized admin password change endpoint access
Test: Regular user data access isolation
SummaryThe
Critical improvements:
No breaking changes in functionality. All existing behavior is preserved while improving the codebase structure. The changes are approved for merging, with a minor recommendation to consider more explicit naming for the Generated by Mistral Vibe |
Description
Subsection of PR #2748 for users.
This refactors the userEndpointAccess function in CaslAbilityFactory, extracting the ability builder into a separate module and adding userAccess in CaslAbilityFactory. Instance-level Action elements are removed and the affected controllers adjusted to accommodate the change. The user-specific code is extracted into a separate module.
Changes:
Tests included
Documentation
Summary by Sourcery
Refactor CASL user access control by introducing a dedicated UserAbility, simplifying user actions, and updating controllers and tests to use the new unified permissions model.
New Features:
Enhancements:
Tests: