PluginProbe
User Access Manager / 2.2.15
User Access Manager v2.2.15
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 +124 -21 2.3.13 → 2.2.15 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 <[email protected]>
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;
@@ -9,48 +22,118 @@
9 22 use UserAccessManager\Database\Database;
10 23 use UserAccessManager\Object\ObjectHandler;
11 24 use UserAccessManager\User\UserHandler;
12 25 use UserAccessManager\UserGroup\AbstractUserGroup;
26 +use UserAccessManager\UserGroup\DynamicUserGroup;
13 27 use UserAccessManager\UserGroup\UserGroupHandler;
14 28 use UserAccessManager\UserGroup\UserGroupTypeException;
15 29 use UserAccessManager\Wrapper\Wordpress;
16 30
31 +/**
32 + * Class AccessHandler
33 + * @package UserAccessManager\AccessHandler
34 + */
17 35 class AccessHandler
18 36 {
19 - private ?array $excludedTerms = null;
20 - private ?array $excludedPosts = null;
21 - private array $objectAccess = [];
22 - private ?array $noneHiddenPostTypes = null;
37 + /**
38 + * @var Wordpress
39 + */
40 + private $wordpress;
23 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 + */
24 96 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
97 + Wordpress $wordpress,
98 + MainConfig $mainConfig,
99 + Database $database,
100 + ObjectHandler $objectHandler,
101 + UserHandler $userHandler,
102 + UserGroupHandler $userGroupHandler
31 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;
32 110 }
33 111
34 - private function hasAuthorAccess(string $objectType, int|string|null $objectId): bool
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
35 119 {
36 120 if ($this->mainConfig->authorsHasAccessToOwn() === true
37 121 && $this->objectHandler->isPostType($objectType)
38 122 ) {
39 123 $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;
124 + return $post !== false
125 + && $this->wordpress->getCurrentUser()->ID === (int) $post->post_author;
48 126 }
49 127
50 128 return false;
51 129 }
52 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 + */
53 136 private function isAdmin(?bool $isAdmin): bool
54 137 {
55 138 return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin;
56 139 }
@@ -55,11 +138,14 @@
55 138 return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin;
56 139 }
57 140
58 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[]
59 145 * @throws UserGroupTypeException
60 146 */
61 - private function getUserUserGroupsForObjectAccess(?bool $isAdmin = null): array
147 + private function getUserUserGroupsForObjectAccess($isAdmin = null): array
62 148 {
63 149 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
64 150
65 151 if ($this->isAdmin($isAdmin) === true) {
@@ -74,12 +160,17 @@
74 160 return $this->wordpress->applyFilters('uam_get_user_user_groups_for_object_access', $userUserGroups, $isAdmin);
75 161 }
76 162
77 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
78 169 * @throws UserGroupTypeException
79 170 * @throws Exception
80 171 */
81 - public function checkObjectAccess(?string $objectType, int|string|null $objectId, ?bool $isAdmin = null): bool
172 + public function checkObjectAccess(?string $objectType, $objectId, $isAdmin = null): bool
82 173 {
83 174 $isAdmin = $this->isAdmin($isAdmin);
84 175
85 176 if (isset($this->objectAccess[$isAdmin][$objectType][$objectId]) === false) {
@@ -104,8 +195,12 @@
104 195 return $this->objectAccess[$isAdmin][$objectType][$objectId];
105 196 }
106 197
107 198 /**
199 + * Returns the excluded objects.
200 + * @param string $type
201 + * @param array $filterTypesMap
202 + * @return array
108 203 * @throws UserGroupTypeException
109 204 * @throws Exception
110 205 */
111 206 private function getExcludedObjects(string $type, array $filterTypesMap = []): array
@@ -136,8 +231,10 @@
136 231 return array_combine($objectIds, $objectIds);
137 232 }
138 233
139 234 /**
235 + * Returns the excluded terms for a user.
236 + * @return array
140 237 * @throws UserGroupTypeException
141 238 */
142 239 public function getExcludedTerms(): ?array
143 240 {
@@ -151,8 +248,12 @@
151 248
152 249 return $this->excludedTerms;
153 250 }
154 251
252 + /**
253 + * Returns the none hidden post types map.
254 + * @return array
255 + */
155 256 private function getNoneHiddenPostTypes(): ?array
156 257 {
157 258 if ($this->noneHiddenPostTypes === null) {
158 259 $this->noneHiddenPostTypes = [];
@@ -171,8 +272,10 @@
171 272 return $this->noneHiddenPostTypes;
172 273 }
173 274
174 275 /**
276 + * Returns the excluded posts.
277 + * @return array
175 278 * @throws UserGroupTypeException
176 279 */
177 280 public function getExcludedPosts(): ?array
178 281 {