cakephp 2.0 how to update auth data?

后端 未结 7 1341
别那么骄傲
别那么骄傲 2021-02-01 08:12

when I update data in the User model, the Auth data is not updated.

How can I \"refresh\" the data which is returned by $this->Auth->user() when I am updating user model

7条回答
  •  面向向阳花
    2021-02-01 08:46

    I think func0der's answer is good, but it may be improved:

    protected function renewLogin() {
        if(!empty($this->Auth->user())) {
            $this->loadModel('User');
            $this->User->contain(false);
            $user = $this->User->read(null, $this->Auth->user('id'))['User'];
            unset($user['password']);
            $this->Auth->login($user);
        }
    }
    

    You can add this to your AppController.php and use it from any controller after modifying the logged in user.

    I believe it's much cleaner and even though it implies access to the database, I think it's not going to be executed often enough to be an issue.

    You should ALWAYS try to do things the "Cake" way. And avoid shortcuts like editing a session variable. It'll make things easier for you on the long run.

    Please comment and add your opinions :)

提交回复
热议问题