Hola, estoy desde hace un par de días intentado crear un login simple, he probado el FOSUserBundle pero me fué todavía peor ya que me daban mucho errores.
He conseguido que acceda con los usuarios en memoria "admin" pero con los usuarios de la base de datos me dice siempre bad credentials, y lo estoy metiendo correctamente. No sé que ocurre, os dejo los fuentes:
Clase Users:
Código PHP:
<?php
namespace cosmonautasPortadaBundleEntity;
use SymfonyComponentSecurityCoreUserUserInterface;
use DoctrineORMMapping as ORM;
/**
* Users
*/
class Users implements UserInterface
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $login;
/**
* @var string
*/
private $password;
/**
* @var \DateTime
*/
private $createdat;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $lastname;
/**
* Set id
*
* @param integer $id
* @return Users
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set login
*
* @param string $login
* @return Users
*/
public function setLogin($login)
{
$this->login = $login;
return $this;
}
/**
* Get login
*
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* Set password
*
* @param string $password
* @return Users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Get createdat
*
* @return \DateTime
*/
public function getCreatedat()
{
return $this->createdat;
}
/**
* Set name
*
* @param string $name
* @return Users
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set lastname
*
* @param string $lastname
* @return Users
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* @var string
*/
private $salt;
/**
* @var boolean
*/
private $isactived;
/**
* @var \cosmonautas\PortadaBundle\Entity\Roles
*/
private $roles;
/**
* Set salt
*
* @param string $salt
* @return Users
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set isactived
*
* @param boolean $isactived
* @return Users
*/
public function setIsactived($isactived)
{
$this->isactived = $isactived;
return $this;
}
/**
* Get isactived
*
* @return boolean
*/
public function getIsactived()
{
return $this->isactived;
}
/**
* Set roles
*
* @param \cosmonautas\PortadaBundle\Entity\Roles $roles
* @return Users
*/
public function setRoles(cosmonautasPortadaBundleEntityRoles $roles = null)
{
$this->roles = $roles;
return $this;
}
/**
* Get roles
*
* @return \cosmonautas\PortadaBundle\Entity\Roles
*/
public function getRoles()
{
return $this->roles;
}
public function setUsername($username)
{
$this->login = $username;
}
public function getUsername()
{
return $this->login;
}
public function eraseCredentials()
{
}
public function equals(UserInterface $user)
{
return $user->getUsername() == $this->getUsername();
}
public function __construct()
{
$this->isActive = true;
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
}
}
Clase Roles:
Código PHP:
<?php
namespace cosmonautasPortadaBundleEntity;
use SymfonyComponentSecurityCoreRoleRoleInterface;
use DoctrineORMMapping as ORM;
/**
* Roles
*/
class Roles implements RoleInterface
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $rol;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set rol
*
* @param string $rol
* @return Roles
*/
public function setRol($rol)
{
$this->rol = $rol;
return $this;
}
/**
* Get rol
*
* @return string
*/
public function getRol()
{
return $this->rol;
}
public function __toString()
{
return $this->getRol();
}
public function getRole()
{
return $this->rol;
}
}
Security.yml:
Código PHP:
jms_security_extra:
secure_all_services: false
expressions: true
security:
encoders:
SymfonyComponentSecurityCoreUserUser: plaintext
cosmonautasPortadaBundleEntityUsers:
algorithm: sha1
iterations: 1
encode_as_base64: false
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
chain_provider:
chain:
providers: [in_memory, user_db]
in_memory:
memory:
users:
admin: { password: velazquez118, roles: [ 'ROLE_ADMIN' ] }
user_db:
entity: { class: cosmonautasPortadaBundleEntityUsers, property: login }
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
access_control:
#- { path: ^/login, roles: ROLE_USER }
#
# You must uncomment the two following rules to restrict access to paths
# starting with the /_internal prefix to only localhost
#- { path: ^/_internal/secure, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }
#- { path: ^/_internal/secure, roles: ROLE_NO_ACCESS }
El formulario es el típico, no merece la pena que lo ponga.
Muchas gracias de antemano.