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

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

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

Conversation

usystemsoftwares
Copy link

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

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

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

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

Choose a reason for hiding this comment

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

Why?

@edgahan
Copy link

edgahan commented Jul 5, 2023

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
      }
    }
}

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

Successfully merging this pull request may close these issues.

6 participants