<?php
namespace Moto\Application\Acl; use Zend\Permissions\Acl\Acl; use Zend\Permissions\Acl\Role\GenericRole as Role; use Moto; class Adapter extends Acl { static protected $_instance = null; public static function addRules($rules) { $acl = static::getInstance(); if (!empty($rules['roles'])) { $acl->_addRoles($rules['roles']); } if (!empty($rules['resources'])) { $acl->_addResources($rules['resources']); } if (!empty($rules['access'])) { $acl->_addAccessMap($rules['access']); } } public function isAllowed($role = null, $resource = null, $privilege = null) { $result = false; try { $result = parent::isAllowed($role, $resource, $privilege); } catch (\Zend\Permissions\Acl\Exception\InvalidArgumentException $e) { } return $result; } protected function _addRoles($roles) { foreach ($roles as $role => $options) { $this->_addRole($role, $options); } } protected function _addRole($role, $options) { try { $acl = $this; $role = new Role($role); $parents = null; if (!empty($options['parents'])) $parents = $options['parents']; $acl->addRole($role, $parents); if (isset($options['allow'])) { if ($options['allow']) { $acl->allow($role); } else { $acl->deny($role); } } } catch(\Exception $e) { return false; } return true; } protected function _addResources($resources) { $acl = $this; foreach ($resources as $resource => $options) { $acl->addResource($resource); } } public function addResource($resource, $parent = null) { try { parent::addResource($resource, $parent); } catch(\Exception $e) { return false; } return true; } protected function _addAccessMap($map) { for ($i = 0, $count = count($map); $i < $count; $i++) { $this->_addAccess($map[ $i ]); } } protected function _addAccess($rule) { try { if (!isset($rule['role']) || !isset($rule['resource'])) { return false; } $method = 'deny'; if (isset($rule['allow']) && $rule['allow']) { $method = 'allow'; } $privileges = null; if (isset($rule['privileges'])) { $privileges = $rule['privileges']; } $this->{$method}($rule['role'], $rule['resource'], $privileges); } catch(\Exception $e) { return false; } return true; } static public function getInstance() { if (null === static::$_instance) static::$_instance = new self(); return static::$_instance; } }