0

我正在尝试通过用户激活页面更改注册用户的状态CakePHP 2。激活页面包含位于此用户表中的散列字符串:

id    username    email            password                  active    key
1     bill        me@domain.com    4u2iu4hk24(hashed str)    0         2ol3p(hashed str)

激活过程适用于这样的 url:

http://url.com/users/activate/2ol3pth3i89txc2zd6tg3

我所做的就是获取散列键,用它搜索注册用户key,加载它,删除散列键并将active状态更改为1. 我在这种Controller方法中执行此过程:

public function activate ($code = false) {
    if (!empty ($code)) {
        $this->User->findByActivationKey($code); // magic find instead of $this->User->find('contidions', array('User.activation_key' => $code));
        if (!empty($user)) {
            $this->User->set(array (
                'activation_key' => '0',
                'active' => 1
            ));
            if ($this->User->save()) {
                $this->render('activation_successful');
            } else {
                // here is always where I get with this code
                $this->render('activation_fail');
            }
        } else {
            $this->render('activation_fail');
        }
    }
}

一切都很好,但$this->User->save()不会工作。

使用debug($this->User->invalidFields());将返回此错误:

app/Controller/UsersController.php (line 64)
Array
(
[password] => Array
    (
        [0] => Password must contain <span5</span> chars min and <span>20</span> chars max message.
        [1] => Password must contain <span5</span> chars min and <span>20</span> chars max message.
    )

)

显然,错误来自模型,但为什么password会提到?

<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
    public $name = 'User';
    var $validate = array (
        'username' => array (
            'MustBeCompiled' => array (
                'rule' => 'notEmpty',
                'message' => 'Error message'
            )
        ), 
        'password' => array (
            'not_empty' => array (
                'rule' => 'notEmpty',
                'message' => 'Password cannot be empty message.'
            ),
            'between' => array (
                'rule' => array ('between', 5, 20),
                'message' => 'Password must contain <span5</span> chars min and <span>20</span> chars max message.'
            )
        ),
        'email' => array (
            'valid_email' => array (
                'rule' => 'email',
                'message' => 'Mail invalid message.'
            ),
            'existing_email' => array (
                'rule' => 'isUnique',
                'message' => 'Mail in use message.'
            )
        )
    );

    function beforeSave ($options = array()) {
        // the way to hash the password
        if (!empty ($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
            return true;
        }
    }
}
?>

也许问题可能出在beforeSave()?我哪里错了?

4

1 回答 1

0

你没有将 id 传递给模型,我猜你有验证规则,所以它不会保存记录。在 save() 之后调试($this->User->invalidFields())。

所有这些都应该在一个模型中完成,比如 User::verify($code) 并在找不到用户/令牌时抛出异常。记住:小控制器,胖模型。模型也更容易测试。

于 2011-11-02T02:04:24.480 回答