为什么 CakePHP 身份验证组件不散列我的密码?

发布于 2024-07-14 19:14:02 字数 2470 浏览 6 评论 0原文

我正在使用 CakePHP 1.2 以及 Auth 和 ACL 组件。

在我的用户注册操作中,密码未经过哈希处理。 具体来说,这个表达式:

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

即使我为 passwordconfirm_password 提交相同的值,This 的计算结果仍为 true。 我知道密码未经过哈希处理,因为当我删除对 Auth->password 的调用时,表达式的计算结果为 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,其中包含 AuthAcl 模块:

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';
        }
    }
}

我究竟做错了什么?

I'm using CakePHP 1.2 with Auth and ACL components.

In my user register action, the password is coming in unhashed. Specifically, this expression:

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

This is evaluating to true, even when I submit identical values for password and confirm_password. I know that password is unhashed because when I remove the call to Auth->password, the expression evaluates to false.

I expected the Auth module to automagically hash the password. What am I doing wrong?

Here is my view:

<?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');
?>

Here is my register action from the user controller:

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));
            }
        }
    }
}

And here is my appController where I include the Auth and Acl modules:

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';
        }
    }
}

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微暖i 2024-07-21 19:14:02

CakePHP 不会对密码进行哈希处理,除非用户名包含提交的值。 我正在用电子邮件替换用户名字段。 但是,我通过设置 Auth->fields 数组重新映射了这些字段。 但是,我是在 appController 而不是 userController 中执行此操作。 因此,将这一行:

$this->Auth->fields = array('username' => 'email', 'password' => 'password');

从 appController 移到 userController 中解决了它。
现在问题变成了“为什么我不能重置 appController 中的 Auth-> 字段?”

CakePHP won't hash passwords unless username contains a submitted value. I'm replacing the username field with email. However, I remapped those fields by setting the Auth->fields array. However, I was doing that in the appController instead of userController. So moving this line:

$this->Auth->fields = array('username' => 'email', 'password' => 'password');

out of appController into userController solved it.
Now the question becomes "Why can't I reset the Auth->fields in appController?"

谁人与我共长歌 2024-07-21 19:14:02

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

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

You're probably overriding AppController::beforeFilter() with your UsersController::beforeFilter().

To "fix" it, just put parent::beforeFilter() at the beginning of the function.

岛徒 2024-07-21 19:14:02

您应该在保存到数据库之前对密码进行哈希处理。 将此函数放入您的用户模型中:

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');
}

这意味着密码在注册过程中不会被散列(如果验证失败)登记表)。

You should hash password before saving to database. Place this function into your User model:

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;
}

And don't forget to put this in beforeFilter() to your users controller:

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

That means that password will NOT be hashed during registration process (in case of failed validation of register form).

阿楠 2024-07-21 19:14:02

我认为您正在寻找

hashPasswords ($data)

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

AuthComponent-Methods

Cakephp 故障排除

抱歉,我无能为力,只能为您指明正确的方向。 我是 cakephp 的新手。

i think you are looking for

hashPasswords ($data)

look at these pages. They should point you in the right direction. You can also try changing your debugging level in the core config file. changing it from 0 (production) to 3 allows you to see you sql output. may be helpful.

AuthComponent-Methods

Cakephp troubleshooting

Sorry i can't do anything but point you in the right direction. I'm new to cakephp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文