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.

Rules with empty field array

See original GitHub issue

The following might be a far-fetched use case (I tried it as a workaround when implementing it into our current project), but I’d be really interested in some feedback.

I define rules which permit the admin to access a/some fields in the article subject. For the normal user role I defined an empty array, which should mean: “Do not allow to access any fields” (I realize, that it would be more appropriate to simply give no permissions at all, but it was needed as workaround in our environment).

function defineAbilities(role: 'user' | 'admin') {
  const { can, rules } = new AbilityBuilder<Ability>();
  can('get', 'article', []);
  if (role === 'admin') {
    can('get', 'article', ['secret']);
  }
  return new Ability(rules);
}

When I check the permitted fields for the user type, I get an empty array as expected:

const abilities = defineAbilities('user');
permittedFieldsOf(abilities, 'get', 'article'); // []

… but when I check my ability to get the secret field, this returns true:

abilities.can('get', 'article', 'secret'); // true

I’d appreciate any feedback!

Complete test case:

import { Ability, AbilityBuilder } from '@casl/ability';
import { permittedFieldsOf } from '@casl/ability/extra';

describe('abilities', () => {
  function defineAbilities(role: 'user' | 'admin') {
    const { can, rules } = new AbilityBuilder<Ability>();
    can('get', 'article', []);
    if (role === 'admin') {
      can('get', 'article', ['secret']);
    }
    return new Ability(rules);
  }

  describe('as admin', () => {
    const abilities = defineAbilities('admin');
    it('has permitted field secret', () => {
      expect(permittedFieldsOf(abilities, 'get', 'article')).toEqual(['secret']);
    });
    it('can get article secret', () => {
      expect(abilities.can('get', 'article', 'secret')).toBeTrue();
    });
  });

  describe('as user', () => {
    const abilities = defineAbilities('user');
    it('has not permitted fields', () => {
      expect(permittedFieldsOf(abilities, 'get', 'article')).toEqual([]);
    });
    it('cannot get article secret', () => {
      expect(abilities.can('get', 'article', 'secret')).toBeFalse(); // <-- fails
    });
  });
});

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
cyrus-zacommented, Dec 31, 2020

I know the issue is a few months old, but I believe it’s worth pointing out that, the result here is helping me a lot. It might seem strange to pass in an empty array as it seems nonsensical, but with a data-driven solution, one requires an ‘all’ value and defaulting to an empty array to allow all fields is great.

1reaction
stalniycommented, May 27, 2020

will change to error in the next major version

Read more comments on GitHub >

github_iconTop Results From Across the Web

Check if array fields are empty in PHP - Stack Overflow
I need to check if the fields in an array are empty. I would like to know if there is any function in...
Read more >
Empty arrays and collections should be returned instead of null
Returning null instead of an actual array, collection or map forces callers of the method to explicitly test for nullity, making them more...
Read more >
How to check whether an array is empty using PHP?
Using sizeof() function: This method check the size of array. If the size of array is zero then array is empty otherwise array...
Read more >
Require field only if another field is a non-empty array : r/laravel
I have one field that I need to make required only if another field is a non-empty array. date is required if products...
Read more >
How to display an empty field in an error message - ServiceNow
So here, all you would need to do is add the field name to the "fields" array at the top of the script...
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