Replaced Permissionable/-Interface with an abstract Actor class.

This commit is contained in:
Vassyli
2017-01-02 15:10:03 +01:00
parent f8057077bc
commit 64cb22d3c0
5 changed files with 62 additions and 71 deletions
@@ -1,22 +1,26 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tools\Model;
namespace LotGD\Core\Models;
use LotGD\Core\Exceptions\PermissionAlreadyExistsException;
use LotGD\Core\Exceptions\PermissionDoesNotExistException;
use LotGD\Core\Models\Permission;
use LotGD\Core\Models\PermissionAssociationInterface;
/**
* Tools to work with a permission type field.
* This abtract class provides functionality for user entities that crates might
* want to implement, such as permissions.
*/
trait Permissionable
abstract class Actor
{
/** @var array Associations between permission-id and PermissionAssociation entity. */
private $_permissions = [];
/**
* Loads all associated permissions for this actor.
* @throws PermissionAssociationEntityMissingException
*/
protected function loadPermissions()
{
if (empty($this->permissionAssociationEntity)) {
@@ -32,6 +36,12 @@ trait Permissionable
}
}
/**
* Checks if the actor is associated with a given permission. For permission
* checking, use only the PermissionManager class.
* @param string $permissionId
* @return bool
*/
public function hasPermission(string $permissionId): bool
{
$this->loadPermissions();
@@ -39,6 +49,12 @@ trait Permissionable
return isset($this->_permissions[$permissionId]);
}
/**
* Returns the associated permission given by an id. For permission
* checking, use only the PermissionManager class.
* @param string $permissionId
* @return PermissionAssociationInterface
*/
public function getPermission(string $permissionId): PermissionAssociationInterface
{
$this->loadPermissions();
@@ -46,6 +62,12 @@ trait Permissionable
return $this->_permissions[$permissionId];
}
/**
* Returns the raw permission given by the id. For permission
* checking, use only the PermissionManager class.
* @param string $permissionId
* @return Permission
*/
public function getRawPermission(string $permissionId): Permission
{
$this->loadPermissions();
@@ -53,6 +75,13 @@ trait Permissionable
return $this->_permissions[$permissionId]->getPermission();
}
/**
* Associates a permission with this actor in a given state. For permission
* manipulation, use only the PermissionManager class.
* @param Permission $permission
* @param int $state
* @throws PermissionAlreadyExistsException
*/
public function addPermission(Permission $permission, int $state)
{
$this->loadPermissions();
@@ -67,6 +96,12 @@ trait Permissionable
}
}
/**
* Removes an associated permission from this actor by a given id. For permission
* manipulation, use only the PermissionManager class.
* @param string $permissionId
* @throws PermissionDoesNotExistException
*/
public function removePermission(string $permissionId)
{
$this->loadPermissions();
-49
View File
@@ -1,49 +0,0 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Models;
/**
* Implement this interface if an entity has associates permissions.
*/
interface PermissionableInterface
{
/**
* Returns a name to identify the actor.
* @return string
*/
public function getActorName(): string;
/**
* Returns true if objects has a entry related to a given permission.
* @param string $permissionId
* @return bool
*/
public function hasPermission(string $permissionId): bool;
/**
* Returns the permission association.
* @param string $permissionId
* @return PermissionAssociationInterface
*/
public function getPermission(string $permissionId): PermissionAssociationInterface;
/**
* Returns the raw permission entity.
* @param string $permissionId
* @return Permission
*/
public function getRawPermission(string $permissionId): Permission;
/**
* Adds a permission with a set state.
* @param \LotGD\Core\Models\Permission $permission
*/
public function addPermission(Permission $permission, int $state);
/**
* Removes a permission with a given id.
* @param string $permissionId
*/
public function removePermission(string $permissionId);
}
+13 -11
View File
@@ -4,16 +4,18 @@ declare(strict_types=1);
namespace LotGD\Core;
use LotGD\Core\Exceptions\PermissionIdNotFoundException;
use LotGD\Core\Models\Actor;
use LotGD\Core\Models\PermissionableInterface;
use LotGD\Core\Models\Permission;
/**
* Permissions can be managed with the PermissionManager.
* The PermissionManager manages (checks and manipulates) of actors.
*
* The PermissionManager class provides methods to work with permissions. It can
* be used to create or delete permissions, to remove, allow or deny permissions
* to actors and to check whether an actor has a certain permission or if it is
* explicitely denied from him.
* The PermissionManager class provides methods to work with permissions and is
* the only way to check and manipulate permissions. It can be used to create or
* delete permissions, to remove, allow or deny permissions to actors and to
* check whether an actor has a certain permission or if it is explicitely
* denied from him.
*
* The wording used in this class is:
* - allowed, the actor has a certain permission in the allowed state.
@@ -60,7 +62,7 @@ class PermissionManager
* @return bool True if the permission has been set, be it allowed or denied.
*/
public function hasPermissionSet(
PermissionableInterface $actor,
Actor $actor,
string $permissionId
): bool {
if ($actor->hasPermission($permissionId)) {
@@ -77,7 +79,7 @@ class PermissionManager
* @return bool True if the actor has the permission set and it's state is allowed.
*/
public function isAllowed(
PermissionableInterface $actor,
Actor $actor,
string $permissionId
): bool {
if ($actor->hasPermission($permissionId)) {
@@ -94,7 +96,7 @@ class PermissionManager
* @return bool True if the actor has the permission set and it's state is denied.
*/
public function isDenied(
PermissionableInterface $actor,
Actor $actor,
string $permissionId
): bool {
if ($actor->hasPermission($permissionId)) {
@@ -128,7 +130,7 @@ class PermissionManager
* @param string $permissionId
*/
public function allow(
PermissionableInterface $actor,
Actor $actor,
string $permissionId
) {
if ($actor->hasPermission($permissionId)) {
@@ -154,7 +156,7 @@ class PermissionManager
* @param string $permissionId
*/
public function deny(
PermissionableInterface $actor,
Actor $actor,
string $permissionId
) {
if ($actor->hasPermission($permissionId)) {
@@ -180,7 +182,7 @@ class PermissionManager
* @param string $permissionId
*/
public function remove(
PermissionableInterface $actor,
Actor $actor,
string $permissionId
) {
if ($actor->hasPermission($permissionId)) {
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace LotGD\Core\Tools\Model;
use LotGD\Core\Models\Actor;
use LotGD\Core\Models\Permission;
use LotGD\Core\Models\PermissionableInterface;
@@ -19,7 +20,7 @@ trait PermissionAssociationable
/** @Column(type="integer") */
protected $permissionState;
public function __construct(PermissionableInterface $owner, Permission $permission, int $state) {
public function __construct(Actor $owner, Permission $permission, int $state) {
$this->owner = $owner;
$this->permission = $permission;
$this->permissionState = $state;
+8 -6
View File
@@ -7,25 +7,27 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use LotGD\Core\Models\PermissionableInterface;
use LotGD\Core\Tools\Model\Permissionable;
use Lotgd\Core\Models\Actor;
#use LotGD\Core\Models\PermissionableInterface;
#use LotGD\Core\Tools\Model\Permissionable;
/**
* @Entity
* @Table("TestUsers")
*/
class User implements PermissionableInterface {
use Permissionable;
class User extends Actor #implements PermissionableInterface {
{
#use Permissionable;
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
/** @Column(type="string", length=50); */
private $name;
/** @OneToMany(targetEntity="UserPermissionAssociation", mappedBy="owner", cascade={"persist", "remove"}, orphanRemoval=true) */
private $permissions;
protected $permissions;
private $permissionAssociationEntity = UserPermissionAssociation::class;
protected $permissionAssociationEntity = UserPermissionAssociation::class;
public function __construct()
{