search

Home  >  Q&A  >  body text

php - yii2 rbac 每次走一个方法都要自己验证一次有没有权限吗?如果是配置的话怎么配置?

yii2 rbac 每次走一个方法都要自己验证一次用户有没有该控制器与Action的权限吗?如果是配置的话怎么配置?

阿神阿神2915 days ago296

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-10 15:34:47

    yii\filters\AccessRule::matchCalllback. 注意传递的参数 $rule, $action

    [
        'actions' => ['special-callback'],
        'allow' => true,
        'matchCallback' => function ($rule, $action) {
            # write arbitrary access check logic
            # permission = (rule [xxoo] action)
            # return user->can(permission);
        }
    ]
    

    或者使用下面的方式, 见 yii2-admin

    return [
        'components' => [
            'authManager' => [
                'class' => 'yii\rbac\PhpManager', // or use 'yii\rbac\DbManager'
            ]
        ],
        'as access' => [
            'class' => 'mdm\admin\components\AccessControl',
        ],
    ];
    

    reply
    0
  • ringa_lee

    ringa_lee2017-04-10 15:34:47

    AccessControl 支持配置 role

    例如:

    [
        'actions' => ['edit'],
        'allow' => true,
        'roles' => ['@', 'author'],
    ],
    

    reply
    0
  • Cancelreply