-
Notifications
You must be signed in to change notification settings - Fork 178
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
Does readOwn method include readAny inheritance? #14
Comments
No @sidewalksalsa. It's the other way around.
Checking only for own is not sensible and will typically return ..Own requires you to also check for the actual possession. The "check" and .. So, you could write it like this: const grantedIfOwn = req.user.name === req.params.username
&& ac.can(role).updateOwn('photo').granted; Now, although That's why you should conditionally (not optionally) also check for ..Any: const permission = (req.user.name === req.params.username)
? ac.can(role).updateOwn('photo')
: ac.can(role).updateAny('photo');
console.log(permission.granted); |
Cool, definitely makes sense. Thanks for the response @onury! Cheers. |
No problem at all. Thanks. |
How about using middleware for express.js? |
Maybe i'm wrong but with |
That's a pseudo express.js example. End-user can try and set |
I'm a a user foo. req.user.name will be set by server and equals to foo. I try to update a photo that is own by user bar. I set this object for example into the request
The condition will validate and i will be able to update the path of the photo. The only way i see is to retrieve the ressource from the server to get the original username Something like that :
|
The example/idea was making a request to a REST endpoint like: You could handle this request like below: app.post('/users/:username/photos/:photoid', (req, res) => {
const realUsername = req.user.name;
const targetUsername = req.params.username;
const permission = (realUsername === targetUsername)
? ac.can(req.user.role).updateOwn('photo')
: ac.can(req.user.role).updateAny('photo');
if (permission.granted) {
// get photo by the endpoint params — :username AND :photoid
// so this does not return a photo object if username does not match.
const photo = photoService.getUserPhoto(targetUsername, Number(req.params.photoid));
// return Not Found if no photo
if (!photo) return res.status(404).end();
const updateData = permission.filter(req.body);
const result = photoService.updatePhoto(photo.id, updateData);
return res.json(result);
}
// otherwise, return forbidden
return res.status(403).end();
}); Your concern would be correct if the endpoint was: app.post('/photos/:photoid', (req, res) => {
const photo = photoService.getPhoto(Number(req.params.photoid));
if (!photo) return res.status(404).end();
const permission = (req.user.name === photo.username)
? ac.can(req.user.role).updateOwn('photo')
: ac.can(req.user.role).updateAny('photo');
if (permission.granted) {
const result = photoService.updatePhoto(photo.id, permission.filter(req.body));
return res.json(result);
}
return res.status(403).end();
}); |
Ok thanks for you time :) |
Is there an option to set-up accesscontrol that some group of users, lets say Moderators,
Is there a better way? Thanks |
Why don't we add another chain function to check possession like this
|
Hi, So if I use "updateOwn", I don't know where the check the permissions. Thanks for your help |
Hi Onury,
Really like what you did with this library 👍 .
I have a question for you. If I have a permission structure like this:
And in my controller, I do a check for
can(role).readOwn(resource)
... if the user I am checking on has a role ofadmin
does the code assume that because admin's have access toread:any
they can alsoread:own
?That is what I am seeing happen in my debugger, which makes sense to me. I just want to confirm that is happening?
For instance, if I am logged in as an
admin
and a run a check forcan(role).readOwn(resource)
, I am returned TRUE fromgranted
, even though I did not specifically state an admin canread:own
in my permissions above.I ask because in another question I see that you suggest the following:
Is this necessary or can I just check
ac.can(role).updateOwn('photo')
?Thanks!
The text was updated successfully, but these errors were encountered: