tarlepp /
symfony-flex-backend
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | /** |
||
| 4 | * /src/Security/RolesService.php |
||
| 5 | * |
||
| 6 | * @author TLe, Tarmo Leppänen <[email protected]> |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace App\Security; |
||
| 10 | |||
| 11 | use App\Enum\Role; |
||
| 12 | use App\Security\Interfaces\RolesServiceInterface; |
||
| 13 | use BackedEnum; |
||
| 14 | use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
||
| 15 | use function array_map; |
||
| 16 | use function array_unique; |
||
| 17 | use function array_values; |
||
| 18 | use function mb_strtolower; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class RolesService |
||
| 22 | * |
||
| 23 | * @package App\Security |
||
| 24 | * @author TLe, Tarmo Leppänen <[email protected]> |
||
| 25 | */ |
||
| 26 | class RolesService implements RolesServiceInterface |
||
| 27 | { |
||
| 28 | 718 | public function __construct( |
|
| 29 | private readonly RoleHierarchyInterface $roleHierarchy, |
||
| 30 | ) { |
||
| 31 | 718 | } |
|
| 32 | |||
| 33 | 13 | public function getRoles(): array |
|
| 34 | { |
||
| 35 | 13 | return array_map(static fn (BackedEnum $enum): string => $enum->value, Role::cases()); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 36 | } |
||
| 37 | |||
| 38 | 18 | public function getRoleLabel(string $role): string |
|
| 39 | { |
||
| 40 | 18 | $enum = Role::tryFrom($role); |
|
| 41 | |||
| 42 | 18 | return $enum instanceof Role |
|
| 43 | 17 | ? $enum->label() |
|
| 44 | 18 | : 'Unknown - ' . $role; |
|
| 45 | } |
||
| 46 | |||
| 47 | 18 | public function getShort(string $role): string |
|
| 48 | { |
||
| 49 | 18 | $enum = Role::tryFrom($role); |
|
| 50 | |||
| 51 | 18 | return $enum instanceof Role |
|
| 52 | 17 | ? mb_strtolower($enum->name) |
|
|
0 ignored issues
–
show
|
|||
| 53 | 18 | : 'Unknown - ' . $role; |
|
| 54 | } |
||
| 55 | |||
| 56 | 286 | public function getInheritedRoles(array $roles): array |
|
| 57 | { |
||
| 58 | 286 | return array_values(array_unique($this->roleHierarchy->getReachableRoleNames($roles))); |
|
| 59 | } |
||
| 60 | } |
||
| 61 |