开发者

Updating a different user's session in CakePHP

开发者 https://www.devze.com 2023-02-10 10:09 出处:网络
Here\'s my CakePHP scenario. I have the controller MembersController which handles all user information, and also cotnains the login() and logout() functions. Staff members have the ability to edit u

Here's my CakePHP scenario.

I have the controller MembersController which handles all user information, and also cotnains the login() and logout() functions. Staff members have the ability to edit users through the edit function. However, when a staff member edits a user, the user's own session does not update.

I am looking for some sort of function to go into AppController that compares the users session with their corresponding database entry. If there is a mismatch (ie. the database has been updated), I would like to refresh the user's session开发者_开发技巧.

I have seen many solutions that work when the user is editing their own details, but not when a staff member is editing another users details.

Thanks a lot for your help!


Well you could do this but you'd end up hitting the database for the user information on every page request. Your best bet would be to do something like this, probably.

function _updateUserSession() {
    if ($this->Auth->user()) {
        $user_id = $this->Auth->user($this->User->primaryKey);
        $user    = $this->User->read(null, $user_id);
        $this->Session->write($this->Auth->sessionKey, $user[$this->User->alias]);
    }
}

This checks that the user is logged in and then grabs their ID (as referenced by the primaryKey) and then requests the the corresponding User record from the database. After it gets the record it writes that information to the Session at the location that AuthComponent expects.


if you use the database you can delete the user's session. just create a backend for all current sessions. i use "online_activities" which are connected to the session to track the user_id.

assuming that you have cookie auth enabled (implemented) you can easily force the browser to refresh the user session this way. the user itself doesnt even notice.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号