Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

chore(php) Move to PHP 7 #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion .State

This file was deleted.

76 changes: 18 additions & 58 deletions Acl.php → Source/Acl.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Hoa
*
Expand Down Expand Up @@ -45,9 +47,6 @@
* The ACL main class. It contains all users, groups, and services collections.
* It also proposes to check if a user is allow or not to do an action according
* to its groups and services.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class Acl
{
Expand Down Expand Up @@ -75,7 +74,7 @@ class Acl
/**
* Underlying graph.
*
* @var \Hoa\Graph
* @var ?Graph
*/
protected $_groups = null;

Expand All @@ -100,20 +99,14 @@ public function __construct()

/**
* Add a group, i.e. add a node in the underlying graph.
*
* @param \Hoa\Acl\Group $group Group to add.
* @param array $parents Parent groups (will inherit
* permissions).
* @return \Hoa\Acl\Acl
* @throws \Hoa\Acl\Exception
*/
public function addGroup(Group $group, array $parents = [])
public function addGroup(Group $group, array $parents = []): self
{
foreach ($parents as $parent) {
if (!($parent instanceof Group)) {
throw new Exception(
'Group %s must be an instance of Hoa\Acl\Group.',
1,
0,
$parent
);
}
Expand All @@ -130,21 +123,16 @@ public function addGroup(Group $group, array $parents = [])

/**
* Delete a group, i.e. delete a node in the underlying graph.
*
* @param \Hoa\Acl\Group $group Group.
* @param bool $propagate Propagate the erasure.
* @return \Hoa\Acl\Acl
* @throws \Hoa\Acl\Exception
*/
public function deleteGroup(Group $group, $propagate = self::DELETE_RESTRICT)
public function deleteGroup(Group $group, bool $propagate = self::DELETE_RESTRICT): self
{
try {
$this->getGroups()->deleteNode($group, $propagate);
} catch (Graph\Exception $e) {
throw new Exception(
'Apparently it is not possible to delete the group %s, ' .
'probably because it has at least one child.',
42,
1,
$group->getId(),
$e
);
Expand All @@ -156,58 +144,44 @@ public function deleteGroup(Group $group, $propagate = self::DELETE_RESTRICT)
/**
* Check if a group exists or not, i.e. if a node in the underlying graph
* exists.
*
* @param mixed $groupId Group ID.
* @return bool
*/
public function groupExists($groupId)
public function groupExists($groupId): bool
{
return $this->getGroups()->nodeExists($groupId);
}

/**
* Get a specific group, i.e. a specific node in the underlying graph.
*
* @param string $groupId Group ID.
* @return \Hoa\Acl\Group
* @throws \Hoa\Acl\Exception
*/
protected function getGroup($groupId)
protected function getGroup(string $groupId): Group
{
if (false === $this->groupExists($groupId)) {
throw new Exception('Group %s does not exist.', 6, $groupId);
throw new Exception('Group %s does not exist.', 2, $groupId);
}

return $this->getGroups()->getNode($groupId);
}

/**
* Get all groups, i.e. get the underlying graph.
*
* @return \Hoa\Graph
*/
protected function getGroups()
protected function getGroups(): Graph
{
return $this->_groups;
}

/**
* Attach one or more permissions to a group.
*
* @param Group $group Group.
* @param array $permissions Collection of permissions.
* @return \Hoa\Acl\Acl
* @throws \Hoa\Acl\Exception
*/
public function allow(Group $group, array $permissions = [])
public function allow(Group $group, array $permissions = []): self
{
$id = $group->getId();

if (false === $this->groupExists($id)) {
throw new Exception(
'Group %s is not declared in the current ACL instance, ' .
'cannot add permissions.',
2,
3,
$id
);
}
Expand All @@ -219,21 +193,16 @@ public function allow(Group $group, array $permissions = [])

/**
* Detach one or more permission to a group.
*
* @param Group $group Group.
* @param array $permissions Collection of permissions.
* @return \Hoa\Acl\Acl
* @throws \Hoa\Acl\Exception
*/
public function deny(Group $group, array $permissions = [])
public function deny(Group $group, array $permissions = []): self
{
$id = $group->getId();

if (false === $this->groupExists($id)) {
throw new Exception(
'Group %s is not declared in the current ACL instance, ' .
'cannot delete permissions.',
3,
4,
$id
);
}
Expand All @@ -245,20 +214,13 @@ public function deny(Group $group, array $permissions = [])

/**
* Check if a user is allowed to do something according to the permission.
*
* @param mixed $userId User ID (or instance).
* @param mixed $permissionId Permission ID (or instance).
* @param mixed $serviceId Service ID (or instance).
* @param Hoa\Acl\Assertable $assert Assert.
* @return bool
* @throws \Hoa\Acl\Exception
*/
public function isAllowed(
$userId,
$permissionId,
$serviceId = null,
Assertable $assert = null
) {
): bool {
if ($userId instanceof User) {
$userId = $userId->getId();
}
Expand Down Expand Up @@ -333,10 +295,8 @@ public function isAllowed(

/**
* Transform the groups to DOT language.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->getGroups()->__toString();
}
Expand All @@ -345,4 +305,4 @@ public function __toString()
/**
* Flex entity.
*/
Consistency::flexEntity('Hoa\Acl\Acl');
Consistency::flexEntity(Acl::class);
12 changes: 3 additions & 9 deletions Assertable.php → Source/Assertable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Hoa
*
Expand Down Expand Up @@ -40,21 +42,13 @@
* Interface \Hoa\Acl\Assertable.
*
* Force to implement the assert method.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
interface Assertable
{
/**
* Write an assert.
* Must return a boolean, because the comparison will be strict (using
* ===).
*
* @param string $userId User ID.
* @param string $permissionId Permission ID.
* @param string $serviceId Service ID (can be null).
* @return bool
*/
public function assert($userId, $permissionId, $serviceId);
public function assert(string $userId, string $permissionId, string $serviceId): bool;
}
5 changes: 2 additions & 3 deletions Exception.php → Source/Exception.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Hoa
*
Expand Down Expand Up @@ -42,9 +44,6 @@
* Class \Hoa\Acl\Exception.
*
* Extending the \Hoa\Exception\Exception class.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class Exception extends HoaException
{
Expand Down
Loading