-crear la entidad User que implemente la interfaz UserInterface
-en config/packages/security.yaml
security:
encoders:
App\Entity\User:
algorithm: bcrypt
cost: 12
providers:
users:
entity:
class: App:User
property: email #esto es para que el logueo se haga por el email
firewalls:
main:
anonymous: ~
form_login:
username_parameter: _email #esto es para que el logueo se haga por el email
login_path: login
check_path: login
provider:
users
logout:
path: /logout
target: /
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/*, roles: ROLE_ADMIN }
en este caso estamos dando acceso al sitio sólo a los usuarios que tengan el rol ROLE_ADMIN
salvo para el caso de login
-en config/routes.yaml
crear la ruta para logout
logout:
path: /logout
-en config/packages/framework.yaml
habilitar la session
framework:
secret: '%env(APP_SECRET)%'
session:
# The native PHP session handler will be used
handler_id: ~
-crear el controlador que manejará el login
App\Controller\SecurityController
/**
* @Route("/login", name="login")
* @param Request $request
* @param AuthenticationUtils $authUtils
* @return Response
*/
public function loginAction(Request $request, AuthenticationUtils $authUtils)
{
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
-crear la vista template/security/login.html.twig
{% extends '@EasyAdmin/default/layout.html.twig' %}
{% block header_custom_menu %}
{% endblock header_custom_menu %}
{% block sidebar %}
<section class="sidebar">
</section>
{% endblock sidebar %}
{% block content %}
<div class="container">
<div class="row">
{% if error %}
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><i class="zmdi-close"></i></button>
{{ error.messageKey|trans(error.messageData, 'security') }}
</div>
{% endif %}
<div class="col-xs-12">
<h1>Login</h1>
<form action="{{ path('login') }}" method="post">
<div class="form-group">
<!-- label for="username" class="control-label required">Username:</label-->
<!-- input type="text" id="username" name="_username" value="{{ last_username }}" /-->
<label for="email" class="control-label required">Email:</label>
<input type="text" id="email" name="_email" value="{{ last_username }}" />
</div>
<div class="form-group">
<label for="password" class="control-label required">Password:</label>
<input type="password" id="password" name="_password" />
</div>
<button type="submit" class="btn btn-primary">login</button>
</form>
</div>
</div>
</div>
{% endblock %}
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('', 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_ADMIN');
}
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);
}
public function getId() {
return $this->id;
}
public function setUsername($username) {
$this->username = $username;
return $this;
}
public function setPassword($password) {
$this->password = $password;
return $this;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
return $this;
}
public function getIsActive() {
return $this->isActive;
}
public function setIsActive($isActive) {
$this->isActive = $isActive;
return $this;
}
}
voy a insertar un usuario inicial,
pero como necesito saber cual es el password codificado, utilizo el siguiente comando:
php bin/console security:encode-password
e ingreso el password que le quiero asignar.
por ejemplo para "admin"
me devuelve el siguiente password "$2y$12$c6FDDMJQl/AfflUe4.U74OPb1SLKUyJ.eGoRDiR8f6eVCNcTJOk4a"
entonces en la migration agrego la siguiente linea de insercion:
insert into user(username,email,password) values("israel bazan","israelbazan76@gmail.com",$2y$12$c6FDDMJQl/AfflUe4.U74OPb1SLKUyJ.eGoRDiR8f6eVCNcTJOk4a);