嗨,大家好,我看到了很多的Yii 2 RBAC的教程,但我不能真正體會到如何實施這些規則。在Yii的2引導他們推出的規則是如何製造的,但沒有真正如何能在控制器的行爲或者其他地方實施。對於這件事我真的需要一些啓發。Yii2 RBAC規則實施
我現在有一個文件上傳系統,在那裏我有兩個角色,即管理&編碼器。基本上,管理角色可以完成所有工作,而編碼角色只能創建,查看擁有,更新擁有和刪除擁有。我已經創建了一個名爲encodedBy的規則。
這是我在我的EncoderRule
namespace app\rbac; use yii\rbac\Rule;
/** * Checks if encoded_by matches user passed via params */ class EncoderRule extends Rule {
public $name = 'encodedBy';
/**
* @param string|integer $user the user ID.
* @param Item $item the role or permission that this rule is associated with
* @param array $params parameters passed to ManagerInterface::checkAccess().
* @return boolean a value indicating whether the rule permits the role or permission it is associated with.
*/
public function execute($user, $item, $params)
{
return isset($params['document']) ? $params['document']->encoded_by == $user : false;
} }
的代碼,我將數據存儲在「文件」表在那裏我有一個字段名爲「encoded_by」 我希望你們能幫助我在此。 非常感謝!
對於任何不確定其工作原理的人來說,本視頻對解釋RBAC做得非常好:https://www.youtube.com/watch?v = vLb8YATO-HU – TheStoryCoder