PluginProbe
User Access Manager / 2.2.18
User Access Manager v2.2.18
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.18, at src/UserGroup/UserGroupHandler.php

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