PluginProbe
User Access Manager / 2.2.2
User Access Manager v2.2.2
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
← All changes | src/Access/AccessHandler.php +123 -25 2.3.152.2.2 View file →
@@ -1,5 +1,18 @@
1 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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Access;
@@ -13,44 +26,113 @@
13 26 use UserAccessManager\UserGroup\UserGroupHandler;
14 27 use UserAccessManager\UserGroup\UserGroupTypeException;
15 28 use UserAccessManager\Wrapper\Wordpress;
16 29
30 +/**
31 + * Class AccessHandler
32 + * @package UserAccessManager\AccessHandler
33 + */
17 34 class AccessHandler
18 35 {
19 - private ?array $excludedTerms = null;
20 - private ?array $excludedPosts = null;
21 - private array $objectAccess = [];
22 - private ?array $noneHiddenPostTypes = null;
36 + /**
37 + * @var Wordpress
38 + */
39 + private $wordpress;
23 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 + */
24 95 public function __construct(
25 - private Wordpress $wordpress,
26 - private MainConfig $mainConfig,
27 - private Database $database,
28 - private ObjectHandler $objectHandler,
29 - private UserHandler $userHandler,
30 - private UserGroupHandler $userGroupHandler
96 + Wordpress $wordpress,
97 + MainConfig $mainConfig,
98 + Database $database,
99 + ObjectHandler $objectHandler,
100 + UserHandler $userHandler,
101 + UserGroupHandler $userGroupHandler
31 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;
32 109 }
33 110
34 - private function hasAuthorAccess(string $objectType, int|string|null $objectId): bool
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
35 118 {
36 119 if ($this->mainConfig->authorsHasAccessToOwn() === true
37 120 && $this->objectHandler->isPostType($objectType)
38 121 ) {
39 122 $post = $this->objectHandler->getPost($objectId);
40 -
41 - if ($post === false) {
42 - return false;
43 - }
44 -
45 - $currentUserId = $this->wordpress->getCurrentUser()->ID;
46 - return $currentUserId !== 0
47 - && $currentUserId === (int) $post->post_author;
123 + return $post !== false
124 + && $this->wordpress->getCurrentUser()->ID === (int) $post->post_author;
48 125 }
49 126
50 127 return false;
51 128 }
52 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 + */
53 135 private function isAdmin(?bool $isAdmin): bool
54 136 {
55 137 return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin;
56 138 }
@@ -55,11 +137,14 @@
55 137 return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin;
56 138 }
57 139
58 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[]
59 144 * @throws UserGroupTypeException
60 145 */
61 - private function getUserUserGroupsForObjectAccess(?bool $isAdmin = null): array
146 + private function getUserUserGroupsForObjectAccess($isAdmin = null): array
62 147 {
63 148 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
64 149
65 150 if ($this->isAdmin($isAdmin) === true) {
@@ -74,12 +159,17 @@
74 159 return $this->wordpress->applyFilters('uam_get_user_user_groups_for_object_access', $userUserGroups, $isAdmin);
75 160 }
76 161
77 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
78 168 * @throws UserGroupTypeException
79 169 * @throws Exception
80 170 */
81 - public function checkObjectAccess(?string $objectType, int|string|null $objectId, ?bool $isAdmin = null): bool
171 + public function checkObjectAccess(?string $objectType, $objectId, $isAdmin = null): bool
82 172 {
83 173 $isAdmin = $this->isAdmin($isAdmin);
84 174
85 175 if (isset($this->objectAccess[$isAdmin][$objectType][$objectId]) === false) {
@@ -91,12 +181,8 @@
91 181 } else {
92 182 $membership = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId);
93 183 $access = $membership === []
94 184 || array_intersect_key($membership, $this->getUserUserGroupsForObjectAccess($isAdmin)) !== [];
95 -
96 - if ($access && $this->wordpress->isUserLoggedIn() && $this->wordpress->isMultiSite()) {
97 - $access = $this->wordpress->isUserMemberOfBlog();
98 - }
99 185 }
100 186
101 187 $this->objectAccess[$isAdmin][$objectType][$objectId] = $access;
102 188 }
@@ -104,8 +190,12 @@
104 190 return $this->objectAccess[$isAdmin][$objectType][$objectId];
105 191 }
106 192
107 193 /**
194 + * Returns the excluded objects.
195 + * @param string $type
196 + * @param array $filterTypesMap
197 + * @return array
108 198 * @throws UserGroupTypeException
109 199 * @throws Exception
110 200 */
111 201 private function getExcludedObjects(string $type, array $filterTypesMap = []): array
@@ -136,8 +226,10 @@
136 226 return array_combine($objectIds, $objectIds);
137 227 }
138 228
139 229 /**
230 + * Returns the excluded terms for a user.
231 + * @return array
140 232 * @throws UserGroupTypeException
141 233 */
142 234 public function getExcludedTerms(): ?array
143 235 {
@@ -151,8 +243,12 @@
151 243
152 244 return $this->excludedTerms;
153 245 }
154 246
247 + /**
248 + * Returns the none hidden post types map.
249 + * @return array
250 + */
155 251 private function getNoneHiddenPostTypes(): ?array
156 252 {
157 253 if ($this->noneHiddenPostTypes === null) {
158 254 $this->noneHiddenPostTypes = [];
@@ -171,8 +267,10 @@
171 267 return $this->noneHiddenPostTypes;
172 268 }
173 269
174 270 /**
271 + * Returns the excluded posts.
272 + * @return array
175 273 * @throws UserGroupTypeException
176 274 */
177 275 public function getExcludedPosts(): ?array
178 276 {