私はFOSUserBundleからインスピレーションを得た独自のプロバイダを作成しました。
は基本的に私は、プロバイダのサービスを作り、このようにそれを設定します。
providers:
admins:
id: user_provider.admins
users:
id: user_provider.users
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
admin_secured_area:
host: "%admin_host%"
pattern: ^/*
form_login:
provider: admins
login_path: /login
check_path: /login_check
csrf_token_generator: security.csrf.token_manager
logout:
path: /logout
target:/
anonymous: true
users_secured_area:
host: "%users_host%"
pattern: ^/*
form_login:
provider: users
login_path: /login
check_path: /login_check
csrf_token_generator: security.csrf.token_manager
logout:
path: /logout
target:/
anonymous: true
という名前のサービスuser_provider.admins
<?php
namespace APP\AppBundle\Security;
use ...;
class AdminsProvider extends AbstractUserProvider
{
/** @var Admins */
protected $userClass;
/** @var Admins */
protected $repositoryClass;
/**
* @inheritdoc
*/
public function setUserClass()
{
$this->userClass = Admins::class;
}
/**
* @return Admins
*/
public function getUserClass()
{
return $this->userClass;
}
/**
* @inheritdoc
*/
public function setUserRepository()
{
$this->repositoryClass = Admins::class;
}
/**
* @return Admins
*/
public function getUserRepository()
{
return Admins::class;
}
}
そして終わるために抽象ユーザープロバイダがあるその後、私のプロバイダクラス例:
<?php
namespace App\AppBundle\Security;
use ...;
abstract class AbstractUserProvider implements UserProviderInterface
{
/**
* @var EntityManager
*/
protected $em;
/**
* UserProvider constructor.
*
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @return mixed
*/
abstract protected function setUserRepository();
/**
* @return mixed
*/
abstract protected function getUserRepository();
/**
* @return mixed
*/
abstract protected function setUserClass();
/**
* @return mixed
*/
abstract protected function getUserClass();
/**
* {@inheritDoc}
*/
public function loadUserByUsername($username)
{
$oUser = $this->em->getRepository($this->getUserRepository())->findOneByUsername($username);
if (!$oUser) {
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
return $oUser;
}
/**
* {@inheritDoc}
*/
public function refreshUser(UserInterface $oUser)
{
if (!$this->supportsClass(get_class($oUser))) {
throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".', get_class($this->getUserClass()), get_class($oUser)));
}
if (null === $reloadedUser = $this->em->getRepository($this->getUserRepository())->findOneById($oUser->getId())
) {
throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $oUser->getId()));
}
return $reloadedUser;
}
/**
* {@inheritDoc}
*/
public function supportsClass($class)
{
$userClass = $this->getUserRepository();
return $userClass === $class || is_subclass_of($class, $userClass);
}
}