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

Recurse check #115

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions PhpRbac/src/PhpRbac/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public function assign($role, $permission)
return Jf::$Rbac->assign($role, $permission);
}

public function check($permission, $user_id)
public function check($permission, $user_id, $recurse = false)
{
return Jf::$Rbac->check($permission, $user_id);
return Jf::$Rbac->check($permission, $user_id, $recurse);
}

public function enforce($permission, $user_id)
public function enforce($permission, $user_id, $recurse = false)
{
return Jf::$Rbac->enforce($permission, $user_id);
return Jf::$Rbac->enforce($permission, $user_id, $recurse);
}

public function reset($ensure = false)
Expand Down
22 changes: 17 additions & 5 deletions PhpRbac/src/PhpRbac/core/lib/rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,15 @@ function assign($Role, $Permission)
* containing a number)
* @param string|integer $UserID
* User ID of a user
* @param bool $Recurse
* Use when $Permission is a path, allow system to recursively
* find permission
*
* @throws RbacPermissionNotFoundException
* @throws RbacUserNotProvidedException
* @return boolean
*/
function check($Permission, $UserID = null)
function check($Permission, $UserID = null, $Recurse = false)
{
if ($UserID === null)
throw new \RbacUserNotProvidedException ("\$UserID is a required argument.");
Expand All @@ -586,10 +589,15 @@ function check($Permission, $UserID = null)
}
else
{
if (substr ( $Permission, 0, 1 ) == "/")
if (substr ( $Permission, 0, 1 ) == "/") {
$PermissionID = $this->Permissions->pathId ( $Permission );
else
if ($PermissionID === null && $Recurse) {
$newPath = implode('/', explode('/', $Permission, -1));
return $this->check($newPath, $UserID, true);
}
} else {
$PermissionID = $this->Permissions->titleId ( $Permission );
}
}

// if invalid, throw exception
Expand Down Expand Up @@ -636,14 +644,18 @@ function check($Permission, $UserID = null)
*
* @param integer $UserID
*
* @param bool $Recurse
* Use when $Permission is a path, allow system to recursively
* find permission
*
* @throws RbacUserNotProvidedException
*/
function enforce($Permission, $UserID = null)
function enforce($Permission, $UserID = null, $Recurse = false)
{
if ($UserID === null)
throw new \RbacUserNotProvidedException ("\$UserID is a required argument.");

if (! $this->check($Permission, $UserID)) {
if (! $this->check($Permission, $UserID, $Recurse)) {
header('HTTP/1.1 403 Forbidden');
die("<strong>Forbidden</strong>: You do not have permission to access this resource.");
}
Expand Down
59 changes: 55 additions & 4 deletions PhpRbac/tests/src/RbacManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,54 @@ public function testManagerCheckTitle()
public function testManagerCheckPath()
{
self::$rbac->Permissions->addPath('/permissions_1/permissions_2/permissions_3');
$perm_id_1 = self::$rbac->Permissions->pathId('/permissions_1/permissions_2/permissions_3');
$perm_id_1 = self::$rbac->Permissions->pathId('/permissions_1/permissions_2');

self::$rbac->Roles->addPath('/roles_1/roles_2/roles_3');
$role_id_1 = self::$rbac->Roles->pathId('/roles_1/roles_2/roles_3');

self::$rbac->Roles->assign($role_id_1, 3);
self::$rbac->Roles->assign($role_id_1, $perm_id_1);
self::$rbac->Users->assign($role_id_1, 5);

$result = self::$rbac->check('/permissions_1/permissions_2', 5);

$this->assertTrue($result);
}

public function testManagerCheckRecursivePath()
{
self::$rbac->Permissions->addPath('/permissions_1/permissions_2');
$perm_id_1 = self::$rbac->Permissions->pathId('/permissions_1/permissions_2');

self::$rbac->Roles->addPath('/roles_1/roles_2/roles_3');
$role_id_1 = self::$rbac->Roles->pathId('/roles_1/roles_2/roles_3');

self::$rbac->Roles->assign($role_id_1, $perm_id_1);
self::$rbac->Users->assign($role_id_1, 5);

$result = self::$rbac->check('/permissions_1/permissions_2/permissons_3', 5, true);

$this->assertTrue($result);
}

/**
* @expectedException RbacPermissionNotFoundException
*/

public function testManagerCheckNonRecursivePath()
{
self::$rbac->Permissions->addPath('/permissions_1/permissions_2');
$perm_id_1 = self::$rbac->Permissions->pathId('/permissions_1/permissions_2');

self::$rbac->Roles->addPath('/roles_1/roles_2/roles_3');
$role_id_1 = self::$rbac->Roles->pathId('/roles_1/roles_2/roles_3');

self::$rbac->Roles->assign($role_id_1, $perm_id_1);
self::$rbac->Users->assign($role_id_1, 5);

self::$rbac->check('/permissions_1/permissions_2/permissons_3', 5, false);

}

public function testManagerCheckBadPermBadUserFalse()
{
$result = self::$rbac->check(5, 5);
Expand Down Expand Up @@ -222,19 +257,35 @@ public function testManagerEnforceTitle()
public function testManagerEnforcePath()
{
self::$rbac->Permissions->addPath('/permissions_1/permissions_2/permissions_3');
$perm_id_1 = self::$rbac->Permissions->pathId('/permissions_1/permissions_2/permissions_3');
$perm_id_1 = self::$rbac->Permissions->pathId('/permissions_1/permissions_2');

self::$rbac->Roles->addPath('/roles_1/roles_2/roles_3');
$role_id_1 = self::$rbac->Roles->pathId('/roles_1/roles_2/roles_3');

self::$rbac->Roles->assign($role_id_1, 3);
self::$rbac->Roles->assign($role_id_1, $perm_id_1);
self::$rbac->Users->assign($role_id_1, 5);

$result = self::$rbac->enforce('/permissions_1/permissions_2', 5);

$this->assertTrue($result);
}

public function testManagerEnforceRecursivePath()
{
self::$rbac->Permissions->addPath('/permissions_1/permissions_2');
$perm_id_1 = self::$rbac->Permissions->pathId('/permissions_1/permissions_2');

self::$rbac->Roles->addPath('/roles_1/roles_2/roles_3');
$role_id_1 = self::$rbac->Roles->pathId('/roles_1/roles_2/roles_3');

self::$rbac->Roles->assign($role_id_1, $perm_id_1);
self::$rbac->Users->assign($role_id_1, 5);

$result = self::$rbac->enforce('/permissions_1/permissions_2/permissions_3', 5, true);

$this->assertTrue($result);
}

/**
* @expectedException RbacUserNotProvidedException
*/
Expand Down