版本号: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
在我的用户表. 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');
}
在我的用户控制器. 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'));
}
}
在我的应用控制器. 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/应用程序. 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: Login over 2 tables解决方案的问题
1条答案
按热度按时间mv1qrgav1#
谢谢你把我推向正确的方向。
从我的AppController.php中删除了旧的loadComponent
在UsersTable.php中使用自定义绑定/外键链接表
并在src/Application.php中定义了我的自定义解析器
谢谢