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