0

I'm working on simple app which handles authentication for two kind of users, User and Admin entities. I want to have two separate firewalls and providers for that so my security.yml file looks like this:

security:
    firewalls:
        admin_firewall:
            pattern:   ^/admin
            anonymous: ~
            form_login:
                check_path: admin_login_check
                login_path: admin_login
            logout:
                path: admin_logout
            provider: admin_provider
        main_firewall:
            pattern:   ^/
            anonymous: ~
            form_login:
                check_path: login_check
                login_path: login
            logout:
                path: logout
            provider: main_provider

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

    providers:
        main_provider: 
            entity:
                class: My\UserBundle\Entity\User
                property: taxId
        admin_provider: 
            entity:
                class: My\UserBundle\Entity\Admin

    encoders:
        My\UserBundle\Entity\User: sha512
        My\UserBundle\Entity\Admin: sha512

My routing:

login:
    path:     /logowanie
    defaults:  
        _controller: MyUserBundle:Security:login
logout:
    path:     /wylogowanie
    login_check:
    path:     /login_check

admin_login_check:
    path:     /admin/login_check

The problem is very strange. With this configuration when I open up the browser with /admin/login url it shows me the login form and when I submit the form with proper credentials it gives me following exception:

exception 'Symfony\Component\Security\Core\Exception\AuthenticationServiceException' with message 'The Doctrine repository "My\UserBundle\Entity\AdminRepository" must implement UserProviderInterface.' in /home/karol/www/my/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:94 

but my Admin entity looks like this:

<?php

namespace My\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Admin
 *
 * @ORM\Table(name="my_admin")
 * @ORM\Entity(repositoryClass="My\UserBundle\Entity\AdminRepository")
 * @UniqueEntity("username")
 */
class Admin implements UserInterface, \Serializable {

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @Assert\NotBlank(message="Proszę podać nazwę użytkownika")
 * @ORM\Column(type="string", name="username", length=25, unique=true)
 */
private $username;

/**
 * @ORM\Column(type="string", length=32)
 */
private $salt;

/**
 * @Assert\NotBlank(message="Proszę podać hasło")
 * @ORM\Column(type="string", length=255)
 */
private $password;

/**
 * @Assert\Email(message="Proszę podać adres e-mail")
 * @Assert\NotBlank(message="Proszę podać adres e-mail")
 * @ORM\Column(type="string", length=60)
 */
private $email;

/**
 * @ORM\Column(name="is_active", type="boolean")
 */
private $isActive;

public function __construct() {
    $this->isActive = true;
    $this->salt = md5(uniqid(null, true));
}

/**
 * @inheritDoc
 */
public function getUsername() {
    return $this->username;
}

/**
 * @inheritDoc
 */
public function setUsername($username) {
    $this->username = $username;
}

/**
 * @inheritDoc
 */
public function getSalt() {
    return $this->salt;
}

/**
 * @inheritDoc
 */
public function getPassword() {
    return $this->password;
}

public function setPassword($password) {
    $this->password = $password;
    return $this;
}

/**
 * @inheritDoc
 */
public function getRoles() {
    return array('ROLE_USER');
}

/**
 * @inheritDoc
 */
public function eraseCredentials() {
}

/**
 * @see \Serializable::serialize()
 */
public function serialize() {
    return serialize(array($this->id,));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized) {
    list($this->id, ) = unserialize($serialized);
}

public function getTaxId() {
    return $this->taxId;
}

public function setTaxId($taxId) {
    $this->taxId = $taxId;
    return $this;
}

public function getEmail() {
    return $this->email;
}

public function setEmail($email) {
    $this->email = $email;
    return $this;
}

}
Karol F
  • 1,556
  • 2
  • 18
  • 33

2 Answers2

1

Your repository of entity My\UserBundle\Entity\Admin need to implement UserProviderInterface or you need to specify username field in provider "admin_provider" configuration

security:
    providers:
        main:
            entity:
               class: Acme\UserBundle\Entity\User
               property: username
Anatolie
  • 21
  • 3
-1

Your User entity implements UserInterface instead of UserProviderInterface

EDITED

The exception was:

exception 'Symfony\Component\Security\Core\Exception\AuthenticationServiceException'

with the message

'The Doctrine repository "My\UserBundle\Entity\AdminRepository" must implement UserProviderInterface.' in /home/karol/www/my/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:94

So to fix it, You should in Your My\UserBundle\Entity\Admin just change

class Admin implements UserInterface (...)

to:

class Admin implements UserProviderInterface (...)
user2284570
  • 2,891
  • 3
  • 26
  • 74
wkukielczak
  • 429
  • 4
  • 8
  • Please explain more: This look short for understanding. – user2284570 Nov 24 '13 at 21:58
  • 1
    I think that the problem is just missing `property` in `admin_provider`. And there is no reason to replace `UserInterface` with `UserProviderInterface`, class `Admin` should still implement `UserInterface` because it is a user. Even just adding `UserProviderInterface` to `Admin` class it's a responsibility mixing. – pwc Mar 26 '14 at 12:45