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

215 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Access;
6
7 use Exception;
8 use UserAccessManager\Config\MainConfig;
9 use UserAccessManager\Database\Database;
10 use UserAccessManager\Object\ObjectHandler;
11 use UserAccessManager\User\UserHandler;
12 use UserAccessManager\UserGroup\AbstractUserGroup;
13 use UserAccessManager\UserGroup\UserGroupHandler;
14 use UserAccessManager\UserGroup\UserGroupTypeException;
15 use UserAccessManager\Wrapper\Wordpress;
16
17 class AccessHandler
18 {
19 private ?array $excludedTerms = null;
20 private ?array $excludedPosts = null;
21 private array $objectAccess = [];
22 private ?array $noneHiddenPostTypes = null;
23
24 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
31 ) {
32 }
33
34 private function hasAuthorAccess(string $objectType, int|string|null $objectId): bool
35 {
36 if ($this->mainConfig->authorsHasAccessToOwn() === true
37 && $this->objectHandler->isPostType($objectType)
38 ) {
39 $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;
48 }
49
50 return false;
51 }
52
53 private function isAdmin(?bool $isAdmin): bool
54 {
55 return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin;
56 }
57
58 /**
59 * @throws UserGroupTypeException
60 */
61 private function getUserUserGroupsForObjectAccess(?bool $isAdmin = null): array
62 {
63 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
64
65 if ($this->isAdmin($isAdmin) === true) {
66 $userUserGroups = array_filter(
67 $userUserGroups,
68 function (AbstractUserGroup $userGroup) {
69 return $userGroup->getWriteAccess() !== 'none';
70 }
71 );
72 }
73
74 return $this->wordpress->applyFilters('uam_get_user_user_groups_for_object_access', $userUserGroups, $isAdmin);
75 }
76
77 /**
78 * @throws UserGroupTypeException
79 * @throws Exception
80 */
81 public function checkObjectAccess(?string $objectType, int|string|null $objectId, ?bool $isAdmin = null): bool
82 {
83 $isAdmin = $this->isAdmin($isAdmin);
84
85 if (isset($this->objectAccess[$isAdmin][$objectType][$objectId]) === false) {
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;
102 }
103
104 return $this->objectAccess[$isAdmin][$objectType][$objectId];
105 }
106
107 /**
108 * @throws UserGroupTypeException
109 * @throws Exception
110 */
111 private function getExcludedObjects(string $type, array $filterTypesMap = []): array
112 {
113 $excludedObjects = [];
114 $userGroups = $this->userGroupHandler->getFullUserGroups();
115
116 foreach ($userGroups as $userGroup) {
117 $excludedObjects += $userGroup->getAssignedObjectsByType($type);
118 }
119
120 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
121
122 foreach ($userUserGroups as $userGroup) {
123 $excludedObjects = array_diff_key($excludedObjects, $userGroup->getAssignedObjectsByType($type));
124 }
125
126 if ($filterTypesMap !== []) {
127 $excludedObjects = array_filter(
128 $excludedObjects,
129 function ($element) use ($filterTypesMap) {
130 return isset($filterTypesMap[$element]) === false;
131 }
132 );
133 }
134
135 $objectIds = array_keys($excludedObjects);
136 return array_combine($objectIds, $objectIds);
137 }
138
139 /**
140 * @throws UserGroupTypeException
141 */
142 public function getExcludedTerms(): ?array
143 {
144 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
145 $this->excludedTerms = [];
146 }
147
148 if ($this->excludedTerms === null) {
149 $this->excludedTerms = $this->getExcludedObjects(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
150 }
151
152 return $this->excludedTerms;
153 }
154
155 private function getNoneHiddenPostTypes(): ?array
156 {
157 if ($this->noneHiddenPostTypes === null) {
158 $this->noneHiddenPostTypes = [];
159
160 if ($this->wordpress->isAdmin() === false) {
161 $postTypes = $this->objectHandler->getPostTypes();
162
163 foreach ($postTypes as $postType) {
164 if ($this->mainConfig->hidePostType($postType) === false) {
165 $this->noneHiddenPostTypes[$postType] = $postType;
166 }
167 }
168 }
169 }
170
171 return $this->noneHiddenPostTypes;
172 }
173
174 /**
175 * @throws UserGroupTypeException
176 */
177 public function getExcludedPosts(): ?array
178 {
179 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
180 $this->excludedPosts = [];
181 }
182
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;
210 }
211
212 return $this->excludedPosts;
213 }
214 }
215