Autenticación¶
Referencia: http://symfony.com/doc/current/cookbook/security/entity_provider.html
Creo el archivo
src/AppBundle/Entity/User.php
con el codigo siguiente
<?php // src/AppBundle/Entity/User.php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\UserInterface; /** * @ORM\Table(name="app_users") * @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository") */ class User implements UserInterface, \Serializable { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(type="string", length=25, unique=true) */ private $username; /** * @ORM\Column(type="string", length=64) */ private $password; /** * @ORM\Column(type="string", length=60, unique=true) */ private $email; /** * @ORM\Column(name="is_active", type="boolean") */ private $isActive; public function __construct() { $this->isActive = true; // may not be needed, see section on salt below // $this->salt = md5(uniqid(null, true)); } public function getUsername() { return $this->username; } public function getSalt() { // you *may* need a real salt depending on your encoder // see section on salt below return null; } public function getPassword() { return $this->password; } public function getRoles() { return array('ROLE_USER'); } public function eraseCredentials() { } /** @see \Serializable::serialize() */ public function serialize() { return serialize(array( $this->id, $this->username, $this->password, // see section on salt below // $this->salt, )); } /** @see \Serializable::unserialize() */ public function unserialize($serialized) { list ( $this->id, $this->username, $this->password, // see section on salt below // $this->salt ) = unserialize($serialized); } }
Luego ejecuto la sentencia
php app/console doctrine:generate:entities AppBundle/Entity/User
Traslado el a la base de datos
php app/console doctrine:schema:update --force
INSERT INTO `jobeet`.`app_users` (`id`, `username`, `password`, `email`, `is_active`) VALUES (NULL, 'admin', MD5, 'ingguillermoz@gmail.com', '1');
php app/console security:encode-password admin 'AppBundle\Entity\User' Symfony Password Encoder Utility ================================ ------------------ --------------------------------------------------------------- Key Value ------------------ --------------------------------------------------------------- Encoder used Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder Encoded password $2y$13$uXR5DGA7EMAg3QLaeaIYP.3a4dVdOvgDLCEqnNQwfUgz72uBMB1kW ------------------ --------------------------------------------------------------- Se tomo el valor generado y se almaceno en la password del usuario y la tomo $2y$13$uXR5DGA7EMAg3QLaeaIYP.3a4dVdOvgDLCEqnNQwfUgz72uBMB1kW -> este codigo lo llevo a la base de datos ! [NOTE] Bcrypt encoder used: the encoder generated its own built-in salt. [OK] Password encoding succeeded
UPDATE `jobeet`.`app_users` SET `password` = '$2y$13$uXR5DGA7EMAg3QLaeaIYP.3a4dVdOvgDLCEqnNQwfUgz72uBMB1kW' WHERE `app_users`.`id` =1;
Para excluir usuarios inactivos se modifica y agrega metodos
// src/AppBundle/Entity/User.php use Symfony\Component\Security\Core\User\AdvancedUserInterface; // ... class User implements AdvancedUserInterface, \Serializable { // ... 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); } }
Tambien se crean 3 metodos
// src/AppBundle/Entity/UserRepository.php namespace AppBundle\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; class UserRepository extends EntityRepository implements UserProviderInterface { 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 = sprintf( 'Unable to find an active admin AppBundle:User object identified by "%s".', $username ); throw new UsernameNotFoundException($message); } 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()); } }
Como paso final se retira de security.yml
property: username