Skip to content

Commit

Permalink
Merge pull request #9 from Becklyn/update
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannik committed Apr 28, 2020
2 parents 2cdb443 + ce6a449 commit 20c8cd2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
4 changes: 0 additions & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public function getConfigTreeBuilder ()
->scalarNode("description")->end()
->booleanNode("hidden")->defaultFalse()->end()
->arrayNode("included_roles")
->validate()
->ifTrue(function (array $roles) { return !empty($roles); })
->thenInvalid("Don't define your included roles in the `becklyn_static_roles` config, but in symfony's role hierarchy instead")
->end()
->prototype('scalar')->end()
->end()
->arrayNode("tags")
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ services:
Becklyn\StaticRolesBundle\Role\RoleCollection:
decorates: 'security.role_hierarchy'
arguments:
$coreHierarchy: '@Becklyn\StaticRolesBundle\Role\RoleCollection.inner'
$coreHierarchy: '%security.role_hierarchy.roles%'
38 changes: 34 additions & 4 deletions src/Role/RoleCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Becklyn\StaticRolesBundle\Role;

use Symfony\Component\Security\Core\Role\RoleHierarchy;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;

/**
Expand All @@ -10,18 +11,47 @@
final class RoleCollection implements RoleHierarchyInterface
{
/** @var RoleHierarchyInterface */
private $coreHierarchy;
private $hierarchy;

/** @var StaticRole[] */
private $roles;


/**
*/
public function __construct (RoleHierarchyInterface $coreHierarchy, array $config = [])
public function __construct (
array $config = [],
array $coreHierarchy = []
)
{
$this->coreHierarchy = $coreHierarchy;
$this->roles = $this->prepareRoleCollection($config);
$this->hierarchy = $this->buildFullHierarchy($coreHierarchy, $this->roles);
}


/**
* @param StaticRole[] $roles
*/
private function buildFullHierarchy (array $coreHierarchy, array $roles) : RoleHierarchyInterface
{
foreach ($roles as $staticRole)
{
$role = $staticRole->getRole();

if (!\array_key_exists($role, $coreHierarchy))
{
$coreHierarchy[$role] = [];
}

foreach ($staticRole->getIncludedRoles() as $included)
{
$coreHierarchy[$role][] = $included;
}

$coreHierarchy[$role] = \array_unique($coreHierarchy[$role]);
}

return new RoleHierarchy($coreHierarchy);
}


Expand Down Expand Up @@ -77,7 +107,7 @@ public function getRoleByKey (string $roleKey) : ?StaticRole
*/
public function getReachableRoleNames (array $roles) : array
{
$roles = $this->coreHierarchy->getReachableRoleNames($roles);
$roles = $this->hierarchy->getReachableRoleNames($roles);

$result = $roles;

Expand Down
35 changes: 26 additions & 9 deletions src/Role/StaticRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class StaticRole
/**
* The title of the role
*
* @var string|null
* @var string
*/
private $title;

Expand All @@ -36,6 +36,14 @@ final class StaticRole
private $hidden;


/**
* A list of all included role names
*
* @var string[]
*/
private $includedRoles;


/**
* A list of tags
*
Expand All @@ -58,9 +66,10 @@ final class StaticRole
*/
public function __construct (
string $role,
?string $title,
string $title,
?string $description,
bool $hidden,
array $includedRoles,
array $tags,
array $actions
)
Expand All @@ -69,6 +78,7 @@ public function __construct (
$this->title = $title;
$this->description = $description;
$this->hidden = $hidden;
$this->includedRoles = $includedRoles;
$this->tags = $tags;
$this->actions = $actions;
}
Expand All @@ -77,18 +87,15 @@ public function __construct (

/**
* Creates the role from the configuration
*
* @param string $role
*
* @return StaticRole
*/
public static function createFromConfiguration ($role, array $configuration)
public static function createFromConfiguration (string $role, array $configuration) : self
{
return new self(
$role,
$configuration["title"] ?? null,
$configuration["title"] ?? $role,
$configuration["description"] ?? null,
$configuration["hidden"] ?? false,
$configuration["included_roles"] ?? [],
$configuration["tags"] ?? [],
$configuration["actions"] ?? []
);
Expand All @@ -98,7 +105,7 @@ public static function createFromConfiguration ($role, array $configuration)

/**
*/
public function getTitle () : ?string
public function getTitle () : string
{
return $this->title;
}
Expand All @@ -123,6 +130,16 @@ public function getTags () : array



/**
* @return string[]
*/
public function getIncludedRoles () : array
{
return $this->includedRoles;
}



/**
*/
public function getActions () : array
Expand Down
19 changes: 19 additions & 0 deletions src/Voter/RoleActionsVoter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Becklyn\StaticRolesBundle\Voter;

use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;

/**
* Votes on role actions
*/
final class RoleActionsVoter extends RoleHierarchyVoter
{
/**
*/
public function __construct (RoleHierarchyInterface $roleHierarchy)
{
parent::__construct($roleHierarchy, "CAN_");
}
}
2 changes: 1 addition & 1 deletion tests/Role/StaticRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testEmptyConfigurationFromArray ()
{
$role = StaticRole::createFromConfiguration("ROLE_TEST", []);

$this->assertNull($role->getTitle());
$this->assertSame("ROLE_TEST", $role->getTitle());
$this->assertNull($role->getDescription());
$this->assertFalse($role->isHidden());
}
Expand Down

0 comments on commit 20c8cd2

Please sign in to comment.