Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use simple storages, PHP 8.1 and PHPUnit 10.5.2 #76

Merged
merged 4 commits into from
Dec 12, 2023
Merged
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 .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
- windows-latest

php:
- 8.0
- 8.1
- 8.2

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.0', '8.1', '8.2']
['8.1', '8.2']
extensions: uopz
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ build:

environment:
php:
version: 8.0.18
version: 8.1
pecl_extensions:
- uopz
ini:
Expand Down
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

- 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)
- Enh #76: Use simple storages for items and assignments from the base `rbac` package (@arogachev)
- Chg #76: Raise PHP version to 8.1 (@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"]
arogachev marked this conversation as resolved.
Show resolved Hide resolved
},
"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,102 +26,19 @@
*/
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'
) {
$this->assignmentFile = $directory . DIRECTORY_SEPARATOR . $assignmentFile;

Check warning on line 33 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

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

Check warning on line 33 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

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

Check warning on line 33 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

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

Check warning on line 33 in src/AssignmentsStorage.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ private string $assignmentFile; public function __construct(string $directory, string $assignmentFile = 'assignments.php') { - $this->assignmentFile = $directory . DIRECTORY_SEPARATOR . $assignmentFile; + $this->assignmentFile = $directory . $assignmentFile; $this->loadAssignments(); } public function add(Assignment $assignment) : void
$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,14 +47,9 @@
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 {
}

public function remove(string $itemName, string $userId): void
Expand All @@ -147,33 +58,32 @@
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 @@
}
}

/**
* Saves assignments data into persistent storage.
*/
private function saveAssignments(): void
{
$assignmentData = [];
Expand All @@ -198,6 +105,6 @@
$assignmentData[$userId][] = $assignment->getItemName();
}
}
$this->saveToFile($assignmentData, $this->assignmentFile);

Check warning on line 108 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 @@ @@ $assignmentData[$userId][] = $assignment->getItemName(); } } - $this->saveToFile($assignmentData, $this->assignmentFile); + } }

Check warning on line 108 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 @@ @@ $assignmentData[$userId][] = $assignment->getItemName(); } } - $this->saveToFile($assignmentData, $this->assignmentFile); + } }
}
}
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 All @@ -23,7 +23,7 @@
*
* @see saveToFile()
*/
protected function loadFromFile(string $file): array

Check warning on line 26 in src/FileStorageTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ * * @see saveToFile() */ - protected function loadFromFile(string $file) : array + private function loadFromFile(string $file) : array { if (is_file($file)) { /**
{
if (is_file($file)) {
/**
Expand Down
Loading