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.

Abilities don't behave the way I expected from the docs

See original GitHub issue

Hey @stalniy, First of all, thank you for the great library. I’ve been going through the docs and, no matter what, I can’t make the rules work as I would like to.

Describe the bug

My goal is to define a set of abilities so that we can only access a Person with id = 2. Therefore:

  • I shouldn’t be able to access a Person with an ID != 2
  • I shouldn’t be able to access a Person without specifying an ID

To Reproduce

Minimal example to reproduce:

import { Ability, InferSubjects, AbilityBuilder, subject } from "@casl/ability";

export type Actions = "create" | "read" | "update" | "delete";

interface Person {
  id: number;
}

export type SubjectTypes = Person | "Person";
export type Subjects = InferSubjects<SubjectTypes>;
export type MyAbility = Ability<[Actions, Subjects]>;

const getAbilities = (): MyAbility => {
  const { can, cannot, build } = new AbilityBuilder<MyAbility>();
  can("read", "Person", { id: { $in: [ 2 ] } })
  return build()
}

const abilities = getAbilities()
console.log(abilities.can("read", "Person"))
console.log(abilities.can("read", subject("Person", {id: 1})))
console.log(abilities.can("read", subject("Person", {id: 2})))

Expected behavior I would expect to get: -> console.log(abilities.can("read", "Person")) -> False (because I should only be able to access Person with ID = 2) -> console.log(abilities.can("read", subject("Person", {id: 1}))) -> False -> console.log(abilities.can("read", subject("Person", {id: 2}))) -> True

What I get instead is true to all the conditions.

I also tried using defineAbility instead:

const abilities = defineAbility((can) => {
  can("read", "Person", { id: { $in: [2] } });
});

and in that case, I get: -> console.log(abilities.can("read", "Person")) -> True (shouldn’t it be false?) -> console.log(abilities.can("read", subject("Person", {id: 1}))) -> False Ok -> console.log(abilities.can("read", subject("Person", {id: 2}))) -> True Ok

Interactive example

https://codesandbox.io/s/modern-breeze-q5ue1?file=/src/index.ts

CASL Version

@casl/ability - v 4.1.6

Environment:

Windows 10, Node JS: v12.16.1

Do you have any idea why I’m facing these problems? Thank you for your time

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
stalniycommented, Oct 9, 2020

I read about grammar difference between may and might. And the last one fits better. So let’s stick to “might” for now

1reaction
stalniycommented, Oct 6, 2020

RFC is a good idea. Take into consideration:

  1. rule definitions through AbilityBuilder (so we don’t need to add may to AbilityBuilder)
  2. accessibleBy method in @casl/mongoose. Will it be clear what records will be returned if you define both can have Apple and can have green apple

So, we don’t confuse people more

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conflict Resolution Skills
Managing and Resolving Conflict in a Positive Way. Conflict is a normal, and even healthy, part of relationships. After all, two people can't...
Read more >
Complete Guide to Managing Behavior Problems
Handling big emotions in a healthy, mature way requires a variety of skills, including: Impulse control; Emotional self-regulation; Problem ...
Read more >
Managing Your Emotional Reactions (for Teens)
Don't worry. Everyone can develop the skill of responding well when emotions run high. It just takes a bit more practice for some...
Read more >
Disrespectful Behavior in Health Care - PMC - NCBI - NIH
Disrespect causes the recipient to experience fear, anger, shame, confusion, uncertainty, isolation, self-doubt, depression, and a whole host of physical ...
Read more >
Driving Skills Test Study Guide
The examiner will observe and score you on specific maneuvers as well as on your general driving behavior. You are expected to drive...
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