ACL and appearance manipulation of links, forms an

2019-05-31 07:47发布

I would like to find out which strategy is the best for links, forms and DOM elements appearance manipulation (show/hide) on top level of multi modular application (admin, default, etc) ??

It should be done through ACL and it's usage I know well, permission are stored in DB. If someone tries to access certain page (module/controller/action) that is not allowed for him, an info page is passed which says that he is not allowed. In that case all elements are visible.

I have a few strategies for elements appearance manipulation on my mind:

  1. To set in controller

    $this->view->allow_delete_link = $acl->isAllowed($role, 'delete_post'); // boolean
    

    and to ask in view file if it is true or false and show/hide

    <?php if ($this->allow_delete_link): ?>[link html] <?php endif; ?>
    

    The drawback here is if i have 50 links on page I will need to have 50 lines of code in my controller where I am doing this and I don't like that very much.

  2. Similar to the first except ACL is static class so view file asks if:

    <?php if(My_Custom_Acl::getIsAllowed('some_resource', 'delete_post_action'){ echo 'link'; } ?>
    
  3. To make one view helper which I will call upon creating every link in which I would ask if user that is logged in has access, if yes return the whole link, if not, return "" or false. View helpers are very slow so that's my last resort.

  4. To make separate view.phtml file for every group of users, then in controller, in which user is logged, show appropriate view. $this->render('xx_view'); This violates DRYS, so I think this method is not good.

Is there some other strategy for something like this, because I already see that I will have headache if I choose any of these 4. Maybe some existing plugin/class for that would be the best?

thanks in advance !

I forgot to mention, that I am using Zend framework and Zend_Acl.

1条回答
聊天终结者
2楼-- · 2019-05-31 08:10

3 is the best solution, because you often need to pass the database row to the isAllowed call (for example, if you need to test the owner of the post to choose if you can show the delete link).

You can't do this in the controller like you suggest in 1, because you will need one line for each row.

2 is ugly.

You can speed up resolution of view helpers by extending Zend_View: http://framework.zend.com/manual/fr/performance.view.html.

查看更多
登录 后发表回答