通过用户名或电子邮件进行身份验证,但dbal例外:未定义的偏移:1



从电子邮件(在4.2.4中)成功身份验证开始,尝试关注用户提供商文档以

添加用户名添加身份验证

d bal例外:...未定义的偏移:1

奇怪的是,如果我运行的SQL语句如调试分析器View runnable query所示,则查询将正确运行并返回非null结果。因此,至少存储库可以返回用户名。

我尝试使用entityManager重写查询构建器语句;提供带有两个参数的setParameters()语句;一切都无济于事。

编辑#2:

按照跟踪中标识的学说代码戳(见下文),例外发生在线上

[$query, $params, $types] = SQLParserUtils::expandListParameters($query, $params, $types);

$query$params(如dd(...)所示)对我很有意义。但是,$types变量是不透明的:这是一个数组[0 => 102, 1 => 102]。语句SQLParserUtils...

发生例外

结束编辑#2

编辑#3:

这是学说中发生的事情,但我不知道代码的意图。在DoctrineDBALSQLParserUtils中,第129-133行包含在foreach ()循环中,在$paramsPos数组[0 => 496, 1 => 513]中为每个元素运行一次,其中值是SQL语句中可更换参数的位置。$ params数组最初包含

array:2 [▼
  0 => array:1 [▼
    "username" => "gbrooks"
  ]
  1 => array:1 [▼
    "username" => "gbrooks"
  ]
]

在执行125-133(如下所示)中的代码之后

$needle    += $paramOffset;
$needlePos += $queryOffset;
$count      = count($params[$needle]);
$params = array_merge(
    array_slice($params, 0, $needle),
    $params[$needle],
    array_slice($params, $needle + 1)
);

$params数组包含

array:2 [▼
  "username" => "gbrooks"
  0 => array:1 [▼
    "username" => "gbrooks"
  ]
]

注意不再存在键1,因此,当第127行的count($params[$needle])语句通过下一个通过循环遇到(现在$needle现在为1时, DBAL Exception Undefined offset: 1就会抛出。

结束编辑#3

用户介绍:

namespace AppRepository;
use SymfonyBridgeDoctrineSecurityUserUserLoaderInterface;
use DoctrineORMEntityRepository;
class UserRepository extends EntityRepository implements UserLoaderInterface
{
    public function loadUserByUsername($usernameOrEmail)
    {
        $user = $this->createQueryBuilder('u')
            ->where('u.username = :query OR u.email = :query')
            ->setParameter('query', $usernameOrEmail)
            ->getQuery()
            ->getOneOrNullResult();
    }
}

loginformauthenticator包含:

public function getUser($credentials, UserProviderInterface $userProvider)
{
    $token = new CsrfToken('authenticate', $credentials['csrf_token']);
    if (!$this->csrfTokenManager->isTokenValid($token)) {
        throw new InvalidCsrfTokenException();
    }
    $user = $this->entityManager->getRepository(User::class)->loadUserByUsername(['username' => $credentials['username']]);
    if (!$user) {
        // fail authentication with a custom error
        throw new CustomUserMessageAuthenticationException('Email could not be found.');
    }
    return $user;
}

跟踪:

G:DocumentsworkspacemanavendordoctrinedballibDoctrineDBALDBALException.php:172 {▶}
  G:DocumentsworkspacemanavendordoctrinedballibDoctrineDBALDBALException.php:145 {▶}
  G:DocumentsworkspacemanavendordoctrinedballibDoctrineDBALConnection.php:911 {▶}
  G:DocumentsworkspacemanavendordoctrineormlibDoctrineORMQueryExecSingleSelectExecutor.php:50 {▶}
  G:DocumentsworkspacemanavendordoctrineormlibDoctrineORMQuery.php:334 {▶}
  G:DocumentsworkspacemanavendordoctrineormlibDoctrineORMAbstractQuery.php:967 {▶}
  G:DocumentsworkspacemanavendordoctrineormlibDoctrineORMAbstractQuery.php:922 {▶}
  G:DocumentsworkspacemanavendordoctrineormlibDoctrineORMAbstractQuery.php:765 {▶}
  G:DocumentsworkspacemanasrcRepositoryUserRepository.php:24 {▶}
  G:DocumentsworkspacemanasrcSecurityLoginFormAuthenticator.php:74 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-guardProviderGuardAuthenticationProvider.php:102 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-guardProviderGuardAuthenticationProvider.php:96 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-coreAuthenticationAuthenticationProviderManager.php:76 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-guardFirewallGuardAuthenticationListener.php:130 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-guardFirewallGuardAuthenticationListener.php:82 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-bundleDebugWrappedListener.php:46 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-bundleDebugTraceableFirewallListener.php:35 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-httpFirewall.php:90 {▶}
  G:Documentsworkspacemanavendorsymfonysecurity-bundleEventListenerFirewallListener.php:48 {▶}
  G:Documentsworkspacemanavendorsymfonyevent-dispatcherDebugWrappedListener.php:115 {▶}
  G:Documentsworkspacemanavendorsymfonyevent-dispatcherEventDispatcher.php:212 {▶}
  G:Documentsworkspacemanavendorsymfonyevent-dispatcherEventDispatcher.php:44 {▶}
  G:Documentsworkspacemanavendorsymfonyevent-dispatcherDebugTraceableEventDispatcher.php:145 {▶}
  G:Documentsworkspacemanavendorsymfonyhttp-kernelHttpKernel.php:126 {▶}
  G:Documentsworkspacemanavendorsymfonyhttp-kernelHttpKernel.php:67 {▶}
  G:Documentsworkspacemanavendorsymfonyhttp-kernelKernel.php:198 {▶}
  G:Documentsworkspacemanapublicindex.php:25 {▶}

将您的存储库更新到此:

namespace AppRepository;
use SymfonyBridgeDoctrineSecurityUserUserLoaderInterface;
use DoctrineORMEntityRepository;
class UserRepository extends EntityRepository implements UserLoaderInterface
{
    public function loadUserByUsername($usernameOrEmail)
    {
        return $this->createQueryBuilder('u')
            ->where('u.username = :query')
            ->orWhere('u.email = :query')
            ->setParameter('query', $usernameOrEmail)
            ->getQuery()
            ->getOneOrNullResult();
    }
}

整个问题是this->entityManager->getRepository(User::class)->loadUserByUsername(...)的参数不应该是数组,而是标量。

也许我可以从创建类似爱丽丝的兔子洞中创造业务。

相关内容

  • 没有找到相关文章

最新更新