Symfony不使用自定义方法
我在UserRepository中创建了一个方法:loadUserbyIdentifier和loaduserbyusername。但是,这些没有解决。为什么?
实际上,应该出现转储输出。
我是否必须在某个地方明确指定这些方法才能用作登录方法?
当我单击登录时,登录表单仅重新加载。
我的UserRepository:
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use function get_class;
/**
* @extends ServiceEntityRepository<User>
*
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
public function add(User $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(User $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
$user->setPassword($newHashedPassword);
$this->add($user, true);
}
public function findAllUsers($ownUser, $q = null)
{
$qb = $this->createQueryBuilder('user');
$query = $qb
->select('u')
->from('App\Entity\User', 'u')
->where('u.firstname LIKE :user')
->orWhere('u.lastname LIKE :user')
->andWhere($qb->expr()->neq('u.id', $ownUser))
->setParameter('user', $q)
->setParameter('user', '%'. $q . '%')
->orderBy('u.firstname', 'ASC')
->distinct()
->getQuery();
return $query->getArrayResult();
}
public function loadUserByUsername(string $usernameOrEmail)
{
dump($usernameOrEmail);
exit();
return $this->loadUserByIdentifier($usernameOrEmail);
}
public function loadUserByIdentifier(string $usernameOrEmail): ?User
{
if (!filter_var($usernameOrEmail, FILTER_VALIDATE_EMAIL)) {
$name = explode(" ", $usernameOrEmail);
dump($name);
exit();
}
return $this->createQueryBuilder('u')
->where('u.email = :emailOrUsername')
->orWhere('u.username = :emailOrUsername')
->setParameter('emailOrUsername', $usernameOrEmail)
->getQuery()
->getOneOrNullResult();
}
// /**
// * @return User[] Returns an array of User objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('u.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?User
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}
我的Security.yaml:
security:
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
App\Entity\User:
algorithm: auto
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
# used to reload user from session & other features (e.g. switch_user)
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
logout:
path: app_logout
I created a method in the UserRepository: loadUserByIdentifier and loadUserByUsername. However, these are not addressed. Why?
Actually, a dump output should appear.
Do I have to explicitly specify these methods somewhere in order to be used as a login method?
When I click on Login, the login form simply reloads.
My UserRepository:
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use function get_class;
/**
* @extends ServiceEntityRepository<User>
*
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
public function add(User $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(User $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
$user->setPassword($newHashedPassword);
$this->add($user, true);
}
public function findAllUsers($ownUser, $q = null)
{
$qb = $this->createQueryBuilder('user');
$query = $qb
->select('u')
->from('App\Entity\User', 'u')
->where('u.firstname LIKE :user')
->orWhere('u.lastname LIKE :user')
->andWhere($qb->expr()->neq('u.id', $ownUser))
->setParameter('user', $q)
->setParameter('user', '%'. $q . '%')
->orderBy('u.firstname', 'ASC')
->distinct()
->getQuery();
return $query->getArrayResult();
}
public function loadUserByUsername(string $usernameOrEmail)
{
dump($usernameOrEmail);
exit();
return $this->loadUserByIdentifier($usernameOrEmail);
}
public function loadUserByIdentifier(string $usernameOrEmail): ?User
{
if (!filter_var($usernameOrEmail, FILTER_VALIDATE_EMAIL)) {
$name = explode(" ", $usernameOrEmail);
dump($name);
exit();
}
return $this->createQueryBuilder('u')
->where('u.email = :emailOrUsername')
->orWhere('u.username = :emailOrUsername')
->setParameter('emailOrUsername', $usernameOrEmail)
->getQuery()
->getOneOrNullResult();
}
// /**
// * @return User[] Returns an array of User objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('u.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?User
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}
My security.yaml:
security:
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
App\Entity\User:
algorithm: auto
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
# used to reload user from session & other features (e.g. switch_user)
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
logout:
path: app_logout
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好的方法是这样做(根据Symfony doc 在这里):
您必须从security.yaml中的用户提供商中删除属性密钥并将其写入:
并将此功能适应与文档:
另外,您必须删除
enable_authenticator_manager:true
。edit(要回答您的问题)
有关您的问题,以下是“主要”防火墙配置:
The better way is to do that (according to the Symfony doc here) :
You have to remove the property key from the user provider in security.yaml and write it like that :
And adapt this function to coresspond with the doc :
Also you have to remove
enable_authenticator_manager: true
.EDIT (to answer your question)
Regarding your question, here is the minimum `main' firewall configuration: