src/Manager/UserManager.php
public function setRole(UserInterface $user, array $selectedRole)
    {
        $choices = $this->rolesManager->getChoices();

        if (!isset($choices[$selectedRole['roles']])) {
            throw new InvalidArgumentException(
                sprintf('Impossible to set role %s', $selectedRole['roles'])
            );
        }

        $user->setRoles([$choices[$selectedRole['roles']]]);

        $this->entityManager->flush();
    }
public function setRole(UserInterface $user, array $selectedRole)
    {
        $choices = $this->rolesManager->getChoices();

        if (!isset($choices[$selectedRole['roles']])) {
            throw new InvalidArgumentException(
                sprintf('Impossible to set role %s', $selectedRole['roles'])
            );
        }

        $user->setRoles([$choices[$selectedRole['roles']]]);

        $this->entityManager->flush();
    }
public function setRole ( UserInterface $ user , array $ selectedRole ) { $ choices = $ this -> rolesManager -> getChoices ( ) ; if ( ! isset ( $ choices [ $ selectedRole [ 'roles' ] ] ) ) { throw new InvalidArgumentException ( sprintf ( 'Impossible to set role %s' , $ selectedRole [ 'roles' ] ) ) ; } $ user -> setRoles ( [ $ choices [ $ selectedRole [ 'roles' ] ] ] ) ; $ this -> entityManager -> flush ( ) ; }
Sets a user role.

New role is provided as an key-value array:
[
'roles' => 'ROLE_TO_SET',
]

@param UserInterface $user
@param array         $selectedRole
Sets a user role .