| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\UserGroup; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\Config\MainConfig; |
| 9 |
use UserAccessManager\Database\Database; |
| 10 |
use UserAccessManager\Object\ObjectHandler; |
| 11 |
use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException; |
| 12 |
use UserAccessManager\Util\Util; |
| 13 |
use UserAccessManager\Wrapper\Php; |
| 14 |
use UserAccessManager\Wrapper\Wordpress; |
| 15 |
|
| 16 |
abstract class AbstractUserGroup |
| 17 |
{ |
| 18 |
public const NONE_ROLE = '_none-role_'; |
| 19 |
|
| 20 |
protected ?string $type = null; |
| 21 |
protected ?string $name = null; |
| 22 |
protected ?string $description = null; |
| 23 |
protected string $readAccess = 'group'; |
| 24 |
protected string $writeAccess = 'group'; |
| 25 |
protected bool $ignoreDates = false; |
| 26 |
protected array $assignedObjects = []; |
| 27 |
protected array $objectMembership = []; |
| 28 |
protected array $fullObjectMembership = []; |
| 29 |
protected ?array $defaultTypes = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* @throws UserGroupTypeException |
| 33 |
*/ |
| 34 |
public function __construct( |
| 35 |
protected Php $php, |
| 36 |
protected Wordpress $wordpress, |
| 37 |
protected Database $database, |
| 38 |
protected MainConfig $config, |
| 39 |
protected Util $util, |
| 40 |
protected ObjectHandler $objectHandler, |
| 41 |
protected AssignmentInformationFactory $assignmentInformationFactory, |
| 42 |
protected int|string|null $id = null |
| 43 |
) { |
| 44 |
if ($this->type === null) { |
| 45 |
throw new UserGroupTypeException('User group type must not null.'); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function getId(): int|string|null |
| 50 |
{ |
| 51 |
return $this->id; |
| 52 |
} |
| 53 |
|
| 54 |
public function getType(): ?string |
| 55 |
{ |
| 56 |
return $this->type; |
| 57 |
} |
| 58 |
|
| 59 |
public function getName(): ?string |
| 60 |
{ |
| 61 |
return $this->name; |
| 62 |
} |
| 63 |
|
| 64 |
public function setName(string $name): void |
| 65 |
{ |
| 66 |
$this->name = $name; |
| 67 |
} |
| 68 |
|
| 69 |
public function getDescription(): ?string |
| 70 |
{ |
| 71 |
return $this->description; |
| 72 |
} |
| 73 |
|
| 74 |
public function setDescription(string $description): void |
| 75 |
{ |
| 76 |
$this->description = $description; |
| 77 |
} |
| 78 |
|
| 79 |
public function getReadAccess(): string |
| 80 |
{ |
| 81 |
return $this->readAccess; |
| 82 |
} |
| 83 |
|
| 84 |
public function setReadAccess(string $readAccess): void |
| 85 |
{ |
| 86 |
$this->readAccess = $readAccess; |
| 87 |
} |
| 88 |
|
| 89 |
public function getWriteAccess(): string |
| 90 |
{ |
| 91 |
return $this->writeAccess; |
| 92 |
} |
| 93 |
|
| 94 |
public function setWriteAccess(string $writeAccess): void |
| 95 |
{ |
| 96 |
$this->writeAccess = $writeAccess; |
| 97 |
} |
| 98 |
|
| 99 |
public function setIgnoreDates(bool $ignoreDates): void |
| 100 |
{ |
| 101 |
if ($this->ignoreDates !== $ignoreDates) { |
| 102 |
$this->resetObjects(); |
| 103 |
} |
| 104 |
|
| 105 |
$this->ignoreDates = $ignoreDates; |
| 106 |
} |
| 107 |
|
| 108 |
public function getIgnoreDates(): bool |
| 109 |
{ |
| 110 |
return $this->ignoreDates; |
| 111 |
} |
| 112 |
|
| 113 |
protected function resetObjects(): void |
| 114 |
{ |
| 115 |
$this->assignedObjects = []; |
| 116 |
$this->objectMembership = []; |
| 117 |
$this->fullObjectMembership = []; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @throws Exception |
| 122 |
*/ |
| 123 |
public function delete(): bool |
| 124 |
{ |
| 125 |
$allObjectTypes = $this->objectHandler->getAllObjectTypes(); |
| 126 |
|
| 127 |
foreach ($allObjectTypes as $objectType) { |
| 128 |
$this->removeObject($objectType); |
| 129 |
} |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @throws Exception |
| 136 |
*/ |
| 137 |
public function addObject(string $objectType, int|string|null $objectId, $fromDate = null, $toDate = null): bool |
| 138 |
{ |
| 139 |
$generalObjectType = $this->objectHandler->getGeneralObjectType($objectType); |
| 140 |
|
| 141 |
if ($generalObjectType === null |
| 142 |
|| $this->objectHandler->isValidObjectType($objectType) === false |
| 143 |
) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
$return = $this->database->replace( |
| 148 |
$this->database->getUserGroupToObjectTable(), |
| 149 |
[ |
| 150 |
'group_id' => $this->id, |
| 151 |
'group_type' => $this->type, |
| 152 |
'object_id' => $objectId, |
| 153 |
'general_object_type' => $generalObjectType, |
| 154 |
'object_type' => $objectType, |
| 155 |
'from_date' => $fromDate, |
| 156 |
'to_date' => $toDate |
| 157 |
], |
| 158 |
[ |
| 159 |
'%s', |
| 160 |
'%s', |
| 161 |
'%s', |
| 162 |
'%s', |
| 163 |
'%s', |
| 164 |
'%s', |
| 165 |
'%s' |
| 166 |
] |
| 167 |
); |
| 168 |
|
| 169 |
if ($return !== false) { |
| 170 |
$this->resetObjects(); |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* @throws Exception |
| 179 |
*/ |
| 180 |
public function removeObject(string $objectType, $objectId = null, bool $ignoreGeneralType = false): bool |
| 181 |
{ |
| 182 |
$generalObjectType = $this->objectHandler->getGeneralObjectType($objectType); |
| 183 |
|
| 184 |
if ($generalObjectType === null |
| 185 |
|| $this->objectHandler->isValidObjectType($objectType) === false |
| 186 |
) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
$objectTypeQuery = " AND object_type = '%s' "; |
| 191 |
$values = [ |
| 192 |
$this->id, |
| 193 |
$this->type, |
| 194 |
$objectType |
| 195 |
]; |
| 196 |
|
| 197 |
if ($ignoreGeneralType === false) { |
| 198 |
$objectTypeQuery = " AND (object_type = '%s' OR general_object_type = '%s') "; |
| 199 |
$values[] = $generalObjectType; |
| 200 |
} |
| 201 |
|
| 202 |
$query = "DELETE FROM {$this->database->getUserGroupToObjectTable()} |
| 203 |
WHERE group_id = %d |
| 204 |
AND group_type = '%s' |
| 205 |
$objectTypeQuery"; |
| 206 |
|
| 207 |
if ($objectId !== null) { |
| 208 |
$query .= ' AND object_id = %d'; |
| 209 |
$values[] = $objectId; |
| 210 |
} |
| 211 |
|
| 212 |
$query = $this->database->prepare($query, $values); |
| 213 |
$success = ($this->database->query($query) !== false); |
| 214 |
|
| 215 |
if ($success === true) { |
| 216 |
$this->resetObjects(); |
| 217 |
} |
| 218 |
|
| 219 |
return $success; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* @return AssignmentInformation[] |
| 224 |
*/ |
| 225 |
public function getAssignedObjects(string $objectType): array |
| 226 |
{ |
| 227 |
if (isset($this->assignedObjects[$objectType]) === false) { |
| 228 |
$query = "SELECT object_id AS id, object_type AS objectType, from_date AS fromDate, to_date AS toDate |
| 229 |
FROM {$this->database->getUserGroupToObjectTable()} |
| 230 |
WHERE group_id = '%s' |
| 231 |
AND group_type = '%s' |
| 232 |
AND object_id != '' |
| 233 |
AND (general_object_type = '%s' OR object_type = '%s')"; |
| 234 |
|
| 235 |
$parameters = [ |
| 236 |
$this->id, |
| 237 |
$this->type, |
| 238 |
$objectType, |
| 239 |
$objectType |
| 240 |
]; |
| 241 |
|
| 242 |
if ($this->ignoreDates === false) { |
| 243 |
$query .= " AND (from_date IS NULL OR from_date <= '%s') AND (to_date IS NULL OR to_date >= '%s')"; |
| 244 |
$time = $this->wordpress->currentTime('mysql'); |
| 245 |
$parameters = array_merge($parameters, [$time, $time]); |
| 246 |
} |
| 247 |
|
| 248 |
$query = $this->database->prepare($query, $parameters); |
| 249 |
$results = (array) $this->database->getResults($query); |
| 250 |
$this->assignedObjects[$objectType] = []; |
| 251 |
|
| 252 |
foreach ($results as $result) { |
| 253 |
$this->assignedObjects[$objectType][$result->id] = $this->assignmentInformationFactory |
| 254 |
->createAssignmentInformation($result->objectType, $result->fromDate, $result->toDate); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
return $this->assignedObjects[$objectType]; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* @throws Exception |
| 263 |
*/ |
| 264 |
public function addDefaultType( |
| 265 |
string $objectType, |
| 266 |
int|string|null $fromTime = null, |
| 267 |
int|string|null $toTime = null |
| 268 |
): bool { |
| 269 |
$fromDate = ($fromTime !== null) ? gmdate('Y-m-d H:i:s', $fromTime) : null; |
| 270 |
$toTime = ($toTime !== null && $toTime <= $fromTime) ? $fromTime + 1 : $toTime; |
| 271 |
$toDate = ($toTime !== null) ? gmdate('Y-m-d H:i:s', $toTime) : null; |
| 272 |
|
| 273 |
return $this->addObject($objectType, '', $fromDate, $toDate); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* @throws Exception |
| 278 |
*/ |
| 279 |
public function removeDefaultType(string $objectType): bool |
| 280 |
{ |
| 281 |
return $this->removeObject($objectType, '', true); |
| 282 |
} |
| 283 |
|
| 284 |
public function getDefaultGroupForObjectTypes(): ?array |
| 285 |
{ |
| 286 |
if ($this->defaultTypes === null) { |
| 287 |
$this->defaultTypes = []; |
| 288 |
|
| 289 |
$query = "SELECT object_type AS objectType, from_date AS fromDate, to_date AS toDate |
| 290 |
FROM {$this->database->getUserGroupToObjectTable()} |
| 291 |
WHERE group_id = '%s' |
| 292 |
AND group_type = '%s' |
| 293 |
AND object_id = ''"; |
| 294 |
|
| 295 |
$parameters = [ |
| 296 |
$this->id, |
| 297 |
$this->type |
| 298 |
]; |
| 299 |
|
| 300 |
$query = $this->database->prepare($query, $parameters); |
| 301 |
$results = (array) $this->database->getResults($query); |
| 302 |
|
| 303 |
foreach ($results as $result) { |
| 304 |
$this->defaultTypes[$result->objectType] = [ |
| 305 |
($result->fromDate !== null) ? strtotime($result->fromDate) : null, |
| 306 |
($result->toDate !== null) ? strtotime($result->toDate) : null |
| 307 |
]; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
return $this->defaultTypes; |
| 312 |
} |
| 313 |
|
| 314 |
public function isDefaultGroupForObjectType(string $objectType, int &$fromTime = null, int &$toTime = null): bool |
| 315 |
{ |
| 316 |
$defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes(); |
| 317 |
|
| 318 |
// Reset reference values anyway |
| 319 |
$fromTime = null; |
| 320 |
$toTime = null; |
| 321 |
|
| 322 |
if (isset($defaultGroupForObjectTypes[$objectType])) { |
| 323 |
$fromTime = $defaultGroupForObjectTypes[$objectType][0] !== null ? |
| 324 |
(int) $defaultGroupForObjectTypes[$objectType][0] : null; |
| 325 |
$toTime = $defaultGroupForObjectTypes[$objectType][1] !== null ? |
| 326 |
(int) $defaultGroupForObjectTypes[$objectType][1] : null; |
| 327 |
|
| 328 |
return true; |
| 329 |
} |
| 330 |
|
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
public function isObjectAssignedToGroup( |
| 335 |
string $objectType, |
| 336 |
int|string|null $objectId, |
| 337 |
AssignmentInformation &$assignmentInformation = null |
| 338 |
): bool { |
| 339 |
$assignmentInformation = null; |
| 340 |
$assignedObjects = $this->getAssignedObjects($objectType); |
| 341 |
|
| 342 |
if (isset($assignedObjects[$objectId]) === true) { |
| 343 |
$assignmentInformation = $assignedObjects[$objectId]; |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* @throws Exception |
| 352 |
*/ |
| 353 |
public function isObjectMember( |
| 354 |
string $objectType, |
| 355 |
int|string|null $objectId, |
| 356 |
AssignmentInformation &$assignmentInformation = null |
| 357 |
): bool { |
| 358 |
if (isset($this->objectMembership[$objectType][$objectId]) === false) { |
| 359 |
try { |
| 360 |
$isMember = $this->objectHandler->getObjectMembershipHandler($objectType)->isMember( |
| 361 |
$this, |
| 362 |
$this->config->lockRecursive(), |
| 363 |
$objectId, |
| 364 |
$assignmentInformation |
| 365 |
); |
| 366 |
} catch (MissingObjectMembershipHandlerException) { |
| 367 |
$isMember = false; |
| 368 |
} |
| 369 |
|
| 370 |
$this->objectMembership[$objectType][$objectId] = ($isMember === true) ? |
| 371 |
$assignmentInformation : false; |
| 372 |
} |
| 373 |
|
| 374 |
$assignmentInformation = ($this->objectMembership[$objectType][$objectId] instanceof AssignmentInformation) ? |
| 375 |
$this->objectMembership[$objectType][$objectId] : null; |
| 376 |
|
| 377 |
return ($this->objectMembership[$objectType][$objectId] !== false); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* @throws Exception |
| 382 |
*/ |
| 383 |
public function isRoleMember(int|string $roleId, ?AssignmentInformation &$assignmentInformation = null): bool |
| 384 |
{ |
| 385 |
return $this->isObjectMember(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, $roleId, $assignmentInformation); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* @throws Exception |
| 390 |
*/ |
| 391 |
public function isUserMember(int|string $userId, AssignmentInformation &$assignmentInformation = null): bool |
| 392 |
{ |
| 393 |
return $this->isObjectMember(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId, $assignmentInformation); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* @throws Exception |
| 398 |
*/ |
| 399 |
public function isTermMember(int|string $termId, AssignmentInformation &$assignmentInformation = null): bool |
| 400 |
{ |
| 401 |
return $this->isObjectMember(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId, $assignmentInformation); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* @throws Exception |
| 406 |
*/ |
| 407 |
public function isPostMember(int|string $postId, AssignmentInformation &$assignmentInformation = null): bool |
| 408 |
{ |
| 409 |
return $this->isObjectMember(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, $assignmentInformation); |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* @throws Exception |
| 414 |
*/ |
| 415 |
public function getRecursiveMembershipForObject(string $objectType, int|string|null $objectId): array |
| 416 |
{ |
| 417 |
/** |
| 418 |
* @var AssignmentInformation $assignmentInformation |
| 419 |
*/ |
| 420 |
if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) { |
| 421 |
return $assignmentInformation->getRecursiveMembership(); |
| 422 |
} |
| 423 |
|
| 424 |
return []; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* @throws Exception |
| 429 |
*/ |
| 430 |
public function isLockedRecursive(string $objectType, int|string|null $objectId): bool |
| 431 |
{ |
| 432 |
/** |
| 433 |
* @var AssignmentInformation $assignmentInformation |
| 434 |
*/ |
| 435 |
if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) { |
| 436 |
return (count($assignmentInformation->getRecursiveMembership()) > 0); |
| 437 |
} |
| 438 |
|
| 439 |
return false; |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* @throws Exception |
| 444 |
*/ |
| 445 |
public function getAssignedObjectsByType(string $objectType): array |
| 446 |
{ |
| 447 |
if (isset($this->fullObjectMembership[$objectType]) === false) { |
| 448 |
try { |
| 449 |
$handler = $this->objectHandler->getObjectMembershipHandler($objectType); |
| 450 |
$this->fullObjectMembership[$objectType] = $handler->getFullObjects( |
| 451 |
$this, |
| 452 |
$this->config->lockRecursive(), |
| 453 |
($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType |
| 454 |
); |
| 455 |
} catch (MissingObjectMembershipHandlerException) { |
| 456 |
$this->fullObjectMembership[$objectType] = []; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
return $this->fullObjectMembership[$objectType]; |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* @throws Exception |
| 465 |
*/ |
| 466 |
public function getFullRoles(): array |
| 467 |
{ |
| 468 |
return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
/** |
| 473 |
* @throws Exception |
| 474 |
*/ |
| 475 |
public function getFullUsers(): array |
| 476 |
{ |
| 477 |
return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_USER_OBJECT_TYPE); |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* @throws Exception |
| 482 |
*/ |
| 483 |
public function getFullTerms(): array |
| 484 |
{ |
| 485 |
return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_TERM_OBJECT_TYPE); |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* @throws Exception |
| 490 |
*/ |
| 491 |
public function getFullPosts(): array |
| 492 |
{ |
| 493 |
return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_POST_OBJECT_TYPE); |
| 494 |
} |
| 495 |
} |
| 496 |
|