当电子邮件和密码位于单独的表中时验证用户cakephp
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了当电子邮件和密码位于单独的表中时验证用户cakephp,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2587字,纯文字阅读大概需要4分钟。
内容图文
![当电子邮件和密码位于单独的表中时验证用户cakephp](/upload/InfoBanner/zyjiaocheng/685/244a69d52f77420cab22714696a94a08.jpg)
表格接受电子邮件和密码
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
电子邮件作为ID存储在“用户”中,“密码”存储在密码表中
地址是“电子邮件”表中的属性,用于存储实际的电子邮件地址
密码是pw的存储位置
身份验证组件采用地址-
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
//here we define what is compared to be authenticated
'username' => 'address',
'password' => 'password'
]
]...
登录功能类似于正常情况:
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');
}
}`
我的看法是,我们比较电子邮件ID或获取表格以直接查看关联类的“地址”属性.一个人如何将身份验证指向另一个表中的属性
谢谢
解决方法:
我建议使用一种不那么麻烦的方法,即使用包含/加入用户表的自定义查找器,并使用别名在主查询上设置密码字段,或者在主实体上将其设置为虚拟字段,这样构建-in身份验证器检索所需的数据,这对身份验证器来说很重要.
例如,在您的EmailsTable类中,添加一个这样的查找器,该查找器为password字段选择适当的值:
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']
]);
}
使用这样的查找程序,您要做的就是为auth组件配置字段,userModel和查找程序选项,例如:
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
// set the field to `email`, just like in your form
'username' => 'email'
],
'userModel' => 'Emails',
'finder' => 'auth'
]
// ...
]
]);
本示例假定使用联接策略通过Email或hasOne关联将电子邮件与用户关联.
还要注意,就像在示例表单中一样,用户名字段设置为电子邮件,您也可以将其都设置为地址(或您实际喜欢的任何内容),因为创建新查询并使用它不会影响查找者查询.通过配置字段从请求数据中提取的用户名值(提取的值将始终传递到$options数组的用户名键中,除非finder查找器配置将是已经具有名为username的键的数组).
也可以看看
> Cookbook > Controllers > Components > Authentication > Customizing Find Query
内容总结
以上是互联网集市为您收集整理的当电子邮件和密码位于单独的表中时验证用户cakephp全部内容,希望文章能够帮你解决当电子邮件和密码位于单独的表中时验证用户cakephp所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。