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
user-access-manager / src / Access / AccessHandler.php

AccessHandler.php in User Access Manager 2.2.13, at src/Access/AccessHandler.php

327 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AccessHandler.php
4 *
5 * The AccessHandler 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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Access;
19
20 use Exception;
21 use UserAccessManager\Config\MainConfig;
22 use UserAccessManager\Database\Database;
23 use UserAccessManager\Object\ObjectHandler;
24 use UserAccessManager\User\UserHandler;
25 use UserAccessManager\UserGroup\AbstractUserGroup;
26 use UserAccessManager\UserGroup\DynamicUserGroup;
27 use UserAccessManager\UserGroup\UserGroupHandler;
28 use UserAccessManager\UserGroup\UserGroupTypeException;
29 use UserAccessManager\Wrapper\Wordpress;
30
31 /**
32 * Class AccessHandler
33 * @package UserAccessManager\AccessHandler
34 */
35 class AccessHandler
36 {
37 /**
38 * @var Wordpress
39 */
40 private $wordpress;
41
42 /**
43 * @var MainConfig
44 */
45 private $mainConfig;
46
47 /**
48 * @var Database
49 */
50 private $database;
51
52 /**
53 * @var ObjectHandler
54 */
55 private $objectHandler;
56
57 /**
58 * @var UserHandler
59 */
60 private $userHandler;
61
62 /**
63 * @var UserGroupHandler
64 */
65 private $userGroupHandler;
66
67 /**
68 * @var null|array
69 */
70 private $excludedTerms = null;
71
72 /**
73 * @var null|array
74 */
75 private $excludedPosts = null;
76
77 /**
78 * @var array
79 */
80 private $objectAccess = [];
81
82 /**
83 * @var null|array
84 */
85 private $noneHiddenPostTypes = null;
86
87 /**
88 * AccessHandler constructor.
89 * @param Wordpress $wordpress
90 * @param MainConfig $mainConfig
91 * @param Database $database
92 * @param ObjectHandler $objectHandler
93 * @param UserHandler $userHandler
94 * @param UserGroupHandler $userGroupHandler
95 */
96 public function __construct(
97 Wordpress $wordpress,
98 MainConfig $mainConfig,
99 Database $database,
100 ObjectHandler $objectHandler,
101 UserHandler $userHandler,
102 UserGroupHandler $userGroupHandler
103 ) {
104 $this->wordpress = $wordpress;
105 $this->mainConfig = $mainConfig;
106 $this->database = $database;
107 $this->objectHandler = $objectHandler;
108 $this->userHandler = $userHandler;
109 $this->userGroupHandler = $userGroupHandler;
110 }
111
112 /**
113 * Checks it the user has access because he is the author.
114 * @param string $objectType
115 * @param int|string $objectId
116 * @return bool
117 */
118 private function hasAuthorAccess(string $objectType, $objectId): bool
119 {
120 if ($this->mainConfig->authorsHasAccessToOwn() === true
121 && $this->objectHandler->isPostType($objectType)
122 ) {
123 $post = $this->objectHandler->getPost($objectId);
124 return $post !== false
125 && $this->wordpress->getCurrentUser()->ID === (int) $post->post_author;
126 }
127
128 return false;
129 }
130
131 /**
132 * Checks if the is admin value is set if not grabs it from the wordpress function.
133 * @param null|bool $isAdmin
134 * @return bool
135 */
136 private function isAdmin(?bool $isAdmin): bool
137 {
138 return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin;
139 }
140
141 /**
142 * Returns the user user groups filtered by the write access.
143 * @param null|bool $isAdmin If set we force the admin mode.
144 * @return AbstractUserGroup[]
145 * @throws UserGroupTypeException
146 */
147 private function getUserUserGroupsForObjectAccess($isAdmin = null): array
148 {
149 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
150
151 if ($this->isAdmin($isAdmin) === true) {
152 $userUserGroups = array_filter(
153 $userUserGroups,
154 function (AbstractUserGroup $userGroup) {
155 return $userGroup->getWriteAccess() !== 'none';
156 }
157 );
158 }
159
160 return $this->wordpress->applyFilters('uam_get_user_user_groups_for_object_access', $userUserGroups, $isAdmin);
161 }
162
163 /**
164 * Checks if the current_user has access to the given post.
165 * @param string|null $objectType The object type which should be checked.
166 * @param int|string $objectId The id of the object.
167 * @param null|bool $isAdmin If set we force the admin mode.
168 * @return bool
169 * @throws UserGroupTypeException
170 * @throws Exception
171 */
172 public function checkObjectAccess(?string $objectType, $objectId, $isAdmin = null): bool
173 {
174 $isAdmin = $this->isAdmin($isAdmin);
175
176 if (isset($this->objectAccess[$isAdmin][$objectType][$objectId]) === false) {
177 if ($this->objectHandler->isValidObjectType($objectType) === false
178 || $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true
179 || $this->hasAuthorAccess($objectType, $objectId) === true
180 ) {
181 $access = true;
182 } else {
183 $access = true;
184 $membership = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId);
185
186 if (count($membership) > 0) {
187 $userGroupDiff = array_intersect_key(
188 $membership,
189 $this->getUserUserGroupsForObjectAccess($isAdmin)
190 );
191 $nonLoggedInGroupKey = DynamicUserGroup::USER_TYPE . '|'
192 . DynamicUserGroup::NOT_LOGGED_IN_USER_ID;
193
194 $access = count($userGroupDiff) > 0 && (
195 isset($userGroupDiff[$nonLoggedInGroupKey]) ||
196 $this->wordpress->isUserMemberOfBlog()
197 );
198 }
199 }
200
201 $this->objectAccess[$isAdmin][$objectType][$objectId] = $access;
202 }
203
204 return $this->objectAccess[$isAdmin][$objectType][$objectId];
205 }
206
207 /**
208 * Returns the excluded objects.
209 * @param string $type
210 * @param array $filterTypesMap
211 * @return array
212 * @throws UserGroupTypeException
213 * @throws Exception
214 */
215 private function getExcludedObjects(string $type, array $filterTypesMap = []): array
216 {
217 $excludedObjects = [];
218 $userGroups = $this->userGroupHandler->getFullUserGroups();
219
220 foreach ($userGroups as $userGroup) {
221 $excludedObjects += $userGroup->getAssignedObjectsByType($type);
222 }
223
224 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
225
226 foreach ($userUserGroups as $userGroup) {
227 $excludedObjects = array_diff_key($excludedObjects, $userGroup->getAssignedObjectsByType($type));
228 }
229
230 if ($filterTypesMap !== []) {
231 $excludedObjects = array_filter(
232 $excludedObjects,
233 function ($element) use ($filterTypesMap) {
234 return isset($filterTypesMap[$element]) === false;
235 }
236 );
237 }
238
239 $objectIds = array_keys($excludedObjects);
240 return array_combine($objectIds, $objectIds);
241 }
242
243 /**
244 * Returns the excluded terms for a user.
245 * @return array
246 * @throws UserGroupTypeException
247 */
248 public function getExcludedTerms(): ?array
249 {
250 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
251 $this->excludedTerms = [];
252 }
253
254 if ($this->excludedTerms === null) {
255 $this->excludedTerms = $this->getExcludedObjects(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
256 }
257
258 return $this->excludedTerms;
259 }
260
261 /**
262 * Returns the none hidden post types map.
263 * @return array
264 */
265 private function getNoneHiddenPostTypes(): ?array
266 {
267 if ($this->noneHiddenPostTypes === null) {
268 $this->noneHiddenPostTypes = [];
269
270 if ($this->wordpress->isAdmin() === false) {
271 $postTypes = $this->objectHandler->getPostTypes();
272
273 foreach ($postTypes as $postType) {
274 if ($this->mainConfig->hidePostType($postType) === false) {
275 $this->noneHiddenPostTypes[$postType] = $postType;
276 }
277 }
278 }
279 }
280
281 return $this->noneHiddenPostTypes;
282 }
283
284 /**
285 * Returns the excluded posts.
286 * @return array
287 * @throws UserGroupTypeException
288 */
289 public function getExcludedPosts(): ?array
290 {
291 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
292 $this->excludedPosts = [];
293 }
294
295 if ($this->excludedPosts === null) {
296 $noneHiddenPostTypes = $this->getNoneHiddenPostTypes();
297 $excludedPosts = $this->getExcludedObjects(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $noneHiddenPostTypes);
298
299 if ($this->mainConfig->authorsHasAccessToOwn() === true) {
300 $query = $this->database->prepare(
301 "SELECT ID FROM {$this->database->getPostsTable()}
302 WHERE post_author = %d",
303 $this->wordpress->getCurrentUser()->ID
304 );
305
306 $ownPosts = array_filter(
307 (array) $this->database->getResults($query),
308 function ($ownPost) {
309 return isset($ownPost->ID);
310 }
311 );
312 $ownPostIds = [];
313
314 foreach ($ownPosts as $ownPost) {
315 $ownPostIds[$ownPost->ID] = $ownPost->ID;
316 }
317
318 $excludedPosts = array_diff_key($excludedPosts, $ownPostIds);
319 }
320
321 $this->excludedPosts = $excludedPosts;
322 }
323
324 return $this->excludedPosts;
325 }
326 }
327