2 pièce(s) jointe(s)
Authentification sous symfony2.8
Bonjour à tous, j'ai un problème pour authentifier mes utilisateurs avec une base de donnée, lorsque je valide mon formulaire, malgré la présence de l'utilisateur dans la base de donnée il n'est pas authentifier, je reste anonyme...
Pouvez vous trouver pourquoi je n'arrive pas à m'authentifier :/
Mes fichiers :
Pièce jointe 209565
security.yml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| # To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
encoders:
AppBundle\Entity\utilisateur:
algorithm: bcrypt
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
our_db_provider:
entity:
class: AppBundle:utilisateur
#in_memory:
#memory: ~
access_control:
- { path: ^/login_check, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_firewall:
pattern: ^/login$
anonymous: ~
login_register:
pattern: ^/register$
anonymous: ~
login_cheker:
pattern: ^/login_check$
anonymous: ~
main:
#pattern: ^/
#http_basic: ~
provider: our_db_provider
form_login:
login_path: login
check_path: _security_check
secured_area:
# ...
pattern: ^/
form_login: ~
#form_register: ~
logout:
path: /logout
target: / |
SecurityController :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?php
// src/AppBundle/Controller/SecurityController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
var_dump($authenticationUtils);
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'security/login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
/**
* @Route("/login_check", name="_security_check")
*/
public function securityCheckAction() {
// The security layer will intercept this request
}
/**
* @Route("/logout", name="_logout")
*/
public function logoutAction() {
// The security layer will intercept this request
}
} |
utilisateur.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
| <?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* utilisateur
*
* @ORM\Table(name="utilisateur")
* @ORM\Entity(repositoryClass="AppBundle\Repository\utilisateurRepository")
*/
class utilisateur implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="prenom", type="string", length=255)
*/
private $prenom;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="mail", type="string", length=255, unique=true)
*/
private $mail;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=64)
*/
private $password;
/**
* @Assert\NotBlank()
* @Assert\Length(max=4096)
*/
private $plainPassword;
/**
* @var string
*
* @ORM\Column(name="telephone", type="string", length=14)
*/
private $telephone;
/**
* @var string
*
* @ORM\Column(name="poste", type="string", length=100)
*/
private $poste;
/**
* @var array
*
* @ORM\Column(name="roles", type="array")
*/
private $roles = array();
/**
* @var boolean
*
* @ORM\Column(name="isActive", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
$this->roles = array('ROLE_USER');
// may not be needed, see section on salt below
// $this->salt = md5(uniqid(null, true));
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
* @return utilisateur
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set prenom
*
* @param string $prenom
* @return utilisateur
*/
public function setPrenom($prenom)
{
$this->prenom = $prenom;
return $this;
}
/**
* Get prenom
*
* @return string
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* Set mail
*
* @param string $mail
* @return utilisateur
*/
public function setMail($mail)
{
$this->mail = $mail;
return $this;
}
/**
* Get mail
*
* @return string
*/
public function getMail()
{
return $this->mail;
}
/**
* Set password
*
* @param string $password
* @return utilisateur
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set telephone
*
* @param string $telephone
* @return utilisateur
*/
public function setTelephone($telephone)
{
$this->telephone = $telephone;
return $this;
}
/**
* Get telephone
*
* @return string
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* Set poste
*
* @param string $poste
* @return utilisateur
*/
public function setPoste($poste)
{
$this->poste = $poste;
return $this;
}
/**
* Get poste
*
* @return string
*/
public function getPoste()
{
return $this->poste;
}
/**
* Set roles
*
* @param array $roles
* @return utilisateur
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* Get roles
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return null;
}
/**
* Set isActive
*
* @param boolean $isActive
* @return utilisateur
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
public function eraseCredentials()
{
}
/**
* Set username
*
* @param string $username
* @return utilisateur
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
// serialize and unserialize must be updated - see below
public function serialize()
{
return serialize(array(
// ...
$this->isActive
));
}
public function unserialize($serialized)
{
list (
// ...
$this->isActive
) = unserialize($serialized);
}
public function getPlainPassword()
{
return $this->plainPassword;
}
public function setPlainPassword($password)
{
$this->plainPassword = $password;
}
} |
utilisateurRepository.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* utilisateurRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class utilisateurRepository extends EntityRepository implements UserLoaderInterface
{
public function loadUserByUsername($username)
{
return $this->createQueryBuilder('u')
->where('u.username = :username OR u.mail = :mail')
->setParameter('username', $username)
->setParameter('mail', $username)
->getQuery()
->getOneOrNullResult();
}
} |
ma base de donnée l'utilisateur est créé par un formulaire (qui lui fonctionne) :
Pièce jointe 209570
le formulaire d'enregistrement d'utilisateur :
registratiionController :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?php
namespace AppBundle\Controller;
use AppBundle\Form\UtilisateurType;
use AppBundle\Entity\utilisateur;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class RegistrationController extends Controller
{
/**
* @Route("/register", name="user_registration")
*/
public function registerAction(Request $request)
{
// 1) build the form
$user = new utilisateur();
$form = $this->createForm(UtilisateurType::class, $user);
// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// 3) Encode the password (you could also do this via Doctrine listener)
$password = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
// 4) save the User!
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
// ... do any other work - like sending them an email, etc
// maybe set a "flash" success message for the user
$this->addFlash(
'valide',
'Utilisateur ajouté !'
);
return $this->redirectToRoute('user_registration');
}
return $this->render(
'registration/register.html.twig',
array('form' => $form->createView())
);
}
} |
utilisateurType :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class UtilisateurType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom', TextType::class)
->add('prenom', TextType::class)
->add('mail', EmailType::class)
->add('username', TextType::class)
->add('telephone', TextType::class)
->add('poste', TextType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
)
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\utilisateur',
));
}
} |
register.html.twig :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| {% extends 'base.html.twig' %}
{% block body %}
{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.mail) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}
<button type="submit">Register!</button>
{{ form_end(form) }}
{% endblock %} |
Merci de votre aide, :calim2::calim2:
Cordialement
Choco7
Petite info suplémentaire
En commentant toutes les lignes correspondant à mon formulaire perso de login et en activant http basic, j'arrive à me connecter :( Au moins le problème ne vient pas de ma base de donnée...
Bonjour et merci de ton aide :)
Bonjour :)
D'abord merci pour ta réponse, effectivement je n'arrive pas à m'authentifier après l'inscription, pour ce qui est du:
Code:
$authenticationUtils = $this->get('security.authentication_utils');
J'ai utiliser ce qui se trouve dans le cookbook de Symfony :
http://symfony.com/doc/2.8/cookbook/...gin_setup.html
Et je ne sais pas à quoi ça correspond :?
J'ai probablement du louper quelque chose ^^
Parce que je pense avoir tout suivi et pourtant ca ne marche toujours pas.... :calim2::calim2:
Merci encore :)