PluginProbe
User Access Manager / 2.3.4
User Access Manager v2.3.4
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / UserGroup / UserGroupAssignmentHandler.php

UserGroupAssignmentHandler.php in User Access Manager 2.3.4, at src/UserGroup/UserGroupAssignmentHandler.php

132 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $isValid = isset($data[$name]['date']) === true && isset($data[$name]['time']) === true
24 && (string) $data[$name]['date'] !== '' && (string) $data[$name]['time'] !== '';
25
26 return ($isValid === true) ? ((string) $data[$name]['date']) . 'T' . $data[$name]['time'] : null;
27 }
28
29 /**
30 * @throws Exception
31 */
32 private function setUserGroups(
33 array $filteredUserGroups,
34 string $objectType,
35 int|string $objectId,
36 array $addUserGroups,
37 array $removeUserGroups
38 ): void {
39 /** @var UserGroup $userGroup */
40 foreach ($filteredUserGroups as $groupId => $userGroup) {
41 if (isset($removeUserGroups[$groupId]) === true) {
42 $userGroup->removeObject($objectType, $objectId);
43 }
44
45 if (isset($addUserGroups[$groupId]['id']) === true
46 && (int) $addUserGroups[$groupId]['id'] === (int) $groupId
47 ) {
48 $userGroup->addObject(
49 $objectType,
50 $objectId,
51 $this->getDateParameter($addUserGroups[$groupId], 'fromDate'),
52 $this->getDateParameter($addUserGroups[$groupId], 'toDate')
53 );
54 }
55 }
56 }
57
58 /**
59 * @throws UserGroupAssignmentException
60 * @throws UserGroupTypeException
61 */
62 private function setDynamicGroups(string $objectType, int|string $objectId, array $addDynamicUserGroups): void
63 {
64 foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
65 $dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
66
67 if (count($dynamicUserGroupData) === 2
68 && $addDynamicUserGroup['id'] === $dynamicUserGroupKey
69 ) {
70 $dynamicUserGroup = $this->userGroupFactory->createDynamicUserGroup(
71 $dynamicUserGroupData[0],
72 $dynamicUserGroupData[1]
73 );
74
75 $dynamicUserGroup->addObject(
76 $objectType,
77 $objectId,
78 $this->getDateParameter($addDynamicUserGroup, 'fromDate'),
79 $this->getDateParameter($addDynamicUserGroup, 'toDate')
80 );
81 }
82 }
83 }
84
85 /**
86 * @throws UserGroupTypeException
87 * @throws Exception
88 */
89 private function setDefaultGroups(array $filteredUserGroups, string $objectType, int|string $objectId): void
90 {
91 /**
92 * @var UserGroup[] $userGroupsToCheck
93 */
94 $userGroupsToCheck = array_diff_key($this->userGroupHandler->getFullUserGroups(), $filteredUserGroups);
95
96 foreach ($userGroupsToCheck as $userGroupToCheck) {
97 if ($userGroupToCheck->isDefaultGroupForObjectType($objectType, $fromTime, $toTime) === true) {
98 $userGroupToCheck->addObject(
99 $objectType,
100 $objectId,
101 $this->dateUtil->getDateFromTime($fromTime),
102 $this->dateUtil->getDateFromTime($toTime)
103 );
104 }
105 }
106 }
107
108 /**
109 * @throws UserGroupAssignmentException
110 * @throws UserGroupTypeException
111 * @throws Exception
112 */
113 public function assignObjectToUserGroups(
114 string $objectType,
115 int|string $objectId,
116 array $addUserGroups,
117 array $removeUserGroups,
118 array $addDynamicUserGroups
119 ): void {
120 $filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups();
121 $this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
122
123 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {
124 $this->setDynamicGroups($objectType, $objectId, $addDynamicUserGroups);
125 } else {
126 $this->setDefaultGroups($filteredUserGroups, $objectType, $objectId);
127 }
128
129 $this->userGroupHandler->unsetUserGroupsForObject();
130 }
131 }
132