[英]Store Logged in user data in sonata admin bundle
在Sonata Admin Bundle中,有一个注册表。 管理员和超级管理员可以使用该注册表单创建用户。 一切正常。 但是,现在我想保留trackwho(管理员或超级管理员ID)创建的此用户。因此我想存储登录的用户ID。 在商业实体中:
/**
* Set createdBy
*
* @param integer $createdBy
* @return Commercant
*/
public function setCreatedBy($createdBy)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* Get createdBy
*
* @return integer
*/
public function getCreatedBy()
{
return $this->createdBy;
}
有了这个在奏鸣曲管理类中,我放置了以下代码:
public function preUpdate($commercant)
{
$objUser = $this->getConfigurationPool()->getContainer()->get('security.context')->getToken()->getUser();
if(!empty($objUser)) {
$commercant->setCreatedBy($objUser->getId());
}
}
但什么也没发生。
试试这个:
public function prePersist($commercant)
{
parent::prePersist($commercant);
$isSuperAdmin = $this->getConfigurationPool()->getContainer()->get('security.context')->isGranted('ROLE_SUPER_ADMIN');
$isAdmin = $this->getConfigurationPool()->getContainer()->get('security.context')->isGranted('ROLE_ADMIN');
if($isAdmin || $isSuperAdmin) {
$objUser = $this->getConfigurationPool()->getContainer()->get('security.context')->getToken()->getUser();
if(!empty($objUser)) {
$commercant->setCreatedBy($objUser->getId());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.