PluginProbe
User Access Manager / 2.3.4
User Access Manager v2.3.4
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 +84 -91 trunk2.3.4 View file →
@@ -18,9 +18,9 @@
18 18 {
19 19 private ?array $excludedTerms = null;
20 20 private ?array $excludedPosts = null;
21 21 private array $objectAccess = [];
22 - private ?array $visiblePostTypes = null;
22 + private ?array $noneHiddenPostTypes = null;
23 23
24 24 public function __construct(
25 25 private Wordpress $wordpress,
26 26 private MainConfig $mainConfig,
@@ -30,30 +30,19 @@
30 30 private UserGroupHandler $userGroupHandler
31 31 ) {
32 32 }
33 33
34 - private function canManageUserGroups(): bool
34 + private function hasAuthorAccess(string $objectType, int|string $objectId): bool
35 35 {
36 - return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true;
37 - }
38 -
39 - private function hasAuthorAccess(string $objectType, int|string|null $objectId): bool
40 - {
41 - if ($this->mainConfig->authorsHasAccessToOwn() !== true
42 - || $this->objectHandler->isPostType($objectType) === false
36 + if ($this->mainConfig->authorsHasAccessToOwn() === true
37 + && $this->objectHandler->isPostType($objectType)
43 38 ) {
44 - return false;
39 + $post = $this->objectHandler->getPost($objectId);
40 + return $post !== false
41 + && $this->wordpress->getCurrentUser()->ID === (int) $post->post_author;
45 42 }
46 43
47 - $post = $this->objectHandler->getPost($objectId);
48 -
49 - if ($post === false) {
50 - return false;
51 - }
52 -
53 - $currentUserId = $this->wordpress->getCurrentUser()->ID;
54 -
55 - return $currentUserId !== 0 && $currentUserId === (int) $post->post_author;
44 + return false;
56 45 }
57 46
58 47 private function isAdmin(?bool $isAdmin): bool
59 48 {
@@ -69,9 +58,11 @@
69 58
70 59 if ($this->isAdmin($isAdmin) === true) {
71 60 $userUserGroups = array_filter(
72 61 $userUserGroups,
73 - fn(AbstractUserGroup $userGroup) => $userGroup->getWriteAccess() !== 'none'
62 + function (AbstractUserGroup $userGroup) {
63 + return $userGroup->getWriteAccess() !== 'none';
64 + }
74 65 );
75 66 }
76 67
77 68 return $this->wordpress->applyFilters('uam_get_user_user_groups_for_object_access', $userUserGroups, $isAdmin);
@@ -80,42 +71,29 @@
80 71 /**
81 72 * @throws UserGroupTypeException
82 73 * @throws Exception
83 74 */
84 - private function resolveObjectAccess(?string $objectType, int|string|null $objectId, bool $isAdmin): bool
85 - {
86 - if ($this->objectHandler->isValidObjectType($objectType) === false
87 - || $this->canManageUserGroups() === true
88 - || $this->hasAuthorAccess($objectType, $objectId) === true
89 - ) {
90 - return true;
91 - }
92 -
93 - $membership = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId);
94 - $access = $membership === []
95 - || array_intersect_key($membership, $this->getUserUserGroupsForObjectAccess($isAdmin)) !== [];
96 -
97 - if ($access === true && $this->wordpress->isUserLoggedIn() && $this->wordpress->isMultiSite()) {
98 - return $this->wordpress->isUserMemberOfBlog();
99 - }
100 -
101 - return $access;
102 - }
103 -
104 - /**
105 - * @throws UserGroupTypeException
106 - * @throws Exception
107 - */
108 75 public function checkObjectAccess(?string $objectType, int|string|null $objectId, ?bool $isAdmin = null): bool
109 76 {
110 77 $isAdmin = $this->isAdmin($isAdmin);
111 78
112 79 if (isset($this->objectAccess[$isAdmin][$objectType][$objectId]) === false) {
113 - $this->objectAccess[$isAdmin][$objectType][$objectId] = $this->resolveObjectAccess(
114 - $objectType,
115 - $objectId,
116 - $isAdmin
117 - );
80 + if ($this->objectHandler->isValidObjectType($objectType) === false
81 + || $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true
82 + || $this->hasAuthorAccess($objectType, $objectId) === true
83 + ) {
84 + $access = true;
85 + } else {
86 + $membership = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId);
87 + $access = $membership === []
88 + || array_intersect_key($membership, $this->getUserUserGroupsForObjectAccess($isAdmin)) !== [];
89 +
90 + if ($access && $this->wordpress->isUserLoggedIn() && $this->wordpress->isMultiSite()) {
91 + $access = $this->wordpress->isUserMemberOfBlog();
92 + }
93 + }
94 +
95 + $this->objectAccess[$isAdmin][$objectType][$objectId] = $access;
118 96 }
119 97
120 98 return $this->objectAccess[$isAdmin][$objectType][$objectId];
121 99 }
@@ -120,32 +98,36 @@
120 98 return $this->objectAccess[$isAdmin][$objectType][$objectId];
121 99 }
122 100
123 101 /**
124 - * $ignoredObjectTypes is matched against the assigned object type (the array value), not the object id.
125 - *
126 102 * @throws UserGroupTypeException
127 103 * @throws Exception
128 104 */
129 - private function getExcludedObjects(string $type, array $ignoredObjectTypes = []): array
105 + private function getExcludedObjects(string $type, array $filterTypesMap = []): array
130 106 {
131 107 $excludedObjects = [];
108 + $userGroups = $this->userGroupHandler->getFullUserGroups();
132 109
133 - foreach ($this->userGroupHandler->getFullUserGroups() as $userGroup) {
110 + foreach ($userGroups as $userGroup) {
134 111 $excludedObjects += $userGroup->getAssignedObjectsByType($type);
135 112 }
136 113
137 - foreach ($this->userGroupHandler->getUserGroupsForUser() as $userGroup) {
114 + $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
115 +
116 + foreach ($userUserGroups as $userGroup) {
138 117 $excludedObjects = array_diff_key($excludedObjects, $userGroup->getAssignedObjectsByType($type));
139 118 }
140 119
141 - $excludedObjects = array_filter(
142 - $excludedObjects,
143 - fn($objectType) => isset($ignoredObjectTypes[$objectType]) === false
144 - );
120 + if ($filterTypesMap !== []) {
121 + $excludedObjects = array_filter(
122 + $excludedObjects,
123 + function ($element) use ($filterTypesMap) {
124 + return isset($filterTypesMap[$element]) === false;
125 + }
126 + );
127 + }
145 128
146 129 $objectIds = array_keys($excludedObjects);
147 -
148 130 return array_combine($objectIds, $objectIds);
149 131 }
150 132
151 133 /**
@@ -152,11 +134,13 @@
152 134 * @throws UserGroupTypeException
153 135 */
154 136 public function getExcludedTerms(): ?array
155 137 {
156 - if ($this->canManageUserGroups() === true) {
138 + if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
157 139 $this->excludedTerms = [];
158 - } elseif ($this->excludedTerms === null) {
140 + }
141 +
142 + if ($this->excludedTerms === null) {
159 143 $this->excludedTerms = $this->getExcludedObjects(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
160 144 }
161 145
162 146 return $this->excludedTerms;
@@ -161,54 +145,63 @@
161 145
162 146 return $this->excludedTerms;
163 147 }
164 148
165 - private function getVisiblePostTypes(): array
149 + private function getNoneHiddenPostTypes(): ?array
166 150 {
167 - if ($this->visiblePostTypes !== null) {
168 - return $this->visiblePostTypes;
169 - }
151 + if ($this->noneHiddenPostTypes === null) {
152 + $this->noneHiddenPostTypes = [];
170 153
171 - $this->visiblePostTypes = [];
154 + if ($this->wordpress->isAdmin() === false) {
155 + $postTypes = $this->objectHandler->getPostTypes();
172 156
173 - if ($this->wordpress->isAdmin() === false) {
174 - foreach ($this->objectHandler->getPostTypes() as $postType) {
175 - if ($this->mainConfig->hidePostType($postType) === false) {
176 - $this->visiblePostTypes[$postType] = $postType;
157 + foreach ($postTypes as $postType) {
158 + if ($this->mainConfig->hidePostType($postType) === false) {
159 + $this->noneHiddenPostTypes[$postType] = $postType;
160 + }
177 161 }
178 162 }
179 163 }
180 164
181 - return $this->visiblePostTypes;
165 + return $this->noneHiddenPostTypes;
182 166 }
183 167
184 - private function getOwnPostIds(): array
185 - {
186 - $query = $this->database->prepare(
187 - "SELECT `ID` FROM `{$this->database->getPostsTable()}`
188 - WHERE `post_author` = %d",
189 - $this->wordpress->getCurrentUser()->ID
190 - );
191 -
192 - return array_column((array) $this->database->getResults($query), 'ID', 'ID');
193 - }
194 -
195 168 /**
196 169 * @throws UserGroupTypeException
197 170 */
198 171 public function getExcludedPosts(): ?array
199 172 {
200 - if ($this->canManageUserGroups() === true) {
173 + if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
201 174 $this->excludedPosts = [];
202 - } elseif ($this->excludedPosts === null) {
203 - $excludedPosts = $this->getExcludedObjects(
204 - ObjectHandler::GENERAL_POST_OBJECT_TYPE,
205 - $this->getVisiblePostTypes()
206 - );
175 + }
207 176
208 - $this->excludedPosts = ($this->mainConfig->authorsHasAccessToOwn() === true)
209 - ? array_diff_key($excludedPosts, $this->getOwnPostIds())
210 - : $excludedPosts;
177 + if ($this->excludedPosts === null) {
178 + $noneHiddenPostTypes = $this->getNoneHiddenPostTypes();
179 + $excludedPosts = $this->getExcludedObjects(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $noneHiddenPostTypes);
180 +
181 + if ($this->mainConfig->authorsHasAccessToOwn() === true) {
182 + $query = $this->database->prepare(
183 + "SELECT ID FROM {$this->database->getPostsTable()}
184 + WHERE post_author = %d",
185 + $this->wordpress->getCurrentUser()->ID
186 + );
187 +
188 + $ownPosts = array_filter(
189 + (array) $this->database->getResults($query),
190 + function ($ownPost) {
191 + return isset($ownPost->ID);
192 + }
193 + );
194 + $ownPostIds = [];
195 +
196 + foreach ($ownPosts as $ownPost) {
197 + $ownPostIds[$ownPost->ID] = $ownPost->ID;
198 + }
199 +
200 + $excludedPosts = array_diff_key($excludedPosts, $ownPostIds);
201 + }
202 +
203 + $this->excludedPosts = $excludedPosts;
211 204 }
212 205
213 206 return $this->excludedPosts;
214 207 }