简体   繁体   English

Sonata Admin类中的SecurityToken null

[英]SecurityToken null in Sonata Admin class

I have a problem with getting the logged in user in and Admin class. 我在获取登录用户和Admin类时遇到问题。 What I would like to do is to restrict a choice field to logged in users organization (so that he is not able to pick another organization when creating an event). 我想做的是将选择字段限制为登录用户的组织(以便他在创建事件时不能选择其他组织)。 Therefore I've injected TokenStorage into my CalendarAdmin, but $tokenStorage->getToken() is null even if I'm logged in. Here is my relevant code: 因此,我已经将TokenStorage注入了我的CalendarAdmin中,但是即使我已经登录,$ tokenStorage-> getToken()也为null。这是我的相关代码:

security.yml: security.yml:

    providers:
    in_memory:
        memory: ~
    fos_userbundle:
        id: fos_user.user_manager

    admin:
        pattern:            /admin(.*)
        context:            user
        form_login:
            provider:       fos_userbundle
            login_path:     /admin/login
            use_forward:    false
            check_path:     /admin/login_check
            failure_path:   null
        logout:
            path:           /admin/logout
        anonymous:          true

services.yml: services.yml:

    pozsonyba.calendar_bundle.admin.calendar:
    class: Pozsonyba\Bundle\CalendarBundle\Admin\CalendarAdmin
    arguments: [~, Pozsonyba\Bundle\CalendarBundle\Entity\Calendar, SonataAdminBundle:CRUD, @security.token_storage, @pozsonyba_organization.repository.organization_repository]
    tags:
        - {name: sonata.admin, manager_type: orm, group: admin, label: Calendar}

I read that this security.yml might have been set up wrong, that the firewall is missing something, I just can't figure out what. 我读到此security.yml可能设置错误,防火墙缺少某些内容,我只是不知道是什么原因。

Thank you for help. 谢谢你的帮助。 CalendarAdmin.php: CalendarAdmin.php:

    public function __construct($code, $class, $baseControllerName, TokenStorage $tokenStorage, OrganizationRepository $organizationRepository)
    {
        parent::__construct($code, $class, $baseControllerName);

        VarDumper::dump($tokenStorage->getToken());die;
        $this->organizationRepository = $organizationRepository;
    }

Check out the \\Sonata\\AdminBundle\\Admin\\AbstractAdmin class. 检出\\Sonata\\AdminBundle\\Admin\\AbstractAdmin类。 You can get access to the container and the token storage via the configuration pool: 您可以通过配置池访问容器和令牌存储:

$this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser()

I guess, the token is not set when the admin object is created, so as an alternative way you can try to inject the TokenStorage via setter injection: 我猜想,在创建admin对象时未设置令牌,因此,作为另一种方法,您可以尝试通过setter注入来注入TokenStorage

# CalendarAdmin.php
/** @var  TokenStorageInterface */
private $tokenStorage;

/**
 * @param TokenStorageInterface $tokenStorage
 */
public function setTokenStorage($tokenStorage)
{
    $this->tokenStorage = $tokenStorage;
}

update services definition 更新服务定义

# services.yml
pozsonyba.calendar_bundle.admin.calendar:
    class: Pozsonyba\Bundle\CalendarBundle\Admin\CalendarAdmin
    arguments: [~, Pozsonyba\Bundle\CalendarBundle\Entity\Calendar, SonataAdminBundle:CRUD, @security.token_storage, @pozsonyba_organization.repository.organization_repository]
    calls: 
        - [setTokenStorage, ["@security.token_storage"]]
    tags:
        - {name: sonata.admin, manager_type: orm, group: admin, label: Calendar}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM