yii-starter-kit / yii2-starter-kit

Yii2 Starter Kit
http://yii2-starter-kit.terentev.net
Other
1.41k stars 646 forks source link

ownModelRule example? #321

Closed iadj closed 8 years ago

iadj commented 8 years ago

ownModelRule seems really useful, but I can't find any documentation on how to use it. I've also looked at generic Yii2 RBAC tutorials but couldn't get it to work. Is there an example or documentation on how to use the ownModelRule to easily restrict certain actions to the creator of the model (i.e. author can only edit their own post)?

trntv commented 8 years ago

i'll add an example to the docs

iadj commented 8 years ago

Thanks @trntv looking forward to it

iadj commented 8 years ago

Hey @trntv, is there any update on this? I'm currently using if(Model::findOne(['user_id']=>Yii::$app->user->identity->id) on every controller but I feel like the ownModelRule would be much more straightforward, except that I don't know how to implement it :tongue:

trntv commented 8 years ago
  1. Copy and run rbac migration
  2. Rename OwnModel to OwnModelRule
  3. Use it: Yii::$app->user->can('editOwnModel', ['model' => $model])