LoginSignup
3
2

More than 5 years have passed since last update.

[baserCMS]ユーザー(UserModel)保存時にパスワードの暗号化処理が不要な動作となっている件

Last updated at Posted at 2014-10-24

ユーザーモデルをオーバーライドして機能作成中に気付いたんでシェア。

環境

  • baserCMS 3.0.6.1
  • PHP 5.4.19

発生するタイミングと状況

  • baserCMSのUserModelを利用して会員機能とか作ってるとき
  • 会員のパスワードを保存するフィールド名が「password」のとき
  • 以下のように、コントローラーでパスワードに対して暗号化処理を入れずに保存
if (!empty($this->request->data)) {
    $this->Member->create($this->request->data);
    if ($this->Member->save()) {
        $this->setMessage('追加しました。', false, true);
        $this->redirect(array('action' => 'edit', $id));
    } else {
        $this->setMessage('入力エラーです。内容を修正してください。', true);
    }
}
  • DBの中身を見るとしっかり暗号化されている

あれ?どうして?
ちなみに通常であればこんな感じになるかな、と。

if (!empty($this->request->data)) {
    $this->request->data['Member']['password'] = $this->BcAuth->password($this->data['Member']['password']);
    $this->Member->create($this->request->data);
    if ($this->Member->save()) {
        $this->setMessage('追加しました。', false, true);
        $this->redirect(array('action' => 'edit', $id));
    } else {
        $this->setMessage('入力エラーです。内容を修正してください。', true);
    }
}

正体

UserModelのbeforeSave()で、passwordというフィールド名が存在する場合は、暗号化処理を施すようにしてありました。

/lib/Baser/Model/User.php
/**
 * beforeSave
 * 
 * @param type $options
 * @return boolean
 */
    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            App::uses('AuthComponent', 'Controller/Component');
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }

baserCMSは、制作者さんにやさしいCMSですね。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2