php - CakePHP:无法在身份验证模块上设置自定义查找器
问题描述
版本:4.2.9
我已按照本教程设置登录。 https://book.cakephp.org/authentication/2/en/index.html
问题是,我的自定义查找器没有被识别,因为在调试消息中我没有看到 Userdata 表的连接。
这是我的查询错误,因为未连接 Userdata 表。
SELECT Users.ID AS Users__ID, Users.UUID AS Users__UUID, Users.PasswordHashed AS Users__PasswordHashed, Users.Role AS Users__Role, Users.ReleaseGroup AS Users__ReleaseGroup FROM users Users WHERE Users.email = :c0 LIMIT 1
在我的 UsersTable.php
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('ID');
$this->setPrimaryKey('ID');
$this->hasOne('Userdata');
}
public function findAuth(\Cake\ORM\Query $query, array $options) {
return $query->contain('Userdata');
}
在我的 UsersController.php
public function beforeFilter(\Cake\Event\EventInterface $event)
{
parent::beforeFilter($event);
// Configure the login action to not require authentication, preventing
// the infinite redirect loop issue
$this->Authentication->addUnauthenticatedActions(['login']);
}
public function login()
{
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
// regardless of POST or GET, redirect if user is logged in
if ($result->isValid()) {
// redirect to /articles after login success
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Articles',
'action' => 'index',
]);
return $this->redirect($redirect);
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}
}
在我的 AppController.php
public function initialize(): void
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Authentication.Authentication');
$this->loadComponent('Auth', [
'authorize'=> 'Controller',
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'fmail',
'password' => 'password'
],
'finder' => 'auth',
]
],
//use isAuthorized in Controllers
'authorize' => ['Controller'],
// If unauthorized, return them to page they were just on
'unauthorizedRedirect' => $this->referer()
]);
在我的 src/Application.php
public function bootstrap(): void
{
// Call parent to load bootstrap from files.
parent::bootstrap();
$this->addPlugin('Authentication');
if (PHP_SAPI === 'cli') {
$this->bootstrapCli();
} else {
FactoryLocator::add(
'Table',
(new TableLocator())->allowFallbackClass(false)
);
}
/*
* Only try to load DebugKit in development mode
* Debug Kit should not be installed on a production system
*/
if (Configure::read('debug')) {
$this->addPlugin('DebugKit');
}
// Load more plugins here
}
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
$middlewareQueue
// ... other middleware added before
->add(new RoutingMiddleware($this))
// add Authentication after RoutingMiddleware
->add(new AuthenticationMiddleware($this));
return $middlewareQueue;
}
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => Router::url('/users/login'),
'queryParam' => 'redirect',
]);
// Load identifiers, ensure we check email and password fields
$authenticationService->loadIdentifier('Authentication.Password', [
'fields' => [
'username' => 'email',
'password' => 'password',
],
]);
// Load the authenticators, you want session first
$authenticationService->loadAuthenticator('Authentication.Session');
// Configure form data check to pick email and password
$authenticationService->loadAuthenticator('Authentication.Form', [
'fields' => [
'username' => 'email',
'password' => 'password',
],
'loginUrl' => Router::url('/users/login'),
]);
return $authenticationService;
}
这是我对 3.x(旧) CakePHP 解决方案的问题:登录 2 个表
解决方案
感谢您朝着正确的方向前进。
从我的 AppControler.php 中删除了旧的 loadComponent
public function initialize(): void
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Authentication.Authentication');
/*
* Enable the following component for recommended CakePHP form protection settings.
* see https://book.cakephp.org/4/en/controllers/components/form-protection.html
*/
//$this->loadComponent('FormProtection');
}
将表与 UsersTable.php 中的自定义绑定/外键链接
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('ID');
$this->setPrimaryKey('ID');
$this->hasOne('Userdata', [
'bindingKey' => 'UUID',
'foreignKey' => 'UUID',
]);
}
public function findForAuthentication(\Cake\ORM\Query $query, array $options): \Cake\ORM\Query
{
return $query->contain('Userdata');
}
并在我的 src/Application.php 中定义了我的自定义解析器
// Load identifiers, ensure we check email and password fields
$authenticationService->loadIdentifier('Authentication.Password', [
'fields' => [
'username' => 'userdata.email',
'password' => 'PasswordHashed',
],
'resolver' => [
'className' => 'Authentication.Orm',
'userModel' => 'Users',
'finder' => 'forAuthentication',
],
]);
谢谢
推荐阅读
- javascript - 我怎样才能让一个 div 持有较小的 div 以在一行上保存 3 然后继续到下一行?
- javascript - 如何使用来自 Django 的 models.py 的数据创建一个按钮来更改我的 html 中的文本?
- rust - 可选参数/空值处理
- firebase - API 密钥对本地开发环境中的 firebase、nextjs、vercel 项目无效
- php - 使用 PHP 获取名称服务器?
- android - Android Intent Chooser 在某些情况下无法打开文件
- python - TFLite 解释器:定义最佳线程数
- pygithub - 我需要一种更快的方法来使用 PyGitHub 查找存储库
- oracle - Oracle 应用程序 - 下一步属性
- tensorflow - 检测完整的姿势图像