question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected ability checking behavior

See original GitHub issue

Describe the bug Result of ability checking not obviously depends on ability definition

To Reproduce Steps to reproduce the behavior:

  1. Ability configuration (rules, detectSubjectType, etc)
class Lot {
  constructor(
    public id: number,
    public isPublished: boolean,
    public userId: number,
  ) {}
}

class User {
  constructor(public id: number) {}
}

enum Action {
  Manage = 'manage',
  Create = 'create',
  Read = 'read',
  Update = 'update',
  Delete = 'delete',
}

type Subjects = typeof Lot | Lot | 'Lot';

export type LotAbility = Ability<[Action, Subjects]>;

export class LotAbilityFactory {
  createForUser(user: User) {
    const { can, build, rules } = new AbilityBuilder<LotAbility>(
      Ability as AbilityClass<LotAbility>,
    );

    can(Action.Read, 'Lot');
    can(Action.Update, Lot);

    console.log('rules', rules);

    return build();
  }
}
  1. How do you check abilities
const user = new User(1);
const lot = new Lot(2, false, user.id);
const lotAbility = new LotAbilityFactory().createForUser(user);

lotAbility.can(Action.Update, lot); // false

Expected behavior Obviously that lot is a instance of Lot and it’s should be matched with can(Action.Update, Lot) and return true

Interactive example (optional, but highly desirable) My example and Rewritten example

CASL Version

@casl/ability”: “5.2.1”,

Environment:

node -v v15.5.1 ts-node -v v9.1.1 “typescript”: “4.0.5”

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Blaze34commented, Jan 24, 2021

Alternatively, you may use strings and replace can(Action.Update, Lot); with can(Action.Update, ‘Lot’); (pay attention that the 2nd argument is a string!)

I found out it while was testing, but it’s unobviously. Anyway documentation is confusing and should be fixed asap.

0reactions
stalniycommented, Jan 24, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Teaching Expected and Unexpected Behaviors
One of the key components of talking about expected and unexpected behavior is encouraging students to consider how their behaviors make others ......
Read more >
Expected And Unexpected Behaviors Teaching Resources | TPT
Expected and Unexpected Behavior ... This is a great tool to review behavioral expectations with students in primary grades. Use it before ...
Read more >
Obsessive-Compulsive Disorder: When Unwanted Thoughts ...
People with OCD may feel the urge to check things repeatedly or perform routines for more than an hour each day as a...
Read more >
OCD - 8 Simple Steps to Stop Compulsively Checking
Delay your compulsion to check. This is called gradual exposure. Allow yourself time to monitor your “What If” thoughts and to create a...
Read more >
Obsessive-Compulsive Disorder (OCD) - HelpGuide.org
The sudden development of intense OCD symptoms can also be a sign of pediatric ... For example, if your compulsive behavior involves checking...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found