src/Connect.php
public function switchLocalUsername($localUsername, $role, $application = null)
    {
        if ($this->isAuthenticated()) {
            // If no Application, get current Application
            if (!$application) {
                $application = $this->getConfig()->getEntityID();
            }

            try {
                $switchedRole = null;

                /** @var Attribution $attribution **/
                foreach ($this->getUser()->getAttributions() as $attribution) {
                    $pattern = '/:' . $role . ':' . $localUsername . '/';
                    if ($application == $attribution->getTarget()->getUrl()
                        && preg_match($pattern, $attribution->getRole()->getRole())
                    ) {
                        $switchedRole = $attribution->getRole();
                    }
                }

                if ($switchedRole) {
                    $roles = explode(':', $switchedRole->getRole());
                    if (count($roles) == 3) {
                        $role = $roles[1];
                        $this->getUser()->setCurrentRole($role);
                        $this->getUser()->setLocalUsername($roles[2]);

                        $this->setUser($this->getUser());
                    } else {
                        $this->setUser($this->getUser()->setCurrentRole($switchedRole->getRole()));
                    }
                } else {
                    throw new UserAttributionException('Role not found.', 400);
                }
            } catch (\Exception $e) {
                throw new UserAttributionException($e->getMessage(), $e->getCode(), $e->getPrevious());
            }
        } else {
            throw new UserAttributionException('User not found.', 400);
        }
    }
public function switchLocalUsername($localUsername, $role, $application = null)
    {
        if ($this->isAuthenticated()) {
            // If no Application, get current Application
            if (!$application) {
                $application = $this->getConfig()->getEntityID();
            }

            try {
                $switchedRole = null;

                /** @var Attribution $attribution **/
                foreach ($this->getUser()->getAttributions() as $attribution) {
                    $pattern = '/:' . $role . ':' . $localUsername . '/';
                    if ($application == $attribution->getTarget()->getUrl()
                        && preg_match($pattern, $attribution->getRole()->getRole())
                    ) {
                        $switchedRole = $attribution->getRole();
                    }
                }

                if ($switchedRole) {
                    $roles = explode(':', $switchedRole->getRole());
                    if (count($roles) == 3) {
                        $role = $roles[1];
                        $this->getUser()->setCurrentRole($role);
                        $this->getUser()->setLocalUsername($roles[2]);

                        $this->setUser($this->getUser());
                    } else {
                        $this->setUser($this->getUser()->setCurrentRole($switchedRole->getRole()));
                    }
                } else {
                    throw new UserAttributionException('Role not found.', 400);
                }
            } catch (\Exception $e) {
                throw new UserAttributionException($e->getMessage(), $e->getCode(), $e->getPrevious());
            }
        } else {
            throw new UserAttributionException('User not found.', 400);
        }
    }
public function switchLocalUsername ( $ localUsername , $ role , $ application = null ) { if ( $ this -> isAuthenticated ( ) ) { // If no Application, get current Application if ( ! $ application ) { $ application = $ this -> getConfig ( ) -> getEntityID ( ) ; } try { $ switchedRole = null ; /** @var Attribution $attribution **/ foreach ( $ this -> getUser ( ) -> getAttributions ( ) as $ attribution ) { $ pattern = '/:' . $ role . ':' . $ localUsername . '/' ; if ( $ application == $ attribution -> getTarget ( ) -> getUrl ( ) && preg_match ( $ pattern , $ attribution -> getRole ( ) -> getRole ( ) ) ) { $ switchedRole = $ attribution -> getRole ( ) ; } } if ( $ switchedRole ) { $ roles = explode ( ':' , $ switchedRole -> getRole ( ) ) ; if ( count ( $ roles ) == 3 ) { $ role = $ roles [ 1 ] ; $ this -> getUser ( ) -> setCurrentRole ( $ role ) ; $ this -> getUser ( ) -> setLocalUsername ( $ roles [ 2 ] ) ; $ this -> setUser ( $ this -> getUser ( ) ) ; } else { $ this -> setUser ( $ this -> getUser ( ) -> setCurrentRole ( $ switchedRole -> getRole ( ) ) ) ; } } else { throw new UserAttributionException ( 'Role not found.' , 400 ) ; } } catch ( \ Exception $ e ) { throw new UserAttributionException ( $ e -> getMessage ( ) , $ e -> getCode ( ) , $ e -> getPrevious ( ) ) ; } } else { throw new UserAttributionException ( 'User not found.' , 400 ) ; } }
Switch the User role by localUsername

@param string $localUsername
@param string $role
@param null $application
Switch the User role by localUsername