| 1 |
<?php |
| 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 |
const COLUMN_NAME = 'uam_access'; |
| 48 |
const BULK_REMOVE = 'remove'; |
| 49 |
const DEFAULT_GROUPS_FORM_NAME = 'uam_user_groups'; |
| 50 |
const DEFAULT_DYNAMIC_GROUPS_FORM_NAME = 'uam_dynamic_user_groups'; |
| 51 |
const UPDATE_GROUPS_FORM_NAME = 'uam_update_groups'; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var MainConfig |
| 55 |
*/ |
| 56 |
protected $mainConfig; |
| 57 |
|
| 58 |
/** |
| 59 |
* @var Database |
| 60 |
*/ |
| 61 |
protected $database; |
| 62 |
|
| 63 |
/** |
| 64 |
* @var DateUtil |
| 65 |
*/ |
| 66 |
protected $dateUtil; |
| 67 |
|
| 68 |
/** |
| 69 |
* @var ObjectHandler |
| 70 |
*/ |
| 71 |
protected $objectHandler; |
| 72 |
|
| 73 |
/** |
| 74 |
* @var UserHandler |
| 75 |
*/ |
| 76 |
protected $userHandler; |
| 77 |
|
| 78 |
/** |
| 79 |
* @var UserGroupHandler |
| 80 |
*/ |
| 81 |
protected $userGroupHandler; |
| 82 |
|
| 83 |
/** |
| 84 |
* @var AccessHandler |
| 85 |
*/ |
| 86 |
protected $accessHandler; |
| 87 |
|
| 88 |
/** |
| 89 |
* @var UserGroupAssignmentHandler |
| 90 |
*/ |
| 91 |
protected $userGroupAssignmentHandler; |
| 92 |
|
| 93 |
/** |
| 94 |
* @var ObjectInformation |
| 95 |
*/ |
| 96 |
protected $objectInformation; |
| 97 |
|
| 98 |
/** |
| 99 |
* @var null|string |
| 100 |
*/ |
| 101 |
protected $groupsFromName = null; |
| 102 |
|
| 103 |
/** |
| 104 |
* ObjectController constructor. |
| 105 |
* @param Php $php |
| 106 |
* @param Wordpress $wordpress |
| 107 |
* @param WordpressConfig $wordpressConfig |
| 108 |
* @param MainConfig $mainConfig |
| 109 |
* @param Database $database |
| 110 |
* @param DateUtil $dateUtil |
| 111 |
* @param ObjectHandler $objectHandler |
| 112 |
* @param UserHandler $userHandler |
| 113 |
* @param UserGroupHandler $userGroupHandler |
| 114 |
* @param UserGroupAssignmentHandler $userGroupAssignmentHandler |
| 115 |
* @param AccessHandler $accessHandler |
| 116 |
* @param ObjectInformationFactory $objectInformationFactory |
| 117 |
*/ |
| 118 |
public function __construct( |
| 119 |
Php $php, |
| 120 |
Wordpress $wordpress, |
| 121 |
WordpressConfig $wordpressConfig, |
| 122 |
MainConfig $mainConfig, |
| 123 |
Database $database, |
| 124 |
DateUtil $dateUtil, |
| 125 |
ObjectHandler $objectHandler, |
| 126 |
UserHandler $userHandler, |
| 127 |
UserGroupHandler $userGroupHandler, |
| 128 |
UserGroupAssignmentHandler $userGroupAssignmentHandler, |
| 129 |
AccessHandler $accessHandler, |
| 130 |
ObjectInformationFactory $objectInformationFactory |
| 131 |
) { |
| 132 |
parent::__construct($php, $wordpress, $wordpressConfig); |
| 133 |
$this->mainConfig = $mainConfig; |
| 134 |
$this->database = $database; |
| 135 |
$this->dateUtil = $dateUtil; |
| 136 |
$this->objectHandler = $objectHandler; |
| 137 |
$this->userHandler = $userHandler; |
| 138 |
$this->userGroupHandler = $userGroupHandler; |
| 139 |
$this->userGroupAssignmentHandler = $userGroupAssignmentHandler; |
| 140 |
$this->accessHandler = $accessHandler; |
| 141 |
$this->objectInformation = $objectInformationFactory->createObjectInformation(); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Sets the current object type, the object id and the user groups. |
| 146 |
* @param string $objectType |
| 147 |
* @param int|string|null $objectId |
| 148 |
* @param array|null $objectUserGroups |
| 149 |
* @throws UserGroupTypeException |
| 150 |
*/ |
| 151 |
protected function setObjectInformation(string $objectType, $objectId, ?array $objectUserGroups = null) |
| 152 |
{ |
| 153 |
$userGroupDiff = 0; |
| 154 |
|
| 155 |
if ($objectUserGroups === null && $objectId !== null) { |
| 156 |
$objectUserGroups = $this->userGroupHandler->getFilteredUserGroupsForObject($objectType, $objectId, true); |
| 157 |
$fullObjectUserGroups = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId, true); |
| 158 |
$userGroupDiff = count((array) $fullObjectUserGroups) - count((array) $objectUserGroups); |
| 159 |
} |
| 160 |
|
| 161 |
$this->objectInformation->setObjectType($objectType) |
| 162 |
->setObjectId($objectId) |
| 163 |
->setObjectUserGroups((array) $objectUserGroups) |
| 164 |
->setUserGroupDiff($userGroupDiff); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns the object information. |
| 169 |
* @return ObjectInformation |
| 170 |
*/ |
| 171 |
public function getObjectInformation(): ObjectInformation |
| 172 |
{ |
| 173 |
return $this->objectInformation; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Returns the default groups form name. |
| 178 |
* @return string |
| 179 |
*/ |
| 180 |
public function getGroupsFormName(): string |
| 181 |
{ |
| 182 |
return ($this->groupsFromName !== null) ? (string) $this->groupsFromName : self::DEFAULT_GROUPS_FORM_NAME; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns the filtered user groups. |
| 187 |
* @return AbstractUserGroup[] |
| 188 |
* @throws UserGroupTypeException |
| 189 |
*/ |
| 190 |
public function getFilteredUserGroups(): array |
| 191 |
{ |
| 192 |
return $this->userGroupHandler->getFilteredUserGroups(); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Sorts the user groups. |
| 197 |
* @param array $userGroups |
| 198 |
*/ |
| 199 |
public function sortUserGroups(array &$userGroups) |
| 200 |
{ |
| 201 |
uasort( |
| 202 |
$userGroups, |
| 203 |
function ( |
| 204 |
AbstractUserGroup $userGroupOne, |
| 205 |
AbstractUserGroup $userGroupTwo |
| 206 |
) { |
| 207 |
$notLoggedInUserGroupId = DynamicUserGroup::USER_TYPE . '|' . DynamicUserGroup::NOT_LOGGED_IN_USER_ID; |
| 208 |
|
| 209 |
if ($userGroupOne->getId() === $notLoggedInUserGroupId) { |
| 210 |
return 1; |
| 211 |
} elseif ($userGroupTwo->getId() === $notLoggedInUserGroupId) { |
| 212 |
return -1; |
| 213 |
} |
| 214 |
|
| 215 |
return strnatcasecmp($userGroupOne->getName(), $userGroupTwo->getName()); |
| 216 |
} |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns the date util. |
| 222 |
* @return DateUtil |
| 223 |
*/ |
| 224 |
public function getDateUtil(): DateUtil |
| 225 |
{ |
| 226 |
return $this->dateUtil; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Checks if the current user is an admin. |
| 231 |
* @return bool |
| 232 |
*/ |
| 233 |
public function isCurrentUserAdmin(): bool |
| 234 |
{ |
| 235 |
if ($this->objectInformation->getObjectType() === ObjectHandler::GENERAL_USER_OBJECT_TYPE |
| 236 |
&& $this->objectInformation->getObjectId() !== null |
| 237 |
) { |
| 238 |
return $this->userHandler->userIsAdmin($this->objectInformation->getObjectId()); |
| 239 |
} |
| 240 |
|
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Returns the wordpress role names. |
| 246 |
* @return array |
| 247 |
*/ |
| 248 |
public function getRoleNames(): array |
| 249 |
{ |
| 250 |
$roles = $this->wordpress->getRoles(); |
| 251 |
return $roles->role_names; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Checks the user access. |
| 256 |
* @return bool |
| 257 |
*/ |
| 258 |
public function checkUserAccess(): bool |
| 259 |
{ |
| 260 |
return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Returns the recursive object membership. |
| 265 |
* @param AbstractUserGroup $userGroup |
| 266 |
* @return array |
| 267 |
* @throws Exception |
| 268 |
* @throws Exception |
| 269 |
*/ |
| 270 |
public function getRecursiveMembership(AbstractUserGroup $userGroup): array |
| 271 |
{ |
| 272 |
$recursiveMembership = []; |
| 273 |
$objectType = $this->objectInformation->getObjectType(); |
| 274 |
$objectId = $this->objectInformation->getObjectId(); |
| 275 |
$recursiveMembershipForObject = $userGroup->getRecursiveMembershipForObject($objectType, $objectId); |
| 276 |
|
| 277 |
/** |
| 278 |
* @var AssignmentInformation[] $assignmentInformation |
| 279 |
*/ |
| 280 |
foreach ($recursiveMembershipForObject as $recursiveType => $assignmentInformation) { |
| 281 |
foreach ($assignmentInformation as $objectId => $information) { |
| 282 |
try { |
| 283 |
$membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType()); |
| 284 |
$typeName = $membershipHandler->getGeneralObjectType(); |
| 285 |
$objectName = $membershipHandler->getObjectName($objectId, $typeName); |
| 286 |
$recursiveMembership[$typeName][$objectId] = $objectName; |
| 287 |
} catch (MissingObjectMembershipHandlerException $exception) { |
| 288 |
// Do nothing |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return $recursiveMembership; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Checks the access and dies if the user has no access. |
| 298 |
* @param string $objectType |
| 299 |
* @param int|string $objectId |
| 300 |
* @throws UserGroupTypeException |
| 301 |
*/ |
| 302 |
private function dieOnNoAccess(string $objectType, $objectId) |
| 303 |
{ |
| 304 |
if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) { |
| 305 |
$this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Shows the error if the user has no rights to edit the content. |
| 311 |
* @throws UserGroupTypeException |
| 312 |
*/ |
| 313 |
public function checkRightsToEditContent() |
| 314 |
{ |
| 315 |
$postIdParameter = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id')); |
| 316 |
|
| 317 |
if ($postIdParameter !== null) { |
| 318 |
$postIds = is_array($postIdParameter) === false ? [$postIdParameter] : $postIdParameter; |
| 319 |
|
| 320 |
foreach ($postIds as $postId) { |
| 321 |
$this->dieOnNoAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
$tagId = $this->getRequestParameter('tag_ID'); |
| 326 |
|
| 327 |
if ($tagId !== null) { |
| 328 |
$this->dieOnNoAccess(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $tagId); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Returns the user groups by reference which should be add and removed from the object. |
| 334 |
* @param string $objectType |
| 335 |
* @param int|string $objectId |
| 336 |
* @param array|null $addUserGroups |
| 337 |
* @param array|null $removeUserGroups |
| 338 |
* @throws UserGroupTypeException |
| 339 |
*/ |
| 340 |
private function getAddRemoveGroups( |
| 341 |
string $objectType, |
| 342 |
$objectId, |
| 343 |
?array &$addUserGroups = [], |
| 344 |
?array &$removeUserGroups = [] |
| 345 |
) { |
| 346 |
if ($addUserGroups === null) { |
| 347 |
$addUserGroups = (array) $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []); |
| 348 |
} |
| 349 |
|
| 350 |
$filteredUserGroupsForObject = $this->userGroupHandler->getFilteredUserGroupsForObject( |
| 351 |
$objectType, |
| 352 |
$objectId |
| 353 |
); |
| 354 |
$removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject)); |
| 355 |
$bulkType = $this->getRequestParameter('uam_bulk_type'); |
| 356 |
|
| 357 |
if ($bulkType === self::BULK_REMOVE) { |
| 358 |
$removeUserGroups = $addUserGroups; |
| 359 |
$addUserGroups = []; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Saves the object data to the database. |
| 365 |
* @param string $objectType The object type. |
| 366 |
* @param int|string|null $objectId The id of the object. |
| 367 |
* @param array|null $addUserGroups The new user groups for the object. |
| 368 |
* @param bool $force If true we force the assignment. |
| 369 |
* @throws UserGroupTypeException |
| 370 |
*/ |
| 371 |
public function saveObjectData(string $objectType, $objectId, ?array $addUserGroups = null, $force = false) |
| 372 |
{ |
| 373 |
$isUpdateForm = (bool) $this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true |
| 374 |
|| $this->getRequestParameter('uam_bulk_type') !== null; |
| 375 |
|
| 376 |
$hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true; |
| 377 |
|
| 378 |
if ($isUpdateForm === true && $hasRights === true || $force === true) { |
| 379 |
$this->getAddRemoveGroups($objectType, $objectId, $addUserGroups, $removeUserGroups); |
| 380 |
|
| 381 |
try { |
| 382 |
$this->userGroupAssignmentHandler->assignObjectToUserGroups( |
| 383 |
$objectType, |
| 384 |
$objectId, |
| 385 |
$addUserGroups, |
| 386 |
$removeUserGroups, |
| 387 |
$this->getRequestParameter(self::DEFAULT_DYNAMIC_GROUPS_FORM_NAME, []) |
| 388 |
); |
| 389 |
} catch (UserGroupAssignmentException $exception) { |
| 390 |
$this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage())); |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Removes the object data. |
| 397 |
* @param string $objectType The object type. |
| 398 |
* @param int|string $id The object id. |
| 399 |
*/ |
| 400 |
public function removeObjectData(string $objectType, $id) |
| 401 |
{ |
| 402 |
$this->database->delete( |
| 403 |
$this->database->getUserGroupToObjectTable(), |
| 404 |
[ |
| 405 |
'object_id' => $id, |
| 406 |
'object_type' => $objectType, |
| 407 |
], |
| 408 |
[ |
| 409 |
'%d', |
| 410 |
'%s' |
| 411 |
] |
| 412 |
); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Returns the group selection form for pluggable objects. |
| 417 |
* @param string $objectType The object type. |
| 418 |
* @param int|string $objectId The id of the object. |
| 419 |
* @param null $formName The formName. |
| 420 |
* @param array|null $objectUserGroups If set we force this user groups for the object. |
| 421 |
* @return string |
| 422 |
* @throws UserGroupTypeException |
| 423 |
*/ |
| 424 |
public function showGroupSelectionForm( |
| 425 |
string $objectType, |
| 426 |
$objectId, |
| 427 |
$formName = null, |
| 428 |
array $objectUserGroups = null |
| 429 |
): string { |
| 430 |
$this->setObjectInformation($objectType, $objectId, $objectUserGroups); |
| 431 |
|
| 432 |
$this->groupsFromName = $formName; |
| 433 |
$formContent = $this->getIncludeContents('GroupSelectionForm.php'); |
| 434 |
$this->groupsFromName = null; |
| 435 |
|
| 436 |
return $formContent; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Returns the column for a pluggable object. |
| 441 |
* @param string $objectType The object type. |
| 442 |
* @param int|string $objectId The object id. |
| 443 |
* @return string |
| 444 |
* @throws UserGroupTypeException |
| 445 |
*/ |
| 446 |
public function getGroupColumn(string $objectType, $objectId): string |
| 447 |
{ |
| 448 |
$this->setObjectInformation($objectType, $objectId); |
| 449 |
return $this->getIncludeContents('ObjectColumn.php'); |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Checks if the current object is a new object. |
| 454 |
* @return bool |
| 455 |
* @throws Exception |
| 456 |
*/ |
| 457 |
public function isNewObject(): bool |
| 458 |
{ |
| 459 |
$objectType = $this->objectInformation->getObjectType(); |
| 460 |
|
| 461 |
if ($objectType !== null) { |
| 462 |
$generalObjectType = $this->objectHandler->getGeneralObjectType($objectType); |
| 463 |
|
| 464 |
return ($this->objectInformation->getObjectId() === null |
| 465 |
|| ($generalObjectType === ObjectHandler::GENERAL_POST_OBJECT_TYPE && |
| 466 |
$this->getRequestParameter('action') !== 'edit') |
| 467 |
); |
| 468 |
} |
| 469 |
|
| 470 |
return false; |
| 471 |
} |
| 472 |
} |
| 473 |
|