Skip to content

Commit

Permalink
initial refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
s3b4stian committed Nov 20, 2023
1 parent 8b91ab0 commit 820a035
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 405 deletions.
6 changes: 3 additions & 3 deletions src/Linna/Authorization/FetchByPermissionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface FetchByPermissionInterface
* Fetch from permission.
*
* <p>From a <code>Permission</code> instance as argument, this method must return an array
* containing a <code>EnhancedUser|Role</code> instance for every <code>EnhancedUser|Role</code>
* containing a <code>User|Role</code> instance for every <code>User|Role</code>
* that have the given permission.</p>
*
* @param Permission $permission The permission which will be used to fetch as <code>Permission</code> instance.
Expand All @@ -38,7 +38,7 @@ public function fetchByPermission(Permission $permission): array;
* Fetch from permission.
*
* <p>From a permission id as argument, this method must return an array
* containing a <code>EnhancedUser|Role</code> instance for every <code>EnhancedUser|Role</code>
* containing a <code>User|Role</code> instance for every <code>User|Role</code>
* that have the given permission.</p>
*
* @param int|string $permissionId The permission which will be used to fetch as permission id.
Expand All @@ -51,7 +51,7 @@ public function fetchByPermissionId(int|string $permissionId): array;
* Fetch from permission.
*
* <p>From a permission name as argument, this method must return an array
* containing a <code>EnhancedUser|Role</code> instance for every <code>EnhancedUser|Role</code>
* containing a <code>User|Role</code> instance for every <code>User|Role</code>
* that have the given permission.</p>
*
* @param string $permissionName The permission which will be used to fetch as permission name.
Expand Down
8 changes: 4 additions & 4 deletions src/Linna/Authorization/FetchByRoleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ interface FetchByRoleInterface
* Fetch from role.
*
* <p>From a <code>Role</code> instance as argument, this method must return an array
* containing a <code>EnhancedUser|Permission</code> instance for every
* <code>EnhancedUser|Permission</code> that belongs at the given role.</p>
* containing a <code>User|Permission</code> instance for every
* <code>User|Permission</code> that belongs at the given role.</p>
*
* @param Role $role The role which will be used to fetch as <code>Role</code> instance.
*
Expand All @@ -38,7 +38,7 @@ public function fetchByRole(Role $role): array;
* Fetch from role.
*
* <p>From a role id as argument, this method must return an array containing
* a <code>EnhancedUser|Permission</code> instance for every <code>EnhancedUser|Permission</code>
* a <code>User|Permission</code> instance for every <code>User|Permission</code>
* that belongs at the given role.</p>
*
* @param int|string $roleId The role which will be used to fetch as role id.
Expand All @@ -51,7 +51,7 @@ public function fetchByRoleId(int|string $roleId): array;
* Fetch from role.
*
* <p>From a role name as argument, this method must return an array containing
* a <code>EnhancedUser|Permission</code> instance for every <code>EnhancedUser|Permission</code>
* a <code>User|Permission</code> instance for every <code>User|Permission</code>
* that belongs at the given role.</p>
*
* @param string $roleName The role which will be used to fetch as role name.
Expand Down
8 changes: 4 additions & 4 deletions src/Linna/Authorization/FetchByUserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Linna\Authorization;

use Linna\Authorization\EnhancedUser;
use Linna\Authorization\User;

/**
* Fetch By User Interface.
Expand All @@ -24,15 +24,15 @@ interface FetchByUserInterface
/**
* Fetch from user.
*
* From <code>EnhancedUser</code> instance as argument, this method must return an
* From <code>User</code> instance as argument, this method must return an
* array containing a <code>Permission|Role</code> object instance for every
* <code>Permission|Role</code> owned by the given user.
*
* @param EnhancedUser $user The user which will be used to fetch as <code>EnhancedUser</code> instance.
* @param User $user The user which will be used to fetch as <code>User</code> instance.
*
* @return array<mixed> Permissions or roles permissions granted to the user.
*/
public function fetchByUser(EnhancedUser $user): array;
public function fetchByUser(User $user): array;

/**
* Fetch from user.
Expand Down
11 changes: 6 additions & 5 deletions src/Linna/Authorization/PermissionMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
*/
interface PermissionMapperInterface extends
MapperInterface,
FetchByNameInterface,
FetchByUserInterface,
FetchByRoleInterface
FetchByNameInterface
//,
//FetchByUserInterface,
//FetchByRoleInterface
{
/**
* Check if a permission exist.
Expand All @@ -33,7 +34,7 @@ interface PermissionMapperInterface extends
*
* @return bool True if the permission exists, false otherwise.
*/
public function permissionExistById(int|string $permissionId): bool;
//public function permissionExistById(int|string $permissionId): bool;

/**
* Check if a permission exist.
Expand All @@ -42,5 +43,5 @@ public function permissionExistById(int|string $permissionId): bool;
*
* @return bool True if the permission exists, false otherwise.
*/
public function permissionExistByName(string $permissionName): bool;
//public function permissionExistByName(string $permissionName): bool;
}
10 changes: 5 additions & 5 deletions src/Linna/Authorization/PermissionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
namespace Linna\Authorization;

/**
* User PermissionTrait.
* Permission Trait.
*
* <p>Use it to add the permission functionality to a class.</p>
*/
trait PermissionTrait
{
/** @var array<mixed> User permissions. */
/** @var array<mixed> Permissions. */
protected array $permission = [];

/**
* Check if a permission is granted to a user or to a role, use Permission
* Check if the class that use the trait has the permission, use Permission
* instance.
*
* @param Permission $permission The permission which will be checked as <code>Permission</code> instance.
Expand All @@ -36,7 +36,7 @@ public function can(Permission $permission): bool
}

/**
* Check if a Permission is granted to a user or to a role, use permission
* Check if the class that use the trait has the permission, use permission
* id.
*
* @param null|int|string $permissionId The permission which will be checked as permission id.
Expand All @@ -53,7 +53,7 @@ public function canById(null|int|string $permissionId): bool
}

/**
* Check if a Permission is granted to a user or to a role, use permission
* Check if the class that use the trait has the permission, use permission
* name.
*
* @param string $permissionName The permission which will be checked as permission name.
Expand Down
46 changes: 23 additions & 23 deletions src/Linna/Authorization/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class Role extends DomainObjectAbstract
{
use PermissionTrait;
//use PermissionTrait;

/**
* Constructor.
Expand All @@ -46,18 +46,18 @@ public function __construct(
public int $active = 0,

//users in role
private array $users = [],
//private array $users = [],

//permissions in role
array $permissions = [],
//array $permissions = [],

//creation datetime
?DateTimeImmutable $created = new DateTimeImmutable(),

//last updated datetime
?DateTimeImmutable $lastUpdate = new DateTimeImmutable()
) {
$this->permission = $permissions;
//$this->permission = $permissions;
}

/**
Expand All @@ -67,10 +67,10 @@ public function __construct(
*
* @return bool True if the user is in role, false otherwise.
*/
public function isUserInRole(User $user): bool
{
return $this->isUserInRoleById($user->getId());
}
//public function isUserInRole(User $user): bool
//{
// return $this->isUserInRoleById($user->getId());
//}

/**
* Check if an user is in role, use the user id.
Expand All @@ -79,14 +79,14 @@ public function isUserInRole(User $user): bool
*
* @return bool True if the user is in role, false otherwise.
*/
public function isUserInRoleById(null|int|string $userId): bool
{
if (isset($this->users[$userId])) {
return true;
}
//public function isUserInRoleById(null|int|string $userId): bool
//{
// if (isset($this->users[$userId])) {
// return true;
// }

return false;
}
// return false;
//}

/**
* Check if an user is in role, use the user name.
Expand All @@ -95,12 +95,12 @@ public function isUserInRoleById(null|int|string $userId): bool
*
* @return bool True if the user is in role, false otherwise.
*/
public function isUserInRoleByName(string $userName): bool
{
if (\in_array($userName, \array_column($this->users, 'name'), true)) {
return true;
}

return false;
}
//public function isUserInRoleByName(string $userName): bool
//{
// if (\in_array($userName, \array_column($this->users, 'name'), true)) {
// return true;
// }

// return false;
//}
}
77 changes: 77 additions & 0 deletions src/Linna/Authorization/RoleExtended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Linna Framework.
*
* @author Sebastian Rapetti <sebastian.rapetti@tim.it>
* @copyright (c) 2018, Sebastian Rapetti
* @license http://opensource.org/licenses/MIT MIT License
*/

namespace Linna\Authorization;

use DateTimeImmutable;
use Linna\Authentication\User;
use Linna\DataMapper\DomainObjectAbstract;

/**
* RoleExtended domain object.
*/
class RoleExtended extends Role
{
use PermissionTrait;
use UserTrait;

/**
* Constructor.
*
* @param string $name The name of the role.
* @param string $description The description of the role.
* @param integer $active Specify is the role is atctive.
* @param array<mixed> $users Users in role.
* @param array<mixed> $permissions Permissions granted by the role.
*/
public function __construct(
//role id
null|int|string $id = null,

/** @var string Group name. */
public string $name = '',

/** @var string Group description. */
public string $description = '',

/** @var int It say if group is active or not. */
public int $active = 0,

//users in role
array $users = [],

//permissions in role
array $permissions = [],

//creation datetime
?DateTimeImmutable $created = new DateTimeImmutable(),

//last updated datetime
?DateTimeImmutable $lastUpdate = new DateTimeImmutable()
) {
//initialize parent
parent::__construct(
id: $id,
name: $name,
description: $description,
active: $active,
created: $created,
lastUpdate: $lastUpdate
);

//from user trait
$this->user = $users;

//from permission trait
$this->permission = $permissions;
}
}
Loading

0 comments on commit 820a035

Please sign in to comment.