PluginProbe
User Access Manager / 2.2.15
User Access Manager v2.2.15
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.15, at src/UserGroup/UserGroupHandler.php

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