我正在尝试通过CakePHP 2
的用户激活页面更改注册用户的状态。激活页面包含一个散列字符串,该字符串位于此用户表中:
id username email password active key
1 bill me@domain.com 4u2iu4hk24(hashed str) 0 2ol3p(hashed str)
激活程序适用于这样的网址:
http://url.com/users/activate/2ol3pth3i89txc2zd6tg3
我所做的就是获取散列密钥,使用此key
搜索注册用户,加载,删除散列密钥并将active
状态更改为{ {1}}。
我在这个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;
}
}
}
?>
?
哪里我错了?
答案 0 :(得分:0)
你没有将id传递给模型,我猜你有适当的验证规则,所以它不会保存记录。 save()之后调试($ this-&gt; User-&gt; invalidFields())。
所有这一切都应该在一个模型中完成,比如User :: verify($ code),如果找不到用户/令牌则抛出异常。记住:微小的控制器,胖模型。模型也更容易测试。