ACL and allow all only for special role
Suppose I have an application that is allowed only for admins. So I need to deny it for all users and allow for Admins role.
$acl = new \Phalcon\Acl\Adapter\Memory;
$acl->setDefaultAction(\Phalcon\Acl::DENY);
$acl->addRole(new \Phalcon\Acl\Role('Admins'));
$acl->addResource(new \Phalcon\Acl\Resource('index'), ['index', 'new', 'etc']); // I believe this step is unnecessary
$acl->allow('Admins', '*', '*');
var_dump($acl->isAllowed('Admins', 'index', 'index')); // int 1
var_dump($acl->isAllowed('Admins', 'anyController', 'anyAction')); // int 0
I have to specify each resource to get it run. Correct me if I'm wrong, but that's a lot of useless code.
There's no need for ACL just to dany all pages or module. Just attach event listener to beforDispatch and check if user is logged in as admin. If no, forward to login controller/action.
http://docs.phalconphp.com/en/latest/reference/dispatching.html#dispatch-loop-events
Yeah, why not use just a beforeDispatch event to check if the role is correct instead of implementing an ACL?
It was just a simplified example.
I can have several roles:
- "Writers" to write news
- "Moderators" to approve comments
- "Adv_Managers" to control banners
- "Some_People" to do something
- and "Admins" to manage over 100500 controllers
It's not a good idea to use ACL for all roles except Admins. ACL must to decide is Admins allowed, not dispatcher events.
$acl->allow('Admins', '', ''); ACL grant's access only to all controllers and actions which he knows (you specify). So if he must to decide, he should know about "anyController" and "anyAction". BTW, if you use Phalcon, you care about app performanc. And if you care about performance, you will never write redundant code just because "ACL must decide" (at least I - definitely).
if ($di->session->get("user-role") != "Admin") {
// run acl
}
this one if saves you from "but that's a lot of useless code". + smaller CPU+MEM usage = faster execution time.
Have you ever written an application slightly more than "hello world"? Have you ever support other developer's apps? Have you ever heard of such things as the structure of the application?
If I was only interested in CPU+MEM usage, I would use a single file with spaghetti-code without any framework. The hardware is not so expensive as developers time.
In most cases I should think about perfomance, but in this case the framework should take care of my comfort.
$acl->allow('Admins', '', ''); ACL grant's access only to all controllers and actions which he knows (you specify). So if he must to decide, he should know about "anyController" and "anyAction".
No, it shouldn't. * means Any. It doesn't matter whether ACL knows it or not.
this one if saves you from "but that's a lot of useless code".
This should save me $acl->allow('Admins', '*', '*'); + smaller CPU+MEM usage = faster execution time.
I don't see any arguments against this issue.
It checks if the resource exists in the list before check if the role is allowed so nonexistent resources are validated as not allowed
In my opinion the framework shouldn't use extra checks if the rule allows everything.
Yes, you're right, I'm going to remove that check in the 1.0.1 branch
Thanks. This is not most important issue, but happiness consists of trifles =)
@Agent-J the resource checking was removed in 1.0.1, thanks
Thank you