support creating new roles (#1050)
This commit is contained in:
@@ -19,9 +19,14 @@ class RolePermissionManager
|
||||
* @var string[]
|
||||
*/
|
||||
protected $knownPermissions = [];
|
||||
/**
|
||||
* @var RoleService
|
||||
*/
|
||||
private $roles;
|
||||
|
||||
public function __construct(array $permissions)
|
||||
public function __construct(RoleService $roles, array $permissions)
|
||||
{
|
||||
$this->roles = $roles;
|
||||
$this->permissions = $permissions;
|
||||
|
||||
foreach ($permissions as $role => $perms) {
|
||||
@@ -46,7 +51,7 @@ class RolePermissionManager
|
||||
|
||||
public function getRoles(): array
|
||||
{
|
||||
return array_keys($this->permissions);
|
||||
return $this->roles->getAvailableNames();
|
||||
}
|
||||
|
||||
public function getPermissions(): array
|
||||
|
||||
@@ -9,12 +9,16 @@
|
||||
|
||||
namespace App\Security;
|
||||
|
||||
class RoleService
|
||||
final class RoleService
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $roles;
|
||||
private $roles;
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $roleNames = [];
|
||||
|
||||
public function __construct(array $roles)
|
||||
{
|
||||
@@ -23,16 +27,20 @@ class RoleService
|
||||
|
||||
public function getAvailableNames(): array
|
||||
{
|
||||
$roles = [];
|
||||
foreach ($this->roles as $key => $value) {
|
||||
$roles[] = $key;
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $name) {
|
||||
$roles[] = $name;
|
||||
if (empty($this->roleNames)) {
|
||||
$roles = [];
|
||||
foreach ($this->roles as $key => $value) {
|
||||
$roles[] = $key;
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $name) {
|
||||
$roles[] = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->roleNames = array_values(array_unique($roles));
|
||||
}
|
||||
|
||||
return array_values(array_unique($roles));
|
||||
return $this->roleNames;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user