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

Implementing proposed changes #56

Merged
merged 19 commits into from
Jul 10, 2024
Merged
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
5 changes: 2 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Copyright (c) 2013, ZF-Commons and LM-Commons Contributors
All rights reserved.
Copyright (c) 2020-2024, LM-Commons Organization and its contributors. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Expand All @@ -11,7 +10,7 @@ are permitted provided that the following conditions are met:
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of the ZF-Commons, nor the name of LM-Commons nor the names of its
Neither the name of the name of LM-Commons nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

Expand Down
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@
![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.github.com%2Frepos%2Flm-commons%2Flmcrbac%2Fproperties%2Fvalues&query=%24%5B%3A1%5D.value&label=Maintenance%20Status)


Role-based access control module to provide additional features on top of Zend\Permissions\Rbac

Based on [ZF-Commons/zfc-rbac](https://github.com/ZF-Commons/zfc-rbac) v3.x. If you are looking for the Laminas version
of zfc-rbac v2, please use [LM-Commons/LmcRbacMvc](https://github.com/LM-Commons/LmcRbacMvc).
Role-based access control module to provide additional features on top of Laminas\Permissions\Rbac

## Requirements

- PHP 7.3 or higher

**Note:** PHP 7.3 and 8.0 are no longer actively tested
- PHP 8.1 or higher

## Optional

Expand Down
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The ZF-Commons orgnisation has been moved to Laminas-Commons and ZfcRbac has bee
To upgrade

- Uninstall `zf-commons/zfc-rbac:3.0.0-alpha.1`.
- Install `laminas-commons/lmc-rbac:^1.0`
- Install `lm-commons/lmc-rbac:^1.0`
- Change `zfc-rbac.global.php` to `lmc-rbac.global.php` and update the key `zfc_rbac` to `lmc_rbac`.
- Review your code for usages of the `ZfcRbac/*` namespace to `LmcRbac/*` namespace.

Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@
{
"name": "Bas Kamer",
"email": "[email protected]"
},
{
"name": "Eric Richer",
"email": "[email protected]"
}
],
"require": {
"php": "^8.1 || ^8.2 || ^8.3",
"laminas/laminas-authentication": "^2.0",
"laminas/laminas-servicemanager": "^3.3",
"laminas/laminas-stdlib": "^3.1",
"doctrine/persistence": "^2.0 || ^3.0"
Expand All @@ -47,7 +52,8 @@
"phpspec/prophecy-phpunit": "^2.0",
"friendsofphp/php-cs-fixer": "^3.43",
"php-coveralls/php-coveralls": "^2.0",
"doctrine/collections": "^2.0"
"doctrine/orm": "^2.13 | ^3.0",
"symfony/cache": "^4.0| ^5.0 |^6.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -78,6 +84,7 @@
"cs-fix": "php-cs-fixer fix -v --diff",
"test": "phpunit --colors=always",
"header": "docheader check src test",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"test-coverage-html": "phpunit --colors=always --coverage-html ./build/html"
}
}
10 changes: 9 additions & 1 deletion config/config.global.php → config/lmcrbac.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
return [
'lmc_rbac' => [

/**
* Key that is used to fetch the identity provider
*
* Please note that when an identity is found, it MUST implement the LmcRbac\Identity\IdentityProviderInterface
* interface, otherwise it will throw an exception.
*/
// 'identity_provider' => 'LmcRbac\Identity\AuthenticationIdentityProvider',

/**
* Set the guest role
*
Expand Down Expand Up @@ -55,7 +63,7 @@
* Defining the assertion map
*
* The assertion map can automatically map permissions to assertions. This means that every times you check for
* a permission methioned in the assertion map, you'll include the assertion in your check.
* a permission mentioned in the assertion map, you will include the assertion in your check.
*
* example:
* 'assertion_map' => [
Expand Down
60 changes: 43 additions & 17 deletions data/FlatRole.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
* and is licensed under the MIT license.
*/

declare(strict_types=1);

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Rbac\Role\RoleInterface;
use LmcRbac\Permission\PermissionInterface;
use LmcRbac\Role\RoleInterface;

/**
* @ORM\Entity
* @ORM\Table(name="roles")
*/
#[ORM\Entity]
#[ORM\Table(name: 'roles')]
class FlatRole implements RoleInterface
{
/**
Expand All @@ -34,21 +39,25 @@ class FlatRole implements RoleInterface
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected ?int $id;

/**
* @var string|null
*
* @ORM\Column(type="string", length=48, unique=true)
* @ORM\Column(type="string", length=255, unique=true)
*/
protected $name;
#[ORM\Column(name: 'name', type: 'string', length: 255, unique: true)]
protected ?string $name;

/**
* @var PermissionInterface[]|\Doctrine\Common\Collections\Collection
*
* @var Permission[]|Collection
* @ORM\ManyToMany(targetEntity="Permission", indexBy="name", fetch="EAGER", cascade={"persist"})
*/
protected $permissions;
#[ORM\ManyToMany(targetEntity: "Persmission", cascade: ["persist"], fetch: "EAGER", indexBy: "name")]
protected array|Collection|ArrayCollection $permissions;

/**
* Init the Doctrine collection
Expand All @@ -61,38 +70,38 @@ class FlatRole implements RoleInterface
/**
* Get the role identifier
*
* @return int
* @return int|null
*/
public function getId()
public function getId(): ?int
{
return $this->id;
}

/**
* Set the role name
*
* @param string $name
* @param string $name
* @return void
*/
public function setName($name)
public function setName(string $name): void
{
$this->name = (string) $name;
$this->name = $name;
}

/**
* Get the role name
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* {@inheritDoc}
* @inheritDoc
*/
public function addPermission($permission)
public function addPermission(PermissionInterface|string $permission): void
{
if (is_string($permission)) {
$permission = new Permission($permission);
Expand All @@ -102,13 +111,30 @@ class FlatRole implements RoleInterface
}

/**
* {@inheritDoc}
* @inheritDoc
*/
public function hasPermission($permission)
public function hasPermission(PermissionInterface|string $permission): bool
{
// This can be a performance problem if your role has a lot of permissions. Please refer
// to the cookbook to an elegant way to solve this issue

return isset($this->permissions[(string) $permission]);
}



public function getChildren(): iterable
{
return [];
}

public function hasChildren(): bool
{
return false;
}

public function addChild(RoleInterface $role): void
{
// Do nothing
}
}
Loading