Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 7 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| HasRole | |
0.00% |
0 / 7 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| applies | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Attributes; |
| 3 | |
| 4 | use Apie\Common\Interfaces\HasRolesInterface; |
| 5 | use Apie\Core\Context\ApieContext; |
| 6 | use Apie\Core\ContextConstants; |
| 7 | |
| 8 | /** |
| 9 | * Tell Apie you need to be logged in with a specific role to see/execute a class/method/property. |
| 10 | */ |
| 11 | final class HasRole implements ApieContextAttribute |
| 12 | { |
| 13 | /** @var array<int, string> */ |
| 14 | public readonly array $roles; |
| 15 | public function __construct( |
| 16 | string... $roles |
| 17 | ) { |
| 18 | $this->roles = $roles; |
| 19 | } |
| 20 | public function applies(ApieContext $context): bool |
| 21 | { |
| 22 | $user = $context->getContext(ContextConstants::AUTHENTICATED_USER, false); |
| 23 | if ($user instanceof HasRolesInterface) { |
| 24 | $roles = $user->getRoles()->toArray(); |
| 25 | $diff = array_intersect($this->roles, $roles); |
| 26 | return !empty($diff); |
| 27 | } |
| 28 | |
| 29 | return false; |
| 30 | } |
| 31 | } |