0

私は、User エンティティと Admin エンティティの 2 種類のユーザーの認証を処理する単純なアプリに取り組んでいます。そのために 2 つの別個のファイアウォールとプロバイダーを用意したいので、security.yml ファイルは次のようになります。

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

私のルーティング:

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

admin_login_check:
    path:     /admin/login_check

問題は非常に奇妙です。この構成では、/admin/login url でブラウザーを開くと、ログイン フォームが表示され、適切な資格情報を使用してフォームを送信すると、次の例外が発生します。

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 

しかし、私の管理エンティティは次のようになります。

<?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;
}

}
4

2 に答える 2

1

エンティティ My\UserBundle\Entity\Admin のリポジトリは UserProviderInterface を実装する必要があるか、プロバイダーの「admin_provider」構成でユーザー名フィールドを指定する必要があります

security:
    providers:
        main:
            entity:
               class: Acme\UserBundle\Entity\User
               property: username
于 2014-02-06T09:21:16.813 に答える