| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\ObjectMembership; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\ObjectMembership\Exception\MissingObjectTypeException; |
| 9 |
use UserAccessManager\UserGroup\AbstractUserGroup; |
| 10 |
use UserAccessManager\UserGroup\AssignmentInformation; |
| 11 |
use UserAccessManager\UserGroup\AssignmentInformationFactory; |
| 12 |
|
| 13 |
abstract class ObjectMembershipHandler |
| 14 |
{ |
| 15 |
protected ?string $generalObjectType = null; |
| 16 |
|
| 17 |
/** |
| 18 |
* @throws Exception |
| 19 |
*/ |
| 20 |
public function __construct( |
| 21 |
protected AssignmentInformationFactory $assignmentInformationFactory |
| 22 |
) { |
| 23 |
if ($this->generalObjectType === null) { |
| 24 |
throw new MissingObjectTypeException('Missing general object type, Object type must be set.'); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
abstract public function getObjectName(int|string|null $objectId, string &$typeName = ''): int|string; |
| 29 |
|
| 30 |
public function getGeneralObjectType(): string |
| 31 |
{ |
| 32 |
return $this->generalObjectType; |
| 33 |
} |
| 34 |
|
| 35 |
public function getHandledObjects(): array |
| 36 |
{ |
| 37 |
return [$this->generalObjectType => $this->generalObjectType]; |
| 38 |
} |
| 39 |
|
| 40 |
public function handlesObject(mixed $objectType): bool |
| 41 |
{ |
| 42 |
return isset($this->getHandledObjects()[$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 |
return array_map( |
| 72 |
fn(AssignmentInformation $assignmentInformation) => $assignmentInformation->getType(), |
| 73 |
$userGroup->getAssignedObjects($objectType) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
abstract public function isMember( |
| 78 |
AbstractUserGroup $userGroup, |
| 79 |
bool $lockRecursive, |
| 80 |
int|string|null $objectId, |
| 81 |
?AssignmentInformation &$assignmentInformation = null |
| 82 |
): bool; |
| 83 |
|
| 84 |
abstract public function getFullObjects( |
| 85 |
AbstractUserGroup $userGroup, |
| 86 |
bool $lockRecursive, |
| 87 |
?string $objectType = null |
| 88 |
): array; |
| 89 |
} |
| 90 |
|