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