我正在使用带有Auth和ACL组件的 CakePHP 1.2。

在我的用户注册操作中,密码是未加入的。具体来说,这个表达式:

if ($this->data['User']['password'] !=
    $this->Auth->password($this->data['User']['confirm_password']))

即使我为 password confirm_password 提交相同的值,也会评估为true。我知道密码没有散列,因为当我删除对 Auth->密码的调用时,表达式的计算结果为false。

我希望Auth模块自动散列密码。我做错了什么?

以下是我的观点:

<?php
    echo $form->create('User', array('action' => 'register'));

    echo $form->input('email',
                      array('after' => $form->error(
                        'email_unique', 'This email is already registered.')));
    echo $form->input('password');
    echo $form->input('confirm_password', array('type' => 'password'));
    echo $form->end('Register');
?>

以下是来自用户控制器的注册操作:

function register(){
    if ($this->data) {
        if ($this->data['User']['password'] !=
            $this->Auth->password($this->data['User']['confirm_password'])) {

            $this->Session->setFlash(__('Password and Confirm Password must match.', true));
            $this->data['User']['password'] = '';
            $this->data['User']['confirm_password'] = '';
        }
        else{
            $this->User->create();
            if ($this->User->save($this->data)){
                $this->redirect(array('action' => 'index'), null, true);
            }
            else {
                $this->data['User']['password'] = '';
                $this->data['User']['confirm_password'] = '';
                $this->Session->setFlash(__('Some problem saving your information.', true));
            }
        }
    }
}

这是我的 appController ,其中包含 Auth Acl 模块:

class AppController extends Controller {
    var $components = array('Acl', 'Auth');

    function beforeFilter(){
        if (isset($this->Auth)) {
            $this->Auth->allow('display');
            $this->Auth->fields =
              array(
                'username' => 'email',
                'password' => 'password');
            $this->Auth->authorize = 'actions';
        }
    }
}

我做错了什么?

有帮助吗?

解决方案

CakePHP不会散列密码,除非username包含提交的值。我正在用电子邮件替换用户名字段。但是,我通过设置Auth-&gt; fields数组重新映射了这些字段。但是,我在appController而不是userController中这样做。所以移动这一行:

$this->Auth->fields = array('username' => 'email', 'password' => 'password');
从appController到userController解决了它。
现在问题变成“为什么我不能重置appController中的Auth-&gt;字段?”

其他提示

您可能正在使用 UsersController :: beforeFilter()覆盖 AppController :: beforeFilter()

要“修复”它,只需在函数的开头放入 parent :: beforeFilter()

您应该在保存到数据库之前散列密码。将此功能放入用户模型中:

function beforeSave() {
  if(isset($this->data[$this->alias]['password']))
    $this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], null, true);
  return true;
}

不要忘记将 beforeFilter()中的内容添加到用户控制器中:

if(in_array($this->action, array('register'))) {
  $this->Auth->fields = array('username' => 'email', 'password' => 'wrongfield');
}

这意味着在注册过程中不会对密码进行哈希处理(如果注册表单的验证失败)。

我认为你在寻找

hashPasswords ($data)

看看这些页面。他们应该指出你正确的方向。您还可以尝试在核心配置文件中更改调试级别。将它从0(生产)更改为3可以让您看到SQL输出。可能会有所帮助。

AuthComponent-Methods

Cakephp疑难解答

对不起,我做不了什么,只是指出了正确的方向。我是cakephp的新手。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top