An easy way to load ACL in Zend Framework 2?
Asked Answered
R

5

5

I have been following this guide to load my menu configuration and i think it is very nice and clean way to load the menu.

My question is simple, is there a way to load your ACL configuration on the same way with a config array and some kinda of factory?

If there isn't, how do i load a ACL configuration and use with that menu in a easy way?

Thanks!

Edit: This is a very good blog post on why use modules that is already done and not make your own, http://hounddog.github.com/blog/there-is-a-module-for-that/

Regulable answered 18/11, 2012 at 8:33 Comment(0)
F
5

ZF2 contains ACL and also RBAC (role based ACL - might be in ZF2.1), but to put it in place, easier is to use module which you can plug into your application. BjyAuthorize seems to me a bit bloated, you have to use ZfcUser module. I prefer ZfcRbac, the ACL rules are based on user roles (group) and their access to controller, action or route. Configuration stored in one config file, really easy to implement.

Fibro answered 18/11, 2012 at 22:38 Comment(1)
Yea, it all feels like adding so much code for something that was so easy in ZF1. I guess i will have to use modules like this to make it.Regulable
I
5

Most likely there are several ways to do it, but I prefer to do it in getViewHelperConfig() of application's Module.php (here I use BjyAuthorize module to simplify work with ACL, and in particular it allows to set ACL rules in configuration file module.bjyauthorize.global.php)

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'navigation' => function($sm) {
                $auth = $sm->getServiceLocator()->get('BjyAuthorize\Service\Authorize');
                $role = $auth->getIdentityProvider()->getIdentityRoles();
                if (is_array($role))
                    $role = $role[0];

                $navigation = $sm->get('Zend\View\Helper\Navigation');
                $navigation->setAcl($auth->getAcl())->setRole($role);

                return $navigation;
            }
        )
    );
}
Innovation answered 18/11, 2012 at 10:54 Comment(2)
I use something like this in ZF1, very simple and clear. I find it strange that i can't find something that simple for ZF2. All this modules feels way to complicated.Regulable
That way is possible too (Zend\Permissions\Acl\Acl is almost identical to Zend_Acl), and BjyAuthorize allows to write less code to add Acl to the project. Looks like there is no comprehensive guide on combining Acl, Authentication, Navigation, etc. I've googled only: p0l0.binware.org/index.php/2012/02/18/… samsonasik.wordpress.com/2012/08/23/…Innovation
F
5

ZF2 contains ACL and also RBAC (role based ACL - might be in ZF2.1), but to put it in place, easier is to use module which you can plug into your application. BjyAuthorize seems to me a bit bloated, you have to use ZfcUser module. I prefer ZfcRbac, the ACL rules are based on user roles (group) and their access to controller, action or route. Configuration stored in one config file, really easy to implement.

Fibro answered 18/11, 2012 at 22:38 Comment(1)
Yea, it all feels like adding so much code for something that was so easy in ZF1. I guess i will have to use modules like this to make it.Regulable
T
3

Play with This structure . get role and resource from database and save this in session for or any caching .

Play with This structure

Thunderstorm answered 25/8, 2014 at 5:43 Comment(1)
In my case I put role_id in user tabel, because never I never had a case for related table user-role...Dian
D
2

I've just created an ACL module that creates an ACL Service parsing the routes.

To manage your access control to your application you only need to define roles and add a new key 'roles' in every route. If you do not define that key or its array is empty, then the route becomes public. It also works with child routes.

As an example:

array(
    'router' => array(
        'routes' => array(
            'user\users\view' => array(
                'type'              => 'Segment',
                'options'           => array(
                    'route'         => '/admin/users/view/id/:id/',
                    'constraints'   => array(
                        'id' => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'User\Controller\Users',
                        'action'     => 'view',
                        'roles'      => ['admin', 'user'],
                    ),
                ),
            ),
        ),
    ),
);

The module can be installed via composer and it is now listed in the zend modules repository: http://zfmodules.com/itrascastro/TrascastroACL

You can get more detailed info about use and installation from my blog: http://www.ismaeltrascastro.com/acl-module-zend-framework/

Despite answered 26/2, 2015 at 21:57 Comment(0)
P
1

You are right, there is no out-of-the-box-all-in-one solution. You have to build some bridges between the modules.

Another easy way to integrate BjyAuthorize is using **Zend Navigation**s default methods as described by Rob Allen: Integrating BjyAuthorize with ZendNavigation

    $sm = $e->getApplication()->getServiceManager();

    // Add ACL information to the Navigation view helper
    $authorize = $sm->get('BjyAuthorizeServiceAuthorize');
    $acl = $authorize->getAcl();
    $role = $authorize->getIdentity();
    ZendViewHelperNavigation::setDefaultAcl($acl);
    ZendViewHelperNavigation::setDefaultRole($role);

You can also use ZfcRbac and use a listener to make it work with Zend Navigation.

Since this is a lot of code I simply post the link here: Check Zend Navigation page permissions with ZfcRbac – Webdevilopers Blog

Piraeus answered 3/6, 2014 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.