Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add Rule (such as Author Rule) to an permission #107

Open
thanhnambkhn opened this issue Sep 17, 2018 · 8 comments
Open

How to add Rule (such as Author Rule) to an permission #107

thanhnambkhn opened this issue Sep 17, 2018 · 8 comments

Comments

@thanhnambkhn
Copy link

thanhnambkhn commented Sep 17, 2018

Hi,
Do we has feature such as adding Rule to a permission?
Let's images that we has a post Editor Role (with permissions as follow create/ update/ delete a post )
To pass checking permissions Update and delete a post, user needs satisfy two conditions:

  • Assigned to Editor Role
  • Be the author of the current post (Rule is a piece of code which do logic, ex: $post->created_by = $currentUserId) ==> So how to do that?**
@thanhnambkhn
Copy link
Author

@abiusx Do you think it is useful feature?

@abiusx
Copy link
Contributor

abiusx commented Sep 17, 2018 via email

@thanhnambkhn
Copy link
Author

I think it's a common case (edit, delete only by owner OR manager).
If you separate that feature out of core, developer must hard code such that:

// To check access control to delete / update a post:
 if($identity === $post->getAuthor() && $identity->hasRole('editor')) {...}

In my opinion, it is not a good way to implement this feature.

@abiusx
Copy link
Contributor

abiusx commented Sep 18, 2018 via email

@thanhnambkhn
Copy link
Author

thanhnambkhn commented Sep 18, 2018

@abiusx thank for your answer.
So do you have any suggestion to implement data-level access control feature within PHPRBAC?
Should I build a AccessControl class, which is extended from Rbac, and add more method suchas:
AccessController->check($userId, $roleId, $DATA_need_to_check)

@abiusx
Copy link
Contributor

abiusx commented Sep 18, 2018 via email

@thanhnambkhn
Copy link
Author

thanhnambkhn commented Sep 18, 2018

Hi,
As I learned from Yii2 framework, they add a concept beside Role, Permission, that is Rule
Rule is a class with piece of code which will execute logic business and return true or false.
After defining Rule, you can add Rule to a permission like that:

$rule = new \app\rbac\AuthorRule;
$auth->add($rule);

// add the "updateOwnPost" permission and associate the rule with it.
$updateOwnPost = $auth->createPermission('updateOwnPost');
$updateOwnPost->description = 'Update own post';
**$updateOwnPost->ruleName = $rule->name;**
$auth->add($updateOwnPost);

You can see more here.
In that way, you can define any logic business not only owner right, and no need to change the 'function-level access control' of PHPRBAC
How do you think about this idea?

@abiusx
Copy link
Contributor

abiusx commented Sep 18, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants