diff --git a/src/Linna/Authorization/FetchByPermissionInterface.php b/src/Linna/Authorization/FetchByPermissionInterface.php index 47165bf..9bf8faa 100644 --- a/src/Linna/Authorization/FetchByPermissionInterface.php +++ b/src/Linna/Authorization/FetchByPermissionInterface.php @@ -25,7 +25,7 @@ interface FetchByPermissionInterface * Fetch from permission. * *

From a Permission instance as argument, this method must return an array - * containing a EnhancedUser|Role instance for every EnhancedUser|Role + * containing a User|Role instance for every User|Role * that have the given permission.

* * @param Permission $permission The permission which will be used to fetch as Permission instance. @@ -38,7 +38,7 @@ public function fetchByPermission(Permission $permission): array; * Fetch from permission. * *

From a permission id as argument, this method must return an array - * containing a EnhancedUser|Role instance for every EnhancedUser|Role + * containing a User|Role instance for every User|Role * that have the given permission.

* * @param int|string $permissionId The permission which will be used to fetch as permission id. @@ -51,7 +51,7 @@ public function fetchByPermissionId(int|string $permissionId): array; * Fetch from permission. * *

From a permission name as argument, this method must return an array - * containing a EnhancedUser|Role instance for every EnhancedUser|Role + * containing a User|Role instance for every User|Role * that have the given permission.

* * @param string $permissionName The permission which will be used to fetch as permission name. diff --git a/src/Linna/Authorization/FetchByRoleInterface.php b/src/Linna/Authorization/FetchByRoleInterface.php index b624cda..a3fc118 100644 --- a/src/Linna/Authorization/FetchByRoleInterface.php +++ b/src/Linna/Authorization/FetchByRoleInterface.php @@ -25,8 +25,8 @@ interface FetchByRoleInterface * Fetch from role. * *

From a Role instance as argument, this method must return an array - * containing a EnhancedUser|Permission instance for every - * EnhancedUser|Permission that belongs at the given role.

+ * containing a User|Permission instance for every + * User|Permission that belongs at the given role.

* * @param Role $role The role which will be used to fetch as Role instance. * @@ -38,7 +38,7 @@ public function fetchByRole(Role $role): array; * Fetch from role. * *

From a role id as argument, this method must return an array containing - * a EnhancedUser|Permission instance for every EnhancedUser|Permission + * a User|Permission instance for every User|Permission * that belongs at the given role.

* * @param int|string $roleId The role which will be used to fetch as role id. @@ -51,7 +51,7 @@ public function fetchByRoleId(int|string $roleId): array; * Fetch from role. * *

From a role name as argument, this method must return an array containing - * a EnhancedUser|Permission instance for every EnhancedUser|Permission + * a User|Permission instance for every User|Permission * that belongs at the given role.

* * @param string $roleName The role which will be used to fetch as role name. diff --git a/src/Linna/Authorization/FetchByUserInterface.php b/src/Linna/Authorization/FetchByUserInterface.php index 36d5f45..1370d0c 100644 --- a/src/Linna/Authorization/FetchByUserInterface.php +++ b/src/Linna/Authorization/FetchByUserInterface.php @@ -12,7 +12,7 @@ namespace Linna\Authorization; -use Linna\Authorization\EnhancedUser; +use Linna\Authorization\User; /** * Fetch By User Interface. @@ -24,15 +24,15 @@ interface FetchByUserInterface /** * Fetch from user. * - * From EnhancedUser instance as argument, this method must return an + * From User instance as argument, this method must return an * array containing a Permission|Role object instance for every * Permission|Role owned by the given user. * - * @param EnhancedUser $user The user which will be used to fetch as EnhancedUser instance. + * @param User $user The user which will be used to fetch as User instance. * * @return array Permissions or roles permissions granted to the user. */ - public function fetchByUser(EnhancedUser $user): array; + public function fetchByUser(User $user): array; /** * Fetch from user. diff --git a/src/Linna/Authorization/PermissionMapperInterface.php b/src/Linna/Authorization/PermissionMapperInterface.php index e9f5f36..4a8dc30 100644 --- a/src/Linna/Authorization/PermissionMapperInterface.php +++ b/src/Linna/Authorization/PermissionMapperInterface.php @@ -22,9 +22,10 @@ */ interface PermissionMapperInterface extends MapperInterface, - FetchByNameInterface, - FetchByUserInterface, - FetchByRoleInterface + FetchByNameInterface + //, + //FetchByUserInterface, + //FetchByRoleInterface { /** * Check if a permission exist. @@ -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. @@ -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; } diff --git a/src/Linna/Authorization/PermissionTrait.php b/src/Linna/Authorization/PermissionTrait.php index 1e1e2d7..7fca0bd 100644 --- a/src/Linna/Authorization/PermissionTrait.php +++ b/src/Linna/Authorization/PermissionTrait.php @@ -13,17 +13,17 @@ namespace Linna\Authorization; /** - * User PermissionTrait. + * Permission Trait. * *

Use it to add the permission functionality to a class.

*/ trait PermissionTrait { - /** @var array User permissions. */ + /** @var array 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 Permission instance. @@ -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. @@ -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. diff --git a/src/Linna/Authorization/Role.php b/src/Linna/Authorization/Role.php index df057df..02b6402 100644 --- a/src/Linna/Authorization/Role.php +++ b/src/Linna/Authorization/Role.php @@ -21,7 +21,7 @@ */ class Role extends DomainObjectAbstract { - use PermissionTrait; + //use PermissionTrait; /** * Constructor. @@ -46,10 +46,10 @@ 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(), @@ -57,7 +57,7 @@ public function __construct( //last updated datetime ?DateTimeImmutable $lastUpdate = new DateTimeImmutable() ) { - $this->permission = $permissions; + //$this->permission = $permissions; } /** @@ -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. @@ -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. @@ -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; + //} } diff --git a/src/Linna/Authorization/RoleExtended.php b/src/Linna/Authorization/RoleExtended.php new file mode 100644 index 0000000..3eb4293 --- /dev/null +++ b/src/Linna/Authorization/RoleExtended.php @@ -0,0 +1,77 @@ + + * @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 $users Users in role. + * @param array $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; + } +} diff --git a/src/Linna/Authorization/RoleExtendedMapperInterface.php b/src/Linna/Authorization/RoleExtendedMapperInterface.php new file mode 100644 index 0000000..79f20d1 --- /dev/null +++ b/src/Linna/Authorization/RoleExtendedMapperInterface.php @@ -0,0 +1,231 @@ + + * @copyright (c) 2018, Sebastian Rapetti + * @license http://opensource.org/licenses/MIT MIT License + */ + +namespace Linna\Authorization; + +use Linna\Authorization\User; +use Linna\Authorization\Permission; +use Linna\Authorization\RoleExtendedExtended; +use Linna\DataMapper\MapperInterface; +use Linna\DataMapper\FetchByNameInterface; + +/** + * Role Extended Mapper Interface. + * + * Contain methods required from concrete RoleExtended Mapper. + */ +interface RoleExtendedMapperInterface extends MapperInterface, FetchByPermissionInterface, FetchByNameInterface, FetchByUserInterface +{ + /** + * Grant a permission at role. + * + *

This method must insert new role-permission coupling in persistent + * storage and update RoleExtended calling RoleExtended->setPermissions() method.

+ * + *

Remind to pass all RoleExtended's permission to RoleExtended->setPermissions(), + * when write concrete mapper is well to pass PermissionMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param Permission $permission The permission to add as Permission instance. + * + * @return void + */ + public function grantPermission(RoleExtended &$role, Permission $permission); + + /** + * Grant a permission at role. + * + *

This method must insert new role-permission coupling in persistent + * storage and update RoleExtended calling RoleExtended->setPermissions() method.

+ * + *

Remind to pass all RoleExtended's permission to RoleExtended->setPermissions(), + * when write concrete mapper is well to pass PermissionMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param int|string $permissionId The permission to add as permission id. + * + * @return void + */ + public function grantPermissionById(RoleExtended &$role, int|string $permissionId); + + /** + * Grant a permission at role. + * + *

This method must insert new role-permission coupling in persistent + * storage and update RoleExtended calling RoleExtended->setPermissions() method.

+ * + *

Remind to pass all RoleExtended's permission to RoleExtended->setPermissions(), + * when write concrete mapper is well to pass PermissionMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param string $permissionName The permission to add as permission name. + * + * @return void + */ + public function grantPermissionByName(RoleExtended &$role, string $permissionName); + + /** + * Revoke a permission at role. + * + *

This method must remove role-permission coupling in persistent + * storage and update RoleExtended calling RoleExtended->setPermissions() method.

+ * + *

As previous method remind to pass all RoleExtended's permission + * to RoleExtended->setPermissions(), when write concrete mapper is well to + * pass PermissionMapper as constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param Permission $permission The permission to revoke as Permission instance. + * + * @return void + */ + public function revokePermission(RoleExtended &$role, Permission $permission); + + /** + * Revoke a permission at role. + * + *

This method must remove role-permission coupling in persistent + * storage and update RoleExtended calling RoleExtended->setPermissions() method.

+ * + *

As previous method remind to pass all RoleExtended's permission + * to RoleExtended->setPermissions(), when write concrete mapper is well to + * pass PermissionMapper as constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param int|string $permissionId The permission to revoke as permission id. + * + * @return void + */ + public function revokePermissionById(RoleExtended &$role, int|string $permissionId); + + /** + * Revoke a permission at role. + * + *

This method must remove role-permission coupling in persistent + * storage and update RoleExtended calling RoleExtended->setPermissions() method.

+ * + *

As previous method remind to pass all RoleExtended's permission + * to RoleExtended->setPermissions(), when write concrete mapper is well to + * pass PermissionMapper as constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param string $permissionName The permission to revoke as permission name. + * + * @return void + */ + public function revokePermissionByName(RoleExtended &$role, string $permissionName); + + /** + * Add an user to a role. + * + *

This method must insert new user-role coupling in persistent + * storage and update RoleExtended calling RoleExtended->setUsers() method.

+ * + *

Remind to pass all RoleExtended's users to RoleExtended->setUsers(), + * when write concrete mapper is well to pass UserMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param User $user The user to add as User instance. + * + * @return void + */ + public function addUser(RoleExtended &$role, User $user); + + /** + * Add an user to a role. + * + *

This method must insert new user-role coupling in persistent + * storage and update RoleExtended calling RoleExtended->setUsers() method.

+ * + *

Remind to pass all RoleExtended's users to RoleExtended->setUsers(), + * when write concrete mapper is well to pass UserMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param int|string $userId The user to add as user id. + * + * @return void + */ + public function addUserById(RoleExtended &$role, int|string $userId); + + /** + * Add an user to a role. + * + *

This method must insert new user-role coupling in persistent + * storage and update RoleExtended calling RoleExtended->setUsers() method.

+ * + *

Remind to pass all RoleExtended's users to RoleExtended->setUsers(), + * when write concrete mapper is well to pass UserMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param string $userName The user to add as user name. + * + * @return void + */ + public function addUserByName(RoleExtended &$role, string $userName); + + /** + * Remove user from a role. + * + *

This method must remove user-role coupling in persistent + * storage and update RoleExtended calling RoleExtended->setUsers() method.

+ * + *

As previous method remind to pass all RoleExtended's users to RoleExtended->setUsers(), + * when write concrete mapper is well to pass UserMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param User $user The user to revoke as User instance. + * + * @return void + */ + public function removeUser(RoleExtended &$role, User $user); + + /** + * Remove user from a role. + * + *

This method must remove user-role coupling in persistent + * storage and update RoleExtended calling RoleExtended->setUsers() method.

+ * + *

As previous method remind to pass all RoleExtended's users to RoleExtended->setUsers(), + * when write concrete mapper is well to pass UserMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param int|string $userId The user to revoke as user id. + * + * @return void + */ + public function removeUserById(RoleExtended &$role, int|string $userId); + + /** + * Remove user from a role. + * + *

This method must remove user-role coupling in persistent + * storage and update RoleExtended calling RoleExtended->setUsers() method.

+ * + *

As previous method remind to pass all RoleExtended's users to RoleExtended->setUsers(), + * when write concrete mapper is well to pass UserMapper as + * constructor dependency.

+ * + * @param RoleExtended $role The RoleExtended class instance. + * @param string $userName The user to revoke as user name. + * + * @return void + */ + public function removeUserByName(RoleExtended &$role, string $userName); +} diff --git a/src/Linna/Authorization/RoleMapperInterface.php b/src/Linna/Authorization/RoleMapperInterface.php deleted file mode 100644 index d6ee4bc..0000000 --- a/src/Linna/Authorization/RoleMapperInterface.php +++ /dev/null @@ -1,231 +0,0 @@ - - * @copyright (c) 2018, Sebastian Rapetti - * @license http://opensource.org/licenses/MIT MIT License - */ - -namespace Linna\Authorization; - -use Linna\Authorization\EnhancedUser; -use Linna\Authorization\Permission; -use Linna\Authorization\Role; -use Linna\DataMapper\MapperInterface; -use Linna\DataMapper\FetchByNameInterface; - -/** - * Group Mapper Interface. - * - * Contain methods required from concrete Role Mapper. - */ -interface RoleMapperInterface extends MapperInterface, FetchByPermissionInterface, FetchByNameInterface, FetchByUserInterface -{ - /** - * Grant a permission at role. - * - *

This method must insert new role-permission coupling in persistent - * storage and update Role calling Role->setPermissions() method.

- * - *

Remind to pass all role's permission to Role->setPermissions(), - * when write concrete mapper is well to pass PermissionMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param Permission $permission The permission to add as Permission instance. - * - * @return void - */ - public function grantPermission(Role &$role, Permission $permission); - - /** - * Grant a permission at role. - * - *

This method must insert new role-permission coupling in persistent - * storage and update Role calling Role->setPermissions() method.

- * - *

Remind to pass all role's permission to Role->setPermissions(), - * when write concrete mapper is well to pass PermissionMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param int|string $permissionId The permission to add as permission id. - * - * @return void - */ - public function grantPermissionById(Role &$role, int|string $permissionId); - - /** - * Grant a permission at role. - * - *

This method must insert new role-permission coupling in persistent - * storage and update Role calling Role->setPermissions() method.

- * - *

Remind to pass all role's permission to Role->setPermissions(), - * when write concrete mapper is well to pass PermissionMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param string $permissionName The permission to add as permission name. - * - * @return void - */ - public function grantPermissionByName(Role &$role, string $permissionName); - - /** - * Revoke a permission at role. - * - *

This method must remove role-permission coupling in persistent - * storage and update Role calling Role->setPermissions() method.

- * - *

As previous method remind to pass all role's permission - * to Role->setPermissions(), when write concrete mapper is well to - * pass PermissionMapper as constructor dependency.

- * - * @param Role $role The Role class instance. - * @param Permission $permission The permission to revoke as Permission instance. - * - * @return void - */ - public function revokePermission(Role &$role, Permission $permission); - - /** - * Revoke a permission at role. - * - *

This method must remove role-permission coupling in persistent - * storage and update Role calling Role->setPermissions() method.

- * - *

As previous method remind to pass all role's permission - * to Role->setPermissions(), when write concrete mapper is well to - * pass PermissionMapper as constructor dependency.

- * - * @param Role $role The Role class instance. - * @param int|string $permissionId The permission to revoke as permission id. - * - * @return void - */ - public function revokePermissionById(Role &$role, int|string $permissionId); - - /** - * Revoke a permission at role. - * - *

This method must remove role-permission coupling in persistent - * storage and update Role calling Role->setPermissions() method.

- * - *

As previous method remind to pass all role's permission - * to Role->setPermissions(), when write concrete mapper is well to - * pass PermissionMapper as constructor dependency.

- * - * @param Role $role The Role class instance. - * @param string $permissionName The permission to revoke as permission name. - * - * @return void - */ - public function revokePermissionByName(Role &$role, string $permissionName); - - /** - * Add an user to a role. - * - *

This method must insert new user-role coupling in persistent - * storage and update Role calling Role->setUsers() method.

- * - *

Remind to pass all role's users to Role->setUsers(), - * when write concrete mapper is well to pass EnhancedUserMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param EnhancedUser $user The user to add as EnhancedUser instance. - * - * @return void - */ - public function addUser(Role &$role, EnhancedUser $user); - - /** - * Add an user to a role. - * - *

This method must insert new user-role coupling in persistent - * storage and update Role calling Role->setUsers() method.

- * - *

Remind to pass all role's users to Role->setUsers(), - * when write concrete mapper is well to pass EnhancedUserMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param int|string $userId The user to add as user id. - * - * @return void - */ - public function addUserById(Role &$role, int|string $userId); - - /** - * Add an user to a role. - * - *

This method must insert new user-role coupling in persistent - * storage and update Role calling Role->setUsers() method.

- * - *

Remind to pass all role's users to Role->setUsers(), - * when write concrete mapper is well to pass EnhancedUserMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param string $userName The user to add as user name. - * - * @return void - */ - public function addUserByName(Role &$role, string $userName); - - /** - * Remove user from a role. - * - *

This method must remove user-role coupling in persistent - * storage and update Role calling Role->setUsers() method.

- * - *

As previous method remind to pass all role's users to Role->setUsers(), - * when write concrete mapper is well to pass EnhancedUserMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param EnhancedUser $user The user to revoke as EnhancedUser instance. - * - * @return void - */ - public function removeUser(Role &$role, EnhancedUser $user); - - /** - * Remove user from a role. - * - *

This method must remove user-role coupling in persistent - * storage and update Role calling Role->setUsers() method.

- * - *

As previous method remind to pass all role's users to Role->setUsers(), - * when write concrete mapper is well to pass EnhancedUserMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param int|string $userId The user to revoke as user id. - * - * @return void - */ - public function removeUserById(Role &$role, int|string $userId); - - /** - * Remove user from a role. - * - *

This method must remove user-role coupling in persistent - * storage and update Role calling Role->setUsers() method.

- * - *

As previous method remind to pass all role's users to Role->setUsers(), - * when write concrete mapper is well to pass EnhancedUserMapper as - * constructor dependency.

- * - * @param Role $role The Role class instance. - * @param string $userName The user to revoke as user name. - * - * @return void - */ - public function removeUserByName(Role &$role, string $userName); -} diff --git a/src/Linna/Authorization/RoleToUserMapperInterface.php b/src/Linna/Authorization/RoleToUserMapperInterface.php deleted file mode 100644 index 19027c6..0000000 --- a/src/Linna/Authorization/RoleToUserMapperInterface.php +++ /dev/null @@ -1,27 +0,0 @@ - - * @copyright (c) 2018, Sebastian Rapetti - * @license http://opensource.org/licenses/MIT MIT License - */ - -namespace Linna\Authorization; - -/** - * Role To User Mapper Interface. - * - *

Contain methods required for concrete Role and EnhancedUser mappers to avoid - * recursion when fetch objects.

- * - *

Using Role and EnhancedUser mappers without this third mapper will be impossible - * because a Role mapper require an EnhancedUser mapper to create an instance - * and vice versa.

- */ -interface RoleToUserMapperInterface extends FetchByRoleInterface, FetchByUserInterface -{ -} diff --git a/src/Linna/Authorization/RoleTrait.php b/src/Linna/Authorization/RoleTrait.php new file mode 100644 index 0000000..ef49f49 --- /dev/null +++ b/src/Linna/Authorization/RoleTrait.php @@ -0,0 +1,68 @@ + + * @copyright (c) 2018, Sebastian Rapetti + * @license http://opensource.org/licenses/MIT MIT License + */ + +namespace Linna\Authorization; + +/** + * Role Trait. + * + *

Use it to add the role functionality to a class.

+ */ +trait RoleTrait +{ + /** @var array Roles. */ + protected array $role = []; + + /** + * Check if the class that use the trait has a role, use the Role instance. + * + * @param Role $role The role as Role object which will be checked. + * + * @return bool True if has the role, false otherwise. + */ + public function hasRole(Role $role): bool + { + return $this->hasRoleById($role->getId()); + } + + /** + * Check if the class that use the trait has a role, use the role id. + * + * @param null|int|string $roleId The role as role id or uuid which will be checked. + * + * @return bool True if has the role, false otherwise. + */ + public function hasRoleById(null|int|string $roleId): bool + { + if (isset($this->role[$roleId])) { + return true; + } + + return false; + } + + /** + * Check if the class that use the trait has a role, use the role name. + * + * @param string $roleName The role as role name which will be checked. + * + * @return bool True if has the role, false otherwise. + */ + public function hasRoleByName(string $roleName): bool + { + if (\in_array($roleName, \array_column($this->role, 'name'), true)) { + return true; + } + + return false; + } +} diff --git a/src/Linna/Authorization/EnhancedUser.php b/src/Linna/Authorization/UserExtended.php similarity index 69% rename from src/Linna/Authorization/EnhancedUser.php rename to src/Linna/Authorization/UserExtended.php index 193fbe9..785242b 100644 --- a/src/Linna/Authorization/EnhancedUser.php +++ b/src/Linna/Authorization/UserExtended.php @@ -17,11 +17,12 @@ use Linna\Authentication\User; /** - * Enhanched User, an user with permissions. + * User Extended, an user with permissions. */ -class EnhancedUser extends User +class UserExtended extends User { use PermissionTrait; + use RoleTrait; /** * Class Constructor. @@ -71,7 +72,7 @@ public function __construct( ?DateTimeImmutable $lastUpdate = new DateTimeImmutable(), //roles granted to the user - private array $roles = [], + array $roles = [], //The permissions granted to the user array $permissions = [] @@ -89,51 +90,10 @@ public function __construct( lastUpdate: $lastUpdate ); + //from role trait + $this->role = $roles; + //from permission trait $this->permission = $permissions; } - - /** - * Check if an user has a role, use Role instance. - * - * @param Role $role The role as Role object which will be checked. - * - * @return bool True if the user has the role, false otherwise. - */ - public function hasRole(Role $role): bool - { - return $this->hasRoleById($role->getId()); - } - - /** - * Check if an user has a role, use role Id. - * - * @param null|int|string $roleId The role as role id or uuid which will be checked. - * - * @return bool True if the user has the role, false otherwise. - */ - public function hasRoleById(null|int|string $roleId): bool - { - if (isset($this->roles[$roleId])) { - return true; - } - - return false; - } - - /** - * Check if an user has a role, use role name. - * - * @param string $roleName The role as role name which will be checked. - * - * @return bool True if the user has the role, false otherwise. - */ - public function hasRoleByName(string $roleName): bool - { - if (\in_array($roleName, \array_column($this->roles, 'name'), true)) { - return true; - } - - return false; - } } diff --git a/src/Linna/Authorization/EnhancedUserMapperInterface.php b/src/Linna/Authorization/UserExtendedMapperInterface.php similarity index 56% rename from src/Linna/Authorization/EnhancedUserMapperInterface.php rename to src/Linna/Authorization/UserExtendedMapperInterface.php index 52aa3c8..5e0266d 100644 --- a/src/Linna/Authorization/EnhancedUserMapperInterface.php +++ b/src/Linna/Authorization/UserExtendedMapperInterface.php @@ -12,7 +12,7 @@ namespace Linna\Authorization; -use Linna\Authorization\EnhancedUser; +use Linna\Authorization\UserExtended; use Linna\Authorization\Permission; use Linna\Authorization\Role; use Linna\DataMapper\MapperInterface; @@ -22,220 +22,220 @@ * * Contains methods required from concrete User Mapper. */ -interface EnhancedUserMapperInterface extends MapperInterface, FetchByPermissionInterface, FetchByRoleInterface +interface UserExtendedMapperInterface extends MapperInterface, FetchByPermissionInterface, FetchByRoleInterface { /** * Grant a permission to an user. * *

This method must insert new user-permission coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setPermissions() + * storage and update UserExtended calling UserExtended->setPermissions() * method.

* - *

Remind to pass all user's permission to EnhancedUser->setPermissions(), + *

Remind to pass all user's permission to UserExtended->setPermissions(), * when write concrete mapper is well to pass PermissionMapper as * constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param Permission $permission The permission to add as Permission class instance. * * @return void */ - public function grantPermission(EnhancedUser &$user, Permission $permission); + public function grantPermission(UserExtended &$user, Permission $permission); /** * Grant a permission to an user. * *

This method must insert new user-permission coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setPermissions() + * storage and update UserExtended calling UserExtended->setPermissions() * method.

* - *

Remind to pass all user's permission to EnhancedUser->setPermissions(), + *

Remind to pass all user's permission to UserExtended->setPermissions(), * when write concrete mapper is well to pass PermissionMapper as * constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param int|string $permissionId The permission to add as persmission id. * * @return void */ - public function grantPermissionById(EnhancedUser &$user, int|string $permissionId); + public function grantPermissionById(UserExtended &$user, int|string $permissionId); /** * Grant a permission to an user. * *

This method must insert new user-permission coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setPermissions() + * storage and update UserExtended calling UserExtended->setPermissions() * method.

* - *

Remind to pass all user's permission to EnhancedUser->setPermissions(), + *

Remind to pass all user's permission to UserExtended->setPermissions(), * when write concrete mapper is well to pass PermissionMapper as * constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param string $permissionName The permission to add as persmission name. * * @return void */ - public function grantPermissionByName(EnhancedUser &$user, string $permissionName); + public function grantPermissionByName(UserExtended &$user, string $permissionName); /** * Revoke a permission to an user. * *

This method must remove user-permission coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setPermissions() + * storage and update UserExtended calling UserExtended->setPermissions() * method.

* *

As previous method remind to pass all user's permission to - * EnhancedUser->setPermissions(), when write concrete mapper is well to + * UserExtended->setPermissions(), when write concrete mapper is well to * pass PermissionMapper as constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param Permission $permission The permission to revoke as Permission class instance.. * * @return void */ - public function revokePermission(EnhancedUser &$user, Permission $permission); + public function revokePermission(UserExtended &$user, Permission $permission); /** * Revoke a permission to an user. * *

This method must remove user-permission coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setPermissions() + * storage and update UserExtended calling UserExtended->setPermissions() * method.

* *

As previous method remind to pass all user's permission to - * EnhancedUser->setPermissions(), when write concrete mapper is well to + * UserExtended->setPermissions(), when write concrete mapper is well to * pass PermissionMapper as constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param int|string $permissionId The permission to revoke as persmission id. * * @return void */ - public function revokePermissionById(EnhancedUser &$user, int|string $permissionId); + public function revokePermissionById(UserExtended &$user, int|string $permissionId); /** * Revoke a permission to an user. * *

This method must remove user-permission coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setPermissions() + * storage and update UserExtended calling UserExtended->setPermissions() * method.

* *

As previous method remind to pass all user's permission to - * EnhancedUser->setPermissions(), when write concrete mapper is well to + * UserExtended->setPermissions(), when write concrete mapper is well to * pass PermissionMapper as constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param string $permissionName The permission to revoke as permission name. * * @return void */ - public function revokePermissionByName(EnhancedUser &$user, string $permissionName); + public function revokePermissionByName(UserExtended &$user, string $permissionName); /** * Add an user to a role. * *

This method must insert new user-role coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setRoles() + * storage and update UserExtended calling UserExtended->setRoles() * method.

* - *

Remind to pass all role's users to EnhancedUser->setRoles(), when write - * concrete mapper is well to pass EnhancedUserMapper as constructor + *

Remind to pass all role's users to UserExtended->setRoles(), when write + * concrete mapper is well to pass UserExtendedMapper as constructor * dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param Role $role The role to add as Role class instance. * * @return void */ - public function addRole(EnhancedUser &$user, Role $role); + public function addRole(UserExtended &$user, Role $role); /** * Add an user to a role. * *

This method must insert new user-role coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setRoles() + * storage and update UserExtended calling UserExtended->setRoles() * method.

* - *

Remind to pass all role's users to EnhancedUser->setRoles(), when write - * concrete mapper is well to pass EnhancedUserMapper as constructor + *

Remind to pass all role's users to UserExtended->setRoles(), when write + * concrete mapper is well to pass UserExtendedMapper as constructor * dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param int|string $roleId The role to add as role id. * * @return void */ - public function addRoleById(EnhancedUser &$user, int|string $roleId); + public function addRoleById(UserExtended &$user, int|string $roleId); /** * Add an user to a role. * *

This method must insert new user-role coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setRoles() + * storage and update UserExtended calling UserExtended->setRoles() * method.

* - *

Remind to pass all role's users to EnhancedUser->setRoles(), when write - * concrete mapper is well to pass EnhancedUserMapper as constructor + *

Remind to pass all role's users to UserExtended->setRoles(), when write + * concrete mapper is well to pass UserExtendedMapper as constructor * dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param string $roleName The role to add as role name. * * @return void */ - public function addRoleByName(EnhancedUser &$user, string $roleName); + public function addRoleByName(UserExtended &$user, string $roleName); /** * Remove an user from a role. * *

This method must insert new user-role coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setRoles() + * storage and update UserExtended calling UserExtended->setRoles() * method.

* - *

Remind to pass all role's users to EnhancedUser->setRoles(), - * when write concrete mapper is well to pass EnhancedUserMapper as + *

Remind to pass all role's users to UserExtended->setRoles(), + * when write concrete mapper is well to pass UserExtendedMapper as * constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param Role $role The role to revoke as Role class instance. * * @return void */ - public function removeRole(EnhancedUser &$user, Role $role); + public function removeRole(UserExtended &$user, Role $role); /** * Remove an user from a role. * *

This method must insert new user-role coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setRoles() method.

+ * storage and update UserExtended calling UserExtended->setRoles() method.

* - *

Remind to pass all role's users to EnhancedUser->setRoles(), - * when write concrete mapper is well to pass EnhancedUserMapper as + *

Remind to pass all role's users to UserExtended->setRoles(), + * when write concrete mapper is well to pass UserExtendedMapper as * constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param int|string $roleId The role to revoke as role id. * * @return void */ - public function removeRoleById(EnhancedUser &$user, int|string $roleId); + public function removeRoleById(UserExtended &$user, int|string $roleId); /** * Remove an user from a role. * *

This method must insert new user-role coupling in persistent - * storage and update EnhancedUser calling EnhancedUser->setRoles() + * storage and update UserExtended calling UserExtended->setRoles() * method.

* - *

Remind to pass all role's users to EnhancedUser->setRoles(), - * when write concrete mapper is well to pass EnhancedUserMapper as + *

Remind to pass all role's users to UserExtended->setRoles(), + * when write concrete mapper is well to pass UserExtendedMapper as * constructor dependency.

* - * @param EnhancedUser $user The EnhancedUser user class instance. + * @param UserExtended $user The UserExtended user class instance. * @param string $roleName The role to revoke as role name. * * @return void */ - public function removeRoleByName(EnhancedUser &$user, string $roleName); + public function removeRoleByName(UserExtended &$user, string $roleName); } diff --git a/src/Linna/Authorization/UserTrait.php b/src/Linna/Authorization/UserTrait.php new file mode 100644 index 0000000..f012133 --- /dev/null +++ b/src/Linna/Authorization/UserTrait.php @@ -0,0 +1,68 @@ + + * @copyright (c) 2018, Sebastian Rapetti + * @license http://opensource.org/licenses/MIT MIT License + */ + +namespace Linna\Authorization; + +/** + * User Trait. + * + *

Use it to add the user functionality to a class.

+ */ +trait UserTrait +{ + /** @var array Users. */ + protected array $user = []; + + /** + * Check if the class that use the trait has a user, use the User instance. + * + * @param User $user The user as User object which will be checked. + * + * @return bool True if has the user, false otherwise. + */ + public function hasUser(User $user): bool + { + return $this->hasUserById($user->getId()); + } + + /** + * Check if the class that use the trait has a user, use the user id. + * + * @param null|int|string $userId The user as user id or uuid which will be checked. + * + * @return bool True if has the user, false otherwise. + */ + public function hasUserById(null|int|string $userId): bool + { + if (isset($this->user[$userId])) { + return true; + } + + return false; + } + + /** + * Check if the class that use the trait has a user, use the user name. + * + * @param string $userName The user as user name which will be checked. + * + * @return bool True if has the user, false otherwise. + */ + public function hasUserByName(string $userName): bool + { + if (\in_array($userName, \array_column($this->user, 'name'), true)) { + return true; + } + + return false; + } +}