PluginProbe
User Access Manager / 2.3.3
User Access Manager v2.3.3
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.3, at src/UserGroup/UserGroupAssignmentHandler.php

131 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 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 $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 $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 $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