Getting and making changes to session storage

Hi there :slight_smile:

I am very new to laminas. I’m trying to do something like this $_SESSION[‘LmcUserNamespace’][‘storage’][‘identity’] = 57; with laminas functions. I basically want to change the identity of the logged in user to make him login as somebody else by changing the value of identity in session storage. The above line of code does work but I know it’s not good to alter superglobals directly. What is the laminas way of doing it?

Thanks for your help in advance.

Kind regards,
Sushma :slight_smile:

Hi @Sushma,

I believe you’re using Laminas MVC and you’re using LmcUser module for registration and login etc. If that is the case then you need to override the Authtentication Database service factory to do your desired task. Because that is where the user/member id is assigned.

// your module.config.php file

   'service_manager' => [
      'factories' => [
         \LmcUser\Authentication\Adapter\Db::class => \Sushma\Factory\Authentication\Adapter\DbFactory::class,
       ]
   ],
// end of module.config.php change

// Your factory class
class Sushma\Factory\Authentication\Adapter\DbFactory extends \LmcUser\Factory\Authentication\Adapter\DbFactory{
    public function camouflageMember(AdapterChainEvent $e, UserInterface $user){
        // regen the id
        $session = new SessionContainer($this->getStorage()->getNameSpace());
        $session->getManager()->regenerateId();

        // Success!
        $e->setIdentity($userObject->getId());
        $this->setSatisfied(true);
        $storage = $this->getStorage()->read();
        $storage['identity'] = $user->getId();
        $this->getStorage()->write($storage);
    }
}

// one of your controller action code looks like below 

   public function camouflageMember(){
        $userObject = getUserObject(); // Todo: get your user Object.
        $adapter = $this->lmcUserAuthentication()->getAuthAdapter();
        $auth = $this->lmcUserAuthentication()->getAuthService()->camouflageMember($adapter, $userObject);
    }