PluginProbe
User Access Manager / 2.2.8
User Access Manager v2.2.8
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.8, at src/Access/AccessHandler.php

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