繁体   English   中英

Yii2 - 覆盖休息ActiveController中的checkAccess

[英]Yii2 - Override checkAccess in rest ActiveController

Product.supplierID = Supplier.supplierID
---------         ----------
|Product|---------|Supplier|
---------         ----------
                       |
                       |  Supplier.supplierID = User.supplierID
                       |
                   ---------
                   |  User |
                   ---------

使用上面的表结构,应用程序使用ActiveController子类,并使用重写的prepareDataProvider来限制登录的每个Productindex列表User可以看到具有匹配的supplierID值的那些。 ProductControlleractions()方法中有类似的东西。

$actions['index']['prepareDataProvider'] = function($action)
{
    $query = Product::find();
    if (Yii::$app->user->can('supplier') &&
        Yii::$app->user->identity->supplierID) {
        $query->andWhere(['supplierID' => Yii::$app->user->identity->supplierID]);
    }
    return new ActiveDataProvider(['query' => $query]);
};

这工作正常,但我希望使用checkAccess()来限制单个Product actionView()

目前,登录User可以通过更改URL中的productID来访问Product ,无论是否具有相应的supplierID

看起来我无法访问Product的特定实例,检查supplierID ,直到actionView()返回,这是我希望检查发生的时间。

我可以覆盖checkAccess()来限制访问并抛出适当的ForbiddenHttpException吗?

如何检查模型是否存在:

protected function modelExist($id)
{
    return Product::find()
    ->where([ 'productID' => $id ])
    ->andWhere(['supplierID' => Yii::$app->user->identity->supplierID ])
    ->exists(); 
}

如果productID是您的产品主键,那么对/products/1的请求将由yii \\ rest \\ UrlRule翻译为/products?productID=1

在这种情况下,当productID作为参数提供时,您可以使用beforeAction快速检查是否存在此类模型并执行操作或如果不执行则抛出错误:

// this array will hold actions to which you want to perform a check
public $checkAccessToActions   = ['view','update','delete'];

public function beforeAction($action) {
    if (!parent::beforeAction($action)) return false;

        $params = Yii::$app->request->queryParams;

        if (isset($params['productID']) {
           foreach ($this->checkAccessToActions as $action) {
              if ($this->action->id === $action) {
                  if ($this->modelExist($params['productID']) === false)
                       throw new NotFoundHttpException("Object not found");
              }
           }
    }
    return true;
}

更新

由于问题是关于覆盖休息ActiveController中的checkAccess方法,我认为留下一个例子会很有用。

在设计Yii2 REST的方式中,所有deleteupdateview操作都会在加载模型实例后调用checkAccess方法:

// code snippet from yii\rest\ViewAction
$model = $this->findModel($id);
if ($this->checkAccess) {
    call_user_func($this->checkAccess, $this->id, $model);
}

对于createindex操作也是如此,除了它们不会将任何模型实例传递给它: call_user_func($this->checkAccess, $this->id)

因此,您尝试做的事情( 当用户试图查看,更新或删除他不是供应商的产品时抛出ForbiddenHttpException )也可以通过以下方式实现:

public function checkAccess($action, $model = null, $params = [])
{
    if ($action === 'view' or $action === 'update' or $action === 'delete') 
    {
        if ( Yii::$app->user->can('supplier') === false
             or Yii::$app->user->identity->supplierID === null
             or $model->supplierID !== \Yii::$app->user->identity->supplierID ) 
        {
             throw new \yii\web\ForbiddenHttpException('You can\'t '.$action.' this product.');
        }

    }
}

暂无
暂无

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

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