I've seen a lot of Yii 2 RBAC tutorials but I can't really appreciate how to implement the rules. In the Yii 2 guide they introduced how rules are made but not really how can it be implemented in the controller's behavior or somewhere else. I really need some enlightenment regarding this said matter.
What I have now is a document uploading system where I have two roles namely admin & encoder. Basically, admin role can do all while the encoder role can only create, view-own, update-own and delete-own. I already created a Rule named encodedBy.
This is my code in my 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;
} }
I store the data in the 'document' table, where I have a field named 'encoded_by'.