简体   繁体   English

激活帐户的最佳方法

[英]Best way for account activation

I'm trying to create an account register page with CakePHP 2.0 where user needs to activate it's new account by clicking on a link in the email he's received after insert username , email and password . 我正在尝试使用CakePHP 2.0创建一个帐户注册页面,用户需要在插入usernameemailpassword后单击收到的电子邮件中的链接来激活它的新帐户。

My question is how can I set an activation code inside the user record. 我的问题是如何在用户记录中设置激活码。

I thought to create a table field named activation_code and then to store an hashed version of the username to be sure the user can activate itself by clicking the email link with the activation key. 我想创建一个名为activation_code的表字段,然后存储usernamehashed版本,以确保用户可以通过单击带有激活密钥的电子邮件链接来激活自己。

All the procedure is done but I don't know how can I set the activation_code inside the $data['User'] object and It's not clear for me if this is a good usage of the MVC framework or I should make it in a different way. 所有过程都已完成,但是我不知道如何在$data['User']对象中设置activation_code对于我来说还不清楚这是否是MVC框架的良好用法,还是应该在MVC框架中使用它?不同的方法。

During the user registration action I've done this but I get an error when I try to create 'activation_code' dynamically: 在用户注册操作期间,我已经这样做了,但是当我尝试动态创建'activation_code'时出现错误:

// from the UserController class
public function register () {
    if (!empty($this->data)) {
        if ($this->data['User']['password'] == $this->data['User']['confirm_password']) {
            // here is where I get the error
            $this->data['User']['activation_key'] = AuthComponent::password($this->data['User']['email']);
            $this->User->create();
            if ($this->User->save($this->data)) {
                // private method
                $this->registrationEmail ($this->data['User']['email'], $this->data['User']['username']);
                $this->redirect(array('controller'=>'users', 'action'=>'registration', 'success'));
            }
        }
    }
}

Obviously the activation_key is an empty field inside my database. 显然, activation_key是数据库中的一个空字段。

So how can I create a filed dynamically from the controller? 那么如何从控制器动态创建文件?

$this->data['User']['activation_key']

should be: 应该:

$this->request->data['User']['activation_key']

(You should change all references to $this->data to the new cakephp2.0 $this->request->data) (您应该将对$ this-> data的所有引用更改为新的cakephp2.0 $ this-> request-> data)

I've solved the problem with the method Model::set() , so: 我已经用Model::set()方法解决了这个问题,所以:

public function register () {
    if (!empty($this->data)) {
        if ($this->data['User']['password'] == $this->data['User']['confirm_password']) {
            $this->User->create();
            // I've used set method
            $this->User->set('activation_key', AuthComponent::password($this->data['User']['email']));
            if ($this->User->save($this->data)) {
                $this->registrationEmail ($this->data['User']['email'], $this->data['User']['username']);
                $this->redirect(array('controller'=>'users', 'action'=>'registration', 'success'));
            }
        }
    }
}

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

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