-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AccessEntry: matchAll() and matchAny()
- Loading branch information
Showing
14 changed files
with
662 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\Auth\Authorization; | ||
|
||
use Orisai\Exceptions\Logic\InvalidArgument; | ||
use function count; | ||
|
||
final class MatchAllOfEntries | ||
{ | ||
|
||
/** @var list<AccessEntry|MatchAllOfEntries|MatchAnyOfEntries> */ | ||
private array $entries; | ||
|
||
/** | ||
* @param list<AccessEntry|MatchAllOfEntries|MatchAnyOfEntries> $entries | ||
*/ | ||
public function __construct(array $entries) | ||
{ | ||
if (count($entries) < 2) { | ||
throw InvalidArgument::create() | ||
->withMessage('At least 2 entries are required.'); | ||
} | ||
|
||
$this->entries = $entries; | ||
} | ||
|
||
/** | ||
* @return list<AccessEntry|MatchAllOfEntries|MatchAnyOfEntries> | ||
*/ | ||
public function getEntries(): array | ||
{ | ||
return $this->entries; | ||
} | ||
|
||
public function match(): bool | ||
{ | ||
foreach ($this->entries as $entry) { | ||
if (!$entry->match()) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\Auth\Authorization; | ||
|
||
use Orisai\Exceptions\Logic\InvalidArgument; | ||
use function count; | ||
|
||
final class MatchAnyOfEntries | ||
{ | ||
|
||
/** @var list<AccessEntry|MatchAllOfEntries|MatchAnyOfEntries> */ | ||
private array $entries; | ||
|
||
/** | ||
* @param list<AccessEntry|MatchAllOfEntries|MatchAnyOfEntries> $entries | ||
*/ | ||
public function __construct(array $entries) | ||
{ | ||
if (count($entries) < 2) { | ||
throw InvalidArgument::create() | ||
->withMessage('At least 2 entries are required.'); | ||
} | ||
|
||
$this->entries = $entries; | ||
} | ||
|
||
/** | ||
* @return list<AccessEntry|MatchAllOfEntries|MatchAnyOfEntries> | ||
*/ | ||
public function getEntries(): array | ||
{ | ||
return $this->entries; | ||
} | ||
|
||
public function match(): bool | ||
{ | ||
foreach ($this->entries as $entry) { | ||
if ($entry->match()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Auth\Doubles; | ||
|
||
use Orisai\Auth\Authorization\AccessEntry; | ||
use Orisai\Auth\Authorization\MatchAllOfEntries; | ||
use Orisai\Auth\Authorization\MatchAnyOfEntries; | ||
|
||
final class EntriesFromContext | ||
{ | ||
|
||
/** @var list<AccessEntry|MatchAllOfEntries|MatchAnyOfEntries> */ | ||
public array $entries; | ||
|
||
/** | ||
* @param list<AccessEntry|MatchAllOfEntries|MatchAnyOfEntries> $entries | ||
*/ | ||
public function __construct(array $entries) | ||
{ | ||
$this->entries = $entries; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Auth\Doubles; | ||
|
||
use Generator; | ||
use Orisai\Auth\Authentication\Identity; | ||
use Orisai\Auth\Authorization\Policy; | ||
use Orisai\Auth\Authorization\PolicyContext; | ||
|
||
/** | ||
* @implements Policy<EntriesFromContext> | ||
*/ | ||
final class EntriesFromContextPolicy implements Policy | ||
{ | ||
|
||
public static function getPrivilege(): string | ||
{ | ||
return 'entries-from-context'; | ||
} | ||
|
||
public static function getRequirementsClass(): string | ||
{ | ||
return EntriesFromContext::class; | ||
} | ||
|
||
/** | ||
* @param EntriesFromContext $requirements | ||
*/ | ||
public function isAllowed(Identity $identity, object $requirements, PolicyContext $context): Generator | ||
{ | ||
yield from $requirements->entries; | ||
} | ||
|
||
} |
Oops, something went wrong.