Skip to content

Commit

Permalink
removed deprecations and permissionhandler interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Dogan Ucar committed Aug 28, 2019
1 parent 80ef412 commit 28279d6
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 25 deletions.
37 changes: 37 additions & 0 deletions src/Common/IPermissionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace doganoo\SimpleRBAC\Common;

/**
* Interface IPermissionHandler
* @package doganoo\SimpleRBAC\Common
*/
interface IPermissionHandler {
public function __construct(IDataProvider $dataProvider);
public function hasPermission(IPermission $permission): bool;
public function hasRole(IRole $role): bool;
}
5 changes: 2 additions & 3 deletions src/Common/IRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@

namespace doganoo\SimpleRBAC\Common;


use doganoo\PHPAlgorithms\Common\Interfaces\Comparable;
use doganoo\PHPAlgorithms\Common\Interfaces\IComparable;
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;

/**
* Interface IRole
*
* @package doganoo\SimpleRBAC\Common
*/
interface IRole extends Comparable {
interface IRole extends IComparable {
/**
* @return string
*/
Expand Down
17 changes: 9 additions & 8 deletions src/Handler/PermissionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
namespace doganoo\SimpleRBAC\Handler;

use doganoo\PHPAlgorithms\Algorithm\Traversal\PreOrder;
use doganoo\PHPAlgorithms\Common\Exception\InvalidBitLengthException;
use doganoo\PHPAlgorithms\Common\Exception\InvalidSearchComparisionException;
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;
use doganoo\PHPAlgorithms\Datastructure\Maps\IntegerVector;
use doganoo\PHPAlgorithms\Datastructure\Vector\BitVector\IntegerVector;
use doganoo\SimpleRBAC\Common\IDataProvider;
use doganoo\SimpleRBAC\Common\IPermission;
use doganoo\SimpleRBAC\Common\IPermissionHandler;
use doganoo\SimpleRBAC\Common\IRole;
use doganoo\SimpleRBAC\Common\IUser;

Expand All @@ -35,7 +38,7 @@
*
* @package doganoo\SimpleRBAC\Handler
*/
class PermissionHandler {
class PermissionHandler implements IPermissionHandler {
/** @var IDataProvider $dataProvider */
private $dataProvider = null;
/** @var IntegerVector|null $permissionVector */
Expand All @@ -50,7 +53,7 @@ class PermissionHandler {
*
* @param IDataProvider $dataProvider
*
* @throws \doganoo\PHPAlgorithms\Common\Exception\InvalidBitLengthException
* @throws InvalidBitLengthException
*/
public function __construct(IDataProvider $dataProvider) {
$this->dataProvider = $dataProvider;
Expand All @@ -66,7 +69,7 @@ public function __construct(IDataProvider $dataProvider) {
* @param IPermission $permission
*
* @return bool
* @throws \doganoo\PHPAlgorithms\Common\Exception\InvalidSearchComparisionException
* @throws InvalidSearchComparisionException
*/
public function hasPermission(IPermission $permission): bool {
//notice that there is no null check necessary since the
Expand Down Expand Up @@ -106,7 +109,7 @@ function ($userRoleId) use ($permission, &$found) {
* @param IPermission $permission
*
* @return bool
* @throws \doganoo\PHPAlgorithms\Common\Exception\InvalidSearchComparisionException
* @throws InvalidSearchComparisionException
*/
private function isDefaultPermission(?IPermission $permission): bool {
if ($this->defaultPermissionVector->get($permission->getId())) return true;
Expand All @@ -133,10 +136,8 @@ private function isOwner(?IUser $currentUser, ?IUser $permissionUser): bool {
/**
* @param IRole $role
*
* TODO globally enabling / disabling this feature?!
*
* @return bool
* @throws \doganoo\PHPAlgorithms\Common\Exception\InvalidSearchComparisionException
* @throws InvalidSearchComparisionException
*/
public function hasRole(IRole $role): bool {
if ($this->roleVector->get($role->getId())) return true;
Expand Down
13 changes: 5 additions & 8 deletions test/DataProvider/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@
*
* @package DataProvider
*/
class DataProvider implements IDataProvider
{
class DataProvider implements IDataProvider {

/**
* the user whose permissions should be validated
*
* @return IUser
*/
public function getUser(): ?IUser
{
public function getUser(): ?IUser {
$user = new User();
$user->setId(1);

Expand All @@ -69,8 +67,7 @@ public function getUser(): ?IUser
* @param int $id
* @return Permission|null
*/
public function getPermission(int $id): ?IPermission
{
public function getPermission(int $id): ?IPermission {
if (in_array($id, [1, 8, 75, 19])) {
$permission = new Permission();
$permission->setId($id);
Expand All @@ -91,13 +88,13 @@ public function getPermission(int $id): ?IPermission
*
* @return null|BinarySearchTree
*/
public function getDefaultPermissions(): ?BinarySearchTree
{
public function getDefaultPermissions(): ?BinarySearchTree {
$tree = new BinarySearchTree();
$tree->insertValue(PermissionUtil::toPermission(101));
$tree->insertValue(PermissionUtil::toPermission(102));
$tree->insertValue(PermissionUtil::toPermission(103));
$tree->insertValue(PermissionUtil::toPermission(104));
return $tree;
}

}
1 change: 1 addition & 0 deletions test/DataProvider/MassiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ public function getPermission(int $id): ?IPermission {
$permission->setRoles($tree);
return $permission;
}

}
1 change: 1 addition & 0 deletions test/DataProvider/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ public function getId(): int {
public function setId(int $id): void {
$this->id = $id;
}

}
6 changes: 3 additions & 3 deletions test/PermissionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@

namespace doganoo\SimpleRBAC\Test;

use doganoo\SimpleRBAC\Test\DataProvider\DataProvider;
use doganoo\SimpleRBAC\Test\DataProvider\MassiveDataProvider;
use doganoo\PHPAlgorithms\Common\Exception\InvalidBitLengthException;
use doganoo\PHPAlgorithms\Common\Exception\InvalidSearchComparisionException;
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;
use doganoo\SimpleRBAC\Common\IUser;
use doganoo\SimpleRBAC\Handler\PermissionHandler;
use PHPUnit\Framework\TestCase;
use doganoo\SimpleRBAC\Test\DataProvider\DataProvider;
use doganoo\SimpleRBAC\Test\DataProvider\MassiveDataProvider;
use doganoo\SimpleRBAC\Test\Util\PermissionUtil;
use doganoo\SimpleRBAC\Test\Util\RoleUtil;
use PHPUnit\Framework\TestCase;

/**
* Class PermissionHandlerTest
Expand Down
2 changes: 1 addition & 1 deletion test/Util/PermissionUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

namespace doganoo\SimpleRBAC\Test\Util;

use doganoo\SimpleRBAC\Test\DataProvider\Permission;
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;
use doganoo\SimpleRBAC\Common\IPermission;
use doganoo\SimpleRBAC\Test\DataProvider\Permission;

/**
* Class PermissionUtil
Expand Down
2 changes: 1 addition & 1 deletion test/Util/RoleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

namespace doganoo\SimpleRBAC\Test\Util;

use doganoo\SimpleRBAC\Test\DataProvider\Role;
use doganoo\PHPAlgorithms\Datastructure\Graph\Tree\BinarySearchTree;
use doganoo\SimpleRBAC\Common\IRole;
use doganoo\SimpleRBAC\Test\DataProvider\Role;

/**
* Class RoleUtil
Expand Down
1 change: 0 additions & 1 deletion test/Util/UserUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public function getName(): string {
* @param string $name
*/
public function setName(string $name): void {
// TODO: Implement setName() method.
}

/**
Expand Down

0 comments on commit 28279d6

Please sign in to comment.