我有一个表单,我想要记录用户 - 我已经将数据库加载到数据库中但是当我尝试了以下教程时,我会不断获得错误的凭据或无效的凭据我会包含所有相关文件,如果有人可以指出的话任何我失去身份的东西都会永远存在你的债务中!
<?php
namespace AppBundle\Forms;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
class LoginType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add(
'username',
TextType::class, [
'constraints' => [
new NotBlank()
]
]
)
->add(
'password',
PasswordType::class, [
'constraints' => [
new NotBlank()
]
]
);
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'data_class' => 'AppBundle\Entity\User'
]);
}
}
和我的控制器 -
<?php
namespace AppBundle\Controller\Login;
use AppBundle\Entity\User;
use AppBundle\Forms\LoginType;
use AppBundle\Repository\UserRepository;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginpageController {
protected $formFactory;
protected $templating;
protected $router;
protected $authenticationUtils;
protected $encoder;
protected $userRepository;
public function __construct (
FormFactoryInterface $formFactory,
EngineInterface $templating,
RouterInterface $router,
AuthenticationUtils $authenticationUtils,
UserPasswordEncoder $encoder,
UserRepository $userRepository
) {
$this->formFactory = $formFactory;
$this->templating = $templating;
$this->router = $router;
$this->authenticationUtils = $authenticationUtils;
$this->encoder = $encoder;
$this->userRepository = $userRepository;
}
public function __invoke(Request $request) {
$user = new User();
$form = $this->formFactory->create(LoginType::class, $user);
$form->handleRequest($request);
if($form->isValid()) {
return new RedirectResponse (
$this->router->generate('home'),
Response::HTTP_TEMPORARY_REDIRECT
);
}
// get the login error if there is one
$error = $this->authenticationUtils->getLastAuthenticationError();
return new Response(
$this->templating->render(
'AppBundle:loginpage:loginpage.html.twig', [
'form' => $form->createView(),
'error' => $error
]
)
);
}
}
我的表格 -
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form method="POST" action="{{ path('login') }}">
{{ form_start(form) }}
{{ form_row(form.username) }}
<br>
{{ form_row(form.password) }}
<br>
<br>
<input type="submit" value="submit"/>
{{ form_end(form) }}
</form>
我的安全 -
security:
providers:
user_database:
entity:
class: AppBundle:User
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
cost: 11
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: ~
provider: user_database
form_login:
login_path: /login
check_path: /login
logout:
path: /logout
target: /
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
回购 -
<?php
namespace AppBundle\Repository;
use AppBundle\Entity\User;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
class UserRepository extends EntityRepository implements UserLoaderInterface {
public function save(User $user) {
$this->_em->persist($user);
$this->_em->flush();
}
public function delete(User $user) {
$this->_em->remove($user);
$this->_em->flush();
}
public function loadUserByUsername($username) {
$user = $this->createQueryBuilder('u')
->where('u.username = :username or u.email = :email')
->setParameter('username', $username)
->setParameter('email', $username)
->getQuery()
->getOneOrNullResult();
if (null === $user) {
$message = print_r(
'Unable to find an active admin AppBundle:User object identified by "%s".',
$username
);
throw new UsernameNotFoundException($message);
}
return $user;
}
}
ORM映射 -
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity repository-class="AppBundle\Repository\UserRepository" name="AppBundle\Entity\User" table="user">
<id name="id" type="guid" column="id">
<generator strategy="UUID"/>
</id>
<field name="firstName" type="string" column="first_name" length="255"/>
<field name="lastName" type="string" column="last_name" length="255"/>
<field name="email" type="string" column="email" length="255" unique="true"/>
<field name="username" type="string" column="username" length="255" unique="true"/>
<field name="password" type="string" column="password" length="4096"/>
<field name="roles" type="string" column="roles" length="255"/>
<field name="createdAt" type="datetime" column="createdAt"/>
<field name="isActive" type="boolean" column="isActive"/>
</entity>
</doctrine-mapping>
答案 0 :(得分:0)
我发现了我的问题!
在我的Fixture数据中,我正在调用另一个变量来编码,因为我的密码在这里是差异
public function load(ObjectManager $manager)
{
foreach ($this->users as $user) {
$userAdmin = new User();
$userAdmin->setName($user['name']);
$userAdmin->setEmail($user['email']);
$userAdmin->setPassword($user['password']);
$encoder = $this->container->get('security.password_encoder');
$password = $encoder->encodePassword($userAdmin, 'secret.password');
$userAdmin->setPassword($password);
$userAdmin->setRoles($user['roles']);
$userAdmin->setUsername($user['username']);
$userAdmin->setIsActive($user['isActive']);
$userAdmin->setCreatedAt(new \DateTime('now'));
$manager->persist($userAdmin);
}
$manager->
public function load(ObjectManager $manager)
{
foreach ($this->users as $user) {
$userAdmin = new User();
$userAdmin->setName($user['name']);
$userAdmin->setEmail($user['email']);
$userAdmin->setPassword($user['password']);
$encoder = $this->container->get('security.password_encoder');
$password = $encoder->encodePassword($userAdmin, $userAdmin->getPassword());
$userAdmin->setPassword($password);
$userAdmin->setRoles($user['roles']);
$userAdmin->setUsername($user['username']);
$userAdmin->setIsActive($user['isActive']);
$userAdmin->setCreatedAt(new \DateTime('now'));
$manager->persist($userAdmin);
}
$manager->flush();
}
答案 1 :(得分:0)
我也被这个错误信息所困扰。
我的原因是我的用户表中有重复的用户名(因此,在用户名上选择时,不会有多个用户返回)。我不知道为什么我忘了这个...
因此,请确保您的用户名列/字段具有唯一索引。很明显,但我希望它节省了一些人的时间。
在您的实体之上:
/**
* @ORM\Entity()
* @UniqueEntity("username")
*/
在实体类的正文中:
/**
* @ORM\Column(unique=true, type="string", nullable=false)
*/
private $username;