dongqiyou0303 2016-08-24 09:15
浏览 30
已采纳

将安全性放在Sonatas Admin Extensions上

I want to add a 'quick publish' function when saving pages in sonata admin - this function needs to be locked down to certain individuals with the correct 'role' or 'permission'.

I initially played around with changing the base template to add a form button and doing it all in the PageAdmin postUpdate/postPersist functions but felt this was a bit too messy.

I have now looked at using an AdminExtension - doing this meant removing the button from the form as I didn't really want to overwrite the template for this one change. To do this i've added a configureFormFields function:

public function configureFormFields(FormMapper $formMapper)
{
    $formMapper->with('Options')
        ->add('quickPublish', 'checkbox', [
            'label' => 'Quick Publish',
            'required' => false,
            'help' => 'Checking this will automatically publish the changes'
        ])
        ->end();

}

I have then added my postPersist and postUpdate functions in here that will make the necessary updates.

public function postPersist(AdminInterface $admin, $object)
{
    if ($object->getQuickPublish()) {
        $this->quickPublish($object);
    }
}

The problem i'm facing is that I only want this admin extension to run based on the security roles not just all the time.

In postPersist and postUpdate it isn't an issue as I can use $admin->isGranted('QUICK_PUBLISH) but $admin isn't available to me in the configureFormFields function.

I can't find a way to put security around the admin extensions, anyone know how this works? I'm using Sonata 2.x

Thanks Kevin

  • 写回答

1条回答 默认 最新

  • donglin7979 2016-08-24 16:24
    关注

    Take a look at the built in Sonata LockExtension

    In the configureFormFields method, you should be able to get the admin object from the FormMapper :

    public function configureFormFields(FormMapper $form)
    {
        $admin = $form->getAdmin();
        //.....
    }
    

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了