Skip to content
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

Feauture request: allow user to set how to detect subject type #349

Open
ppodds opened this issue Jun 28, 2022 · 2 comments
Open

Feauture request: allow user to set how to detect subject type #349

ppodds opened this issue Jun 28, 2022 · 2 comments
Milestone

Comments

@ppodds
Copy link

ppodds commented Jun 28, 2022

Users are not allowed to set how to detect subject type when they are building abilities. It may cause the wrong subject type detection and get an unpredictable result.

casl docs

e.g.

it('test', async () => {
  expect(accessService.hasAbility(user, Actions.delete, new Post())).toBeTruthy(); // not pass
});

The test above would not pass but can be solved by the below solutions.

ability.factory.ts

...
// For PureAbility skip conditions check, conditions will be available for filtering through @CaslConditions() param
if (abilityClass === PureAbility) {
  return ability.build({ conditionsMatcher: nullConditionsMatcher, detectSubjectType: object => object.constructor as ExtractSubjectType<Subjects> });
}
return ability.build({ detectSubjectType: object => object.constructor as ExtractSubjectType<Subjects> });

or

it('test', async () => {
  const post = new Post();
  expect(accessService.hasAbility(user, Actions.delete, subject(post.constructor as any, post))).toBeTruthy(); // pass
});
@liquidautumn liquidautumn added this to the Milestone 0 milestone Oct 31, 2022
@belgamo
Copy link
Contributor

belgamo commented Oct 1, 2023

In addition to that, can we update the library to support string as the subject type? CASL by nature supports that. Currently, this library only accepts class types, making typescript complain when providing string types, although the code works.

@belgamo
Copy link
Contributor

belgamo commented Oct 2, 2023

Here is a practical example of what I mean. I had to cast the string to make TS stop complaining

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants