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