Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
16.67% |
3 / 18 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ShouldApplyAuditablePermission | |
16.67% |
3 / 18 |
|
0.00% |
0 / 1 |
45.04 | |
0.00% |
0 / 1 |
| applies | |
16.67% |
3 / 18 |
|
0.00% |
0 / 1 |
45.04 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Other; |
| 3 | |
| 4 | use Apie\Core\Attributes\ApieContextAttribute; |
| 5 | use Apie\Core\Attributes\Auditable; |
| 6 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 7 | use Apie\Core\Context\ApieContext; |
| 8 | |
| 9 | /** |
| 10 | * Checks if the user has permission to view the audit log based on the #[Auditable] attribute |
| 11 | * of the audited entity. |
| 12 | */ |
| 13 | final class ShouldApplyAuditablePermission implements ApieContextAttribute |
| 14 | { |
| 15 | public function applies(ApieContext $context): bool |
| 16 | { |
| 17 | $auditLog = $context->getContext(AuditLog::class, false); |
| 18 | if (!$auditLog instanceof AuditLog) { |
| 19 | return true; |
| 20 | } |
| 21 | |
| 22 | $reference = $auditLog->getReference(); |
| 23 | $boundedContextId = $reference->getBoundedContextId(); |
| 24 | $entityClassName = $reference->getEntityClass()->toNative(); |
| 25 | |
| 26 | $hashmap = $context->getContext(BoundedContextHashmap::class, false); |
| 27 | if (!$hashmap) { |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | $boundedContext = $hashmap[$boundedContextId->toNative()] ?? null; |
| 32 | if (!$boundedContext) { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | foreach ($boundedContext->resources as $resource) { |
| 37 | if ($resource->getShortName() === $entityClassName || $resource->name === $entityClassName) { |
| 38 | foreach ($resource->getAttributes(Auditable::class) as $attribute) { |
| 39 | $auditable = $attribute->newInstance(); |
| 40 | return $auditable->permission->applies($context); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return true; |
| 46 | } |
| 47 | } |