扩展UserProvider forSymfony 2 FOS UserBundle

时间:2015-02-19 04:19:49

标签: php symfony fosuserbundle

我使用FOS用户包登录我的网站。对于需求,我需要在Fos用户捆绑包用户提供程序中进行更改(

FOS \ UserBundle \安全\ UserProvider)。

我创建了一个扩展原始userProvider的userProvider FOS并重写了loadUserByUsername方法以进行更改。它在下面给出

<?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Common\UtilityBundle\Security;

use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface;
use FOS\UserBundle\Model\User;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Propel\User as PropelUser;
use FOS\UserBundle\Security\UserProvider as FOSProvider;
use Symfony\Component\DependencyInjection\ContainerInterface;

class UserProvider extends FOSProvider
{
/**
 *
 * @var ContainerInterface 
 */
protected $container;
protected $userManager;


public function __construct(UserManagerInterface $userManager, ContainerInterface $container) {
    $this->userManager = $userManager;
    $this->container = $container;
}

/**
 * {@inheritDoc}
 */
public function loadUserByUsername($username)
{

    $user = $this->findUserUsername($username);

    if (!$user) {
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }

    return $user;
}

/**
 * {@inheritDoc}
 */
public function refreshUser(SecurityUserInterface $user)
{
    if (!$user instanceof User && !$user instanceof PropelUser) {
        throw new UnsupportedUserException(sprintf('Expected an instance of FOS\UserBundle\Model\User, but got "%s".', get_class($user)));
    }

    if (!$this->supportsClass(get_class($user))) {
        throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".', $this->userManager->getClass(), get_class($user)));
    }

    if (null === $reloadedUser = $this->userManager->findUserBy(array('id' => $user->getId()))) {
        throw new UsernameNotFoundException(sprintf('User with ID "%d" could not be reloaded.', $user->getId()));
    }

    return $reloadedUser;
}

/**
 * {@inheritDoc}
 */
public function supportsClass($class)
{
    $userClass = $this->userManager->getClass();

    return $userClass === $class || is_subclass_of($class, $userClass);
}

/**
 * Finds a user by username.
 *
 * This method is meant to be an extension point for child classes.
 *
 * @param string $username
 *
 * @return UserInterface|null
 */
protected function findUserUsername($username)
{
    return $this->userManager->findUserByUsername($username);
}

/**
 * Finds a user by username.
 *
 * This method is meant to be an extension point for child classes.
 *
 * @param string $username
 *
 * @return UserInterface|null
 */
protected function findUserClub($username, $club)
{
    return $this->userManager->findUserByClub($club, $username);
}
}

我已使用以下内容配置服务。

fg.security.authentication.userprovider:
        class:  %Common_utility.security.user_provider.class%
        arguments:
          - @fos_user.user_manager
          - @service_container

我的security.yml看起来像这样:

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    providers:
        fg_security_userprovider:
            id: fg.security.authentication.userprovider

在我的parameter.yml中,我将参数设置为:

Common_utility.security.user_provider.class: Common\UtilityBundle\Security\UserProvider

但是我总是得到如下错误:

未捕获的异常 '的Symfony \元器件\ DependencyInjection \异常\ ServiceNotFoundException的'  消息'the service“security.authentication.manager”有一个 依赖于不存在的服务 “security.user.provider.concrete.fos_userbundle”。”在 /vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php:59

有人知道如何让这个工作吗?

0 个答案:

没有答案