Skip to content

Commit

Permalink
Merge branch 'next' into develop
Browse files Browse the repository at this point in the history
* next:
  fix minimum version of psalm
  suppress the error type
  declare all classes pure
  update readme
  simplify implementation
  make User and Group constructor private
  transform Mode into an enum
  use ramsey/composer-install
  remove bc break check
  use innmind/immutable 4
  use CS 2
  update innmind/immutable
  • Loading branch information
Baptouuuu committed Apr 18, 2022
2 parents 481b6d9 + 31b9159 commit 67a02bc
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 314 deletions.
83 changes: 16 additions & 67 deletions .github/workflows/ci.yml
Expand Up @@ -3,21 +3,12 @@ name: CI
on: [push, pull_request]

jobs:
roave_bc_check:
name: Roave BC Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: fetch tags
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Roave BC Check
uses: docker://nyholm/roave-bc-check-ga
phpunit:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
php-version: ['7.4', '8.0']
php-version: ['8.1']
dependencies: ['lowest', 'highest']
name: 'PHPUnit'
steps:
Expand All @@ -29,21 +20,10 @@ jobs:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: none
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
- name: Composer
uses: "ramsey/composer-install@v2"
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-
- name: Install Dependencies
if: ${{ matrix.dependencies == 'lowest' }}
run: composer update --prefer-lowest --no-progress
- name: Install Dependencies
if: ${{ matrix.dependencies == 'highest' }}
run: composer install --no-progress
dependency-versions: ${{ matrix.dependencies }}
- name: PHPUnit
run: vendor/bin/phpunit
env:
Expand All @@ -53,7 +33,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
php-version: ['7.4', '8.0']
php-version: ['8.1']
dependencies: ['lowest', 'highest']
name: 'Coverage'
steps:
Expand All @@ -65,21 +45,10 @@ jobs:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
- name: Composer
uses: "ramsey/composer-install@v2"
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-
- name: Install Dependencies
if: ${{ matrix.dependencies == 'lowest' }}
run: composer update --prefer-lowest --no-progress
- name: Install Dependencies
if: ${{ matrix.dependencies == 'highest' }}
run: composer install --no-progress
dependency-versions: ${{ matrix.dependencies }}
- name: PHPUnit
run: vendor/bin/phpunit --coverage-clover=coverage.clover
env:
Expand All @@ -91,7 +60,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4', '8.0']
php-version: ['8.1']
dependencies: ['lowest', 'highest']
name: 'Psalm'
steps:
Expand All @@ -102,28 +71,17 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
- name: Composer
uses: "ramsey/composer-install@v2"
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-
- name: Install Dependencies
if: ${{ matrix.dependencies == 'lowest' }}
run: composer update --prefer-lowest --no-progress
- name: Install Dependencies
if: ${{ matrix.dependencies == 'highest' }}
run: composer install --no-progress
dependency-versions: ${{ matrix.dependencies }}
- name: Psalm
run: vendor/bin/psalm --shepherd
cs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4']
php-version: ['8.1']
name: 'CS'
steps:
- name: Checkout
Expand All @@ -133,16 +91,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-composer-
- name: Install Dependencies
run: composer install --no-progress
- name: Composer
uses: "ramsey/composer-install@v2"
- name: CS
run: vendor/bin/php-cs-fixer fix --diff --dry-run --diff-format udiff
run: vendor/bin/php-cs-fixer fix --diff --dry-run
File renamed without changes.
26 changes: 13 additions & 13 deletions README.md
Expand Up @@ -24,21 +24,21 @@ use Innmind\ACL\{

$acl = ACL::of('r---w---x user:group');

$acl->allows(new User('foo'), new Group('bar'), Mode::read()); // false
$acl->allows(new User('foo'), new Group('bar'), Mode::write()); // false
$acl->allows(new User('foo'), new Group('bar'), Mode::execute()); // true
$acl->allows(new User('foo'), new Group('group'), Mode::read()); // false
$acl->allows(new User('foo'), new Group('group'), Mode::write()); // true
$acl->allows(new User('foo'), new Group('group'), Mode::execute()); // true
$acl->allows(new User('user'), new Group('bar'), Mode::read()); // true
$acl->allows(new User('user'), new Group('bar'), Mode::write()); // false
$acl->allows(new User('user'), new Group('bar'), Mode::execute()); // true
$acl->allows(new User('user'), new Group('group'), Mode::read()); // true
$acl->allows(new User('user'), new Group('group'), Mode::write()); // true
$acl->allows(new User('user'), new Group('group'), Mode::execute()); // true
$acl->allows(User::of('foo'), Group::of('bar'), Mode::read); // false
$acl->allows(User::of('foo'), Group::of('bar'), Mode::write); // false
$acl->allows(User::of('foo'), Group::of('bar'), Mode::execute); // true
$acl->allows(User::of('foo'), Group::of('group'), Mode::read); // false
$acl->allows(User::of('foo'), Group::of('group'), Mode::write); // true
$acl->allows(User::of('foo'), Group::of('group'), Mode::execute); // true
$acl->allows(User::of('user'), Group::of('bar'), Mode::read); // true
$acl->allows(User::of('user'), Group::of('bar'), Mode::write); // false
$acl->allows(User::of('user'), Group::of('bar'), Mode::execute); // true
$acl->allows(User::of('user'), Group::of('group'), Mode::read); // true
$acl->allows(User::of('user'), Group::of('group'), Mode::write); // true
$acl->allows(User::of('user'), Group::of('group'), Mode::execute); // true
$acl->toString(); // outputs "r---w---x user:group"

$otherAcl = $acl->addUser(Mode::write());
$otherAcl = $acl->addUser(Mode::write);
$acl->toString(); // outputs "r---w---x user:group"
$otherAcl->toString(); // outputs "rw--w---x user:group"
```
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Expand Up @@ -15,8 +15,8 @@
"issues": "http://github.com/Innmind/ACL/issues"
},
"require": {
"php": "~7.4|~8.0",
"innmind/immutable": "~3.3"
"php": "~8.1",
"innmind/immutable": "~4.0"
},
"autoload": {
"psr-4": {
Expand All @@ -30,8 +30,9 @@
},
"require-dev": {
"phpunit/phpunit": "~9.0",
"vimeo/psalm": "^4.1",
"vimeo/psalm": "^4.22",
"nikic/php-parser": "^4.13.2",
"innmind/black-box": "^4.16",
"innmind/coding-standard": "^1.1"
"innmind/coding-standard": "~2.0"
}
}
26 changes: 20 additions & 6 deletions src/ACL.php
Expand Up @@ -4,8 +4,10 @@
namespace Innmind\ACL;

use Innmind\Immutable\Str;
use function Innmind\Immutable\unwrap;

/**
* @psalm-immutable
*/
final class ACL
{
private User $user;
Expand All @@ -19,7 +21,7 @@ public function __construct(
Group $group,
Entries $userEntries,
Entries $groupEntries,
Entries $otherEntries
Entries $otherEntries,
) {
$this->user = $user;
$this->group = $group;
Expand All @@ -28,15 +30,18 @@ public function __construct(
$this->otherEntries = $otherEntries;
}

/**
* @psalm-pure
*/
public static function of(string $string): self
{
$string = Str::of($string);
[$userEntries, $groupEntries, $otherEntries] = unwrap($string->take(9)->chunk(3));
[$user, $group] = unwrap($string->drop(10)->split(':'));
[$userEntries, $groupEntries, $otherEntries] = $string->take(9)->chunk(3)->toList();
[$user, $group] = $string->drop(10)->split(':')->toList();

return new self(
new User($user->toString()),
new Group($group->toString()),
User::of($user->toString()),
Group::of($group->toString()),
Entries::of($userEntries->toString()),
Entries::of($groupEntries->toString()),
Entries::of($otherEntries->toString()),
Expand Down Expand Up @@ -76,6 +81,9 @@ public function addOther(Mode ...$modes): self
);
}

/**
* @no-named-arguments
*/
public function removeUser(Mode ...$modes): self
{
return new self(
Expand All @@ -87,6 +95,9 @@ public function removeUser(Mode ...$modes): self
);
}

/**
* @no-named-arguments
*/
public function removeGroup(Mode ...$modes): self
{
return new self(
Expand All @@ -98,6 +109,9 @@ public function removeGroup(Mode ...$modes): self
);
}

/**
* @no-named-arguments
*/
public function removeOther(Mode ...$modes): self
{
return new self(
Expand Down
44 changes: 23 additions & 21 deletions src/Entries.php
Expand Up @@ -8,55 +8,57 @@
Sequence,
Str,
};
use function Innmind\Immutable\unwrap;

/**
* @psalm-immutable
*/
final class Entries
{
/** @var Set<Mode> */
private Set $entries;

/**
* @no-named-arguments
*/
public function __construct(Mode ...$modes)
{
/** @var Set<Mode> */
$this->entries = Set::of(Mode::class, ...$modes);
$this->entries = Set::of(...$modes);
}

/**
* @psalm-pure
*/
public static function of(string $modes): self
{
/** @var Sequence<Mode> */
/** @var list<Mode> */
$modes = Str::of($modes)
->split()
->reduce(
Sequence::of('?'.Mode::class),
static function(Sequence $modes, Str $mode): Sequence {
return $modes->add(Mode::of($mode->toString()));
}
)
->filter(static function(?Mode $mode): bool {
return $mode instanceof Mode;
});
->map(static fn($mode) => Mode::of($mode->toString()))
->filter(static fn(?Mode $mode) => $mode instanceof Mode)
->toList();

return new self(...unwrap($modes));
return new self(...$modes);
}

public function add(Mode ...$modes): self
{
return new self(...unwrap($this->entries), ...$modes);
return new self(...$this->entries->toList(), ...$modes);
}

/**
* @no-named-arguments
*/
public function remove(Mode ...$modes): self
{
$toRemove = Set::of(Mode::class, ...$modes);
$entries = $this->entries->filter(static function(Mode $entry) use ($toRemove): bool {
return !$toRemove->contains($entry);
});
$toRemove = Set::of(...$modes);
$entries = $this->entries->diff($toRemove);

return new self(...unwrap($entries));
return new self(...$entries->toList());
}

public function allows(Mode $mode, Mode ...$modes): bool
{
return Set::of(Mode::class, $mode, ...$modes)->reduce(
return Set::of($mode, ...$modes)->reduce(
true,
function(bool $allows, Mode $mode): bool {
return $allows && $this->entries->contains($mode);
Expand Down
13 changes: 12 additions & 1 deletion src/Group.php
Expand Up @@ -6,11 +6,14 @@
use Innmind\ACL\Exception\DomainException;
use Innmind\Immutable\Str;

/**
* @psalm-immutable
*/
final class Group
{
private string $value;

public function __construct(string $value)
private function __construct(string $value)
{
$value = Str::of($value);

Expand All @@ -21,6 +24,14 @@ public function __construct(string $value)
$this->value = $value->toString();
}

/**
* @psalm-pure
*/
public static function of(string $value): self
{
return new self($value);
}

public function equals(self $group): bool
{
return $this->value === $group->value;
Expand Down

0 comments on commit 67a02bc

Please sign in to comment.