Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Rbac callback assertion #7327

Closed
wants to merge 6 commits into from
Closed

Rbac callback assertion #7327

wants to merge 6 commits into from

Conversation

kanellov
Copy link
Contributor

Hello,

I've implemented an Rbac Assertion Class that uses a callback to do the assertion.

Here is an example:

<?php
use Zend\Permissions\Rbac\Rbac;
use Zend\Permissions\Rbac\Assertion\Callback;

// User is assigned the foo role with id 5
// News article belongs to userId 5
// Jazz article belongs to userId 6

$rbac = new Rbac();
$user = $mySessionObject->getUser();
$news = $articleService->getArticle(5);
$jazz = $articleService->getArticle(6);

$rbac->addRole($user->getRole());
$rbac->getRole($user->getRole())->addPermission('edit.article');

$assertionCb = function ($user, $article) {
    return function ($rbac) use ($user, $article) {
        return $user->getId() == $article->getUserId();
    };
};

// true always - bad!
if ($rbac->isGranted($user->getRole(), 'edit.article')) {
    // hacks another user's article
}

$assertion = new Callback($assertionCb($user, $news));

// true for user id 5, because he belongs to write group and user id matches
if ($rbac->isGranted($user->getRole(), 'edit.article', $assertion)) {
    // edits his own article
}

$assertion = new Callback($assertionCb($user, $jazz));

// false for user id 5
if ($rbac->isGranted($user->getRole(), 'edit.article', $assertion)) {
    // can not edit another user's article
}

I borrowed the example from Zend Framework manual and from Dynamic Assertions section and I altered it to make use of the proposed Assertion Class.

@kanellov kanellov mentioned this pull request Mar 14, 2015
$assert = new Rbac\Assertion\Callback($callback);
$reflection = new \ReflectionProperty(get_class($assert), 'callback');
$reflection->setAccessible(true);
$this->assertEquals($callback, $reflection->getValue($assert));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be $this->assertAttributeSame($callback, 'callback', $assert);

@kanellov
Copy link
Contributor Author

Thanks for your comments @danizord. I will update my code.

@kanellov
Copy link
Contributor Author

OK @danizord I have updated my code.

@weierophinney weierophinney added this to the 2.4.0 milestone Mar 17, 2015
weierophinney added a commit that referenced this pull request Mar 17, 2015
weierophinney added a commit that referenced this pull request Mar 17, 2015
@weierophinney
Copy link
Member

Merged to develop for release with 2.4.

@kanellov kanellov deleted the feature/rbac_callback_assertion branch March 23, 2015 10:25
weierophinney added a commit to zendframework/zend-permissions-rbac that referenced this pull request May 15, 2015
…ture/rbac_callback_assertion

Rbac callback assertion
weierophinney added a commit to zendframework/zend-permissions-rbac that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants