PluginProbe
User Access Manager / 2.2.4
User Access Manager v2.2.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 / UserGroupHandler.php

UserGroupHandler.php in User Access Manager 2.2.4, at src/UserGroup/UserGroupHandler.php

351 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UserGroupHandler.php
4 *
5 * The UserGroupHandler 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\Config\WordpressConfig;
22 use UserAccessManager\Database\Database;
23 use UserAccessManager\Object\ObjectHandler;
24 use UserAccessManager\User\UserHandler;
25 use UserAccessManager\Wrapper\Wordpress;
26 use WP_User;
27
28 /**
29 * Class UserGroupHandler
30 *
31 * @package UserAccessManager\UserGroup
32 */
33 class UserGroupHandler
34 {
35 /**
36 * @var Wordpress
37 */
38 private $wordpress;
39
40 /**
41 * @var WordpressConfig
42 */
43 private $wordpressConfig;
44
45 /**
46 * @var Database
47 */
48 private $database;
49
50 /**
51 * @var ObjectHandler
52 */
53 private $objectHandler;
54
55 /**
56 * @var UserHandler
57 */
58 private $userHandler;
59
60 /**
61 * @var UserGroupFactory
62 */
63 private $userGroupFactory;
64
65 /**
66 * @var null|UserGroup[]
67 */
68 private $userGroups = null;
69
70 /**
71 * @var null|DynamicUserGroup[]
72 */
73 private $dynamicUserGroups = null;
74
75 /**
76 * @var null|UserGroup[]
77 */
78 private $filteredUserGroups = null;
79
80 /**
81 * @var null|AbstractUserGroup[]
82 */
83 private $userGroupsForUser = null;
84
85 /**
86 * @var array
87 */
88 private $objectUserGroups = [];
89
90 public function __construct(
91 Wordpress $wordpress,
92 WordpressConfig $wordpressConfig,
93 Database $database,
94 ObjectHandler $objectHandler,
95 UserHandler $userHandler,
96 UserGroupFactory $userGroupFactory
97 ) {
98 $this->wordpress = $wordpress;
99 $this->wordpressConfig = $wordpressConfig;
100 $this->database = $database;
101 $this->objectHandler = $objectHandler;
102 $this->userHandler = $userHandler;
103 $this->userGroupFactory = $userGroupFactory;
104 }
105
106 /**
107 * Returns all user groups.
108 * @return UserGroup[]
109 * @throws UserGroupTypeException
110 */
111 public function getUserGroups(): ?array
112 {
113 if ($this->userGroups === null) {
114 $this->userGroups = [];
115
116 $query = "SELECT ID FROM {$this->database->getUserGroupTable()}";
117 $userGroups = (array) $this->database->getResults($query);
118
119 foreach ($userGroups as $userGroup) {
120 $group = $this->userGroupFactory->createUserGroup($userGroup->ID);
121 $this->userGroups[$group->getId()] = $group;
122 }
123 }
124
125 return $this->userGroups;
126 }
127
128 /**
129 * Returns all dynamic user groups.
130 * @return null|DynamicUserGroup[]
131 * @throws UserGroupTypeException
132 */
133 public function getDynamicUserGroups(): ?array
134 {
135 if ($this->dynamicUserGroups === null) {
136 $this->dynamicUserGroups = [];
137
138 $notLoggedInUserGroup = $this->userGroupFactory->createDynamicUserGroup(
139 DynamicUserGroup::USER_TYPE,
140 DynamicUserGroup::NOT_LOGGED_IN_USER_ID
141 );
142 $this->dynamicUserGroups[$notLoggedInUserGroup->getId()] = $notLoggedInUserGroup;
143
144 $userGroupTypes = implode('\', \'', [DynamicUserGroup::ROLE_TYPE, DynamicUserGroup::USER_TYPE]);
145
146 $query = "SELECT group_id AS id, group_type AS type
147 FROM {$this->database->getUserGroupToObjectTable()}
148 WHERE group_type IN ('{$userGroupTypes}')
149 GROUP BY group_type, group_id";
150
151 $dynamicUserGroups = (array) $this->database->getResults($query);
152
153 foreach ($dynamicUserGroups as $dynamicUserGroup) {
154 $group = $this->userGroupFactory->createDynamicUserGroup(
155 $dynamicUserGroup->type,
156 $dynamicUserGroup->id
157 );
158
159 $this->dynamicUserGroups[$group->getId()] = $group;
160 }
161 }
162
163 return $this->dynamicUserGroups;
164 }
165
166 /**
167 * Returns the full user groups
168 * @return AbstractUserGroup[]
169 * @throws UserGroupTypeException
170 */
171 public function getFullUserGroups(): ?array
172 {
173 return $this->getUserGroups() + $this->getDynamicUserGroups();
174 }
175
176 /**
177 * Returns the user groups filtered by the user user groups.
178 * @return AbstractUserGroup[]
179 * @throws UserGroupTypeException
180 */
181 public function getFilteredUserGroups(): array
182 {
183 $userGroups = $this->getFullUserGroups();
184 $userUserGroups = $this->getUserGroupsForUser() + $this->getDynamicUserGroups();
185 return array_intersect_key($userGroups, $userUserGroups);
186 }
187
188 /**
189 * Adds a user group.
190 * @param UserGroup $userGroup
191 * @throws UserGroupTypeException
192 */
193 public function addUserGroup(UserGroup $userGroup)
194 {
195 $this->getUserGroups();
196 $this->userGroups[$userGroup->getId()] = $userGroup;
197 }
198
199 /**
200 * Deletes a user group.
201 * @param string|int $userGroupId
202 * @return bool
203 * @throws UserGroupTypeException
204 * @throws Exception
205 */
206 public function deleteUserGroup($userGroupId): bool
207 {
208 $userGroups = $this->getUserGroups();
209
210 if (isset($userGroups[$userGroupId])
211 && $userGroups[$userGroupId]->delete() === true
212 ) {
213 unset($this->userGroups[$userGroupId]);
214
215 return true;
216 }
217
218 return false;
219 }
220
221 /**
222 * Returns the user groups for the given object.
223 * @param string $objectType The object type.
224 * @param int|string $objectId The id of the object.
225 * @param bool $ignoreDates If true we ignore the dates for the object assignment.
226 * @return AbstractUserGroup[]
227 * @throws UserGroupTypeException
228 * @throws Exception
229 */
230 public function getUserGroupsForObject(string $objectType, $objectId, bool $ignoreDates = false): array
231 {
232 if ($this->objectHandler->isValidObjectType($objectType) === false) {
233 return [];
234 }
235
236 if (isset($this->objectUserGroups[$ignoreDates][$objectType][$objectId]) === false) {
237 $objectUserGroups = [];
238 $userGroups = $this->getFullUserGroups();
239
240 foreach ($userGroups as $userGroup) {
241 $userGroup->setIgnoreDates($ignoreDates);
242
243 if ($userGroup->isObjectMember($objectType, $objectId) === true) {
244 $objectUserGroups[$userGroup->getId()] = $userGroup;
245 }
246 }
247
248 $this->objectUserGroups[$ignoreDates][$objectType][$objectId] = $objectUserGroups;
249 }
250
251 return $this->objectUserGroups[$ignoreDates][$objectType][$objectId];
252 }
253
254 /**
255 * Unset the object user groups.
256 */
257 public function unsetUserGroupsForObject()
258 {
259 $this->objectUserGroups = [];
260 }
261
262 /**
263 * Checks if the current user is in the ip range or if the user group is public.
264 * @param UserGroup $userGroup
265 * @return bool
266 */
267 private function checkUserGroupAccess(UserGroup $userGroup): bool
268 {
269 $userIp = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : $_SERVER['REMOTE_ADDR'];
270
271 return $this->userHandler->isIpInRange($userIp, $userGroup->getIpRangeArray())
272 || $this->wordpressConfig->atAdminPanel() === false && $userGroup->getReadAccess() === 'all'
273 || $this->wordpressConfig->atAdminPanel() === true && $userGroup->getWriteAccess() === 'all';
274 }
275
276 /**
277 * Assigns the dynamic user groups to the user user groups.
278 * @param WP_User $currentUser
279 * @param array $userGroupsForUser
280 * @throws UserGroupTypeException
281 */
282 private function assignDynamicUserGroupsForUser(WP_User $currentUser, array &$userGroupsForUser)
283 {
284 $userUserGroup = $this->userGroupFactory->createDynamicUserGroup(
285 DynamicUserGroup::USER_TYPE,
286 $currentUser->ID
287 );
288 $userGroupsForUser[$userUserGroup->getId()] = $userUserGroup;
289 $roles = $this->userHandler->getUserRole($currentUser);
290
291 foreach ($roles as $role) {
292 $group = $this->userGroupFactory->createDynamicUserGroup(
293 DynamicUserGroup::ROLE_TYPE,
294 $role
295 );
296
297 $userGroupsForUser[$group->getId()] = $group;
298 }
299 }
300
301 /**
302 * Returns the user groups for the user.
303 * @return AbstractUserGroup[]
304 * @throws UserGroupTypeException
305 */
306 public function getUserGroupsForUser(): ?array
307 {
308 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {
309 return $this->getUserGroups();
310 }
311
312 if ($this->userGroupsForUser === null) {
313 $currentUser = $this->wordpress->getCurrentUser();
314 $userGroupsForUser = $this->getUserGroupsForObject(
315 ObjectHandler::GENERAL_USER_OBJECT_TYPE,
316 $currentUser->ID
317 );
318
319 $this->assignDynamicUserGroupsForUser($currentUser, $userGroupsForUser);
320 $userGroups = $this->getUserGroups();
321
322 foreach ($userGroups as $userGroup) {
323 if (isset($userGroupsForUser[$userGroup->getId()]) === false
324 && $this->checkUserGroupAccess($userGroup) === true
325 ) {
326 $userGroupsForUser[$userGroup->getId()] = $userGroup;
327 }
328 }
329
330 $this->userGroupsForUser = $userGroupsForUser;
331 }
332
333 return $this->userGroupsForUser;
334 }
335
336 /**
337 * Returns the user groups for the object filtered by the user user groups.
338 * @param string $objectType
339 * @param string|int $objectId
340 * @param bool $ignoreDates
341 * @return AbstractUserGroup[]
342 * @throws UserGroupTypeException
343 */
344 public function getFilteredUserGroupsForObject(string $objectType, $objectId, bool $ignoreDates = false): array
345 {
346 $userGroups = $this->getUserGroupsForObject($objectType, $objectId, $ignoreDates);
347 $userUserGroups = $this->getUserGroupsForUser() + $this->getDynamicUserGroups();
348 return array_intersect_key($userGroups, $userUserGroups);
349 }
350 }
351