Skip to content

Commit

Permalink
Added PermissionInterface (LM-Commons#55)
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Richer [email protected] <[email protected]>
  • Loading branch information
visto9259 committed Jul 9, 2024
1 parent f743db4 commit b0066d5
Show file tree
Hide file tree
Showing 18 changed files with 656 additions and 115 deletions.
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
}
}
69 changes: 41 additions & 28 deletions data/HierarchicalRole.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +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\HierarchicalRoleInterface;
use LmcRbac\Permission\PermissionInterface;
use LmcRbac\Role\RoleInterface;

/**
* @ORM\Entity
* @ORM\Table(name="roles")
*/
class HierarchicalRole implements HierarchicalRoleInterface
#[ORM\Entity]
#[ORM\Table(name: 'roles')]
class HierarchicalRole implements RoleInterface
{
/**
* @var int|null
Expand All @@ -34,81 +38,89 @@ class HierarchicalRole implements HierarchicalRoleInterface
* @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 = null;

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

/**
* @var HierarchicalRoleInterface[]|\Doctrine\Common\Collections\Collection
* @var RoleInterface[]|Collection
*
* @ORM\ManyToMany(targetEntity="HierarchicalRole")
*/
protected $children = [];
#[ORM\ManyToMany(targetEntity: RoleInterface::class)]
protected array|Collection|ArrayCollection $children = [];

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

/**
* Init the Doctrine collection
*/
public function __construct()
{
$this->children = new ArrayCollection();
$this->children = new ArrayCollection();
$this->permissions = new ArrayCollection();
}

/**
* 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}
* @param RoleInterface $child
* @return void
*/
public function addChild(HierarchicalRoleInterface $child)
public function addChild(RoleInterface $role): void
{
$this->children[] = $child;
$this->children[] = $role;
}

/**
* {@inheritDoc}
* @param string|Permission $permission
* @return void
*/
public function addPermission($permission)
public function addPermission(string|Permission|\LmcRbac\Permission\PermissionInterface $permission): void
{
if (is_string($permission)) {
$permission = new Permission($permission);
Expand All @@ -118,9 +130,10 @@ class HierarchicalRole implements HierarchicalRoleInterface
}

/**
* {@inheritDoc}
* @param $permission
* @return bool
*/
public function hasPermission($permission)
public function hasPermission(string|\LmcRbac\Permission\PermissionInterface $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
Expand All @@ -129,18 +142,18 @@ class HierarchicalRole implements HierarchicalRoleInterface
}

/**
* {@inheritDoc}
* @inheritDoc
*/
public function getChildren()
public function getChildren(): iterable
{
return $this->children;
}

/**
* {@inheritDoc}
* @inheritDoc
*/
public function hasChildren()
public function hasChildren(): bool
{
return !$this->children->isEmpty();
return ! $this->children->isEmpty();
}
}
25 changes: 17 additions & 8 deletions data/Permission.php.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -23,6 +25,8 @@ use LmcRbac\Permission\PermissionInterface;
* @ORM\Entity
* @ORM\Table(name="permissions")
*/
#[ORM\Entity]
#[ORM\Table(name: 'permissions')]
class Permission implements PermissionInterface
{
/**
Expand All @@ -32,37 +36,42 @@ class Permission implements PermissionInterface
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected ?int $id;

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

/**
* Constructor
* @param string $name
*/
public function __construct($name)
public function __construct(string $name)
{
$this->name = (string) $name;
$this->name = $name;
}

/**
* Get the permission identifier
*
* @return int
* @return int|null
*/
public function getId()
public function getId(): ?int
{
return $this->id;
}

/**
* {@inheritDoc}
* @inheritDoc
*/
public function __toString()
public function __toString(): string
{
return $this->name;
}
Expand Down
Loading

0 comments on commit b0066d5

Please sign in to comment.