PluginProbe
User Access Manager / 2.2.12
User Access Manager v2.2.12
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.12, at src/Access/AccessHandler.php

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