PluginProbe
User Access Manager / 2.3.20
User Access Manager v2.3.20
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 / Controller / Backend / ObjectMembership / ObjectController.php

ObjectController.php in User Access Manager 2.3.20, at src/Controller/Backend/ObjectMembership/ObjectController.php

336 lines 11.1 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\Controller\Backend\ObjectMembership;
6
7 use Exception;
8 use UserAccessManager\Access\AccessHandler;
9 use UserAccessManager\Config\MainConfig;
10 use UserAccessManager\Config\WordpressConfig;
11 use UserAccessManager\Controller\Controller;
12 use UserAccessManager\Database\Database;
13 use UserAccessManager\Object\ObjectHandler;
14 use UserAccessManager\ObjectMembership\Exception\MissingObjectMembershipHandlerException;
15 use UserAccessManager\User\UserHandler;
16 use UserAccessManager\UserGroup\AbstractUserGroup;
17 use UserAccessManager\UserGroup\AssignmentInformation;
18 use UserAccessManager\UserGroup\DynamicUserGroup;
19 use UserAccessManager\UserGroup\UserGroupAssignmentException;
20 use UserAccessManager\UserGroup\UserGroupAssignmentHandler;
21 use UserAccessManager\UserGroup\UserGroupHandler;
22 use UserAccessManager\UserGroup\UserGroupTypeException;
23 use UserAccessManager\Util\DateUtil;
24 use UserAccessManager\Wrapper\Php;
25 use UserAccessManager\Wrapper\Wordpress;
26
27 class ObjectController extends Controller
28 {
29 public const COLUMN_NAME = 'uam_access';
30 public const BULK_ADD = 'add';
31 public const BULK_REMOVE = 'remove';
32 public const BULK_OVERWRITE = 'overwrite';
33 public const DEFAULT_GROUPS_FORM_NAME = 'uam_user_groups';
34 public const DEFAULT_DYNAMIC_GROUPS_FORM_NAME = 'uam_dynamic_user_groups';
35 public const UPDATE_GROUPS_FORM_NAME = 'uam_update_groups';
36
37 protected ObjectInformation $objectInformation;
38 protected ?string $groupsFromName = null;
39
40 public function __construct(
41 Php $php,
42 Wordpress $wordpress,
43 WordpressConfig $wordpressConfig,
44 protected MainConfig $mainConfig,
45 protected Database $database,
46 protected DateUtil $dateUtil,
47 protected ObjectHandler $objectHandler,
48 protected UserHandler $userHandler,
49 protected UserGroupHandler $userGroupHandler,
50 protected UserGroupAssignmentHandler $userGroupAssignmentHandler,
51 protected AccessHandler $accessHandler,
52 ObjectInformationFactory $objectInformationFactory
53 ) {
54 parent::__construct($php, $wordpress, $wordpressConfig);
55 $this->objectInformation = $objectInformationFactory->createObjectInformation();
56 }
57
58 /**
59 * @throws UserGroupTypeException
60 */
61 protected function setObjectInformation(
62 string $objectType,
63 int|string|null $objectId,
64 ?array $objectUserGroups = null
65 ): void {
66 $userGroupDiff = 0;
67
68 if ($objectUserGroups === null && $objectId !== null) {
69 $objectUserGroups = $this->userGroupHandler->getFilteredUserGroupsForObject($objectType, $objectId, true);
70 $fullObjectUserGroups = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId, true);
71 $userGroupDiff = count((array) $fullObjectUserGroups) - count((array) $objectUserGroups);
72 }
73
74 $this->objectInformation->setObjectType($objectType)
75 ->setObjectId($objectId)
76 ->setObjectUserGroups($objectUserGroups ?? [])
77 ->setUserGroupDiff($userGroupDiff);
78 }
79
80 public function getObjectInformation(): ObjectInformation
81 {
82 return $this->objectInformation;
83 }
84
85 public function getGroupsFormName(): string
86 {
87 return $this->groupsFromName ?? self::DEFAULT_GROUPS_FORM_NAME;
88 }
89
90 /**
91 * @return AbstractUserGroup[]
92 * @throws UserGroupTypeException
93 */
94 public function getFilteredUserGroups(): array
95 {
96 return $this->userGroupHandler->getFilteredUserGroups();
97 }
98
99 public function sortUserGroups(array &$userGroups): void
100 {
101 $notLoggedInUserGroupId = DynamicUserGroup::USER_TYPE . '|' . DynamicUserGroup::NOT_LOGGED_IN_USER_ID;
102
103 uasort(
104 $userGroups,
105 function (
106 AbstractUserGroup $userGroupOne,
107 AbstractUserGroup $userGroupTwo
108 ) use ($notLoggedInUserGroupId) {
109 if ($userGroupOne->getId() === $notLoggedInUserGroupId) {
110 return 1;
111 }
112
113 if ($userGroupTwo->getId() === $notLoggedInUserGroupId) {
114 return -1;
115 }
116
117 return strnatcasecmp($userGroupOne->getName(), $userGroupTwo->getName());
118 }
119 );
120 }
121
122 public function getDateUtil(): DateUtil
123 {
124 return $this->dateUtil;
125 }
126
127 public function isCurrentUserAdmin(): bool
128 {
129 if ($this->objectInformation->getObjectType() === ObjectHandler::GENERAL_USER_OBJECT_TYPE
130 && $this->objectInformation->getObjectId() !== null
131 ) {
132 return $this->userHandler->userIsAdmin($this->objectInformation->getObjectId());
133 }
134
135 return false;
136 }
137
138 public function getRoleNames(): array
139 {
140 $roles = $this->wordpress->getRoles();
141 return $roles->role_names;
142 }
143
144 public function checkUserAccess(): bool
145 {
146 return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY);
147 }
148
149 /**
150 * @throws Exception
151 */
152 public function getRecursiveMembership(AbstractUserGroup $userGroup): array
153 {
154 $recursiveMembership = [];
155 $recursiveMembershipForObject = $userGroup->getRecursiveMembershipForObject(
156 $this->objectInformation->getObjectType(),
157 $this->objectInformation->getObjectId()
158 );
159
160 /**
161 * @var AssignmentInformation[] $assignmentInformation
162 */
163 foreach ($recursiveMembershipForObject as $assignmentInformation) {
164 foreach ($assignmentInformation as $membershipObjectId => $information) {
165 try {
166 $membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType());
167 $typeName = $membershipHandler->getGeneralObjectType();
168 $recursiveMembership[$typeName][$membershipObjectId] = $membershipHandler->getObjectName(
169 $membershipObjectId,
170 $typeName
171 );
172 } catch (MissingObjectMembershipHandlerException) {
173 }
174 }
175 }
176
177 return $recursiveMembership;
178 }
179
180 /**
181 * @throws UserGroupTypeException
182 */
183 private function dieOnNoAccess(string $objectType, int|string|null $objectId): void
184 {
185 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
186 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
187 }
188 }
189
190 /**
191 * @throws UserGroupTypeException
192 */
193 public function checkRightsToEditContent(): void
194 {
195 $postIdParameter = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id'));
196
197 if ($postIdParameter !== null) {
198 foreach ((array) $postIdParameter as $postId) {
199 $this->dieOnNoAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId);
200 }
201 }
202
203 $tagId = $this->getRequestParameter('tag_ID');
204
205 if ($tagId !== null) {
206 $this->dieOnNoAccess(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $tagId);
207 }
208 }
209
210 /**
211 * @throws UserGroupTypeException
212 */
213 private function getAddRemoveGroups(
214 string $objectType,
215 int|string|null $objectId,
216 ?array &$addUserGroups = [],
217 ?array &$removeUserGroups = []
218 ): void {
219 $groupsToChange = (array) $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
220 $filteredUserGroupsForObject = $this->userGroupHandler->getFilteredUserGroupsForObject(
221 $objectType,
222 $objectId
223 );
224
225 $addUserGroups = $addUserGroups ?? $groupsToChange;
226 $removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject));
227 $bulkType = $this->getRequestParameter('uam_bulk_type');
228
229 if ($bulkType === self::BULK_ADD) {
230 $addUserGroups = $groupsToChange;
231 $removeUserGroups = [];
232 } elseif ($bulkType === self::BULK_REMOVE) {
233 $addUserGroups = [];
234 $removeUserGroups = array_filter(
235 $groupsToChange,
236 function (array $group) {
237 return isset($group['id']);
238 }
239 );
240 }
241 }
242
243 /**
244 * @throws UserGroupTypeException
245 */
246 public function saveObjectData(
247 string $objectType,
248 int|string|null $objectId,
249 ?array $addUserGroups = null,
250 bool $force = false
251 ): void {
252 $isUpdateForm = (bool) $this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
253 || $this->getRequestParameter('uam_bulk_type') !== null;
254
255 $hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true;
256
257 if (($isUpdateForm === true && $hasRights === true) === false && $force === false) {
258 return;
259 }
260
261 $this->getAddRemoveGroups($objectType, $objectId, $addUserGroups, $removeUserGroups);
262
263 try {
264 $this->userGroupAssignmentHandler->assignObjectToUserGroups(
265 $objectType,
266 $objectId,
267 $addUserGroups,
268 $removeUserGroups,
269 $this->getRequestParameter(self::DEFAULT_DYNAMIC_GROUPS_FORM_NAME, [])
270 );
271 } catch (UserGroupAssignmentException $exception) {
272 $this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage()));
273 }
274 }
275
276 public function removeObjectData(string $objectType, int|string|null $id): void
277 {
278 $this->database->delete(
279 $this->database->getUserGroupToObjectTable(),
280 [
281 'object_id' => $id,
282 'object_type' => $objectType,
283 ],
284 [
285 '%d',
286 '%s'
287 ]
288 );
289 }
290
291 /**
292 * @throws UserGroupTypeException
293 */
294 public function showGroupSelectionForm(
295 string $objectType,
296 int|string|null $objectId,
297 ?string $formName = null,
298 ?array $objectUserGroups = null
299 ): string {
300 $this->setObjectInformation($objectType, $objectId, $objectUserGroups);
301
302 $this->groupsFromName = $formName;
303 $formContent = $this->getIncludeContents('GroupSelectionForm.php');
304 $this->groupsFromName = null;
305
306 return $formContent;
307 }
308
309 /**
310 * @throws UserGroupTypeException
311 */
312 public function getGroupColumn(string $objectType, int|string|null $objectId): string
313 {
314 $this->setObjectInformation($objectType, $objectId);
315 return $this->getIncludeContents('ObjectColumn.php');
316 }
317
318 /**
319 * @throws Exception
320 */
321 public function isNewObject(): bool
322 {
323 $objectType = $this->objectInformation->getObjectType();
324
325 if ($objectType === null) {
326 return false;
327 }
328
329 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
330
331 return $this->objectInformation->getObjectId() === null
332 || ($generalObjectType === ObjectHandler::GENERAL_POST_OBJECT_TYPE
333 && $this->getRequestParameter('action') !== 'edit');
334 }
335 }
336