PluginProbe
User Access Manager / 2.3.4
User Access Manager v2.3.4
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 +87 -106 trunk2.3.4 View file →
@@ -35,101 +35,83 @@
35 35 ) {
36 36 }
37 37
38 38 /**
39 - * @return UserGroup[]
39 + * @return null|UserGroup[]
40 40 * @throws UserGroupTypeException
41 41 */
42 - public function getUserGroups(): array
42 + public function getUserGroups(): ?array
43 43 {
44 - return $this->userGroups ??= $this->loadUserGroups();
45 - }
44 + if ($this->userGroups === null) {
45 + $this->userGroups = [];
46 46
47 - /**
48 - * @return UserGroup[]
49 - * @throws UserGroupTypeException
50 - */
51 - private function loadUserGroups(): array
52 - {
53 - $query = "SELECT * FROM `{$this->database->getUserGroupTable()}`";
54 - $userGroups = [];
47 + $query = "SELECT ID FROM {$this->database->getUserGroupTable()}";
48 + $userGroups = (array) $this->database->getResults($query);
55 49
56 - foreach ((array) $this->database->getResults($query) as $databaseUserGroup) {
57 - $userGroup = $this->userGroupFactory->createUserGroupFromDatabaseRow($databaseUserGroup);
58 - $userGroups[$userGroup->getId()] = $userGroup;
50 + foreach ($userGroups as $userGroup) {
51 + $group = $this->userGroupFactory->createUserGroup($userGroup->ID);
52 + $this->userGroups[$group->getId()] = $group;
53 + }
59 54 }
60 55
61 - return $userGroups;
56 + return $this->userGroups;
62 57 }
63 58
64 59 /**
65 - * @return DynamicUserGroup[]
60 + * @return null|DynamicUserGroup[]
66 61 * @throws UserGroupTypeException
67 62 */
68 - public function getDynamicUserGroups(): array
63 + public function getDynamicUserGroups(): ?array
69 64 {
70 - return $this->dynamicUserGroups ??= $this->loadDynamicUserGroups();
71 - }
65 + if ($this->dynamicUserGroups === null) {
66 + $this->dynamicUserGroups = [];
72 67
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];
68 + $notLoggedInUserGroup = $this->userGroupFactory->createDynamicUserGroup(
69 + DynamicUserGroup::USER_TYPE,
70 + DynamicUserGroup::NOT_LOGGED_IN_USER_ID
71 + );
72 + $this->dynamicUserGroups[$notLoggedInUserGroup->getId()] = $notLoggedInUserGroup;
84 73
85 - $userGroupTypes = implode('\', \'', [DynamicUserGroup::ROLE_TYPE, DynamicUserGroup::USER_TYPE]);
74 + $userGroupTypes = implode('\', \'', [DynamicUserGroup::ROLE_TYPE, DynamicUserGroup::USER_TYPE]);
86 75
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`";
76 + $query = "SELECT group_id AS id, group_type AS type
77 + FROM {$this->database->getUserGroupToObjectTable()}
78 + WHERE group_type IN ('$userGroupTypes')
79 + GROUP BY group_type, group_id";
91 80
92 - foreach ((array) $this->database->getResults($query) as $databaseUserGroup) {
93 - $userGroup = $this->userGroupFactory->createDynamicUserGroup(
94 - $databaseUserGroup->type,
95 - $databaseUserGroup->id
96 - );
81 + $dynamicUserGroups = (array) $this->database->getResults($query);
97 82
98 - $dynamicUserGroups[$userGroup->getId()] = $userGroup;
83 + foreach ($dynamicUserGroups as $dynamicUserGroup) {
84 + $group = $this->userGroupFactory->createDynamicUserGroup(
85 + $dynamicUserGroup->type,
86 + $dynamicUserGroup->id
87 + );
88 +
89 + $this->dynamicUserGroups[$group->getId()] = $group;
90 + }
99 91 }
100 92
101 - return $dynamicUserGroups;
93 + return $this->dynamicUserGroups;
102 94 }
103 95
104 96 /**
105 - * @return AbstractUserGroup[]
97 + * @return null|AbstractUserGroup[]
106 98 * @throws UserGroupTypeException
107 99 */
108 - public function getFullUserGroups(): array
100 + public function getFullUserGroups(): ?array
109 101 {
110 102 return $this->getUserGroups() + $this->getDynamicUserGroups();
111 103 }
112 104
113 105 /**
114 - * Reduces the given user groups to those the current user is allowed to see.
115 - *
116 - * @param AbstractUserGroup[] $userGroups
117 106 * @return AbstractUserGroup[]
118 107 * @throws UserGroupTypeException
119 108 */
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 109 public function getFilteredUserGroups(): array
130 110 {
131 - return $this->filterByUserGroupsOfUser($this->getFullUserGroups());
111 + $userGroups = $this->getFullUserGroups();
112 + $userUserGroups = $this->getUserGroupsForUser() + $this->getDynamicUserGroups();
113 + return array_intersect_key($userGroups, $userUserGroups);
132 114 }
133 115
134 116 /**
135 117 * @throws UserGroupTypeException
@@ -147,15 +129,17 @@
147 129 public function deleteUserGroup(int|string $userGroupId): bool
148 130 {
149 131 $userGroups = $this->getUserGroups();
150 132
151 - if (isset($userGroups[$userGroupId]) === false || $userGroups[$userGroupId]->delete() === false) {
152 - return false;
133 + if (isset($userGroups[$userGroupId])
134 + && $userGroups[$userGroupId]->delete() === true
135 + ) {
136 + unset($this->userGroups[$userGroupId]);
137 +
138 + return true;
153 139 }
154 140
155 - unset($this->userGroups[$userGroupId]);
156 -
157 - return true;
141 + return false;
158 142 }
159 143
160 144 /**
161 145 * @return AbstractUserGroup[]
@@ -161,13 +145,10 @@
161 145 * @return AbstractUserGroup[]
162 146 * @throws UserGroupTypeException
163 147 * @throws Exception
164 148 */
165 - public function getUserGroupsForObject(
166 - string $objectType,
167 - int|string|null $objectId,
168 - bool $ignoreDates = false
169 - ): array {
149 + public function getUserGroupsForObject(string $objectType, int|string $objectId, bool $ignoreDates = false): array
150 + {
170 151 if ($this->objectHandler->isValidObjectType($objectType) === false) {
171 152 return [];
172 153 }
173 154
@@ -172,10 +153,11 @@
172 153 }
173 154
174 155 if (isset($this->objectUserGroups[$ignoreDates][$objectType][$objectId]) === false) {
175 156 $objectUserGroups = [];
157 + $userGroups = $this->getFullUserGroups();
176 158
177 - foreach ($this->getFullUserGroups() as $userGroup) {
159 + foreach ($userGroups as $userGroup) {
178 160 $userGroup->setIgnoreDates($ignoreDates);
179 161
180 162 if ($userGroup->isObjectMember($objectType, $objectId) === true) {
181 163 $objectUserGroups[$userGroup->getId()] = $userGroup;
@@ -192,25 +174,21 @@
192 174 {
193 175 $this->objectUserGroups = [];
194 176 }
195 177
196 - private function getUserIp(): string
178 + private function checkUserGroupAccess(UserGroup $userGroup): bool
197 179 {
198 180 $extraIpHeader = $this->mainConfig->getExtraIpHeader();
181 + $userIp = $extraIpHeader !== null ?
182 + $_SERVER[$extraIpHeader] ?? ($_SERVER['REMOTE_ADDR'] ?? '') :
183 + $_SERVER['REMOTE_ADDR'] ?? '';
199 184
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())
185 + return $this->userHandler->isIpInRange($userIp, $userGroup->getIpRangeArray())
207 186 || $this->wordpressConfig->atAdminPanel() === false && $userGroup->getReadAccess() === 'all'
208 187 || $this->wordpressConfig->atAdminPanel() === true && $userGroup->getWriteAccess() === 'all';
209 188 }
210 189
211 190 /**
212 - * @param AbstractUserGroup[] $userGroupsForUser
213 191 * @throws UserGroupTypeException
214 192 */
215 193 private function assignDynamicUserGroupsForUser(WP_User $currentUser, array &$userGroupsForUser): void
216 194 {
@@ -218,51 +196,52 @@
218 196 DynamicUserGroup::USER_TYPE,
219 197 $currentUser->ID
220 198 );
221 199 $userGroupsForUser[$userUserGroup->getId()] = $userUserGroup;
200 + $roles = $this->userHandler->getUserRole($currentUser);
222 201
223 - foreach ($this->userHandler->getUserRole($currentUser) as $role) {
224 - $roleUserGroup = $this->userGroupFactory->createDynamicUserGroup(DynamicUserGroup::ROLE_TYPE, $role);
225 - $userGroupsForUser[$roleUserGroup->getId()] = $roleUserGroup;
202 + foreach ($roles as $role) {
203 + $group = $this->userGroupFactory->createDynamicUserGroup(
204 + DynamicUserGroup::ROLE_TYPE,
205 + $role
206 + );
207 +
208 + $userGroupsForUser[$group->getId()] = $group;
226 209 }
227 210 }
228 211
229 212 /**
230 - * @return AbstractUserGroup[]
213 + * @return AbstractUserGroup[]|null
231 214 * @throws UserGroupTypeException
232 215 */
233 - public function getUserGroupsForUser(): array
216 + public function getUserGroupsForUser(): ?array
234 217 {
235 218 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {
236 219 return $this->getUserGroups();
237 220 }
238 221
239 - return $this->userGroupsForUser ??= $this->loadUserGroupsForUser();
240 - }
222 + if ($this->userGroupsForUser === null) {
223 + $currentUser = $this->wordpress->getCurrentUser();
224 + $userGroupsForUser = $this->getUserGroupsForObject(
225 + ObjectHandler::GENERAL_USER_OBJECT_TYPE,
226 + $currentUser->ID
227 + );
241 228
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 - );
229 + $this->assignDynamicUserGroupsForUser($currentUser, $userGroupsForUser);
230 + $userGroups = $this->getUserGroups();
253 231
254 - $this->assignDynamicUserGroupsForUser($currentUser, $userGroupsForUser);
232 + foreach ($userGroups as $userGroup) {
233 + if (isset($userGroupsForUser[$userGroup->getId()]) === false
234 + && $this->checkUserGroupAccess($userGroup) === true
235 + ) {
236 + $userGroupsForUser[$userGroup->getId()] = $userGroup;
237 + }
238 + }
255 239
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 - }
240 + $this->userGroupsForUser = $userGroupsForUser;
262 241 }
263 242
264 - return $userGroupsForUser;
243 + return $this->userGroupsForUser;
265 244 }
266 245
267 246 /**
268 247 * @return AbstractUserGroup[]
@@ -269,10 +248,12 @@
269 248 * @throws UserGroupTypeException
270 249 */
271 250 public function getFilteredUserGroupsForObject(
272 251 string $objectType,
273 - int|string|null $objectId,
252 + int|string $objectId,
274 253 bool $ignoreDates = false
275 254 ): array {
276 - return $this->filterByUserGroupsOfUser($this->getUserGroupsForObject($objectType, $objectId, $ignoreDates));
255 + $userGroups = $this->getUserGroupsForObject($objectType, $objectId, $ignoreDates);
256 + $userUserGroups = $this->getUserGroupsForUser() + $this->getDynamicUserGroups();
257 + return array_intersect_key($userGroups, $userUserGroups);
277 258 }
278 259 }