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

206 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UserGroupAssignmentHandler.php
4 *
5 * The UserGroupAssignmentHandler class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\UserGroup;
19
20 use Exception;
21 use UserAccessManager\User\UserHandler;
22 use UserAccessManager\Util\DateUtil;
23
24 /**
25 * Class UserGroupAssignmentHandler
26 *
27 * @package UserAccessManager\UserGroup
28 */
29 class UserGroupAssignmentHandler
30 {
31 /**
32 * @var DateUtil
33 */
34 protected $dateUtil;
35
36 /**
37 * @var UserHandler
38 */
39 protected $userHandler;
40
41 /**
42 * @var UserGroupHandler
43 */
44 protected $userGroupHandler;
45
46 /**
47 * @var UserGroupFactory
48 */
49 protected $userGroupFactory;
50
51 /**
52 * UserGroupAssignmentHandler constructor.
53 * @param DateUtil $dateUtil
54 * @param UserHandler $userHandler
55 * @param UserGroupHandler $userGroupHandler
56 * @param UserGroupFactory $userGroupFactory
57 */
58 public function __construct(
59 DateUtil $dateUtil,
60 UserHandler $userHandler,
61 UserGroupHandler $userGroupHandler,
62 UserGroupFactory $userGroupFactory
63 ) {
64 $this->dateUtil = $dateUtil;
65 $this->userHandler = $userHandler;
66 $this->userGroupHandler = $userGroupHandler;
67 $this->userGroupFactory = $userGroupFactory;
68 }
69
70 /**
71 * Processes the date parameter.
72 * @param array $data
73 * @param string $name
74 * @return null|string
75 */
76 private function getDateParameter(array $data, string $name): ?string
77 {
78 $isValid = isset($data[$name]['date']) === true && isset($data[$name]['time']) === true
79 && (string) $data[$name]['date'] !== '' && (string) $data[$name]['time'] !== '';
80
81 return ($isValid === true) ? ((string) $data[$name]['date']) . 'T' . $data[$name]['time'] : null;
82 }
83
84 /**
85 * Updates the user groups for the given object.
86 * @param AbstractUserGroup[] $filteredUserGroups
87 * @param string $objectType
88 * @param int|string $objectId
89 * @param array $addUserGroups
90 * @param array $removeUserGroups
91 * @throws Exception
92 */
93 private function setUserGroups(
94 array $filteredUserGroups,
95 string $objectType,
96 $objectId,
97 array $addUserGroups,
98 array $removeUserGroups
99 ) {
100 foreach ($filteredUserGroups as $groupId => $userGroup) {
101 if (isset($removeUserGroups[$groupId]) === true) {
102 $userGroup->removeObject($objectType, $objectId);
103 }
104
105 if (isset($addUserGroups[$groupId]['id']) === true
106 && (int) $addUserGroups[$groupId]['id'] === (int) $groupId
107 ) {
108 $userGroup->addObject(
109 $objectType,
110 $objectId,
111 $this->getDateParameter($addUserGroups[$groupId], 'fromDate'),
112 $this->getDateParameter($addUserGroups[$groupId], 'toDate')
113 );
114 }
115 }
116 }
117
118 /**
119 * Sets the dynamic user groups for the given object.
120 * @param string $objectType
121 * @param int|string $objectId
122 * @param array $addDynamicUserGroups
123 * @throws UserGroupAssignmentException
124 * @throws UserGroupTypeException
125 */
126 private function setDynamicGroups(string $objectType, $objectId, array $addDynamicUserGroups)
127 {
128 foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
129 $dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
130
131 if (count($dynamicUserGroupData) === 2
132 && $addDynamicUserGroup['id'] === $dynamicUserGroupKey
133 ) {
134 $dynamicUserGroup = $this->userGroupFactory->createDynamicUserGroup(
135 $dynamicUserGroupData[0],
136 $dynamicUserGroupData[1]
137 );
138
139 $dynamicUserGroup->addObject(
140 $objectType,
141 $objectId,
142 $this->getDateParameter($addDynamicUserGroup, 'fromDate'),
143 $this->getDateParameter($addDynamicUserGroup, 'toDate')
144 );
145 }
146 }
147 }
148
149 /**
150 * Sets the default user groups for the given object.
151 * @param AbstractUserGroup[] $filteredUserGroups
152 * @param string $objectType
153 * @param int|string $objectId
154 * @throws UserGroupTypeException
155 * @throws Exception
156 */
157 private function setDefaultGroups(array $filteredUserGroups, string $objectType, $objectId)
158 {
159 /**
160 * @var UserGroup[] $userGroupsToCheck
161 */
162 $userGroupsToCheck = array_diff_key($this->userGroupHandler->getFullUserGroups(), $filteredUserGroups);
163
164 foreach ($userGroupsToCheck as $userGroupToCheck) {
165 if ($userGroupToCheck->isDefaultGroupForObjectType($objectType, $fromTime, $toTime) === true) {
166 $userGroupToCheck->addObject(
167 $objectType,
168 $objectId,
169 $this->dateUtil->getDateFromTime($fromTime),
170 $this->dateUtil->getDateFromTime($toTime)
171 );
172 }
173 }
174 }
175
176 /**
177 * Saves the object data to the database.
178 * @param string $objectType
179 * @param int|string $objectId
180 * @param array $addUserGroups
181 * @param array $removeUserGroups
182 * @param array $addDynamicUserGroups
183 * @throws UserGroupAssignmentException
184 * @throws UserGroupTypeException
185 * @throws Exception
186 */
187 public function assignObjectToUserGroups(
188 string $objectType,
189 $objectId,
190 array $addUserGroups,
191 array $removeUserGroups,
192 array $addDynamicUserGroups
193 ) {
194 $filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups();
195 $this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
196
197 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {
198 $this->setDynamicGroups($objectType, $objectId, $addDynamicUserGroups);
199 } else {
200 $this->setDefaultGroups($filteredUserGroups, $objectType, $objectId);
201 }
202
203 $this->userGroupHandler->unsetUserGroupsForObject();
204 }
205 }
206