PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
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.21, at src/Access/AccessHandler.php

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