簡體   English   中英

CakePHP在用戶升級后自動更改密碼

[英]CakePHP automatically changes password after user upgrade

我有以下星座:

Table `users`
Table `profiles`

profiles表在User hasOne中具有hasOne而在Profile hasOne與具有belongsTo users belongsTo

注冊,登錄,一切正常。 首次登錄后,用戶必須創建一個包含其他數據的profiles條目。 同時上傳用戶圖片。 圖像路徑存儲在users表中。

ProfilesController.php

public function add() {
    $profile = $this->Profile->find('count', array('recusrive'=>-1, 'conditions'=>array('Profile.user_id'=>$this->Auth->user('id'))));
    if($profile > 0) {
        debug("error 1"); exit;
    }

    if ($this->request->is('post')) {

        // move tmp image to uploads folder
        // ... code with uploading image

        // create profile
        $this->Profile->create();

        // unset uploaded picture
        $profile_image = $this->request->data['User']['image']['name'];
        unset($this->request->data['User']);

        // prepare data to save
        $profileSafeData = array();
        $profileSafeData['Profile'] = $this->request->data['Profile'];

        if(!$this->Profile->save($profileSafeData)) {
            debug("error 2"); exit;
        }
        // works correctly, profile created.

        // save image to user
        $this->loadModel('User');
        $user = $this->User->find('first', array('conditions'=>array('User.id'=>$this->Auth->user('id')), 'recursive'=>-1));
        $user['User']['image'] = $profile_image;

        if($this->User->save($user)) { // *** here *** comes the problem. the users password changes after this save.
            $this->Session->setFlash(__('Congratulations! Your profile has been created.'), 'flash/success');

            // update new session data
            $this->Session->write('Auth', $this->User->read(null, $this->Auth->User('id')));

            $this->redirect(array('action' => 'view'));
        } else {
            return $this->Session->setFlash(__('Profile image could not be added to your user'), 'flash/error');
        }
    }

在最后一個用戶find('first')中,您包括所有字段。

正在從數據庫中讀取密碼,因此將其包含在save()中並進行了第二次加密。

解:

只需將字段選項添加到find('first')或從$ user數組中取消設置密碼索引。

$user = $this->User->find('first', array(
    'conditions'=>array('User.id'=>$this->Auth->user('id')), 
    'recursive'=>-1
    'fields' => array('id','image')
));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM