Autenticación
Versión 1 (Guillermo Zdanowicz, 05/10/2015 17:19) → Versión 2/4 (Guillermo Zdanowicz, 05/10/2015 18:03)
h1. 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
<pre>
<?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);
}
}
</pre>
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('admin'), 'ingguillermoz@gmail.com', '1');
<pre>
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
! [NOTE] Bcrypt encoder used: the encoder generated its own built-in salt.
[OK] Password encoding succeeded
</pre>
Referencia: http://symfony.com/doc/current/cookbook/security/entity_provider.html
Creo el archivo
src/AppBundle/Entity/User.php
con el codigo siguiente
<pre>
<?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);
}
}
</pre>
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('admin'), 'ingguillermoz@gmail.com', '1');
<pre>
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
! [NOTE] Bcrypt encoder used: the encoder generated its own built-in salt.
[OK] Password encoding succeeded
</pre>