简体   繁体   English

在 EasyAdmin3 中有条件地禁用主要操作的按钮

[英]Disable conditionally main action's button in EasyAdmin3

I know how to display button with a condition but what I would like is to completly disable the main actions if the user has a type of status.我知道如何显示带有条件的按钮,但是如果用户具有某种状态,我想要完全禁用主要操作。
If my user has the status of employee then he can not create or delete other users, else he can.如果我的用户具有员工状态,则他不能创建或删除其他用户,否则他可以。
I've tried to "display if" but if he hacks the url, he can still do the action.我试图“显示是否”,但如果他破解了 url,他仍然可以执行此操作。

public function configureActions(Actions $actions): Actions
    {
      
        return $actions
            //Here I would like to add my condition
            ->disable(Action::DETAIL, Action::EDIT)
            // I tried this but this is not secure, he can hacks the url
            ->update(Crud::PAGE_INDEX, Action::NEW, fn (Action $action) => $action->setIcon('fa fa-plus')
                ->setLabel('admin.crud.user.button.add_contractor')
                ->displayIf(fn () => $user->getStatus() !== self::EMPLOYEE)
            )
    }

Any idea?任何想法?

I did it differently.我做了不同的事情。 I created a private function with my conditions and I called it in the configureActions:我根据我的条件创建了一个私有 function 并在 configureActions 中调用它:

public function configureActions(Actions $actions): Actions
    {
        $this->disableActions($actions);

        return $actions
            //->disable(Action::DETAIL, Action::EDIT)
            ->update(Crud::PAGE_INDEX, Action::NEW, fn (Action $action) => $action->setIcon('fa fa-plus')
                ->setLabel('admin.crud.user.button.add_contractor')
            )

            ->update(Crud::PAGE_INDEX, Action::DELETE, fn (Action $action) => $action->setLabel('admin.crud.user.button.delete_profile')
                ->setIcon('fa fa-times')
            )
            ;
    }

    private function disableActions(Actions $actions): void
    {
        $contractor  = $this->getUser();

        $actions->disable(Action::DETAIL, Action::EDIT);

        if ($contractor->getStatus() === self::EMPLOYEE) {
            $actions->disable(Action::DELETE, Action::NEW);
        }
    }

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

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