ZF2 Set Zend \ AuthenticationService to use a second session or cookie based on a URL or module

I created two user account modules - administrator and client. My current setup means that when I log in to the administrator, my application thinks that you are logged in as a client. The solution I decided was to create a session in which the cookie path is based on the admin URL, i.e. Set cookie_path as /administrator .

In my admin function Module.php onBootstrap I included:

 $sessionConfig = new SessionConfig(); $sessionConfig->setOptions(['cookie_path' => '/administrator']); $sessionManager = new SessionManager($sessionConfig, null, null); Container::setDefaultManager($sessionManager); 

which sets the path to the cookie, but this affects the entire application; that is, the rest of the site is a cookie because the URLs do not start with /administrator .

How to configure my application so that cookie_path for my admin module is different from other applications?

[edit]

What I get are two cookies: one for the admin route and one for the rest of the application.

[edit]

I am using Zend\Authentication\AuthenticationService for ACL. I am trying to get a user to go into the client section of a website and do something like that, and then go to the admin panel to do something.

As an example, Magento will set one cookie when working with a client account, and then another cookie when working with an administrator account.

How to configure Zend\Authentication\AuthenticationService to use a second session or url / module based cookie?

+7
session-cookies zend-framework2 zend-session
source share
1 answer

To install a new namespace in the authentication service, do the following:

 $auth = $e->getApplication()->getServiceManager()->get('Zend\Authentication\AuthenticationService'); $auth->setStorage(new \Zend\Authentication\Storage\Session($_namespace)); 

In my question, I wanted to create a disparate session for my administration area. In my abstract controller (where I check the details of $auth against my acl setting), I have:

 $params = $e->getRouteMatch()->getParams(); /** @var \Zend\Authentication\AuthenticationService */ $auth = $e->getApplication()->getServiceManager()->get('Zend\Authentication\AuthenticationService'); $_namespace = current(explode('\\', $params['__NAMESPACE__'])); // Most generic session namespace. if(in_array($_namespace, ['Customer', 'Application', null])) { $_namespace = 'Zend_Auth'; } $auth->setStorage(new \Zend\Authentication\Storage\Session($_namespace)); 

This does not create a second cookie, but it means that I can go to domain.dev/account (client section) and be able to log in independently of domain.dev/administrator (admin section), which is what I'm trying to do in the end ,

0
source share

All Articles