Skip to content

Commit

Permalink
Use simple storages, PHP 8.1 and PHPUnit 10.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Dec 12, 2023
1 parent 1bb2d95 commit d7f252f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 505 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

- Chg #63: Raise PHP version to 8.0 (@arogachev)
- Enh #63: Improve performance (@arogachev)
- Enh #?: Implement `getByNames()` and `getAccessTree()` methods in `ItemsStorage` (@arogachev)
- Enh #?: Implement `filterUserItemNames()` method in `AssignmentsStorage` (@arogachev)
- Chg #?: Rename `$name` argument to `$names` and allow array type for it in `getAllChildren()`, `getAllChildRoles()`,
- Enh #70: Implement `getByNames()` and `getAccessTree()` methods in `ItemsStorage` (@arogachev)
- Enh #70: Implement `filterUserItemNames()` method in `AssignmentsStorage` (@arogachev)
- Chg #70: Rename `$name` argument to `$names` and allow array type for it in `getAllChildren()`, `getAllChildRoles()`,
`getAllChildPermissions()` methods in `ItemsStorage` (@arogachev)

## 1.0.0 April 08, 2022
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
],
"minimum-stability": "dev",
"require": {
"php": "^8.0",
"php": "^8.1",
"yiisoft/rbac": "dev-master",
"yiisoft/var-dumper": "^1.0"
},
"require-dev": {
"ext-uopz": "*",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10.5.2",
"psr/log": "^1.1.3",
"roave/infection-static-analysis-plugin": "^1.18",
"slope-it/clock-mock": "0.4.0",
Expand All @@ -46,7 +46,8 @@
"autoload": {
"psr-4": {
"Yiisoft\\Rbac\\Php\\": "src"
}
},
"files": ["tests/bootstrap.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
36 changes: 16 additions & 20 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
>
<php>
<ini name="error_reporting" value="-1"/>
</php>

<testsuites>
<testsuite name="Yii RBAC PHP File Storage tests">
<directory>./tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Yii RBAC PHP File Storage tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
125 changes: 16 additions & 109 deletions src/AssignmentsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
namespace Yiisoft\Rbac\Php;

use Yiisoft\Rbac\Assignment;
use Yiisoft\Rbac\AssignmentsStorageInterface;

use function array_key_exists;
use Yiisoft\Rbac\SimpleAssignmentsStorage;

/**
* Storage stores authorization data in three PHP files specified by {@see Storage::itemFile},
* {@see Storage::assignmentFile} and {@see Storage::ruleFile}.
* Storage stores roles and permissions in PHP file specified in {@see AssignmentsStorage::$assignmentFile}.
*
* It is suitable for authorization data that is not too big (for example, the authorization data for a personal blog
* system).
*/
final class AssignmentsStorage extends CommonStorage implements AssignmentsStorageInterface
final class AssignmentsStorage extends SimpleAssignmentsStorage
{
use FileStorageTrait;

/**
* @var string The path of the PHP script that contains the authorization assignments. Make sure this file is
* writable by the web server process if the authorization needs to be changed online.
Expand All @@ -27,12 +26,6 @@ final class AssignmentsStorage extends CommonStorage implements AssignmentsStora
*/
private string $assignmentFile;

/**
* @var array Array in format is `[userId => [itemName => assignment]]`.
* @psalm-var array<string, array<string, Assignment>>
*/
private array $assignments = [];

public function __construct(
string $directory,
string $assignmentFile = 'assignments.php'
Expand All @@ -41,88 +34,11 @@ public function __construct(
$this->loadAssignments();

Check warning on line 34 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function __construct(string $directory, string $assignmentFile = 'assignments.php') { $this->assignmentFile = $directory . DIRECTORY_SEPARATOR . $assignmentFile; - $this->loadAssignments(); + } public function add(Assignment $assignment) : void {

Check warning on line 34 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function __construct(string $directory, string $assignmentFile = 'assignments.php') { $this->assignmentFile = $directory . DIRECTORY_SEPARATOR . $assignmentFile; - $this->loadAssignments(); + } public function add(Assignment $assignment) : void {
}

public function getAll(): array
{
return $this->assignments;
}

public function getByUserId(string $userId): array
{
return $this->assignments[$userId] ?? [];
}

public function getByItemNames(array $itemNames): array
{
$result = [];

foreach ($this->assignments as $assignments) {
foreach ($assignments as $userAssignment) {
if (in_array($userAssignment->getItemName(), $itemNames, true)) {
$result[] = $userAssignment;
}
}
}

return $result;
}

public function get(string $itemName, string $userId): ?Assignment
{
return $this->getByUserId($userId)[$itemName] ?? null;
}

public function exists(string $itemName, string $userId): bool
{
return isset($this->getByUserId($userId)[$itemName]);
}

public function userHasItem(string $userId, array $itemNames): bool
{
$assignments = $this->getByUserId($userId);
if (empty($assignments)) {
return false;
}

foreach ($itemNames as $itemName) {
if (array_key_exists($itemName, $assignments)) {
return true;
}
}

return false;
}

public function filterUserItemNames(string $userId, array $itemNames): array
{
$assignments = $this->getByUserId($userId);
if (empty($assignments)) {
return [];
}

$userItemNames = [];
foreach ($itemNames as $itemName) {
if (array_key_exists($itemName, $assignments)) {
$userItemNames[] = $itemName;
}
}

return $userItemNames;
}

public function add(Assignment $assignment): void
{
$this->assignments[$assignment->getUserId()][$assignment->getItemName()] = $assignment;
$this->saveAssignments();
}
parent::add($assignment);

public function hasItem(string $name): bool
{
foreach ($this->getAll() as $assignmentInfo) {
if (array_key_exists($name, $assignmentInfo)) {
return true;
}
}
return false;
$this->saveAssignments();

Check warning on line 41 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function add(Assignment $assignment) : void { parent::add($assignment); - $this->saveAssignments(); + } public function renameItem(string $oldName, string $newName) : void {

Check warning on line 41 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function add(Assignment $assignment) : void { parent::add($assignment); - $this->saveAssignments(); + } public function renameItem(string $oldName, string $newName) : void {
}

public function renameItem(string $oldName, string $newName): void
Expand All @@ -131,12 +47,7 @@ public function renameItem(string $oldName, string $newName): void
return;
}

foreach ($this->assignments as &$assignments) {
if (isset($assignments[$oldName])) {
$assignments[$newName] = $assignments[$oldName]->withItemName($newName);
unset($assignments[$oldName]);
}
}
parent::renameItem($oldName, $newName);

$this->saveAssignments();

Check warning on line 52 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ return; } parent::renameItem($oldName, $newName); - $this->saveAssignments(); + } public function remove(string $itemName, string $userId) : void {

Check warning on line 52 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ return; } parent::renameItem($oldName, $newName); - $this->saveAssignments(); + } public function remove(string $itemName, string $userId) : void {
}
Expand All @@ -147,33 +58,32 @@ public function remove(string $itemName, string $userId): void
return;
}

unset($this->assignments[$userId][$itemName]);
parent::remove($itemName, $userId);

$this->saveAssignments();

Check warning on line 63 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ return; } parent::remove($itemName, $userId); - $this->saveAssignments(); + } public function removeByUserId(string $userId) : void {

Check warning on line 63 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ return; } parent::remove($itemName, $userId); - $this->saveAssignments(); + } public function removeByUserId(string $userId) : void {
}

public function removeByUserId(string $userId): void
{
$this->assignments[$userId] = [];
parent::removeByUserId($userId);

$this->saveAssignments();

Check warning on line 70 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function removeByUserId(string $userId) : void { parent::removeByUserId($userId); - $this->saveAssignments(); + } public function removeByItemName(string $itemName) : void {

Check warning on line 70 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function removeByUserId(string $userId) : void { parent::removeByUserId($userId); - $this->saveAssignments(); + } public function removeByItemName(string $itemName) : void {
}

public function removeByItemName(string $itemName): void
{
foreach ($this->assignments as &$assignments) {
unset($assignments[$itemName]);
}
parent::removeByItemName($itemName);

$this->saveAssignments();

Check warning on line 77 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function removeByItemName(string $itemName) : void { parent::removeByItemName($itemName); - $this->saveAssignments(); + } public function clear() : void {

Check warning on line 77 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function removeByItemName(string $itemName) : void { parent::removeByItemName($itemName); - $this->saveAssignments(); + } public function clear() : void {
}

public function clear(): void
{
$this->assignments = [];
parent::clear();

$this->saveAssignments();

Check warning on line 84 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function clear() : void { parent::clear(); - $this->saveAssignments(); + } private function loadAssignments() : void {

Check warning on line 84 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function clear() : void { parent::clear(); - $this->saveAssignments(); + } private function loadAssignments() : void {
}

/**
* Loads authorization data from persistent storage.
*/
private function loadAssignments(): void
{
/** @psalm-var array<string|int, string[]> $assignments */
Expand All @@ -187,9 +97,6 @@ private function loadAssignments(): void
}
}

/**
* Saves assignments data into persistent storage.
*/
private function saveAssignments(): void
{
$assignmentData = [];
Expand Down
2 changes: 1 addition & 1 deletion src/CommonStorage.php → src/FileStorageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use function dirname;
use function function_exists;

abstract class CommonStorage
trait FileStorageTrait
{
/**
* Loads the authorization data from a PHP script file.
Expand Down
Loading

0 comments on commit d7f252f

Please sign in to comment.