PluginProbe
User Access Manager / 2.2.19
User Access Manager v2.2.19
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.19, at src/Access/AccessHandler.php

318 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\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 $membership = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId);
184 $access = $membership === []
185 || array_intersect_key($membership, $this->getUserUserGroupsForObjectAccess($isAdmin)) !== [];
186
187 if ($access && $this->wordpress->isUserLoggedIn() && $this->wordpress->isMultiSite()) {
188 $access = $this->wordpress->isUserMemberOfBlog();
189 }
190 }
191
192 $this->objectAccess[$isAdmin][$objectType][$objectId] = $access;
193 }
194
195 return $this->objectAccess[$isAdmin][$objectType][$objectId];
196 }
197
198 /**
199 * Returns the excluded objects.
200 * @param string $type
201 * @param array $filterTypesMap
202 * @return array
203 * @throws UserGroupTypeException
204 * @throws Exception
205 */
206 private function getExcludedObjects(string $type, array $filterTypesMap = []): array
207 {
208 $excludedObjects = [];
209 $userGroups = $this->userGroupHandler->getFullUserGroups();
210
211 foreach ($userGroups as $userGroup) {
212 $excludedObjects += $userGroup->getAssignedObjectsByType($type);
213 }
214
215 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
216
217 foreach ($userUserGroups as $userGroup) {
218 $excludedObjects = array_diff_key($excludedObjects, $userGroup->getAssignedObjectsByType($type));
219 }
220
221 if ($filterTypesMap !== []) {
222 $excludedObjects = array_filter(
223 $excludedObjects,
224 function ($element) use ($filterTypesMap) {
225 return isset($filterTypesMap[$element]) === false;
226 }
227 );
228 }
229
230 $objectIds = array_keys($excludedObjects);
231 return array_combine($objectIds, $objectIds);
232 }
233
234 /**
235 * Returns the excluded terms for a user.
236 * @return array
237 * @throws UserGroupTypeException
238 */
239 public function getExcludedTerms(): ?array
240 {
241 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
242 $this->excludedTerms = [];
243 }
244
245 if ($this->excludedTerms === null) {
246 $this->excludedTerms = $this->getExcludedObjects(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
247 }
248
249 return $this->excludedTerms;
250 }
251
252 /**
253 * Returns the none hidden post types map.
254 * @return array
255 */
256 private function getNoneHiddenPostTypes(): ?array
257 {
258 if ($this->noneHiddenPostTypes === null) {
259 $this->noneHiddenPostTypes = [];
260
261 if ($this->wordpress->isAdmin() === false) {
262 $postTypes = $this->objectHandler->getPostTypes();
263
264 foreach ($postTypes as $postType) {
265 if ($this->mainConfig->hidePostType($postType) === false) {
266 $this->noneHiddenPostTypes[$postType] = $postType;
267 }
268 }
269 }
270 }
271
272 return $this->noneHiddenPostTypes;
273 }
274
275 /**
276 * Returns the excluded posts.
277 * @return array
278 * @throws UserGroupTypeException
279 */
280 public function getExcludedPosts(): ?array
281 {
282 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
283 $this->excludedPosts = [];
284 }
285
286 if ($this->excludedPosts === null) {
287 $noneHiddenPostTypes = $this->getNoneHiddenPostTypes();
288 $excludedPosts = $this->getExcludedObjects(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $noneHiddenPostTypes);
289
290 if ($this->mainConfig->authorsHasAccessToOwn() === true) {
291 $query = $this->database->prepare(
292 "SELECT ID FROM {$this->database->getPostsTable()}
293 WHERE post_author = %d",
294 $this->wordpress->getCurrentUser()->ID
295 );
296
297 $ownPosts = array_filter(
298 (array) $this->database->getResults($query),
299 function ($ownPost) {
300 return isset($ownPost->ID);
301 }
302 );
303 $ownPostIds = [];
304
305 foreach ($ownPosts as $ownPost) {
306 $ownPostIds[$ownPost->ID] = $ownPost->ID;
307 }
308
309 $excludedPosts = array_diff_key($excludedPosts, $ownPostIds);
310 }
311
312 $this->excludedPosts = $excludedPosts;
313 }
314
315 return $this->excludedPosts;
316 }
317 }
318