-
Notifications
You must be signed in to change notification settings - Fork 6
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
Removed Zfc/Rbac dependency and passed all the tests with some changes. #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,6 @@ | |
use LmcRbacMvc\Options\ModuleOptions; | ||
use LmcRbacMvc\Service\AuthorizationService; | ||
use LmcRbacMvc\Service\RoleService; | ||
use Rbac\Rbac; | ||
|
||
/** | ||
* Factory to create the authorization service | ||
|
@@ -43,8 +42,8 @@ class AuthorizationServiceFactory implements FactoryInterface | |
*/ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
/* @var Rbac $rbac */ | ||
$rbac = $container->get(Rbac::class); | ||
/* @var \Laminas\Permissions\Rbac\Rbac $rbac */ | ||
$rbac = $container->get(\Rbac\Rbac::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should not be any reference to Rbac\Rbac as a service. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition, the service 'Rbac\Rbac' is defined in module.config.php and it should not since there is no longer a namespace called 'Rbac' |
||
|
||
/* @var RoleService $roleService */ | ||
$roleService = $container->get(RoleService::class); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
namespace LmcRbacMvc\Factory; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Rbac\Rbac; | ||
use LmcRbacMvc\Rbac\Rbac; | ||
use Rbac\Traversal\Strategy\GeneratorStrategy; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reference to Rbac needs to be removed |
||
use Laminas\ServiceManager\Factory\FactoryInterface; | ||
use Laminas\ServiceManager\ServiceLocatorInterface; | ||
|
@@ -38,7 +38,7 @@ class RbacFactory implements FactoryInterface | |
*/ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
return new Rbac(new GeneratorStrategy()); | ||
return new Rbac(); | ||
} | ||
|
||
/** | ||
|
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]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you doing this? If role is an array of roles, then we are not processing all the roles. If you remove this then this I think you are trying to make Laminas Rbac the same as zf-fr Rbac which is probably not the right approach. We should strive to use Laminas Rbac even if it is to introduce breaking changes. That's why I want to merge this into a v4 with an explanation of how to migrate. |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why this method is adding a role if it's purpose is to check if it has the role. |
||
} | ||
|
||
return parent::hasRole($role); | ||
} | ||
|
||
} |
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()); | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this? |
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this cause an infinite loop? |
||
RecursiveIteratorIterator::SELF_FIRST | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to avoid creating a LmcRbacMvc\Role class if the Role class from Laminas does the job. |
||
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 ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to think about this test. My intent is to move Doctrine related code to another companion library to remove dependencies on Doctrine. |
||
return $this->children->toArray(); | ||
else | ||
return array_values($this->children); | ||
} | ||
|
||
/** | ||
* Get the parent roles. | ||
* | ||
* @return RoleInterface[] | ||
*/ | ||
public function getParents(): array | ||
{ | ||
return $this->parents; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you rewriting this method? Since you are not rewriting |
||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is not a replacement of a method from the parent class and therefore, there is no 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 = []; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why you are doing this? If |
||
return parent::hasDescendant($role); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasChildren() returns a boolean.
this should be if (!$role->hasChildren()) instead