dtnwm4807 2017-05-03 03:58
浏览 186
已采纳

当电子邮件和密码位于单独的表cakephp中时验证用户

A form takes in an email and pw

<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>

The email is stored as an ID in Users, and Password is in password table Address is the attribute in Emails table that stores the actual email address Password is where pw is stores

The authenticate component takes in address - which

$this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    //here we define what is compared to be authenticated
                    'username' => 'address',    
                    'password' => 'password'
                ]
            ]...

The login function is like normal:

public function login()
{
    if ($this->request->is('post')) {

        //PUT IN STUFF HERE
        $user = $this->Auth->identify();


        if ($user) {
            $user->last_login = Time::now();//dont put this above uif statement, will automatically create a default object
            $this->Auth->setUser($user);
            $this->Flash->success('You have been successfully logged in.');
            $this->log("Login success", 'debug');
          //redirect after login
            return $this->redirect($this->Auth->redirectUrl('/users/index'));
        }
        $this->Flash->error('Your username or password is incorrect.');
        $this->log("Login FAILURE", 'debug');
    }
}`

How i see it, we either compare email id's or get the form to look directly at the Associated classes 'address' attribute. How does one point the authentication to the attribute in another table like that

Thanks

  • 写回答

2条回答 默认 最新

  • dsfdsfdsfdsf1223 2017-05-03 14:19
    关注

    I would suggest a less intrusive way, that is, using a custom finder that contains/joins the users table, and sets the password field on the main query using an alias, or on the main entity as a virtual field, that way the built-in authenticator retrieves the data it needs, which is all that matters for the authenticator.

    For example in your EmailsTable class, add a finder like this, which selects the proper value for the password field:

    public function findAuth(\Cake\ORM\Query $query, array $options)
    {
        return
            $this
                ->find()
                ->select([
                    'Emails.id',
                    'Emails.address', // you may want to alias this one too
                    'password' => 'Users.password'
                ])
                ->leftJoinWith('Users')
                ->where([
                    // the options is always named `username`, this is
                    // not affected by the `fields` configuration
                    'Emails.address' => $options['username']
                ]);
    }
    

    With such a finder, all you then need to do is to configure the fields, userModel, and finder options for the auth component, like:

    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    // set the field to `email`, just like in your form
                    'username' => 'email'
                ],
                'userModel' => 'Emails',
                'finder' => 'auth'
            ]
            // ...
        ]
    ]);
    

    This example assumes that Emails is associated with Users via a belongsTo or hasOne association, using the join strategy.

    Also note that the username field is set to email just like in your example form, you could as well set both to address (or anything you like actually), it will not affect the finders query, as it creates a new query, and uses the username value that was extracted from the request data via the configured field (the extracted value will always be passed in the username key of the $options array, unless the finder configuration would be an array that already has a key named username).

    See also

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化