-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed Zfc/Rbac dependency and passed all the tests with some change…
…s in tests. Signed-off-by: Mubashir <[email protected]>
- Loading branch information
1 parent
8d245e3
commit 81e48b9
Showing
33 changed files
with
502 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LmcRbacMvc\Rbac; | ||
|
||
use Laminas\Permissions\Rbac\Rbac as LaminasRbac; | ||
use Laminas\Permissions\Rbac\RoleInterface; | ||
use Laminas\Permissions\Rbac\AssertionInterface; | ||
use Laminas\Permissions\Rbac\Exception; | ||
|
||
class Rbac extends LaminasRbac{ | ||
/** | ||
* Return all the roles | ||
* | ||
* @return RoleInterface[] | ||
*/ | ||
public function getRoles(): array | ||
{ | ||
return $this->roles; | ||
} | ||
|
||
/** | ||
* Determines if access is granted by checking the role and child roles for permission. | ||
* | ||
* @param RoleInterface|string $role | ||
* @param null|AssertionInterface|Callable $assertion | ||
* @throws Exception\InvalidArgumentException If the role is not found. | ||
* @throws Exception\InvalidArgumentException If the assertion is an invalid type. | ||
*/ | ||
public function isGranted($role, string $permission, $assertion = null): bool | ||
{ | ||
if ( is_array($role) && $role[0] instanceof RoleInterface) { | ||
$role = $role[0]; | ||
} | ||
if (! $this->hasRole($role)) { | ||
throw new Exception\InvalidArgumentException(sprintf( | ||
'No role with name "%s" could be found', | ||
is_object($role) ? $role->getName() : $role | ||
)); | ||
} | ||
|
||
if (is_string($role)) { | ||
$role = $this->getRole($role); | ||
} | ||
|
||
$result = $role->hasPermission($permission); | ||
if (false === $result || null === $assertion) { | ||
return $result; | ||
} | ||
|
||
if ( | ||
! $assertion instanceof AssertionInterface | ||
&& ! is_callable($assertion) | ||
) { | ||
throw new Exception\InvalidArgumentException( | ||
'Assertions must be a Callable or an instance of Laminas\Permissions\Rbac\AssertionInterface' | ||
); | ||
} | ||
|
||
if ($assertion instanceof AssertionInterface) { | ||
return $result && $assertion->assert($this, $role, $permission); | ||
} | ||
|
||
// Callable assertion provided. | ||
return $result && $assertion($this, $role, $permission); | ||
} | ||
|
||
/** | ||
* Is a role registered? | ||
* | ||
* @param RoleInterface|string $role | ||
*/ | ||
public function hasRole($role): bool | ||
{ | ||
if(empty($this->roles)){ | ||
$this->addRole($role); | ||
} | ||
|
||
return parent::hasRole($role); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. | ||
*/ | ||
|
||
namespace LmcRbacMvc\Role; | ||
|
||
use ArrayIterator; | ||
use Laminas\Permissions\Rbac\RoleInterface; | ||
use RecursiveIterator; | ||
use Traversable; | ||
|
||
class RecursiveRoleIterator extends ArrayIterator implements RecursiveIterator | ||
{ | ||
/** | ||
* Override constructor to accept {@link Traversable} as well | ||
* | ||
* @param RoleInterface[]|Traversable $roles | ||
*/ | ||
public function __construct($roles) | ||
{ | ||
if ($roles instanceof Traversable) { | ||
$roles = iterator_to_array($roles); | ||
} | ||
|
||
parent::__construct($roles); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function valid() : bool | ||
{ | ||
return ($this->current() instanceof RoleInterface); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasChildren() : bool | ||
{ | ||
$current = $this->current(); | ||
|
||
if (!$current instanceof RoleInterface) { | ||
return false; | ||
} | ||
|
||
return $current->hasChildren(); | ||
} | ||
|
||
/** | ||
* @return RecursiveRoleIterator | ||
*/ | ||
public function getChildren() :? RecursiveRoleIterator | ||
{ | ||
return new RecursiveRoleIterator($this->current()->getChildren()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. | ||
*/ | ||
|
||
namespace LmcRbacMvc\Role; | ||
|
||
use Laminas\Permissions\Rbac\RoleInterface; | ||
use RecursiveIteratorIterator; | ||
|
||
/** | ||
* Create a {@link RecursiveRoleIterator} and wrap it into a {@link RecursiveIteratorIterator} | ||
*/ | ||
class RecursiveRoleIteratorStrategy implements TraversalStrategyInterface | ||
{ | ||
/** | ||
* @param RoleInterface[] $roles | ||
* @return RecursiveIteratorIterator | ||
*/ | ||
public function getRolesIterator($roles) | ||
{ | ||
return new RecursiveIteratorIterator( | ||
new RecursiveRoleIterator($roles), | ||
RecursiveIteratorIterator::SELF_FIRST | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace LmcRbacMvc\Role; | ||
|
||
use Laminas\Permissions\Rbac\Role as LaminasRole; | ||
use Laminas\Permissions\Rbac\RoleInterface; | ||
|
||
class Role extends LaminasRole { | ||
|
||
/** | ||
* Get all child roles | ||
* | ||
* @return RoleInterface[] | ||
*/ | ||
public function getChildren(): array | ||
{ | ||
if($this->children instanceof \Doctrine\ORM\PersistentCollection ) | ||
return $this->children->toArray(); | ||
else | ||
return array_values($this->children); | ||
} | ||
|
||
/** | ||
* Get the parent roles. | ||
* | ||
* @return RoleInterface[] | ||
*/ | ||
public function getParents(): array | ||
{ | ||
return $this->parents; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function hasChildren() | ||
{ | ||
return !empty($this->children); | ||
} | ||
|
||
/** | ||
* Check if a role is a descendant. | ||
*/ | ||
protected function hasDescendant(RoleInterface $role): bool | ||
{ | ||
if(empty($this->children)) | ||
$this->children = []; | ||
|
||
return parent::hasDescendant($role); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.