ACL – How to make $user->isAllowed(…) consider ownership as well
- BugsBunny
- Member | 5
I'd like my ACL to handle ownership as well. Instead of calling
$user->isAllowed($resource, $privilege)
I'd like to be able to call
$user->isAllowed($resource, $privilege, $userId, $resourceId)
and check whether this user owns the reource in question.
I edited Authorizator.php
in my /app/modules
folder accordingly and it works just fine. What bugs me is that I also had to
edit /nette/security/src/Security/User.php
, which is
baaad! I do NOT want to edit core parts of framework (for too
many obviuos reasons).
What would be the best solution for me?
Last edited by BugsBunny (2015-02-16 07:14)
- Filip Klimeš
- Nette Blogger | 156
Nette\Security\User represents a single signed in user. You shouldn't check other users privileges via this class as it violates SRP. You could create your own service for checking privileges and inject it where you need it.
- Filip Klimeš
- Nette Blogger | 156
Oh, I'm sorry, I got mislead by the $userId – which really is redundant.
Last edited by FilipKlimeš (2015-02-16 14:34)
- Šaman
- Member | 2661
This
way. I use ORM, so i have true entities. In Nette\Database
you must create some your own resources.
My User
extends my IRole, so i can get ID of role owner.
And all my entities
extends my IRole, so i can get ID of their owner.
To check rights you can't use $user->isAllowed()
, because
this method ged only users roles, not instance od User.
Inject ACL to your presenter (or any class where you need check) and use:
<?php
$this->acl->isAllowed($userEntity, $resource, $privilege);
// User entity is instance implements IRole and Resource is instance implements IRole.
?>
P.S. This is used in project, where user has only one role!
Last edited by Šaman (2015-02-16 13:14)
- BugsBunny
- Member | 5
Implemented. I basically had the solution in front of me the whole time (my own /app/model/Authorizator.php implemeting Nette\Security\IAuthorizator), I was just stuck with using it through $this->user->isAllowed(…) instead of straight $this->acl->isAllowed(…). Thanks everyone!