简体   繁体   English

使用CakePHP Auth组件的唯一用户名和电子邮件

[英]Unique username and email with CakePHP Auth component

I'm new to Cake and building an application to learn it. 我是Cake的新手并构建了一个应用程序来学习它。 I'm having a few troubles with my user registration system. 我的用户注册系统遇到了一些麻烦。 So far this is my registration code in my Users controller: 到目前为止,这是我在Users控制器中的注册码:

public function register() {
    $this->set('title_for_layout', 'Register');
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash('The user has been saved');
            $this->redirect(array('action' => 'register'));
        } else {
            $this->Session->setFlash('The user could not be saved. Please, try again.');
        }
    }
}

And within my User model I have this method where I hash the passwords: 在我的User模型中,我有这个方法,我在其中哈希密码:

public function beforeSave() {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}

This works, the user is added to my users table in my database with their username, email and hashed password. 这样做,用户将用他们的用户名,电子邮件和散列密码添加到我的数据库中的users表。 However, there are no checks done to make sure the username and email are unique. 但是,没有进行任何检查以确保用户名和电子邮件是唯一的。

From my limited understanding, I would need to add some validation rules to my User model to make sure the username and email fields are unique before they're entered into the table? 根据我的有限理解,我需要在我的User模型中添加一些验证规则,以确保用户名和电子邮件字段在输入表格之前是唯一的? At the moment I just have these validation rules: 目前我只有这些验证规则:

public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A username is required'
        )
    ),
    'email' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'An email is required'
        )
    ),
    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A password is required'
        )
    )
);

Also, my registration form has a Password (confirm) field called passwordConf . 此外,我的注册表单有一个名为passwordConfPassword (confirm)字段。 I would like to check if the user entered his passwords correctly before they're entered into the users table, but I'm not sure how to do that. 我想检查用户是否在输入用户表之前正确输入了密码,但我不知道该怎么做。 I'm guessing that somewhere in my register method I need to check if the two passwords are the same. 我猜我的寄存器方法中的某个地方我需要检查这两个密码是否相同。

Thanks for any help. 谢谢你的帮助。

The isUnique rule will work with your username and email fields. isUnique规则适用于您的用户名和电子邮件字段。 Here is a sample of code that shows how to use multiple rules per field: 以下是一个代码示例,演示了如何为每个字段使用多个规则:

public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'You must enter a username.'
        ),
        'length' => array(
            'rule' => array('between', 3, 15),
            'message' => 'Your username must be between 3 and 15 characters long.'
        ),
        'unique' => array(
            'rule'    => 'isUnique',
            'message' => 'This username has already been taken.'
        )
    ),
    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'You must enter a password.'
        ),
        'length' => array(
            'rule' => array('minLength', '6'),
            'message' => 'Your password must be at least 6 characters long.'
        )
    ),
        'email' => array(
            'email' => array(
            'rule'    => array('email'),
            'message' => 'Please enter a valid email address.'
        )
    )
);

As for comparing the passwords just edit your beforeSave callback and check the passwords against each other, returning true if they match and false if they do not. 至于比较密码只需编辑你的beforeSave回调并相互检查密码,如果匹配则返回true,如果不匹配则返回false。 Something like this: 像这样的东西:

public function beforeSave() {
    if (isset($this->data[$this->alias]['password'])) {
        if($this->data[$this->alias]['password'] === $this->data[$this->alias]['passwordConf']) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
            return true;
        } else {
            return false;
        }
    }
    return true;
}

CakePHP actually has a validation rule called isUnique , which you can use to check the username and e-mail. CakePHP实际上有一个名为isUnique的验证规则,您可以使用它来检查用户名和电子邮件。 A list of built in rules can be found here . 可以在此处找到内置规则列表。 You can use this and the Data Validation Tutorial to check the user name and e-mail. 您可以使用此数据验证教程来检查用户名和电子邮件。 As to checking if the passwords are the same, you MAY be able to use the EqualTo rule shown in the rules list, assuming you can make your validation rules on the fly every request. 至于检查密码是否相同,您可以使用规则列表中显示的EqualTo规则,假设您可以在每次请求时动态制定验证规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM