PluginProbe
User Access Manager / 2.2.23
User Access Manager v2.2.23
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 +126 -32 2.3.142.2.23 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;
@@ -12,32 +25,91 @@
12 25 use UserAccessManager\User\UserHandler;
13 26 use UserAccessManager\Wrapper\Wordpress;
14 27 use WP_User;
15 28
29 +/**
30 + * Class UserGroupHandler
31 + *
32 + * @package UserAccessManager\UserGroup
33 + */
16 34 class UserGroupHandler
17 35 {
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 = [];
36 + /**
37 + * @var Wordpress
38 + */
39 + private $wordpress;
26 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 +
27 91 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
92 + Wordpress $wordpress,
93 + WordpressConfig $wordpressConfig,
94 + MainConfig $mainConfig,
95 + Database $database,
96 + ObjectHandler $objectHandler,
97 + UserHandler $userHandler,
98 + UserGroupFactory $userGroupFactory
35 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;
36 107 }
37 108
38 109 /**
39 - * @return null|UserGroup[]
110 + * Returns all user groups.
111 + * @return UserGroup[]
40 112 * @throws UserGroupTypeException
41 113 */
42 114 public function getUserGroups(): ?array
43 115 {
@@ -56,8 +128,9 @@
56 128 return $this->userGroups;
57 129 }
58 130
59 131 /**
132 + * Returns all dynamic user groups.
60 133 * @return null|DynamicUserGroup[]
61 134 * @throws UserGroupTypeException
62 135 */
63 136 public function getDynamicUserGroups(): ?array
@@ -74,9 +147,9 @@
74 147 $userGroupTypes = implode('\', \'', [DynamicUserGroup::ROLE_TYPE, DynamicUserGroup::USER_TYPE]);
75 148
76 149 $query = "SELECT group_id AS id, group_type AS type
77 150 FROM {$this->database->getUserGroupToObjectTable()}
78 - WHERE group_type IN ('$userGroupTypes')
151 + WHERE group_type IN ('{$userGroupTypes}')
79 152 GROUP BY group_type, group_id";
80 153
81 154 $dynamicUserGroups = (array) $this->database->getResults($query);
82 155
@@ -93,9 +166,10 @@
93 166 return $this->dynamicUserGroups;
94 167 }
95 168
96 169 /**
97 - * @return null|AbstractUserGroup[]
170 + * Returns the full user groups
171 + * @return AbstractUserGroup[]
98 172 * @throws UserGroupTypeException
99 173 */
100 174 public function getFullUserGroups(): ?array
101 175 {
@@ -102,8 +176,9 @@
102 176 return $this->getUserGroups() + $this->getDynamicUserGroups();
103 177 }
104 178
105 179 /**
180 + * Returns the user groups filtered by the user user groups.
106 181 * @return AbstractUserGroup[]
107 182 * @throws UserGroupTypeException
108 183 */
109 184 public function getFilteredUserGroups(): array
@@ -113,11 +188,13 @@
113 188 return array_intersect_key($userGroups, $userUserGroups);
114 189 }
115 190
116 191 /**
192 + * Adds a user group.
193 + * @param UserGroup $userGroup
117 194 * @throws UserGroupTypeException
118 195 */
119 - public function addUserGroup(UserGroup $userGroup): void
196 + public function addUserGroup(UserGroup $userGroup)
120 197 {
121 198 $this->getUserGroups();
122 199 $this->userGroups[$userGroup->getId()] = $userGroup;
123 200 }
@@ -122,12 +199,15 @@
122 199 $this->userGroups[$userGroup->getId()] = $userGroup;
123 200 }
124 201
125 202 /**
203 + * Deletes a user group.
204 + * @param string|int $userGroupId
205 + * @return bool
126 206 * @throws UserGroupTypeException
127 207 * @throws Exception
128 208 */
129 - public function deleteUserGroup(int|string $userGroupId): bool
209 + public function deleteUserGroup($userGroupId): bool
130 210 {
131 211 $userGroups = $this->getUserGroups();
132 212
133 213 if (isset($userGroups[$userGroupId])
@@ -141,17 +221,18 @@
141 221 return false;
142 222 }
143 223
144 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.
145 229 * @return AbstractUserGroup[]
146 230 * @throws UserGroupTypeException
147 231 * @throws Exception
148 232 */
149 - public function getUserGroupsForObject(
150 - string $objectType,
151 - int|string|null $objectId,
152 - bool $ignoreDates = false
153 - ): array {
233 + public function getUserGroupsForObject(string $objectType, $objectId, bool $ignoreDates = false): array
234 + {
154 235 if ($this->objectHandler->isValidObjectType($objectType) === false) {
155 236 return [];
156 237 }
157 238
@@ -172,13 +253,21 @@
172 253
173 254 return $this->objectUserGroups[$ignoreDates][$objectType][$objectId];
174 255 }
175 256
176 - public function unsetUserGroupsForObject(): void
257 + /**
258 + * Unset the object user groups.
259 + */
260 + public function unsetUserGroupsForObject()
177 261 {
178 262 $this->objectUserGroups = [];
179 263 }
180 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 + */
181 270 private function checkUserGroupAccess(UserGroup $userGroup): bool
182 271 {
183 272 $extraIpHeader = $this->mainConfig->getExtraIpHeader();
184 273 $userIp = $extraIpHeader !== null ?
@@ -190,11 +279,14 @@
190 279 || $this->wordpressConfig->atAdminPanel() === true && $userGroup->getWriteAccess() === 'all';
191 280 }
192 281
193 282 /**
283 + * Assigns the dynamic user groups to the user user groups.
284 + * @param WP_User $currentUser
285 + * @param array $userGroupsForUser
194 286 * @throws UserGroupTypeException
195 287 */
196 - private function assignDynamicUserGroupsForUser(WP_User $currentUser, array &$userGroupsForUser): void
288 + private function assignDynamicUserGroupsForUser(WP_User $currentUser, array &$userGroupsForUser)
197 289 {
198 290 $userUserGroup = $this->userGroupFactory->createDynamicUserGroup(
199 291 DynamicUserGroup::USER_TYPE,
200 292 $currentUser->ID
@@ -212,8 +304,9 @@
212 304 }
213 305 }
214 306
215 307 /**
308 + * Returns the user groups for the user.
216 309 * @return AbstractUserGroup[]|null
217 310 * @throws UserGroupTypeException
218 311 */
219 312 public function getUserGroupsForUser(): ?array
@@ -246,16 +339,17 @@
246 339 return $this->userGroupsForUser;
247 340 }
248 341
249 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
250 347 * @return AbstractUserGroup[]
251 348 * @throws UserGroupTypeException
252 349 */
253 - public function getFilteredUserGroupsForObject(
254 - string $objectType,
255 - int|string|null $objectId,
256 - bool $ignoreDates = false
257 - ): array {
350 + public function getFilteredUserGroupsForObject(string $objectType, $objectId, bool $ignoreDates = false): array
351 + {
258 352 $userGroups = $this->getUserGroupsForObject($objectType, $objectId, $ignoreDates);
259 353 $userUserGroups = $this->getUserGroupsForUser() + $this->getDynamicUserGroups();
260 354 return array_intersect_key($userGroups, $userUserGroups);
261 355 }