douzhuan1467 2016-09-05 14:29
浏览 134
已采纳

Symfony从实体获取连接的用户ID

In a Symfony2.8/Doctrine2 application, I need to store in each row of my SQL tables the id of the user who created or updated the row (users can connect with Ldap).

So all my entities inherited of a GenericEntity wich contains this variable (type would be string if I want to store Ldap username):

/**
 * @var integer
 *
 * @ORM\Column(name="zzCreationId", type="string", nullable=false)
 */
private $creationId;

And I use the prePersistCallback() to automatically assign this value:

/**
 * @ORM\PrePersist
 */
public function prePersistCallback()
{
    $currentUser = /* ...... ????? ....... */ ;
    if ($currentUser->getId() != null) {
        $this->creationId = $currentUser->getId() ;
    } else {
        $this->creationId = 'unknown' ;
    }
    return $this;
}

But I don't know how to retreive the connected user, or how to automatically inject it in the entity... How can I do it?

  • 写回答

3条回答 默认 最新

  • donglu2761 2016-09-05 14:50
    关注

    You can use a Doctrine entity listener/subscriber instead and to inject the security token and gets the current user logged:

    // src/AppBundle/EventListener/EntityListener.php
    namespace AppBundle\EventListener;
    
    use Doctrine\ORM\Event\LifecycleEventArgs;
    use AppBundle\Entity\GenericEntity;
    
    class EntityListener
    {
        private $tokenStorage;
    
        public function __construct(TokenStorageInterface $tokenStorage = null) 
        {
            $this->tokenStorage = $tokenStorage;
        }
    
        public function prePersist(LifecycleEventArgs $args)
        {
            $entity = $args->getEntity();
    
            // only act on some "GenericEntity" entity
            if (!$entity instanceof GenericEntity) {
                return;
            }
    
            if (null !== $currentUser = $this->getUser()) {
                $entity->setCreationId($currentUser->getId());
            } else {
                $entity->setCreationId(0);
            }
        }
    
        public function getUser()
        {
            if (!$this->tokenStorage) {
                throw new \LogicException('The SecurityBundle is not registered in your application.');
            }
    
            if (null === $token = $this->tokenStorage->getToken()) {
                return;
            }
    
            if (!is_object($user = $token->getUser())) {
                // e.g. anonymous authentication
                return;
            }
    
            return $user;
        }
    }
    

    Next register you listener:

    # app/config/services.yml
    services:
        my.listener:
            class: AppBundle\EventListener\EntityListener
            arguments: ['@security.token_storage']
            tags:
                - { name: doctrine.event_listener, event: prePersist }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办