cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

ACL and allow all only for special role

Open iforp opened this issue 13 years ago • 12 comments

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.

iforp avatar Apr 02 '13 12:04 iforp

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

johnnyajax avatar Apr 03 '13 18:04 johnnyajax

Yeah, why not use just a beforeDispatch event to check if the role is correct instead of implementing an ACL?

ghost avatar Apr 04 '13 16:04 ghost

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.

iforp avatar Apr 05 '13 13:04 iforp

$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.

johnnyajax avatar Apr 05 '13 16:04 johnnyajax

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.

iforp avatar Apr 05 '13 22:04 iforp

$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.

iforp avatar Apr 05 '13 22:04 iforp

It checks if the resource exists in the list before check if the role is allowed so nonexistent resources are validated as not allowed

ghost avatar Apr 05 '13 22:04 ghost

In my opinion the framework shouldn't use extra checks if the rule allows everything.

iforp avatar Apr 05 '13 22:04 iforp

Yes, you're right, I'm going to remove that check in the 1.0.1 branch

ghost avatar Apr 05 '13 22:04 ghost

Thanks. This is not most important issue, but happiness consists of trifles =)

iforp avatar Apr 05 '13 22:04 iforp

@Agent-J the resource checking was removed in 1.0.1, thanks

ghost avatar Apr 06 '13 07:04 ghost

Thank you

iforp avatar Apr 06 '13 12:04 iforp