Skip to content

Commit

Permalink
Implements RoleHierarchyUtil.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilov committed Dec 23, 2016
1 parent 63b1ba4 commit efd9a09
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:

glavweb_security.access_voter:
class: Glavweb\SecurityBundle\Security\Authorization\Voter\AccessVoter
arguments: ["@doctrine", "@glavweb_security.access_handler"]
arguments: ["@doctrine", "@glavweb_security.access_handler", "@glavweb_security.role_hierarchy_util"]
public: false
tags:
- { name: security.voter }
Expand All @@ -33,3 +33,7 @@ services:
glavweb_security.admin_security_handler_role:
class: Glavweb\SecurityBundle\Admin\SecurityHandlerRole
arguments: ["@security.authorization_checker", "@glavweb_security.access_handler", [ROLE_SUPER_ADMIN]]

glavweb_security.role_hierarchy_util:
class: Glavweb\SecurityBundle\Util\RoleHierarchyUtil
arguments: ["%security.role_hierarchy.roles%"]
22 changes: 15 additions & 7 deletions Security/Authorization/Voter/AccessVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\EntityManager;
use Glavweb\SecurityBundle\Mapping\Annotation\Access;
use Glavweb\SecurityBundle\Security\AccessHandler;
use Glavweb\SecurityBundle\Util\RoleHierarchyUtil;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -43,16 +44,23 @@ class AccessVoter implements VoterInterface
*/
protected $accessAnnotation;

/**
* @var RoleHierarchyUtil
*/
private $roleHierarchyUtil;

/**
* AccessVoter constructor.
* @param Registry $doctrine
* @param AccessHandler $accessHandler
* @internal param Reader $annotationReader
*
* @param Registry $doctrine
* @param AccessHandler $accessHandler
* @param RoleHierarchyUtil $roleHierarchyUtil
*/
public function __construct(Registry $doctrine, AccessHandler $accessHandler)
public function __construct(Registry $doctrine, AccessHandler $accessHandler, RoleHierarchyUtil $roleHierarchyUtil)
{
$this->doctrine = $doctrine;
$this->accessHandler = $accessHandler;
$this->doctrine = $doctrine;
$this->accessHandler = $accessHandler;
$this->roleHierarchyUtil = $roleHierarchyUtil;
}

/**
Expand Down Expand Up @@ -108,7 +116,7 @@ public function vote(TokenInterface $token, $object, array $attributes)
if (!$user instanceof UserInterface || !method_exists($user, 'getId')) {
return VoterInterface::ACCESS_ABSTAIN;
}
$userRoles = $user->getRoles();
$userRoles = $this->roleHierarchyUtil->getUserRoles($user);

$alias = 't';
foreach ($attributes as $attribute) {
Expand Down
86 changes: 86 additions & 0 deletions Util/RoleHierarchyUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Glavweb SecurityBundle package.
*
* (c) GLAVWEB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Glavweb\SecurityBundle\Util;

use Symfony\Component\Security\Core\User\UserInterface;

/**
* Class RoleHierarchyUtil
*
* @author Andrey Nilov <[email protected]>
* @package Glavweb\SecurityBundle
*/
class RoleHierarchyUtil
{
/**
* @var array
*/
private $roleHierarchy;

/**
* RoleHierarchyUtil constructor.
*
* @param array $roleHierarchy
*/
public function __construct(array $roleHierarchy)
{
$this->roleHierarchy = $roleHierarchy;
}

/**
* @param UserInterface $user
* @return mixed
*/
public function getUserRoles(UserInterface $user)
{
$userRoles = $user->getRoles();

$roles = [];
foreach ($userRoles as $role) {
$roles[] = $role;

if (isset($this->roleHierarchy[$role])) {
$roles = array_unique(array_merge(
$roles,
$this->getRoleByHierarchy($role)
));
}
}

return $roles;
}

/**
* @param string $targetRole
* @return array
*/
public function getRoleByHierarchy($targetRole)
{
$roles = [];

if (isset($this->roleHierarchy[$targetRole])) {
foreach ($this->roleHierarchy[$targetRole] as $role) {
$roles[] = $role;

if (isset($this->roleHierarchy[$role])) {
$roles = array_unique(array_merge(
$roles,
$this->getRoleByHierarchy($role)
));
}
}
}

return $roles;
}

}

0 comments on commit efd9a09

Please sign in to comment.