-
-
Save nuryagdym/3154c004eff682ad79eb2fb1a7f03189 to your computer and use it in GitHub Desktop.
This is an example of how one could create a custom type for roles to add in a User form (Symfony v4.3 >)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
security: | |
roles_label: "Permissions" | |
roles: | |
ROLE_SUPER_ADMIN: "Super Admin" | |
ROLE_ADMIN: "Administration" | |
ROLE_MONITOR: "Monitoring" | |
ROLE_FLEET: "Gestion du parc" | |
ROLE_ADMIN_USER: "Gestion des utilisateurs" | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AdminBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; | |
use Symfony\Component\Security\Core\Role\RoleHierarchy; | |
class RolesType extends AbstractType | |
{ | |
protected $reachableRoles; | |
protected $roleChoices; | |
private $transPrefix = 'security.roles.'; | |
public function __construct(RoleHierarchy $roleHierarchy, AuthorizationChecker $authorizationChecker) | |
{ | |
$superAdminRole = 'ROLE_SUPER_ADMIN'; | |
$currentPermissions = array($superAdminRole); | |
$this->reachableRoles = $roleHierarchy->getReachableRoleNames($currentPermissions); | |
$this->roleChoices = $this->getRoleChoices(); | |
if (!$authorizationChecker->isGranted($superAdminRole)) { | |
unset($this->roleChoices[$this->transPrefix.$superAdminRole]); | |
} | |
} | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'label' => 'security.roles_label', | |
'choices' => $this->roleChoices, | |
'choice_translation_domain' => true, | |
'expanded' => true, | |
'multiple' => true, | |
)); | |
} | |
private function getRoleChoices() | |
{ | |
$roles = array(); | |
/** @var Role $role**/ | |
foreach ($this->reachableRoles as $role) { | |
$roles[$this->transPrefix.$role] = $role; | |
} | |
return $roles; | |
} | |
public function getParent() | |
{ | |
return ChoiceType::class; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
security: | |
role_hierarchy: | |
ROLE_SUPER_ADMIN: | |
- ROLE_ADMIN_USER | |
- ROLE_ADMIN | |
- ROLE_MONITOR | |
- ROLE_FLEET | |
ROLE_ADMIN_USER: | |
- ROLE_ADMIN | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
admin.form.user_roles_type: | |
class: AdminBundle\Form\RolesType | |
arguments: ['@security.role_hierarchy', '@security.authorization_checker'] | |
tags: | |
- { name: form.type} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment