Hi All,
This is a Question for who are Symfony2 developer.
I am trying to study security feature provided with Symfony2.
I tried to get help from
http://symfony.com/doc/current/cookbook/security/entity_provider.html[
^]
But every time I found Bad Credential. I did not use any encryption algorithm ( i mention encoder plaintext).
Only change in my case is , instead of username field i am using email of user for verification.
Following are my files.
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class SecuredController extends Controller
{
public function loginAction(Request $request)
{
$variable = 0;
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$variable = 1;
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$variable = 2;
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
var_dump($error);
}
var_dump($variable);
return array(
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
);
}
public function securityCheckAction()
{
}
public function logoutAction()
{
}
public function helloAction($name)
{
return array('name' => $name);
}
public function helloadminAction($name)
{
return array('name' => $name);
}
}
My entities are as per below.
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
class Users implements UserInterface{
private $id_user;
private $first_name;
private $last_name;
private $email;
private $password;
private $salt;
private $password_change;
private $phone_no;
private $address;
private $created_at;
private $updated_at;
private $is_active;
private $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
$this->salt = md5(uniqid(null,true));
}
public function getIdUser()
{
return $this->id_user;
}
public function setFirstName($firstName)
{
$this->first_name = $firstName;
return $this;
}
public function getFirstName()
{
return $this->first_name;
}
public function setLastName($lastName)
{
$this->last_name = $lastName;
return $this;
}
public function getLastName()
{
return $this->last_name;
}
public function setEmail($email)
{
$this->email = $email;
return $this;
}
public function getEmail()
{
return $this->email;
}
public function setPassword($password)
{
$this->password = $password;
return $this;
}
public function getPassword()
{
return $this->password;
}
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
public function getSalt()
{
return $this->salt;
}
public function setPasswordChange($passwordChange)
{
$this->password_change = $passwordChange;
return $this;
}
public function getPasswordChange()
{
return $this->password_change;
}
public function setPhoneNo($phoneNo)
{
$this->phone_no = $phoneNo;
return $this;
}
public function getPhoneNo()
{
return $this->phone_no;
}
public function setAddress($address)
{
$this->address = $address;
return $this;
}
public function getAddress()
{
return $this->address;
}
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
public function getCreatedAt()
{
return $this->created_at;
}
public function setUpdatedAt($updatedAt)
{
$this->updated_at = $updatedAt;
return $this;
}
public function getUpdatedAt()
{
return $this->updated_at;
}
public function setActive($active)
{
$this->active = $active;
return $this;
}
public function getActive()
{
return $this->active;
}
public function setIsActive($isActive)
{
$this->is_active = $isActive;
return $this;
}
public function getIsActive()
{
return $this->is_active;
}
public function eraseCredentials() {
}
public function getRoles() {
return $this->roles->toArray();
}
public function getUsername() {
return $this->email;
}
public function isAccountNonExpired() {
return true;
}
public function isAccountNonLocked() {
return true;
}
public function isCredentialsNonExpired() {
return true;
}
public function isEnabled() {
return $this->is_active;
}
public function addRole(\OxindDemo\AdminBundle\Entity\Role $roles)
{
$this->roles->add( $roles );
return $this;
}
public function removeRole(\OxindDemo\AdminBundle\Entity\Role $roles)
{
$this->roles->removeElement($roles);
}
}
Role Entity
<?php
namespace Acme\DemoBundle\Entity;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
class Role implements RoleInterface{
private $id_role;
private $role_name;
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
public function getIdRole()
{
return $this->id_role;
}
public function setRoleName($roleName)
{
$this->role_name = $roleName;
return $this;
}
public function getRoleName()
{
return $this->role_name;
}
public function getRole() {
return $this->role_name;
}
public function addUser(\OxindDemo\AdminBundle\Entity\Users $users)
{
$this->users[] = $users;
return $this;
}
public function removeUser(\OxindDemo\AdminBundle\Entity\Users $users)
{
$this->users->removeElement($users);
}
public function getUsers()
{
return $this->users;
}
}
My Securtiy.yml as below.
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Acme\DemoBundle\Entity\Users: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [
admin: { password: adminpass, roles: [
main:
entity:
class: Acme\DemoBundle\Entity\Users
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/demo/secured/login$
security: false
secured_area:
pattern: ^/demo/secured/
form_login:
check_path: _security_check
login_path: _demo_login
logout:
path: _demo_logout
target: _demo
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control:
- { path: ^/demo/secured/hello/admin/, roles: ROLE_ADMIN }
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
I have created a user with email myuser@mail.com and password: mypass and having Role Entry ROLE_ADMIN and a mapping of myuser with ROLE_ADMIN role in user_role table.
Every time i tried to login with myuser@mail.com/mypass. It says Bad credentials. I can't get why it can't verify my user from database?
Sorry For My Bad English.
I spent almost 2 days google it. tried 8 times to follow different tutorials but can't get my problem solved.
Thanks