Symfony2:SonataAdminBundle – 如何在管理类中获取代表当前用户的对象?

栏目: PHP · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/22069541/symfony2-sonataadminbundle-how-can-i-get-the-object-representing-the-current

我使用sonata-admin包.

我在PageEntity中与用户(FOSUserBundle)建立了关系.

我想保存创建或更改页面的当前用户.

我的猜测是获取admin类的postUpdate和postPersist方法中的用户对象,并且此对象在setUser方法中传输.

但是如何实现呢?

在google的小组中,我看到了

public function setSecurityContext($securityContext) {
        $this->securityContext = $securityContext;
    }

    public function getSecurityContext() {
        return $this->securityContext;
    }

    public function prePersist($article) {
        $user = $this->getSecurityContext()->getToken()->getUser();

        $appunto->setOperatore($user->getUsername());
    }

但这不起作用

在admin类中,您可以像这样获取当前登录的用户:

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

编辑基于反馈

而你这样做呢?因为这应该有效.

/**
     * {@inheritdoc}
     */
    public function prePersist($object)
    {

$user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser();
        $object->setUser($user);
    }

    /**
     * {@inheritdoc}
     */
    public function preUpdate($object)
    {
$user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser();
        $object->setUser($user);
    }

翻译自:https://stackoverflow.com/questions/22069541/symfony2-sonataadminbundle-how-can-i-get-the-object-representing-the-current


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Security, Privacy and Commerce, 2nd Edition

Web Security, Privacy and Commerce, 2nd Edition

Simson Garfinkel / O'Reilly Media / 2002-01-15 / USD 44.95

Since the first edition of this classic reference was published, World Wide Web use has exploded and e-commerce has become a daily part of business and personal life. As Web use has grown, so have ......一起来看看 《Web Security, Privacy and Commerce, 2nd Edition》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具