【问题标题】:Best way for account activation帐户激活的最佳方式
【发布时间】:2016-01-21 03:33:10
【问题描述】:

我正在尝试使用CakePHP 2.0 创建一个帐户注册页面,用户需要通过单击插入username、email 和password 后收到的电子邮件中的链接来激活它的新帐户。

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

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

所有过程都已完成,但我不知道如何在 $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 在我的数据库中是一个空字段。

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

【问题讨论】:

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


    【解决方案1】:
    $this->data['User']['activation_key']
    

    应该是:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2023-04-11
        • 2016-03-11
        • 2017-10-03
        • 1970-01-01
        • 2010-10-30
        • 2023-03-16
        • 2017-06-04
        • 2020-12-11
        • 2011-05-16
        相关资源
        最近更新 更多