public function switchOrganization(User $user, OrganizationInterface $organization)
    {
        $role = $this->getOrganizationRole($user, $organization);

        if ($role == false && !$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
            throw new NotFoundHttpException('Roles in organization not found');
        }

        $userRoles = $this->removeOrganizationRoles($user->getRoles());

        $user->setRoles(array_merge($userRoles, $role->getroles()));
        $user->setActiveOrganization($organization);
        $user->setActiveRole($role);

        $this->dm->persist($user);
        $this->dm->flush();

        $token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken(
            $user,
            null,
            'main',
            $user->getRoles()
        );

        $this->tokenStorage->setToken($token);
        $this->userProvider->refreshUser($user);
    }
public function switchOrganization(User $user, OrganizationInterface $organization)
    {
        $role = $this->getOrganizationRole($user, $organization);

        if ($role == false && !$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
            throw new NotFoundHttpException('Roles in organization not found');
        }

        $userRoles = $this->removeOrganizationRoles($user->getRoles());

        $user->setRoles(array_merge($userRoles, $role->getroles()));
        $user->setActiveOrganization($organization);
        $user->setActiveRole($role);

        $this->dm->persist($user);
        $this->dm->flush();

        $token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken(
            $user,
            null,
            'main',
            $user->getRoles()
        );

        $this->tokenStorage->setToken($token);
        $this->userProvider->refreshUser($user);
    }
public function switchOrganization ( User $ user , OrganizationInterface $ organization ) { $ role = $ this -> getOrganizationRole ( $ user , $ organization ) ; if ( $ role == false && ! $ this -> authorizationChecker -> isGranted ( 'ROLE_SUPER_ADMIN' ) ) { throw new NotFoundHttpException ( 'Roles in organization not found' ) ; } $ userRoles = $ this -> removeOrganizationRoles ( $ user -> getRoles ( ) ) ; $ user -> setRoles ( array_merge ( $ userRoles , $ role -> getroles ( ) ) ) ; $ user -> setActiveOrganization ( $ organization ) ; $ user -> setActiveRole ( $ role ) ; $ this -> dm -> persist ( $ user ) ; $ this -> dm -> flush ( ) ; $ token = new \ Symfony \ Component \ Security \ Core \ Authentication \ Token \ UsernamePasswordToken ( $ user , null , 'main' , $ user -> getRoles ( ) ) ; $ this -> tokenStorage -> setToken ( $ token ) ; $ this -> userProvider -> refreshUser ( $ user ) ; }
Switch the working organization of logged user

@todo update security context calls. Deprecated since version 2.6, to be
removed in 3.0. Use AuthorizationCheckerInterface::isGranted() instead.
http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

@param User         $user
@param OrganizationInterface $organization

@return void
Switch the working organization of logged user