简体   繁体   中英

How do I change the user session after a user logs in with CakePHP?

I have

$user = $this->Auth->user();

which retrieves the current user from the session.

I want to make an admin user be able to 'act as' a customer. And I was hoping to be able to just replace the customer_id in the user session when they enter the customer interface.

$user['User']['customer_id'] = 4;

This doesn't work because I can't find a way to push the $user data back into Auth

This should work:

$this->Session->write('Auth.User.customer_id', 4);

Please note that this approach of just changing the customer_id can have some side effects if you are also using Acl and group based permission model.

If you are simply checking the customer_id on the session to determine if that user is of the type customer, instead of:

$user['User']['customer_id'] = 4;

I would try:

$this->Session->write('User.customer_id', '4');

Try combining $this->Auth->logout() and then $this->Auth->login() with the customers' data.

However, you need to implement additional logic if you want your admin to be able to return to his account without entering his credentials again. It's a no brainer, but worth mentioning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM