Rules with empty field array
See original GitHub issueThe 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:
- Created 3 years ago
- Comments:11 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
will change to error in the next major version