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
user-access-manager / src / Access / AccessHandler.php

AccessHandler.php in User Access Manager 2.3.4, at src/Access/AccessHandler.php

209 lines 6.8 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 $objectId): bool
35 {
36 if ($this->mainConfig->authorsHasAccessToOwn() === true
37 && $this->objectHandler->isPostType($objectType)
38 ) {
39 $post = $this->objectHandler->getPost($objectId);
40 return $post !== false
41 && $this->wordpress->getCurrentUser()->ID === (int) $post->post_author;
42 }
43
44 return false;
45 }
46
47 private function isAdmin(?bool $isAdmin): bool
48 {
49 return ($isAdmin === null) ? $this->wordpress->isAdmin() : $isAdmin;
50 }
51
52 /**
53 * @throws UserGroupTypeException
54 */
55 private function getUserUserGroupsForObjectAccess(?bool $isAdmin = null): array
56 {
57 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
58
59 if ($this->isAdmin($isAdmin) === true) {
60 $userUserGroups = array_filter(
61 $userUserGroups,
62 function (AbstractUserGroup $userGroup) {
63 return $userGroup->getWriteAccess() !== 'none';
64 }
65 );
66 }
67
68 return $this->wordpress->applyFilters('uam_get_user_user_groups_for_object_access', $userUserGroups, $isAdmin);
69 }
70
71 /**
72 * @throws UserGroupTypeException
73 * @throws Exception
74 */
75 public function checkObjectAccess(?string $objectType, int|string|null $objectId, ?bool $isAdmin = null): bool
76 {
77 $isAdmin = $this->isAdmin($isAdmin);
78
79 if (isset($this->objectAccess[$isAdmin][$objectType][$objectId]) === false) {
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;
96 }
97
98 return $this->objectAccess[$isAdmin][$objectType][$objectId];
99 }
100
101 /**
102 * @throws UserGroupTypeException
103 * @throws Exception
104 */
105 private function getExcludedObjects(string $type, array $filterTypesMap = []): array
106 {
107 $excludedObjects = [];
108 $userGroups = $this->userGroupHandler->getFullUserGroups();
109
110 foreach ($userGroups as $userGroup) {
111 $excludedObjects += $userGroup->getAssignedObjectsByType($type);
112 }
113
114 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
115
116 foreach ($userUserGroups as $userGroup) {
117 $excludedObjects = array_diff_key($excludedObjects, $userGroup->getAssignedObjectsByType($type));
118 }
119
120 if ($filterTypesMap !== []) {
121 $excludedObjects = array_filter(
122 $excludedObjects,
123 function ($element) use ($filterTypesMap) {
124 return isset($filterTypesMap[$element]) === false;
125 }
126 );
127 }
128
129 $objectIds = array_keys($excludedObjects);
130 return array_combine($objectIds, $objectIds);
131 }
132
133 /**
134 * @throws UserGroupTypeException
135 */
136 public function getExcludedTerms(): ?array
137 {
138 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
139 $this->excludedTerms = [];
140 }
141
142 if ($this->excludedTerms === null) {
143 $this->excludedTerms = $this->getExcludedObjects(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
144 }
145
146 return $this->excludedTerms;
147 }
148
149 private function getNoneHiddenPostTypes(): ?array
150 {
151 if ($this->noneHiddenPostTypes === null) {
152 $this->noneHiddenPostTypes = [];
153
154 if ($this->wordpress->isAdmin() === false) {
155 $postTypes = $this->objectHandler->getPostTypes();
156
157 foreach ($postTypes as $postType) {
158 if ($this->mainConfig->hidePostType($postType) === false) {
159 $this->noneHiddenPostTypes[$postType] = $postType;
160 }
161 }
162 }
163 }
164
165 return $this->noneHiddenPostTypes;
166 }
167
168 /**
169 * @throws UserGroupTypeException
170 */
171 public function getExcludedPosts(): ?array
172 {
173 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY)) {
174 $this->excludedPosts = [];
175 }
176
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;
204 }
205
206 return $this->excludedPosts;
207 }
208 }
209