Skip to content

Commit

Permalink
Raise minimum PHP version to 8.0, add type for $userId parameter (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Aug 6, 2023
1 parent 1d75bae commit f77ad52
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1', '8.2']
2 changes: 1 addition & 1 deletion .github/workflows/composer-require-checker.yml
Expand Up @@ -30,4 +30,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['7.4', '8.0', '8.1', '8.2']
['8.0', '8.1', '8.2']
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1', '8.2']
9 changes: 5 additions & 4 deletions CHANGELOG.md
@@ -1,16 +1,17 @@
# Yii Access

## 1.1.2 under development
## 2.0.0 under development

- no changes in this release.
- Chg #37: Raise minimum PHP version to 8.0, add type for `$userId` parameter of
`AccessCheckerInterface::userHasPermission()` method (@vjik)

## 1.1.1 April 05, 2022

- Bug #18: Add missing `\Stringable` to `$userId` of `AccessCheckerInterface::userHasPermission()` (samdark)
- Bug #18: Add missing `\Stringable` to `$userId` of `AccessCheckerInterface::userHasPermission()` (@samdark)

## 1.1.0 January 14, 2022

- New #15: Add `DenyAll` and `AllowAll` access checkers (vjik)
- New #15: Add `DenyAll` and `AllowAll` access checkers (@vjik)

## 1.0.0 August 24, 2020

Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -20,14 +20,14 @@ for fine-grained access checks. Additionally, `DenyAll` and `AllowAll` implement

## Requirements

- PHP 7.4 or higher.
- PHP 8.0 or higher.

## Installation

The package could be installed with composer:

```shell
composer require yiisoft/access --prefer-dist
composer require yiisoft/access
```

## General usage
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -29,13 +29,13 @@
}
],
"require": {
"php": "^7.4|^8.0"
"php": "^8.0"
},
"require-dev": {
"maglnet/composer-require-checker": "^3.8|^4.2",
"phpunit/phpunit": "^9.5",
"maglnet/composer-require-checker": "^4.4",
"phpunit/phpunit": "^9.6",
"rector/rector": "^0.17.0",
"roave/infection-static-analysis-plugin": "^1.16",
"roave/infection-static-analysis-plugin": "^1.25",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.3"
},
Expand Down
7 changes: 4 additions & 3 deletions src/AccessCheckerInterface.php
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Access;

use InvalidArgumentException;
use Stringable;

/**
* The interface defines checking if certain user has certain permission. Optional parameters could be passed
Expand All @@ -15,8 +16,8 @@ interface AccessCheckerInterface
/**
* Checks if the user with the ID given has the specified permission.
*
* @param int|string|\Stringable|null $userId The user ID representing the unique identifier of a user. If ID is null,
* it means user is a guest.
* @param int|string|Stringable|null $userId The user ID representing the unique identifier of a user.
* If ID is null, it means user is a guest.
* @param string $permissionName The name of the permission to be checked against.
* @param array $parameters Name-value pairs that will be used to determine if access is granted.
*
Expand All @@ -25,5 +26,5 @@ interface AccessCheckerInterface
*
* @return bool Whether the user has the specified permission.
*/
public function userHasPermission($userId, string $permissionName, array $parameters = []): bool;
public function userHasPermission(int|string|Stringable|null $userId, string $permissionName, array $parameters = []): bool;
}
4 changes: 3 additions & 1 deletion src/AllowAll.php
Expand Up @@ -4,12 +4,14 @@

namespace Yiisoft\Access;

use Stringable;

/**
* Allow all access.
*/
final class AllowAll implements AccessCheckerInterface
{
public function userHasPermission($userId, string $permissionName, array $parameters = []): bool
public function userHasPermission(int|string|Stringable|null $userId, string $permissionName, array $parameters = []): bool
{
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/DenyAll.php
Expand Up @@ -4,12 +4,14 @@

namespace Yiisoft\Access;

use Stringable;

/**
* Deny all access.
*/
final class DenyAll implements AccessCheckerInterface
{
public function userHasPermission($userId, string $permissionName, array $parameters = []): bool
public function userHasPermission(int|string|Stringable|null $userId, string $permissionName, array $parameters = []): bool
{
return false;
}
Expand Down
18 changes: 16 additions & 2 deletions tests/AllowAllTest.php
Expand Up @@ -6,13 +6,27 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Access\AllowAll;
use Yiisoft\Access\Tests\Support\StringableObject;

final class AllowAllTest extends TestCase
{
public function testBase(): void
public function dataBase(): array
{
return [
[null],
[7],
['hello'],
[new StringableObject('test')],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(mixed $userId): void
{
$accessChecker = new AllowAll();

$this->assertTrue($accessChecker->userHasPermission(null, 'test'));
$this->assertTrue($accessChecker->userHasPermission($userId, 'test'));
}
}
20 changes: 20 additions & 0 deletions tests/Support/StringableObject.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Access\Tests\Support;

use Stringable;

final class StringableObject implements Stringable
{
public function __construct(
private string $string,
) {
}

public function __toString(): string
{
return $this->string;
}
}

0 comments on commit f77ad52

Please sign in to comment.