I've been trying to create a login form today using Symfony2 where a user can login using their email address and password. I've had lot's of problems and in the end realised it will only work if I have a $username
property in my AdminUser
entity class. I've tried to use email instead of username where possible so can someone please explain why $username
is required or where I have gone wrong? Also, in my login.html.twig
file I am still using _username
rather than _email
if that makes any difference? My code is below (I've removed some getters and setters which aren't applicable):
AdminUser Entity:
namespace XXX\WebsiteBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* AdminUser
*
* @ORM\Table(name="admin_user",indexes={@ORM\Index(name="indexes", columns={"deleted"})})
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class AdminUser implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=45)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=45, unique=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="salt", type="string", length=255)
*/
private $salt;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @var integer
*
* @ORM\Column(name="enabled", type="integer", options={"default" = 0})
*/
private $enabled;
/**
* @var string[] $roles
*
* @ORM\Column(name="roles", type="array")
*/
private $roles = array();
private $username;
/**
* Gets the username.
*
* @return string The username.
*/
public function getUsername()
{
return $this->email;
}
/**
* Erases the user credentials.
*/
public function eraseCredentials()
{
}
/**
* Returns the roles granted to the user.
*
* <code>
* public function getRoles()
* {
* return array('ROLE_USER');
* }
* </code>
*
* Alternatively, the roles might be stored on a ``roles`` property,
* and populated in any number of different ways when the user object
* is created.
*
* @return Role[] The user roles
*/
public function getRoles() {
return $this -> roles;
}
/**
* Set the roles of the user
*
* @var string[] $roles
*
* @return \XXX\WebsiteBundle\Entity\User this
*/
public function setRoles(array $roles) {
$this -> roles = $roles;
return $this;
}
}
My security.yml
file is:
jms_security_extra: secure_all_services: false expressions: true
security:
encoders:
XXX\WebsiteBundle\Entity\AdminUser: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
main_provider:
entity: { class: XXX\WebsiteBundle\Entity\AdminUser, property: email }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
admin_firewall:
pattern: ^/admin.*
anonymous: ~
form_login:
login_path: /admin/login
check_path: /admin/login_check
access_control:
- { path: ^/admin/login.*, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin.*, roles: ROLE_ADMIN }
I guess you should change property with email and also you can change the _username parameter with anything you want by:
entity:
entity:
class: SecurityBundle:User
property: username
and
firewalls
form_login:
username_parameter: _username
may be you should have a look at security configuration document
firewalls:
main:
form_login:
username_parameter: _email
All of your code is correct you only need to make a little change in the security.yml file. just add the username_parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With