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

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