How about a function that checks for a model:
protected function modelExist($id) { return Product::find() ->where([ 'productID' => $id ]) ->andWhere(['supplierID' => Yii::$app->user->identity->supplierID ]) ->exists(); }
If productID is the primary key of your product, the request for /products/1 will translate yii \ rest \ UrlRule to /products?productID=1 .
In this case, when the productID provided as a parameter, you can use beforeAction to do a quick check to see if such a model exists and allow the action to execute or throw an error if it isn't:
// 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; }
Update
As a question about Overriding the checkAccess method in ActiveController break mode I thought it would be useful to leave an example.
In the Yii2 REST design method, all delete , update and view actions will call the checkAccess method after loading the model instance:
The same is true for create and index actions, except that they will not pass it an instance of the model: call_user_func($this->checkAccess, $this->id) .
So, what are you trying to do (throwing a ForbiddenHttpException when a user tries to view, update or delete a product that he is not his supplier) can also be achieved as follows:
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.'); } } }