PluginProbe
User Access Manager / 2.3.17
User Access Manager v2.3.17
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 +84 -100 trunk2.3.17 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 * FROM {$this->database->getUserGroupTable()}";
48 + $databaseUserGroups = (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 ($databaseUserGroups as $databaseUserGroup) {
51 + $userGroup = $this->userGroupFactory->createUserGroupFromDatabaseRow($databaseUserGroup);
52 + $this->userGroups[$userGroup->getId()] = $userGroup;
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[]
@@ -172,10 +156,11 @@
172 156 }
173 157
174 158 if (isset($this->objectUserGroups[$ignoreDates][$objectType][$objectId]) === false) {
175 159 $objectUserGroups = [];
160 + $userGroups = $this->getFullUserGroups();
176 161
177 - foreach ($this->getFullUserGroups() as $userGroup) {
162 + foreach ($userGroups as $userGroup) {
178 163 $userGroup->setIgnoreDates($ignoreDates);
179 164
180 165 if ($userGroup->isObjectMember($objectType, $objectId) === true) {
181 166 $objectUserGroups[$userGroup->getId()] = $userGroup;
@@ -192,25 +177,21 @@
192 177 {
193 178 $this->objectUserGroups = [];
194 179 }
195 180
196 - private function getUserIp(): string
181 + private function checkUserGroupAccess(UserGroup $userGroup): bool
197 182 {
198 183 $extraIpHeader = $this->mainConfig->getExtraIpHeader();
184 + $userIp = $extraIpHeader !== null ?
185 + $_SERVER[$extraIpHeader] ?? ($_SERVER['REMOTE_ADDR'] ?? '') :
186 + $_SERVER['REMOTE_ADDR'] ?? '';
199 187
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())
188 + return $this->userHandler->isIpInRange($userIp, $userGroup->getIpRangeArray())
207 189 || $this->wordpressConfig->atAdminPanel() === false && $userGroup->getReadAccess() === 'all'
208 190 || $this->wordpressConfig->atAdminPanel() === true && $userGroup->getWriteAccess() === 'all';
209 191 }
210 192
211 193 /**
212 - * @param AbstractUserGroup[] $userGroupsForUser
213 194 * @throws UserGroupTypeException
214 195 */
215 196 private function assignDynamicUserGroupsForUser(WP_User $currentUser, array &$userGroupsForUser): void
216 197 {
@@ -218,51 +199,52 @@
218 199 DynamicUserGroup::USER_TYPE,
219 200 $currentUser->ID
220 201 );
221 202 $userGroupsForUser[$userUserGroup->getId()] = $userUserGroup;
203 + $roles = $this->userHandler->getUserRole($currentUser);
222 204
223 - foreach ($this->userHandler->getUserRole($currentUser) as $role) {
224 - $roleUserGroup = $this->userGroupFactory->createDynamicUserGroup(DynamicUserGroup::ROLE_TYPE, $role);
225 - $userGroupsForUser[$roleUserGroup->getId()] = $roleUserGroup;
205 + foreach ($roles as $role) {
206 + $group = $this->userGroupFactory->createDynamicUserGroup(
207 + DynamicUserGroup::ROLE_TYPE,
208 + $role
209 + );
210 +
211 + $userGroupsForUser[$group->getId()] = $group;
226 212 }
227 213 }
228 214
229 215 /**
230 - * @return AbstractUserGroup[]
216 + * @return AbstractUserGroup[]|null
231 217 * @throws UserGroupTypeException
232 218 */
233 - public function getUserGroupsForUser(): array
219 + public function getUserGroupsForUser(): ?array
234 220 {
235 221 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {
236 222 return $this->getUserGroups();
237 223 }
238 224
239 - return $this->userGroupsForUser ??= $this->loadUserGroupsForUser();
240 - }
225 + if ($this->userGroupsForUser === null) {
226 + $currentUser = $this->wordpress->getCurrentUser();
227 + $userGroupsForUser = $this->getUserGroupsForObject(
228 + ObjectHandler::GENERAL_USER_OBJECT_TYPE,
229 + $currentUser->ID
230 + );
241 231
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 - );
232 + $this->assignDynamicUserGroupsForUser($currentUser, $userGroupsForUser);
233 + $userGroups = $this->getUserGroups();
253 234
254 - $this->assignDynamicUserGroupsForUser($currentUser, $userGroupsForUser);
235 + foreach ($userGroups as $userGroup) {
236 + if (isset($userGroupsForUser[$userGroup->getId()]) === false
237 + && $this->checkUserGroupAccess($userGroup) === true
238 + ) {
239 + $userGroupsForUser[$userGroup->getId()] = $userGroup;
240 + }
241 + }
255 242
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 - }
243 + $this->userGroupsForUser = $userGroupsForUser;
262 244 }
263 245
264 - return $userGroupsForUser;
246 + return $this->userGroupsForUser;
265 247 }
266 248
267 249 /**
268 250 * @return AbstractUserGroup[]
@@ -272,7 +254,9 @@
272 254 string $objectType,
273 255 int|string|null $objectId,
274 256 bool $ignoreDates = false
275 257 ): array {
276 - return $this->filterByUserGroupsOfUser($this->getUserGroupsForObject($objectType, $objectId, $ignoreDates));
258 + $userGroups = $this->getUserGroupsForObject($objectType, $objectId, $ignoreDates);
259 + $userUserGroups = $this->getUserGroupsForUser() + $this->getDynamicUserGroups();
260 + return array_intersect_key($userGroups, $userUserGroups);
277 261 }
278 262 }