2

使用 Symfony 2.1 的当前版本,应该可以将 MongoDB 用作 SecurityBundle 的 Userprovider 而无需使用 FOSUserBundle(如此处介绍:mongodb symfony 用户身份验证?)。

无法弄清楚,代码中的实际问题出在哪里,但我无法使用预定义的用户登录test:test

我的security.yml样子是这样的:

安全:
    编码器:
        test\TestBundle\Document\User:纯文本
    提供者:
        文档成员:
            mongodb:{类:testTestBundle:用户,属性:用户名}
    防火墙:
        安全区域:
            模式:^/
            http_basic:
                领域:“登录测试”
    访问控制:
        - { 路径:^/admin,角色:ROLE_ADMIN }
    角色层次结构:
        ROLE_ADMIN:ROLE_USER

-test/TestBundle/Document/User.php文件:

命名空间测试\TestBundle\Document;

使用 Symfony\Component\Security\Core\User\UserInterface;
使用 Symfony\Component\Security\Core\User\EquatableInterface;
使用 Doctrine\ODM\MongoDB\Mapping\Annotations 作为 ODM;

/**
 * @ODM\Document(collection="user", repositoryClass="test\TestBundle\Document\UserRepository")
 */
类 User 实现 UserInterface、EquatableInterface
{

    /**
     * @ODM\ID
     */
    受保护的$id;

    /**
     * @ODM\字符串
     */
    受保护的$用户名;

    /**
     * @ODM\字符串
     */
    受保护的$密码;

    /**
     * @ODM\集合
     */
    受保护的 $roles = array();

    /**
     * @ODM\字符串
     */
    受保护的$盐;

    /**
     * @ODM\布尔值
     */
    受保护的$isActive;

// 设置器

    /**
     * @param 字符串
     */
    公共函数 setUsername($username)
    {
        $this->用户名 = $用户名;
    }

    /**
     * @param 字符串
     */
    公共函数 setPassword($password)
    {
        $this->密码 = $密码;
    }

    /**
     * @param 字符串
     */
    公共函数 setRole($role)
    {
        $this->角色[] = $角色;
    }

    /**
     * @param 数组
     */
    公共函数 setRoles(数组 $roles)
    {
        $this->roles = (array) $roles;
    }

    /**
     * @param 字符串
     */
    公共函数 setSalt($salt)
    {
        $this->salt = $salt;
    }

// 吸气剂

    /**
     * @return 字符串
     */
    公共函数获取用户名()
    {
        返回 $this-> 用户名;
    }

    /**
     * @return 字符串
     */
    公共函数 getPassword()
    {
        返回 $this->密码;
    }

    /**
     * @return 数组
     */
    公共函数 getRoles()
    {
        返回 $this-> 角色;
    }

    /**
     * @return 字符串
     */
    公共函数 getSalt()
    {
        返回 $this->salt;
    }

// 一般的

    公共函数 __construct()
    {
        $this->isActive = true;
        $this->salt = '';
    }

    公共函数 isEqualTo(UserInterface $user)
    {
        返回 $user->getUsername() === $this->username;
    }

    公共功能擦除凭据()
    {
    }

}

test/TestBundle/Document/UserRepository.php:_

命名空间测试\TestBundle\Document;

使用 Doctrine\ODM\MongoDB\DocumentRepository;
使用 Symfony\Component\Security\Core\User\UserInterface;
使用 Symfony\Component\Security\Core\User\UserProviderInterface;
使用 Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
使用 Symfony\Component\Security\Core\Exception\UnsupportedUserException;

类 UserRepository 扩展 DocumentRepository 实现 UserProviderInterface
{
    公共函数 loadUserByUsername($username)
    {
        $q = $this->createQueryBuilder()
            ->field('username')->equals((string) $username)
            ->getQuery();

        尝试
        {
            $user = $q->getSingleResult();
        }
        捕获(NoResultException $e)
        {
            throw new UsernameNotFoundException(sprintf('Can\'t find Username "%s"', $username), null, 0, $e);
        }

        返回$用户;
    }

    公共功能刷新用户(用户界面$用户)
    {
        $class = get_class($user);
        if (!$this->supportsClass($class)) {
            throw new UnsupportedUserException(sprintf('不支持“%s”的实例。', $class));
        }

        返回 $this->loadUserByUsername($user->getUsername());
    }

    公共函数 supportClass($class)
    {
        返回 $class === 'test\TestBundle\Document\User';
    }
}

具体route

行政:
    模式:/管理员
    默认值:{ _controller: testTestBundle:Test:index }

(将导致现有的控制器和视图)

预定义的user-Document 如下所示:

大批
(
    [_id] => 4f59b5731c911ab41e001234
    [用户名] => 测试
    [密码] => 测试
    [角色] => 数组
        (
            [0] => ROLE_ADMIN
        )

    [盐] =>
    [活跃] => 1
)

但是我无法使用用户名和test密码登录test/admin

4

1 回答 1

1

问题与在 apache + fastCGI 上使用 symfony 的问题有关(https://github.com/symfony/symfony/pull/3551)。

上面的代码按预期工作。

于 2012-03-17T09:44:17.297 回答