PluginProbe
User Access Manager / 2.3.13
User Access Manager v2.3.13
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 / ObjectController.php

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

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