I think I tried all the code in all authentication messages using the (doctrine) Entity in Symfony 2. And I can't get it to work. When I use the in_memory provider, everything works fine. I am using PR8.
My security.yml
security: encoders: Partners\FrontendBundle\Entity\User: plaintext Symfony\Component\Security\Core\User\User: plaintext providers: main: entity: { class: FrontendBundle:User, property: username }
My custom object
<?php namespace Partners\FrontendBundle\Entity; use Symfony\Component\Security\Core\User\UserInterface; use Partners\FrontendBundle\Repository\UserRepository; class User implements UserInterface { protected $id; protected $username; protected $email; protected $password; protected $organization; protected $contact; protected $phone; protected $cid; protected $status; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getUsername() { return $this->username; } public function setUsername($username) { $this->username = $username; } public function getPassword() { return $this->password; } public function setPassword($password) { $this->password = $password; } public function setEmail($email) { $this->email = $email; } public function getEmail() { return $this->email; } public function setPhone($phone) { $this->phone = $phone; } public function getPhone() { return $this->phone; } public function setContact($contact) { $this->contact = $contact; } public function getContact() { return $this->contact; } public function setOrganization($org) { $this->organization = $org; } public function getOrganization() { return $this->organization; } public function setCid($cid) { $this->cid = $cid; } public function getCid() { return $this->cid; } public function setStatus($status) { $this->status = $status; } public function getStatus() { return $this->status; } public function __toString() { return $this->getUsername(); } public function getRoles() { return array('ROLE_USER'); } public function eraseCredentials() { return false; } public function getSalt() { return $this->getId(); } public function equals(UserInterface $account) { if ($account->getUsername() != $this->getUsername) { return false; } if ($account->getEmail() != $this->getEmail) { return false; } return true; } }
UserRepository
<?php namespace Partners\FrontendBundle\Repository; use Doctrine\ORM\EntityRepository; use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\User\UserInterface; class UserRepository extends EntityRepository implements UserProviderInterface { const CREATED = 0; const ACTIVE = 10; const INACTIVE = 20; public function loadUserByUsername($username) { return $this->findOneBy(array('username' => $username)); } function loadUser(UserInterface $user) { return $user; } function loadUserByAccount(AccountInterface $account) { return $this->loadUserByUsername($account->getUsername()); } public function supportsClass($class) { return true; } }
When I fill out the registration form, you get an error in $ error = $ this-> get ('request') → getSession () → get (SecurityContext :: AUTHENTICATION_ERROR); is an:
exception 'Symfony\Component\Security\Core\Exception \BadCredentialsException' with message 'Bad credentials' in /var/www/ inspiring/trunk/Symfony/vendor/symfony/src/Symfony/Component/Security/ Core/Authentication/Provider/DaoAuthenticationProvider.php:66 Stack trace: #0 /var/www/inspiring/trunk/Symfony/vendor/symfony/src/Symfony/ Component/HttpFoundation/SessionStorage/NativeSessionStorage.php(81): session_start() #1 /var/www/inspiring/trunk/Symfony/app/cache/dev/ classes-53824.php(284): Symfony\Component\HttpFoundation\SessionStorage \NativeSessionStorage->start()
I do not know where the problem is.
Sergi
source share