src/Sulu/Bundle/SecurityBundle/UserManager/UserManager.php
private function addUserRole(UserInterface $user, $userRoleData)
    {
        $alreadyContains = false;

        $role = $this->roleRepository->findRoleById($userRoleData['role']['id']);

        if (!$role) {
            throw new EntityNotFoundException($this->roleRepository->getClassName(), $userRoleData['role']['id']);
        }

        if ($user->getUserRoles()) {
            foreach ($user->getUserRoles() as $containedRole) {
                if ($containedRole->getRole()->getId() === $role->getId()) {
                    $alreadyContains = true;
                }
            }
        }

        if (false === $alreadyContains) {
            $userRole = new UserRole();
            $userRole->setUser($user);
            $userRole->setRole($role);
            $userRole->setLocale(json_encode($userRoleData['locales']));
            $this->em->persist($userRole);

            $user->addUserRole($userRole);
        }

        return true;
    }
private function addUserRole(UserInterface $user, $userRoleData)
    {
        $alreadyContains = false;

        $role = $this->roleRepository->findRoleById($userRoleData['role']['id']);

        if (!$role) {
            throw new EntityNotFoundException($this->roleRepository->getClassName(), $userRoleData['role']['id']);
        }

        if ($user->getUserRoles()) {
            foreach ($user->getUserRoles() as $containedRole) {
                if ($containedRole->getRole()->getId() === $role->getId()) {
                    $alreadyContains = true;
                }
            }
        }

        if (false === $alreadyContains) {
            $userRole = new UserRole();
            $userRole->setUser($user);
            $userRole->setRole($role);
            $userRole->setLocale(json_encode($userRoleData['locales']));
            $this->em->persist($userRole);

            $user->addUserRole($userRole);
        }

        return true;
    }
private function addUserRole ( UserInterface $ user , $ userRoleData ) { $ alreadyContains = false ; $ role = $ this -> roleRepository -> findRoleById ( $ userRoleData [ 'role' ] [ 'id' ] ) ; if ( ! $ role ) { throw new EntityNotFoundException ( $ this -> roleRepository -> getClassName ( ) , $ userRoleData [ 'role' ] [ 'id' ] ) ; } if ( $ user -> getUserRoles ( ) ) { foreach ( $ user -> getUserRoles ( ) as $ containedRole ) { if ( $ containedRole -> getRole ( ) -> getId ( ) === $ role -> getId ( ) ) { $ alreadyContains = true ; } } } if ( false === $ alreadyContains ) { $ userRole = new UserRole ( ) ; $ userRole -> setUser ( $ user ) ; $ userRole -> setRole ( $ role ) ; $ userRole -> setLocale ( json_encode ( $ userRoleData [ 'locales' ] ) ) ; $ this -> em -> persist ( $ userRole ) ; $ user -> addUserRole ( $ userRole ) ; } return true ; }
Adds a new UserRole to the given user.

@param UserInterface $user
@param $userRoleData

@throws \Sulu\Component\Rest\Exception\EntityNotFoundException

@return bool
Adds a new UserRole to the given user .