Skip to content

Instantly share code, notes, and snippets.

@mkroeders
Last active August 29, 2015 13:57
Show Gist options
  • Save mkroeders/9706301 to your computer and use it in GitHub Desktop.
Save mkroeders/9706301 to your computer and use it in GitHub Desktop.
IsAllowed view helper for Zend Framework 2
<?php
namespace View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\Permissions\Acl\AclInterface;
use Zend\Permissions\Acl\Role\RoleInterface;
/**
* Class IsAllowed
* @package View\Helper
*/
class IsAllowed extends AbstractHelper
{
/**
* @var AclInterface
*/
protected $acl;
/**
* @var RoleInterface
*/
protected $role;
/**
* @var AclInterface
*/
protected static $defaultAcl;
/**
* @var RoleInterface
*/
protected static $defaultRole;
/**
* @param null $resource
* @param null $privilege
* @return $this
*/
public function __invoke($resource = null, $privilege = null)
{
if (is_null($resource)) {
return $this;
}
return $this->isAllowed($resource, $privilege);
}
/**
* @param string|ResourceInterface $resource
* @param string $privilege
* @return bool
* @throws \RuntimeException
*/
public function isAllowed($resource, $privilege = null)
{
$acl = $this->getAcl();
if (!$acl instanceof AclInterface) {
throw new \RuntimeException('No ACL provided');
} elseif (!$resource instanceof ResourceInterface) {
$resource = new GenericResource($resource);
}
return $acl->isAllowed($this->getRole(), $resource, $privilege);
}
public static function setDefaultAcl(AclInterface $acl = null)
{
self::$defaultAcl = $acl;
}
/**
* @param AclInterface $acl
* @return $this
*/
public function setAcl(AclInterface $acl = null)
{
$this->acl = $acl;
return $this;
}
/**
* @return AclInterface
*/
protected function getAcl()
{
if ($this->acl instanceof AclInterface) {
return $this->acl;
}
return self::$defaultAcl;
}
/**
* @param RoleInterface $role
*/
public static function setDefaultRole(RoleInterface $role = null)
{
self::$defaultRole = $role;
}
/**
* @param RoleInterface $role
* @return $this
*/
public function setRole(RoleInterface $role = null)
{
$this->role = $role;
return $this;
}
/**
* @return RoleInterface
*/
protected function getRole()
{
if ($this->role instanceof RoleInterface) {
return $this->role;
}
return self::$defaultRole;
}
}
@seyfer
Copy link

seyfer commented Nov 16, 2014

And how to use it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment