如何从用户界面更改Symfony2安全明文编码器

时间:2013-07-22 17:24:12

标签: security symfony fosuserbundle plaintext

我正在尝试在我的Symfony2应用程序(版本2.3)中为登录表单创建链式提供程序 - 这是我的安全性Yaml:

jms_security_extra:
    secure_all_services: false
    expressions: true

security:
    encoders:
    Symfony\Component\Security\Core\User\User: plaintext
    FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
    chain_provider:
        chain:
        providers: [in_memory, fos_userbundle]
    fos_userbundle:
        id: fos_user.user_provider.username
    in_memory:
        memory:
        users:
            admin: { password: god, roles: [ 'ROLE_ADMIN' ] }

    firewalls:
    main:
        pattern: ^/
        form_login:
        provider: chain_provider
        csrf_provider: form.csrf_provider
        logout:       true
        anonymous:    true
        security: true

    access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, role: ROLE_ADMIN }
    - { path: ^/group/, role: ROLE_ADMIN }

正如你所看到的,我正在使用FOSUserBundle(很棒的东西顺便说一下。)

问题是我使用in_memory admin用户登录后,无法访问/ profile / URL。我收到此错误消息:

AccessDeniedHttpException: This user does not have access to this section

我找到了原因 - 问题出在FOS \ UserBundle \ Controller \ ProfileController类中:

public function showAction()
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    return $this->container->get('templating')->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container->getParameter('fos_user.template.engine'), array('user' => $user));
}

Controller正在检查$ user对象是否是UserInterface的实例,因为它是Symfony \ Component \ Security \ Core \ User \ User(纯文本编码器类)的实例。

我尝试将编码器配置更改为:

security:
    encoders:
    Symfony\Component\Security\Core\User\UserInterface: plaintext

但它没有用。我发现用户类在Symfony引擎的许多地方都是硬编码的。

所以我的问题是:如何从安全性yaml改变这种行为?我错过了什么吗?

2 个答案:

答案 0 :(得分:3)

PHP documentation

  

instanceof足够聪明,知道实现了一个的类   interface是接口的实例

Symfony\Component\Security\Core\User\UserInterface实现AdvancedUserInterface,实现UserInterface

您的问题不是typeof签入FOS\UserBundle\Controller\ProfileController,而是错误的防火墙配置或内存中用户没有正确接收它的角色!

答案 1 :(得分:0)

抱歉,您无法看到内存用户的个人资料。配置文件仅适用于FOS用户。这就是为什么它必须实现FOS\UserBundle\Model\UserInterface