At the moment I’m completely lost, two days when I try to understand why I always get the “Bad Accounting” answer in my login form.
I used a tutorial on loading from a database .
Is there a way to find out what it compares with the "bad credentials" error?
Reset Error:
exception 'Symfony\Component\Security\Core\Exception\BadCredentialsException' with message 'Bad credentials' in D:\dev\workspace\esig_grandprojet\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php:89 Stack trace:
Here are my files:
my security.yml:
security:
encoders:
ESIG\BBC\ManagerCP2Bundle\Entity\Utilisateur:
algorithm: sha512
encode_as_base64: true
iterations: 5000
providers:
users:
entity: { class: ESIGBBCManagerCP2Bundle:Utilisateur }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/login$
security: false
main:
pattern: /.*
form_login:
login_path: _security_login
use_forward: false
check_path: _security_check
post_only: true
always_use_default_target_path: true
default_target_path: /
target_path_parameter: _target_path
use_referer: false
failure_path: null
failure_forward: false
username_parameter: _username
password_parameter: _password
csrf_parameter: _csrf_token
intention: authenticate
logout: true
security: true
anonymous: true
remember_me:
key: "%secret%"
lifetime: 3600
path: /
domain: ~ #Default to the current domain from $_SERVER
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /.*, roles: IS_AUTHENTICATED_REMEMBERED }
my custom object:
<?php
namespace ESIG\BBC\ManagerCP2Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
class Utilisateur implements AdvancedUserInterface, \Serializable
{
private $id;
private $username;
private $salt;
private $password;
private $email;
private $isActive;
private $motDePasseExpire;
private $nom;
private $prenom;
private $adresse;
private $telephone;
private $dateDerniereConnexion;
private $groupesAutorisation;
private $notifications;
public function __construct() {
$this->isActive = True;
$this->salt = md5(uniqid(null, true));
$this->groupesAutorisation = new \Doctrine\Common\Collections\ArrayCollection();
$this->notifications = new \Doctrine\Common\Collections\ArrayCollection();
$this->motDePasseExpire = False;
}
public function getUsername() {
return $this->username;
}
public function getSalt() {
return null;
}
public function getPassword() {
$this->password;
}
public function getRoles() {
return array('ROLE_USER');
}
public function equals(UserInterface $user)
{
return md5($this->getUsername()) == md5($user->getUsername());
}
public function eraseCredentials() {
}
public function serialize()
{
return serialize(array(
$this->id,
));
}
public function unserialize($serialized)
{
list (
$this->id,
) = unserialize($serialized);
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
public function getId()
{
return $this->id;
}
public function setUsername($nomUtilisateur)
{
$this->username = $nomUtilisateur;
return $this;
}
public function setPassword($motDePasse)
{
$this->password = $motDePasse;
return $this;
}
public function setEmail($email)
{
$this->email = $email;
return $this;
}
public function getEmail()
{
return $this->email;
}
public function getGroupes()
{
return $this->groupes;
}
public function addNotification(\ESIG\BBC\ManagerCP2Bundle\Entity\Notification $notifications)
{
$this->notifications[] = $notifications;
return $this;
}
public function addGroupesAutorisation(\ESIG\BBC\ManagerCP2Bundle\Entity\GroupeAutorisation $groupesAutorisation)
{
$this->groupesAutorisation[] = $groupesAutorisation;
return $this;
}
public function removeGroupesAutorisation(\ESIG\BBC\ManagerCP2Bundle\Entity\GroupeAutorisation $groupesAutorisation)
{
$this->groupesAutorisation->removeElement($groupesAutorisation);
}
public function getGroupesAutorisation()
{
return $this->groupesAutorisation;
}
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
public function isActive()
{
return $this->isActive;
}
}
(GetSalt returns "null" to make sure it is not involved in my problem)
my user repository:
<?php
namespace ESIG\BBC\ManagerCP2Bundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use ESIG\BBC\ManagerCP2Bundle\Entity\Notification;
class UtilisateurRepository extends EntityRepository implements UserProviderInterface
{
public function loadUserByUsername($username)
{
$q = $this
->createQueryBuilder('u')
->where('u.username = :username OR u.email = :email')
->setParameter('username', $username)
->setParameter('email', $username)
->getQuery();
try {
$user = $q->getSingleResult();
} catch (NoResultException $e) {
throw new UsernameNotFoundException(sprintf('Unable to find an active admin AcmeUserBundle:User object identified by "%s".', $username), 0, $e);
}
return $user;
}
public function refreshUser(UserInterface $user)
{
$class = get_class($user);
if (!$this->supportsClass($class)) {
throw new UnsupportedUserException(
sprintf(
'Instances of "%s" are not supported.',
$class
)
);
}
return $this->find($user->getId());
}
public function supportsClass($class)
{
return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
}
}
my SecurityController:
<?php
namespace ESIG\BBC\ManagerCP2Bundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
public function GenerateMenuAction()
{
return array();
}
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $session->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return array(
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error
);
}
public function securityCheckAction()
{
}
public function logoutAction()
{
return array();
}
}
my login form template:
{% extends "ESIGBBCManagerCP2Bundle::ManagerCP2.html.twig" %}
{% block right %}
{{ error }}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
{{ test }}
<form action="{{ path('_security_check') }}" method="post">
<label for="username">Login :</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" title="{{ "login.mail.title" | trans }}" /><br />
<label for="password" >Mot de passe :</label>
<input type="password" id="password" name="_password" title="{{ "login.password.title" | trans }}" /><br />
<input type="checkbox" id="remember_me" name="_remember_me" title="{{ "login.rememberme.title" | trans }}" checked /> <label for="remember_me" >Se souvenir de moi</label><br />
{
Si vous voulez contrôler l'URL vers laquelle l'utilisateur est redirigé en cas de succès
(plus de détails ci-dessous)
<input type="hidden" name="_target_path" value="/account" />
<button type="submit" name="login" title="{{ "login.submit.title" | trans }}">{{ "login.submit.value" | trans }}</button>
</form>
{% endblock %}
and finally: my fixture
<?php
namespace ESIG\BBC\ManagerCP2Bundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use ESIG\BBC\ManagerCP2Bundle\Entity\Pompier;
use ESIG\BBC\ManagerCP2Bundle\Entity\Utilisateur;
use \Symfony\Component\Security\Core\Encoder\Pbkdf2PasswordEncoder;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
class LoadUserData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$pwd_encoder = new MessageDigestPasswordEncoder('sha512', true, 5000);
$superAdmin = new Utilisateur();
$superAdmin->setNom("Admin")
->setPrenom("Super")
->setUsername("super.admin")
->setEmail("super@admin.foo")
->setPassword($pwd_encoder->encodePassword("12345678", $superAdmin->getSalt()))
->setAdresse("Chemin de l'administration")
->setTelephone("0041000000000");
$manager->persist($superAdmin);
$manager->flush();
}
}
Please save me T_T