帐户激活的最佳方式

发布于 2024-12-12 21:12:37 字数 1310 浏览 2 评论 0原文

我正在尝试使用 CakePHP 2.0 创建一个帐户注册页面,用户需要通过单击插入用户名后收到的电子邮件中的链接来激活其新帐户,<代码>电子邮件和密码

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

我想创建一个名为 activation_code 的表字段,然后存储 用户名散列版本,以确保用户可以通过单击激活自己带有激活密钥的电子邮件链接。

所有过程均已完成,但我不知道如何在 $data['User'] 对象内设置 activation_code 并且我不清楚这是否是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 是我数据库中的空字段。

那么如何从控制器动态创建一个字段呢?

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.

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.

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.

During the user registration action I've done this but I get an error when I try to create 'activation_code' dynamically:

// 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.

So how can I create a filed dynamically from the controller?

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

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

发布评论

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

评论(2

梦晓ヶ微光ヅ倾城 2024-12-19 21:12:38

我已经用 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'));
            }
        }
    }
}

I've solved the problem with the method Model::set(), so:

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'));
            }
        }
    }
}
初见 2024-12-19 21:12:37
$this->data['User']['activation_key']

应该是:(

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

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

$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)

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