dongtu7567 2015-01-22 17:29
浏览 89
已采纳

在yii2-user中限制删除特定用户

I'm using yii2-admin and yii2-user. I've created a role Creator who have access to the everything. There is another role called Admin who have limited access. But have the power to delete users. Now I want to restrict Admin from deleting Creator. I know that this can be achieved by overriding the delete function of the AdminController of yii2-user. But I cannot figure out the logic of restricting Admin or any other user from deleting Creator.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • ds261634878 2015-01-22 22:49
    关注

    Take a look here: http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#access-control-filter The only thing that you need to do is to limit the access to the delete function to people who have the Creator role. All others will not be able to delete anything.

    your function should look like

        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
    ........
                        [
                            'actions' => ['save', 'update', 'status', 'activate-all', 'deactivate-all'], // Define specific actions
                            'allow' => true, // Has access
                            'roles' => ['Admin', 'Creator'],
                        ],
                        [
                            'actions' => ['delete', 'delete-all'], // Define specific actions
                            'allow' => true, // Has access
                            'roles' => ['Creator'],
                        ],
                        [
                            'allow' => false, // Do not have access
                            'roles' => ['?'], // Guests '?'
                        ],
                    ],
                ],
            ];
        }
    

    This is just an example, modify it to suit your own needs. Probably you should not allow an Admin to edit a Creator too as changing the password would be almost the same thing as deleting.

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

报告相同问题?

悬赏问题

  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素