Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

docs(security): update deprecated method from @casl/ability #2597

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

Open
usystemsoftwares wants to merge 1 commit into nestjs:master
base: master
Choose a base branch
Loading
from usystemsoftwares:master

Conversation

Copy link

@usystemsoftwares usystemsoftwares commented Jan 27, 2023

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Docs
  • Other... Please describe:

What is the current behavior?

Warning message - "@deprecated use createMongoAbility function instead and MongoAbility interface. In the next major version PureAbility will be renamed to Ability and this class will be removed"

What is the new behavior?

Keep official documentation up to date!

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Happy to be able to keep up to date the official documentation of this exceptional framework that is Nest.js.

Copy link
Member

@jmcdo29 jmcdo29 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Ability<[Action, Subjects]>
>(Ability as AbilityClass<AppAbility>);
const { can, cannot, build } = new AbilityBuilder<AppAbility>(
createMongoAbility,
Copy link
Member

@kamilmysliwiec kamilmysliwiec Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guide isn't mongo specific so why "createMongoAbility"?

Copy link
Contributor

@Tony133 Tony133 Feb 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see here: https://github.com/stalniy/casl/blob/master/packages/casl-ability/src/Ability.ts#L9,

If I am honest, the name "Ability" was much better, while "MongoAbility" which is very confusing.

@@ -234,16 +234,16 @@ With this in place, we can define the `createForUser()` method on the `CaslAbili
```typescript
type Subjects = InferSubjects<typeof Article | typeof User> | 'all';

export type AppAbility = Ability<[Action, Subjects]>;
type AppAbility = MongoAbility<[Action, Subjects]>;
Copy link
Member

@kamilmysliwiec kamilmysliwiec Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guide isn't mongo specific so why "MongoAbility"?


if (user.isAdmin) {
if (user) {
Copy link
Member

@kamilmysliwiec kamilmysliwiec Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link

edgahan commented Jul 5, 2023
edited
Loading

Disclaimer: I am not 100% sure about this but I've just tried to get Casl working with Nest today and the only way I got it working was like this:

// I did not use createMongoAbility or anything with the word Mongo in it, instead I used PureAbility
import {
 PureAbility,
 AbilityBuilder,
 ExtractSubjectType,
 InferSubjects,
 AbilityClass,
 MatchConditions,
} from '@casl/ability';
const Ability = PureAbility as AbilityClass<AppAbility>;
// I created a lambdaMatcher as this seemed to be the only way to check attributes (below)
const lambdaMatcher = (matchConditions: MatchConditions) => matchConditions;
type Subjects =
 | InferSubjects<typeof User | typeof Entity>
 | 'all';
export enum Action {
 MANAGE = 'manage',
 CREATE = 'create',
 READ = 'read',
 UPDATE = 'update',
 DESTROY = 'destroy',
}
@Injectable()
export class CaslAbilityFactory {
 createForUser(user: User | AccountUser) {
 const { can, cannot, build } = new AbilityBuilder(Ability);
 
 // Note: probably nicer to check user role here?
 if (user.isAdmin) {
 can(Action.READ, Entity, (e: Entity) => {
 // Return a boolean, note: `e` is an instance
 // Note I could not get it working as per the documentation with the object syntax 
 return e.account.id === user.account.id;
 });
 // Shorthand example
 can(Action.READ, Entity, (e: Entity) => e.userId === user.id);
 }
 return build({
 detectSubjectType: (item) =>
 item.constructor as ExtractSubjectType<Subjects>,
 // Add conditionsMatcher
 conditionsMatcher: lambdaMatcher,
 });
 }
}

And then in an example Entity controller, I have:

export class EntityController {
 constructor(
 private readonly caslAbilityFactory: CaslAbilityFactory,
 ) {}
 
 @Get(':id')
 async findOne(@Param('id') id: string, @Req() req: RequestWithUser) {
 const ability = this.caslAbilityFactory.createForUser(req.user);
 const entity = await this.entitiesService.findOneById(id);
 // Note we pass in entity instance not Entity class
 if (ability.can(Action.READ, entity)) {
 // Authorized
 }
 }
}
hkdobrev, Philipinho, and sergsar reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@kamilmysliwiec kamilmysliwiec kamilmysliwiec left review comments

+2 more reviewers

@Tony133 Tony133 Tony133 approved these changes

@jmcdo29 jmcdo29 jmcdo29 approved these changes

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /