Skip to content

Commit

Permalink
Implement (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Mar 24, 2022
1 parent 7d77d15 commit bdbf2cf
Show file tree
Hide file tree
Showing 15 changed files with 1,090 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Expand Up @@ -26,6 +26,7 @@ jobs:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

env:
extensions: pdo, pdo_sqlite
key: cache-v1

runs-on: ${{ matrix.os }}
Expand All @@ -37,7 +38,6 @@ jobs:
- windows-latest

php:
- 7.4
- 8.0
- 8.1

Expand All @@ -52,6 +52,7 @@ jobs:
ini-values: date.timezone='UTC'
coverage: pcov
tools: composer:v2
extensions: ${{ env.extensions }}

- name: Determine composer cache directory on Linux
if: matrix.os == 'ubuntu-latest'
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/mutation.yml
Expand Up @@ -23,6 +23,9 @@ jobs:
mutation:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

env:
extensions: pdo, pdo_sqlite

runs-on: ${{ matrix.os }}

strategy:
Expand All @@ -44,6 +47,7 @@ jobs:
ini-values: memory_limit=-1
coverage: pcov
tools: composer:v2
extensions: ${{ env.extensions }}

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/static.yml
Expand Up @@ -33,7 +33,6 @@ jobs:
- ubuntu-latest

php:
- 7.4
- 8.0
- 8.1

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Expand Up @@ -22,8 +22,10 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.4|^8.0",
"cycle/database": "^2.1"
"php": "^8.0",
"cycle/database": "^2.1",
"symfony/console": "^5.3|^6.0",
"yiisoft/rbac": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
29 changes: 14 additions & 15 deletions phpunit.xml.dist
@@ -1,28 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
colors="true"
verbose="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true">
failOnWarning="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
</php>

<testsuites>
<testsuite name="Yii _____ tests">
<testsuite name="Yii Rbac Cycle Db tests">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Empty file removed src/.gitkeep
Empty file.
147 changes: 147 additions & 0 deletions src/AssignmentsStorage.php
@@ -0,0 +1,147 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Rbac\Cycle;

use Cycle\Database\DatabaseInterface;
use Cycle\Database\DatabaseProviderInterface;
use Cycle\Database\Table;
use Yiisoft\Rbac\Assignment;
use Yiisoft\Rbac\AssignmentsStorageInterface;

final class AssignmentsStorage implements AssignmentsStorageInterface
{
private DatabaseInterface $database;
/**
* @psalm-var non-empty-string
*/
private string $tableName;

/**
* @param non-empty-string $tableName
* @param DatabaseProviderInterface $dbal
*/
public function __construct(string $tableName, DatabaseProviderInterface $dbal)
{
$this->database = $dbal->database();
$this->tableName = $tableName;
}

/**
* @inheritDoc
*/
public function getAll(): array
{
$assignments = [];
foreach ($this->database->select()->from($this->tableName)->fetchAll() as $item) {
$assignments[$item['userId']][$item['itemName']] = new Assignment(
$item['userId'],
$item['itemName'],
(int)$item['createdAt']
);
}

return $assignments;
}

/**
* @inheritDoc
*/
public function getByUserId(string $userId): array
{
$assignments = $this->database->select()->from($this->tableName)->where(['userId' => $userId])->fetchAll();

return array_map(
static fn (array $item) => new Assignment($userId, $item['itemName'], (int)$item['createdAt']),
$assignments
);
}

/**
* @inheritDoc
*/
public function get(string $itemName, string $userId): ?Assignment
{
$assignment = $this->database
->select()
->from($this->tableName)
->where(['itemName' => $itemName, 'userId' => $userId])
->run()
->fetch();
if (!empty($assignment)) {
return new Assignment($userId, $itemName, (int)$assignment['createdAt']);
}
return null;
}

/**
* @inheritDoc
*/
public function add(string $itemName, string $userId): void
{
$this->database
->insert($this->tableName)
->values(
[
'itemName' => $itemName,
'userId' => $userId,
'createdAt' => time(),
],
)
->run();
}

/**
* @inheritDoc
*/
public function hasItem(string $name): bool
{
return $this->database->select('itemName')->from($this->tableName)->where(['itemName' => $name])->count() > 0;
}

/**
* @inheritDoc
*/
public function renameItem(string $oldName, string $newName): void
{
if ($oldName === $newName) {
return;
}
$this->database->update($this->tableName, ['itemName' => $newName], ['itemName' => $oldName])->run();
}

/**
* @inheritDoc
*/
public function remove(string $itemName, string $userId): void
{
$this->database->delete($this->tableName, ['itemName' => $itemName, 'userId' => $userId])->run();
}

/**
* @inheritDoc
*/
public function removeByUserId(string $userId): void
{
$this->database->delete($this->tableName, ['userId' => $userId])->run();
}

/**
* @inheritDoc
*/
public function removeByItemName(string $itemName): void
{
$this->database->delete($this->tableName, ['itemName' => $itemName])->run();
}

/**
* @inheritDoc
*/
public function clear(): void
{
/** @var Table $table */
$table = $this->database->table($this->tableName);
$table->eraseData();
}
}

0 comments on commit bdbf2cf

Please sign in to comment.