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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)