refactor: casl factory - policies#2811
Open
HayenNico wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
PolicyAbility,accessGroupsis left untyped and initialized from config in the constructor; consider typing it explicitly asAccessGroupsType | undefinedto match existing patterns and make the code safer and clearer. - The logic change from the previous
policyEndpointAccess(which usedif/else if) to two independentifblocks inPolicyAbility.buildAbilitymeans users in both delete and admin/policy groups now get both delete and CRUD permissions instead of only delete; double-check that this change in privilege behavior is intentional. - Since
PolicyAbilitycurrently only exposesbuildAbility, you might consider making this a stateless helper (or a pure function takingaccessGroupsas input) and injectingaccessGroupsfromCaslAbilityFactoryinstead, which would reduce configuration lookups and keep all CASL wiring in one place.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `PolicyAbility`, `accessGroups` is left untyped and initialized from config in the constructor; consider typing it explicitly as `AccessGroupsType | undefined` to match existing patterns and make the code safer and clearer.
- The logic change from the previous `policyEndpointAccess` (which used `if`/`else if`) to two independent `if` blocks in `PolicyAbility.buildAbility` means users in both delete and admin/policy groups now get both delete and CRUD permissions instead of only delete; double-check that this change in privilege behavior is intentional.
- Since `PolicyAbility` currently only exposes `buildAbility`, you might consider making this a stateless helper (or a pure function taking `accessGroups` as input) and injecting `accessGroups` from `CaslAbilityFactory` instead, which would reduce configuration lookups and keep all CASL wiring in one place.
## Individual Comments
### Comment 1
<location path="src/casl/abilities/policies.ability.ts" line_range="21-25" />
<code_context>
+ this.accessGroups =
+ this.configService.get<AccessGroupsType>("accessGroups");
+ }
+ private accessGroups;
+
+ buildAbility(user: JWTUser): MongoAbility<PossibleAbilities, Conditions> {
</code_context>
<issue_to_address>
**suggestion:** Type the `accessGroups` property to match `AccessGroupsType` for stronger safety.
Declare this as `private accessGroups?: AccessGroupsType;` to match the `ConfigService.get<AccessGroupsType>` call, avoid the implicit `any`, and catch type mismatches at compile time.
```suggestion
constructor(private configService: ConfigService) {
this.accessGroups =
this.configService.get<AccessGroupsType>("accessGroups");
}
private accessGroups?: AccessGroupsType;
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Comment on lines
+21
to
+25
| constructor(private configService: ConfigService) { | ||
| this.accessGroups = | ||
| this.configService.get<AccessGroupsType>("accessGroups"); | ||
| } | ||
| private accessGroups; |
There was a problem hiding this comment.
suggestion: Type the accessGroups property to match AccessGroupsType for stronger safety.
Declare this as private accessGroups?: AccessGroupsType; to match the ConfigService.get<AccessGroupsType> call, avoid the implicit any, and catch type mismatches at compile time.
Suggested change
| constructor(private configService: ConfigService) { | |
| this.accessGroups = | |
| this.configService.get<AccessGroupsType>("accessGroups"); | |
| } | |
| private accessGroups; | |
| constructor(private configService: ConfigService) { | |
| this.accessGroups = | |
| this.configService.get<AccessGroupsType>("accessGroups"); | |
| } | |
| private accessGroups?: AccessGroupsType; |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Subsection of PR #2748 for policies.
This refactors the policyEndpointAccess function in CaslAbilityFactory, extracting the ability builder into a separate module and adding policyAccess in CaslAbilityFactory.
Changes:
Tests included
Documentation
Summary by Sourcery
Extract policy access ability building into a dedicated injectable and update consumers to use the new policy access entrypoint.
Enhancements: