帐户激活的最佳方式

时间:2011-10-30 17:06:29

标签: php model-view-controller cakephp-2.0

我正在尝试使用CakePHP 2.0创建帐户注册页面,用户需要通过点击插入username后收到的电子邮件中的链接来激活新帐户,emailpassword

我的问题是如何在用户记录中设置激活码。

我想创建一个名为activation_code的表字段,然后存储hashed版本的username,以确保用户可以通过点击激活的电子邮件链接来激活自己键。

所有程序都已完成,但我不知道如何在activation_code对象中设置$data['User']并且我不清楚这是否是对MVC框架的良好用法或我应该以不同的方式做到。

在用户注册操作期间,我已经完成了此操作,但是当我尝试动态创建“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'));
            }
        }
    }
}

显然activation_key是我数据库中的空字段。

那么如何从控制器动态创建文件?

2 个答案:

答案 0 :(得分:0)

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

应该是:

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

(您应该将所有对$ this->数据的引用更改为新的cakephp2.0 $ this-> request-> data)

答案 1 :(得分:0)

我用方法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'));
            }
        }
    }
}
相关问题