| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\UserGroup; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\User\UserHandler; |
| 9 |
use UserAccessManager\Util\DateUtil; |
| 10 |
|
| 11 |
class UserGroupAssignmentHandler |
| 12 |
{ |
| 13 |
public function __construct( |
| 14 |
protected DateUtil $dateUtil, |
| 15 |
protected UserHandler $userHandler, |
| 16 |
protected UserGroupHandler $userGroupHandler, |
| 17 |
protected UserGroupFactory $userGroupFactory |
| 18 |
) { |
| 19 |
} |
| 20 |
|
| 21 |
private function getDateParameter(array $data, string $name): ?string |
| 22 |
{ |
| 23 |
$value = (string) ($data[$name] ?? ''); |
| 24 |
|
| 25 |
return ($value !== '') ? $value : null; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @throws Exception |
| 30 |
*/ |
| 31 |
private function setUserGroups( |
| 32 |
array $filteredUserGroups, |
| 33 |
string $objectType, |
| 34 |
int|string|null $objectId, |
| 35 |
array $addUserGroups, |
| 36 |
array $removeUserGroups |
| 37 |
): void { |
| 38 |
/** @var UserGroup $userGroup */ |
| 39 |
foreach ($filteredUserGroups as $groupId => $userGroup) { |
| 40 |
if (isset($removeUserGroups[$groupId]) === true) { |
| 41 |
$userGroup->removeObject($objectType, $objectId); |
| 42 |
} |
| 43 |
|
| 44 |
if (isset($addUserGroups[$groupId]['id']) === true |
| 45 |
&& (int) $addUserGroups[$groupId]['id'] === (int) $groupId |
| 46 |
) { |
| 47 |
$userGroup->addObject( |
| 48 |
$objectType, |
| 49 |
$objectId, |
| 50 |
$this->getDateParameter($addUserGroups[$groupId], 'fromDate'), |
| 51 |
$this->getDateParameter($addUserGroups[$groupId], 'toDate') |
| 52 |
); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @throws UserGroupAssignmentException |
| 59 |
* @throws UserGroupTypeException |
| 60 |
*/ |
| 61 |
private function setDynamicGroups(string $objectType, int|string|null $objectId, array $addDynamicUserGroups): void |
| 62 |
{ |
| 63 |
foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) { |
| 64 |
$dynamicUserGroupData = explode('|', $dynamicUserGroupKey); |
| 65 |
|
| 66 |
if (count($dynamicUserGroupData) === 2 |
| 67 |
&& $addDynamicUserGroup['id'] === $dynamicUserGroupKey |
| 68 |
) { |
| 69 |
$dynamicUserGroup = $this->userGroupFactory->createDynamicUserGroup( |
| 70 |
$dynamicUserGroupData[0], |
| 71 |
$dynamicUserGroupData[1] |
| 72 |
); |
| 73 |
|
| 74 |
$dynamicUserGroup->addObject( |
| 75 |
$objectType, |
| 76 |
$objectId, |
| 77 |
$this->getDateParameter($addDynamicUserGroup, 'fromDate'), |
| 78 |
$this->getDateParameter($addDynamicUserGroup, 'toDate') |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @throws UserGroupTypeException |
| 86 |
* @throws Exception |
| 87 |
*/ |
| 88 |
private function setDefaultGroups(array $filteredUserGroups, string $objectType, int|string|null $objectId): void |
| 89 |
{ |
| 90 |
/** |
| 91 |
* @var UserGroup[] $userGroupsToCheck |
| 92 |
*/ |
| 93 |
$userGroupsToCheck = array_diff_key($this->userGroupHandler->getFullUserGroups(), $filteredUserGroups); |
| 94 |
|
| 95 |
foreach ($userGroupsToCheck as $userGroupToCheck) { |
| 96 |
if ($userGroupToCheck->isDefaultGroupForObjectType($objectType, $fromTime, $toTime) === true) { |
| 97 |
$userGroupToCheck->addObject( |
| 98 |
$objectType, |
| 99 |
$objectId, |
| 100 |
$this->dateUtil->getDateFromTime($fromTime), |
| 101 |
$this->dateUtil->getDateFromTime($toTime) |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @throws UserGroupAssignmentException |
| 109 |
* @throws UserGroupTypeException |
| 110 |
* @throws Exception |
| 111 |
*/ |
| 112 |
public function assignObjectToUserGroups( |
| 113 |
string $objectType, |
| 114 |
int|string|null $objectId, |
| 115 |
array $addUserGroups, |
| 116 |
array $removeUserGroups, |
| 117 |
array $addDynamicUserGroups |
| 118 |
): void { |
| 119 |
$filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups(); |
| 120 |
$this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups); |
| 121 |
|
| 122 |
if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) { |
| 123 |
$this->setDynamicGroups($objectType, $objectId, $addDynamicUserGroups); |
| 124 |
} else { |
| 125 |
$this->setDefaultGroups($filteredUserGroups, $objectType, $objectId); |
| 126 |
} |
| 127 |
|
| 128 |
$this->userGroupHandler->unsetUserGroupsForObject(); |
| 129 |
} |
| 130 |
} |
| 131 |
|