| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\ObjectMembership; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\UserGroup\AbstractUserGroup; |
| 9 |
use UserAccessManager\UserGroup\AssignmentInformation; |
| 10 |
use UserAccessManager\UserGroup\AssignmentInformationFactory; |
| 11 |
|
| 12 |
abstract class ObjectMembershipHandler |
| 13 |
{ |
| 14 |
protected ?string $generalObjectType = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* @throws Exception |
| 18 |
*/ |
| 19 |
public function __construct( |
| 20 |
protected AssignmentInformationFactory $assignmentInformationFactory |
| 21 |
) { |
| 22 |
if ($this->generalObjectType === null) { |
| 23 |
throw new MissingObjectTypeException('Missing general object type, Object type must be set.'); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
abstract public function getObjectName(int|string|null $objectId, string &$typeName = ''): int|string; |
| 28 |
|
| 29 |
public function getGeneralObjectType(): string |
| 30 |
{ |
| 31 |
return $this->generalObjectType; |
| 32 |
} |
| 33 |
|
| 34 |
public function getHandledObjects(): array |
| 35 |
{ |
| 36 |
return [$this->generalObjectType => $this->generalObjectType]; |
| 37 |
} |
| 38 |
|
| 39 |
public function handlesObject(mixed $objectType): bool |
| 40 |
{ |
| 41 |
$objectTypes = $this->getHandledObjects(); |
| 42 |
return isset($objectTypes[$objectType]); |
| 43 |
} |
| 44 |
|
| 45 |
protected function assignRecursiveMembership( |
| 46 |
?AssignmentInformation &$assignmentInformation, |
| 47 |
array $recursiveMembership |
| 48 |
): void { |
| 49 |
if ($assignmentInformation === null) { |
| 50 |
$assignmentInformation = $this->assignmentInformationFactory->createAssignmentInformation(); |
| 51 |
} |
| 52 |
|
| 53 |
$assignmentInformation->setRecursiveMembership($recursiveMembership); |
| 54 |
} |
| 55 |
|
| 56 |
protected function checkAccessWithRecursiveMembership( |
| 57 |
bool $isMember, |
| 58 |
array $recursiveMembership, |
| 59 |
?AssignmentInformation &$assignmentInformation |
| 60 |
): bool { |
| 61 |
if ($isMember === true || count($recursiveMembership) > 0) { |
| 62 |
$this->assignRecursiveMembership($assignmentInformation, $recursiveMembership); |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
protected function getSimpleAssignedObjects(AbstractUserGroup $userGroup, string $objectType): array |
| 70 |
{ |
| 71 |
$objects = $userGroup->getAssignedObjects($objectType); |
| 72 |
|
| 73 |
return array_map( |
| 74 |
function (AssignmentInformation $element) { |
| 75 |
return $element->getType(); |
| 76 |
}, |
| 77 |
$objects |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
abstract public function isMember( |
| 82 |
AbstractUserGroup $userGroup, |
| 83 |
bool $lockRecursive, |
| 84 |
int|string|null $objectId, |
| 85 |
?AssignmentInformation &$assignmentInformation = null |
| 86 |
): bool; |
| 87 |
|
| 88 |
abstract public function getFullObjects( |
| 89 |
AbstractUserGroup $userGroup, |
| 90 |
bool $lockRecursive, |
| 91 |
?string $objectType = null |
| 92 |
): array; |
| 93 |
} |
| 94 |
|