| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * AbstractUserGroup.php | |
| 4 | + * | |
| 5 | + * The AbstractUserGroup 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 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\UserGroup; |
| @@ -7,93 +20,247 @@ | ||
| 7 | 20 | use Exception; |
| 8 | 21 | use UserAccessManager\Config\MainConfig; |
| 9 | 22 | use UserAccessManager\Database\Database; |
| 10 | 23 | use UserAccessManager\Object\ObjectHandler; |
| 11 | -use UserAccessManager\ObjectMembership\Exception\MissingObjectMembershipHandlerException; | |
| 24 | +use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException; | |
| 25 | +use UserAccessManager\Util\Util; | |
| 26 | +use UserAccessManager\Wrapper\Php; | |
| 12 | 27 | use UserAccessManager\Wrapper\Wordpress; |
| 13 | 28 | |
| 29 | +/** | |
| 30 | + * Class AbstractUserGroup | |
| 31 | + * | |
| 32 | + * @package UserAccessManager\UserGroup | |
| 33 | + */ | |
| 14 | 34 | abstract class AbstractUserGroup |
| 15 | 35 | { |
| 16 | - public const NONE_ROLE = '_none-role_'; | |
| 36 | + const NONE_ROLE = '_none-role_'; | |
| 17 | 37 | |
| 18 | - protected ?string $type = null; | |
| 19 | - protected ?string $name = null; | |
| 20 | - protected ?string $description = null; | |
| 21 | - protected string $readAccess = 'group'; | |
| 22 | - protected string $writeAccess = 'group'; | |
| 23 | - protected bool $ignoreDates = false; | |
| 24 | - protected array $assignedObjects = []; | |
| 25 | - protected array $objectMembership = []; | |
| 26 | - protected array $fullObjectMembership = []; | |
| 27 | - protected ?array $defaultTypes = null; | |
| 38 | + /** | |
| 39 | + * @var Php | |
| 40 | + */ | |
| 41 | + protected $php; | |
| 28 | 42 | |
| 29 | 43 | /** |
| 44 | + * @var Wordpress | |
| 45 | + */ | |
| 46 | + protected $wordpress; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * @var Database | |
| 50 | + */ | |
| 51 | + protected $database; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * @var MainConfig | |
| 55 | + */ | |
| 56 | + protected $config; | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * @var Util | |
| 60 | + */ | |
| 61 | + protected $util; | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * @var ObjectHandler | |
| 65 | + */ | |
| 66 | + protected $objectHandler; | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * @var AssignmentInformationFactory | |
| 70 | + */ | |
| 71 | + protected $assignmentInformationFactory; | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * @var string | |
| 75 | + */ | |
| 76 | + protected $id = null; | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * @var string | |
| 80 | + */ | |
| 81 | + protected $type = null; | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * @var string | |
| 85 | + */ | |
| 86 | + protected $name = null; | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * @var string | |
| 90 | + */ | |
| 91 | + protected $description = null; | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * @var string | |
| 95 | + */ | |
| 96 | + protected $readAccess = 'group'; | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * @var string | |
| 100 | + */ | |
| 101 | + protected $writeAccess = 'group'; | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * @var bool | |
| 105 | + */ | |
| 106 | + protected $ignoreDates = false; | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * @var array | |
| 110 | + */ | |
| 111 | + protected $assignedObjects = []; | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * @var array | |
| 115 | + */ | |
| 116 | + protected $objectMembership = []; | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * @var array | |
| 120 | + */ | |
| 121 | + protected $fullObjectMembership = []; | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * @var array|null | |
| 125 | + */ | |
| 126 | + protected $defaultTypes = null; | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * AbstractUserGroup constructor. | |
| 130 | + * @param Php $php | |
| 131 | + * @param Wordpress $wordpress | |
| 132 | + * @param Database $database | |
| 133 | + * @param MainConfig $config | |
| 134 | + * @param Util $util | |
| 135 | + * @param ObjectHandler $objectHandler | |
| 136 | + * @param AssignmentInformationFactory $assignmentInformationFactory | |
| 137 | + * @param null|int|string $id | |
| 30 | 138 | * @throws UserGroupTypeException |
| 31 | 139 | */ |
| 32 | 140 | public function __construct( |
| 33 | - protected Wordpress $wordpress, | |
| 34 | - protected Database $database, | |
| 35 | - protected MainConfig $config, | |
| 36 | - protected ObjectHandler $objectHandler, | |
| 37 | - protected AssignedObjectsLoader $assignedObjectsLoader, | |
| 38 | - protected int|string|null $id = null | |
| 141 | + Php $php, | |
| 142 | + Wordpress $wordpress, | |
| 143 | + Database $database, | |
| 144 | + MainConfig $config, | |
| 145 | + Util $util, | |
| 146 | + ObjectHandler $objectHandler, | |
| 147 | + AssignmentInformationFactory $assignmentInformationFactory, | |
| 148 | + $id = null | |
| 39 | 149 | ) { |
| 40 | 150 | if ($this->type === null) { |
| 41 | 151 | throw new UserGroupTypeException('User group type must not null.'); |
| 42 | 152 | } |
| 153 | + | |
| 154 | + $this->php = $php; | |
| 155 | + $this->wordpress = $wordpress; | |
| 156 | + $this->database = $database; | |
| 157 | + $this->config = $config; | |
| 158 | + $this->util = $util; | |
| 159 | + $this->objectHandler = $objectHandler; | |
| 160 | + $this->assignmentInformationFactory = $assignmentInformationFactory; | |
| 161 | + $this->id = $id; | |
| 43 | 162 | } |
| 44 | 163 | |
| 45 | - public function getId(): int|string|null | |
| 164 | + /* | |
| 165 | + * Primary values. | |
| 166 | + */ | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * Returns the group id. | |
| 170 | + * @return int|string|null | |
| 171 | + */ | |
| 172 | + public function getId() | |
| 46 | 173 | { |
| 47 | 174 | return $this->id; |
| 48 | 175 | } |
| 49 | 176 | |
| 177 | + /** | |
| 178 | + * Returns the user group type. | |
| 179 | + * @return string | |
| 180 | + */ | |
| 50 | 181 | public function getType(): ?string |
| 51 | 182 | { |
| 52 | 183 | return $this->type; |
| 53 | 184 | } |
| 54 | 185 | |
| 186 | + /** | |
| 187 | + * Returns the group name. | |
| 188 | + * @return string | |
| 189 | + */ | |
| 55 | 190 | public function getName(): ?string |
| 56 | 191 | { |
| 57 | 192 | return $this->name; |
| 58 | 193 | } |
| 59 | 194 | |
| 60 | - public function setName(string $name): void | |
| 195 | + /** | |
| 196 | + * Sets the group name. | |
| 197 | + * @param string $name The new group name. | |
| 198 | + */ | |
| 199 | + public function setName(string $name) | |
| 61 | 200 | { |
| 62 | 201 | $this->name = $name; |
| 63 | 202 | } |
| 64 | 203 | |
| 204 | + /** | |
| 205 | + * Returns the group description. | |
| 206 | + * @return string | |
| 207 | + */ | |
| 65 | 208 | public function getDescription(): ?string |
| 66 | 209 | { |
| 67 | 210 | return $this->description; |
| 68 | 211 | } |
| 69 | 212 | |
| 70 | - public function setDescription(string $description): void | |
| 213 | + /** | |
| 214 | + * Sets the group description. | |
| 215 | + * @param string $description The new group description. | |
| 216 | + */ | |
| 217 | + public function setDescription(string $description) | |
| 71 | 218 | { |
| 72 | 219 | $this->description = $description; |
| 73 | 220 | } |
| 74 | 221 | |
| 222 | + /** | |
| 223 | + * Returns the read access. | |
| 224 | + * @return string | |
| 225 | + */ | |
| 75 | 226 | public function getReadAccess(): string |
| 76 | 227 | { |
| 77 | 228 | return $this->readAccess; |
| 78 | 229 | } |
| 79 | 230 | |
| 80 | - public function setReadAccess(string $readAccess): void | |
| 231 | + /** | |
| 232 | + * Sets the read access. | |
| 233 | + * @param string $readAccess The read access. | |
| 234 | + */ | |
| 235 | + public function setReadAccess(string $readAccess) | |
| 81 | 236 | { |
| 82 | 237 | $this->readAccess = $readAccess; |
| 83 | 238 | } |
| 84 | 239 | |
| 240 | + /** | |
| 241 | + * Returns the write access. | |
| 242 | + * @return string | |
| 243 | + */ | |
| 85 | 244 | public function getWriteAccess(): string |
| 86 | 245 | { |
| 87 | 246 | return $this->writeAccess; |
| 88 | 247 | } |
| 89 | 248 | |
| 90 | - public function setWriteAccess(string $writeAccess): void | |
| 249 | + /** | |
| 250 | + * Sets the write access. | |
| 251 | + * @param string $writeAccess The write access. | |
| 252 | + */ | |
| 253 | + public function setWriteAccess(string $writeAccess) | |
| 91 | 254 | { |
| 92 | 255 | $this->writeAccess = $writeAccess; |
| 93 | 256 | } |
| 94 | 257 | |
| 95 | - public function setIgnoreDates(bool $ignoreDates): void | |
| 258 | + /** | |
| 259 | + * Sets the ignore dates flag. | |
| 260 | + * @param bool $ignoreDates | |
| 261 | + */ | |
| 262 | + public function setIgnoreDates(bool $ignoreDates) | |
| 96 | 263 | { |
| 97 | 264 | if ($this->ignoreDates !== $ignoreDates) { |
| 98 | 265 | $this->resetObjects(); |
| 99 | 266 | } |
| @@ -100,14 +267,21 @@ | ||
| 100 | 267 | |
| 101 | 268 | $this->ignoreDates = $ignoreDates; |
| 102 | 269 | } |
| 103 | 270 | |
| 271 | + /** | |
| 272 | + * Return the ignore dates flag. | |
| 273 | + * @return bool | |
| 274 | + */ | |
| 104 | 275 | public function getIgnoreDates(): bool |
| 105 | 276 | { |
| 106 | 277 | return $this->ignoreDates; |
| 107 | 278 | } |
| 108 | 279 | |
| 109 | - protected function resetObjects(): void | |
| 280 | + /** | |
| 281 | + * Resets the objects | |
| 282 | + */ | |
| 283 | + protected function resetObjects() | |
| 110 | 284 | { |
| 111 | 285 | $this->assignedObjects = []; |
| 112 | 286 | $this->objectMembership = []; |
| 113 | 287 | $this->fullObjectMembership = []; |
| @@ -112,31 +286,18 @@ | ||
| 112 | 286 | $this->objectMembership = []; |
| 113 | 287 | $this->fullObjectMembership = []; |
| 114 | 288 | } |
| 115 | 289 | |
| 116 | - private function resetObjectsAfterAssignmentChange(): void | |
| 117 | - { | |
| 118 | - $this->assignedObjectsLoader->flush(); | |
| 119 | - $this->resetObjects(); | |
| 120 | - } | |
| 121 | - | |
| 122 | 290 | /** |
| 123 | - * Returns the general object type of an assignable object type, null if the object type can't be assigned. | |
| 124 | - */ | |
| 125 | - private function getAssignableGeneralObjectType(string $objectType): ?string | |
| 126 | - { | |
| 127 | - $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType); | |
| 128 | - | |
| 129 | - return ($generalObjectType !== null && $this->objectHandler->isValidObjectType($objectType) === true) ? | |
| 130 | - $generalObjectType : null; | |
| 131 | - } | |
| 132 | - | |
| 133 | - /** | |
| 291 | + * Deletes the user group. | |
| 292 | + * @return bool | |
| 134 | 293 | * @throws Exception |
| 135 | 294 | */ |
| 136 | 295 | public function delete(): bool |
| 137 | 296 | { |
| 138 | - foreach ($this->objectHandler->getAllObjectTypes() as $objectType) { | |
| 297 | + $allObjectTypes = $this->objectHandler->getAllObjectTypes(); | |
| 298 | + | |
| 299 | + foreach ($allObjectTypes as $objectType) { | |
| 139 | 300 | $this->removeObject($objectType); |
| 140 | 301 | } |
| 141 | 302 | |
| 142 | 303 | return true; |
| @@ -142,19 +303,27 @@ | ||
| 142 | 303 | return true; |
| 143 | 304 | } |
| 144 | 305 | |
| 145 | 306 | /** |
| 307 | + * Adds a object of the given type. | |
| 308 | + * @param string $objectType The object type. | |
| 309 | + * @param int|string $objectId The object id. | |
| 310 | + * @param null $fromDate From date. | |
| 311 | + * @param null $toDate To date. | |
| 312 | + * @return bool | |
| 146 | 313 | * @throws Exception |
| 147 | 314 | */ |
| 148 | - public function addObject(string $objectType, int|string|null $objectId, $fromDate = null, $toDate = null): bool | |
| 315 | + public function addObject(string $objectType, $objectId, $fromDate = null, $toDate = null): bool | |
| 149 | 316 | { |
| 150 | - $generalObjectType = $this->getAssignableGeneralObjectType($objectType); | |
| 317 | + $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType); | |
| 151 | 318 | |
| 152 | - if ($generalObjectType === null) { | |
| 319 | + if ($generalObjectType === null | |
| 320 | + || $this->objectHandler->isValidObjectType($objectType) === false | |
| 321 | + ) { | |
| 153 | 322 | return false; |
| 154 | 323 | } |
| 155 | 324 | |
| 156 | - $success = $this->database->replace( | |
| 325 | + $return = $this->database->replace( | |
| 157 | 326 | $this->database->getUserGroupToObjectTable(), |
| 158 | 327 | [ |
| 159 | 328 | 'group_id' => $this->id, |
| 160 | 329 | 'group_type' => $this->type, |
| @@ -163,30 +332,46 @@ | ||
| 163 | 332 | 'object_type' => $objectType, |
| 164 | 333 | 'from_date' => $fromDate, |
| 165 | 334 | 'to_date' => $toDate |
| 166 | 335 | ], |
| 167 | - ['%s', '%s', '%s', '%s', '%s', '%s', '%s'] | |
| 168 | - ) !== false; | |
| 336 | + [ | |
| 337 | + '%s', | |
| 338 | + '%s', | |
| 339 | + '%s', | |
| 340 | + '%s', | |
| 341 | + '%s', | |
| 342 | + '%s', | |
| 343 | + '%s' | |
| 344 | + ] | |
| 345 | + ); | |
| 169 | 346 | |
| 170 | - if ($success === true) { | |
| 171 | - $this->resetObjectsAfterAssignmentChange(); | |
| 347 | + if ($return !== false) { | |
| 348 | + $this->resetObjects(); | |
| 349 | + return true; | |
| 172 | 350 | } |
| 173 | 351 | |
| 174 | - return $success; | |
| 352 | + return false; | |
| 175 | 353 | } |
| 176 | 354 | |
| 177 | 355 | /** |
| 356 | + * Removes a object of the given type. | |
| 357 | + * @param string $objectType The object type. | |
| 358 | + * @param null $objectId The object id. | |
| 359 | + * @param bool $ignoreGeneralType | |
| 360 | + * @return bool | |
| 178 | 361 | * @throws Exception |
| 179 | 362 | */ |
| 180 | - public function removeObject(string $objectType, $objectId = null, bool $ignoreGeneralType = false): bool | |
| 363 | + public function removeObject(string $objectType, $objectId = null, $ignoreGeneralType = false): bool | |
| 181 | 364 | { |
| 182 | - $generalObjectType = $this->getAssignableGeneralObjectType($objectType); | |
| 365 | + $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType); | |
| 183 | 366 | |
| 184 | - if ($generalObjectType === null) { | |
| 367 | + if ($generalObjectType === null | |
| 368 | + || $this->objectHandler->isValidObjectType($objectType) === false | |
| 369 | + ) { | |
| 185 | 370 | return false; |
| 186 | 371 | } |
| 187 | 372 | |
| 188 | - $objectTypeQuery = " AND `object_type` = '%s' "; | |
| 373 | + $objectTypeQuery = " AND object_type = '%s' "; | |
| 189 | 374 | $values = [ |
| 190 | 375 | $this->id, |
| 191 | 376 | $this->type, |
| 192 | 377 | $objectType |
| @@ -192,26 +377,27 @@ | ||
| 192 | 377 | $objectType |
| 193 | 378 | ]; |
| 194 | 379 | |
| 195 | 380 | if ($ignoreGeneralType === false) { |
| 196 | - $objectTypeQuery = " AND (`object_type` = '%s' OR `general_object_type` = '%s') "; | |
| 381 | + $objectTypeQuery = " AND (object_type = '%s' OR general_object_type = '%s') "; | |
| 197 | 382 | $values[] = $generalObjectType; |
| 198 | 383 | } |
| 199 | 384 | |
| 200 | - $query = "DELETE FROM `{$this->database->getUserGroupToObjectTable()}` | |
| 201 | - WHERE `group_id` = %d | |
| 202 | - AND `group_type` = '%s' | |
| 203 | - $objectTypeQuery"; | |
| 385 | + $query = "DELETE FROM {$this->database->getUserGroupToObjectTable()} | |
| 386 | + WHERE group_id = %d | |
| 387 | + AND group_type = '%s' | |
| 388 | + {$objectTypeQuery}"; | |
| 204 | 389 | |
| 205 | 390 | if ($objectId !== null) { |
| 206 | - $query .= ' AND `object_id` = %d'; | |
| 391 | + $query .= ' AND object_id = %d'; | |
| 207 | 392 | $values[] = $objectId; |
| 208 | 393 | } |
| 209 | 394 | |
| 210 | - $success = $this->database->query($this->database->prepare($query, $values)) !== false; | |
| 395 | + $query = $this->database->prepare($query, $values); | |
| 396 | + $success = ($this->database->query($query) !== false); | |
| 211 | 397 | |
| 212 | 398 | if ($success === true) { |
| 213 | - $this->resetObjectsAfterAssignmentChange(); | |
| 399 | + $this->resetObjects(); | |
| 214 | 400 | } |
| 215 | 401 | |
| 216 | 402 | return $success; |
| 217 | 403 | } |
| @@ -216,28 +402,58 @@ | ||
| 216 | 402 | return $success; |
| 217 | 403 | } |
| 218 | 404 | |
| 219 | 405 | /** |
| 406 | + * Returns the assigned objects. | |
| 407 | + * @param string $objectType The object type. | |
| 220 | 408 | * @return AssignmentInformation[] |
| 221 | 409 | */ |
| 222 | 410 | public function getAssignedObjects(string $objectType): array |
| 223 | 411 | { |
| 224 | - return $this->assignedObjects[$objectType] ??= $this->assignedObjectsLoader->getAssignedObjects( | |
| 225 | - $this->type, | |
| 226 | - $this->id, | |
| 227 | - $objectType, | |
| 228 | - $this->ignoreDates | |
| 229 | - ); | |
| 412 | + if (isset($this->assignedObjects[$objectType]) === false) { | |
| 413 | + $query = "SELECT object_id AS id, object_type AS objectType, from_date AS fromDate, to_date AS toDate | |
| 414 | + FROM {$this->database->getUserGroupToObjectTable()} | |
| 415 | + WHERE group_id = '%s' | |
| 416 | + AND group_type = '%s' | |
| 417 | + AND object_id != '' | |
| 418 | + AND (general_object_type = '%s' OR object_type = '%s')"; | |
| 419 | + | |
| 420 | + $parameters = [ | |
| 421 | + $this->id, | |
| 422 | + $this->type, | |
| 423 | + $objectType, | |
| 424 | + $objectType | |
| 425 | + ]; | |
| 426 | + | |
| 427 | + if ($this->ignoreDates === false) { | |
| 428 | + $query .= " AND (from_date IS NULL OR from_date <= '%s') AND (to_date IS NULL OR to_date >= '%s')"; | |
| 429 | + $time = $this->wordpress->currentTime('mysql'); | |
| 430 | + $parameters = array_merge($parameters, [$time, $time]); | |
| 431 | + } | |
| 432 | + | |
| 433 | + $query = $this->database->prepare($query, $parameters); | |
| 434 | + $results = (array) $this->database->getResults($query); | |
| 435 | + $this->assignedObjects[$objectType] = []; | |
| 436 | + | |
| 437 | + foreach ($results as $result) { | |
| 438 | + $this->assignedObjects[$objectType][$result->id] = $this->assignmentInformationFactory | |
| 439 | + ->createAssignmentInformation($result->objectType, $result->fromDate, $result->toDate); | |
| 440 | + } | |
| 441 | + } | |
| 442 | + | |
| 443 | + return $this->assignedObjects[$objectType]; | |
| 230 | 444 | } |
| 231 | 445 | |
| 232 | 446 | /** |
| 447 | + * Marks the group as default for the object type. | |
| 448 | + * @param string $objectType | |
| 449 | + * @param null|int $fromTime | |
| 450 | + * @param null|int $toTime | |
| 451 | + * @return bool | |
| 233 | 452 | * @throws Exception |
| 234 | 453 | */ |
| 235 | - public function addDefaultType( | |
| 236 | - string $objectType, | |
| 237 | - int|string|null $fromTime = null, | |
| 238 | - int|string|null $toTime = null | |
| 239 | - ): bool { | |
| 454 | + public function addDefaultType(string $objectType, $fromTime = null, $toTime = null): bool | |
| 455 | + { | |
| 240 | 456 | $fromDate = ($fromTime !== null) ? gmdate('Y-m-d H:i:s', $fromTime) : null; |
| 241 | 457 | $toTime = ($toTime !== null && $toTime <= $fromTime) ? $fromTime + 1 : $toTime; |
| 242 | 458 | $toDate = ($toTime !== null) ? gmdate('Y-m-d H:i:s', $toTime) : null; |
| 243 | 459 | |
| @@ -244,8 +460,11 @@ | ||
| 244 | 460 | return $this->addObject($objectType, '', $fromDate, $toDate); |
| 245 | 461 | } |
| 246 | 462 | |
| 247 | 463 | /** |
| 464 | + * Removes the group as default for object type. | |
| 465 | + * @param string $objectType | |
| 466 | + * @return bool | |
| 248 | 467 | * @throws Exception |
| 249 | 468 | */ |
| 250 | 469 | public function removeDefaultType(string $objectType): bool |
| 251 | 470 | { |
| @@ -252,80 +471,103 @@ | ||
| 252 | 471 | return $this->removeObject($objectType, '', true); |
| 253 | 472 | } |
| 254 | 473 | |
| 255 | 474 | /** |
| 256 | - * @return array<string, array{0: int|null, 1: int|null}> Time ranges keyed by the object type. | |
| 475 | + * Returns the object type for which the user group is the default group. | |
| 476 | + * @return array | |
| 257 | 477 | */ |
| 258 | - public function getDefaultGroupForObjectTypes(): array | |
| 478 | + public function getDefaultGroupForObjectTypes(): ?array | |
| 259 | 479 | { |
| 260 | - return $this->defaultTypes ??= $this->loadDefaultGroupForObjectTypes(); | |
| 261 | - } | |
| 480 | + if ($this->defaultTypes === null) { | |
| 481 | + $this->defaultTypes = []; | |
| 262 | 482 | |
| 263 | - /** | |
| 264 | - * @return array<string, array{0: int|null, 1: int|null}> | |
| 265 | - */ | |
| 266 | - private function loadDefaultGroupForObjectTypes(): array | |
| 267 | - { | |
| 268 | - $query = $this->database->prepare( | |
| 269 | - "SELECT `object_type` AS `objectType`, `from_date` AS `fromDate`, `to_date` AS `toDate` | |
| 270 | - FROM `{$this->database->getUserGroupToObjectTable()}` | |
| 271 | - WHERE `group_id` = '%s' | |
| 272 | - AND `group_type` = '%s' | |
| 273 | - AND `object_id` = ''", | |
| 274 | - [ | |
| 483 | + $query = "SELECT object_type AS objectType, from_date AS fromDate, to_date AS toDate | |
| 484 | + FROM {$this->database->getUserGroupToObjectTable()} | |
| 485 | + WHERE group_id = '%s' | |
| 486 | + AND group_type = '%s' | |
| 487 | + AND object_id = ''"; | |
| 488 | + | |
| 489 | + $parameters = [ | |
| 275 | 490 | $this->id, |
| 276 | 491 | $this->type |
| 277 | - ] | |
| 278 | - ); | |
| 492 | + ]; | |
| 279 | 493 | |
| 280 | - $defaultTypes = []; | |
| 494 | + $query = $this->database->prepare($query, $parameters); | |
| 495 | + $results = (array) $this->database->getResults($query); | |
| 281 | 496 | |
| 282 | - foreach ((array) $this->database->getResults($query) as $result) { | |
| 283 | - $defaultTypes[$result->objectType] = [ | |
| 284 | - ($result->fromDate !== null) ? strtotime($result->fromDate) : null, | |
| 285 | - ($result->toDate !== null) ? strtotime($result->toDate) : null | |
| 286 | - ]; | |
| 497 | + foreach ($results as $result) { | |
| 498 | + $this->defaultTypes[$result->objectType] = [ | |
| 499 | + ($result->fromDate !== null) ? strtotime($result->fromDate) : null, | |
| 500 | + ($result->toDate !== null) ? strtotime($result->toDate) : null | |
| 501 | + ]; | |
| 502 | + } | |
| 287 | 503 | } |
| 288 | 504 | |
| 289 | - return $defaultTypes; | |
| 505 | + return $this->defaultTypes; | |
| 290 | 506 | } |
| 291 | 507 | |
| 292 | - public function isDefaultGroupForObjectType(string $objectType, ?int &$fromTime = null, ?int &$toTime = null): bool | |
| 508 | + /** | |
| 509 | + * Checks if the group is the default one for the given object type. | |
| 510 | + * @param string $objectType | |
| 511 | + * @param null|int $fromTime | |
| 512 | + * @param null|int $toTime | |
| 513 | + * @return bool | |
| 514 | + */ | |
| 515 | + public function isDefaultGroupForObjectType(string $objectType, &$fromTime = null, &$toTime = null): bool | |
| 293 | 516 | { |
| 294 | 517 | $defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes(); |
| 295 | 518 | |
| 296 | - // The reference values have to be reset even when the group is no default group for the object type | |
| 519 | + // Reset reference values anyway | |
| 297 | 520 | $fromTime = null; |
| 298 | 521 | $toTime = null; |
| 299 | 522 | |
| 300 | - if (isset($defaultGroupForObjectTypes[$objectType]) === false) { | |
| 301 | - return false; | |
| 523 | + if (isset($defaultGroupForObjectTypes[$objectType])) { | |
| 524 | + $fromTime = $defaultGroupForObjectTypes[$objectType][0] !== null ? | |
| 525 | + (int) $defaultGroupForObjectTypes[$objectType][0] : null; | |
| 526 | + $toTime = $defaultGroupForObjectTypes[$objectType][1] !== null ? | |
| 527 | + (int) $defaultGroupForObjectTypes[$objectType][1] : null; | |
| 528 | + | |
| 529 | + return true; | |
| 302 | 530 | } |
| 303 | 531 | |
| 304 | - [$fromTimestamp, $toTimestamp] = $defaultGroupForObjectTypes[$objectType]; | |
| 305 | - $fromTime = ($fromTimestamp !== null) ? (int) $fromTimestamp : null; | |
| 306 | - $toTime = ($toTimestamp !== null) ? (int) $toTimestamp : null; | |
| 307 | - | |
| 308 | - return true; | |
| 532 | + return false; | |
| 309 | 533 | } |
| 310 | 534 | |
| 535 | + /** | |
| 536 | + * Checks if the object is assigned to the group. | |
| 537 | + * @param string $objectType The object type. | |
| 538 | + * @param int|string $objectId The object id. | |
| 539 | + * @param AssignmentInformation|null $assignmentInformation The assignment information object. | |
| 540 | + * @return bool | |
| 541 | + */ | |
| 311 | 542 | public function isObjectAssignedToGroup( |
| 312 | 543 | string $objectType, |
| 313 | - int|string|null $objectId, | |
| 314 | - ?AssignmentInformation &$assignmentInformation = null | |
| 544 | + $objectId, | |
| 545 | + &$assignmentInformation = null | |
| 315 | 546 | ): bool { |
| 316 | - $assignmentInformation = $this->getAssignedObjects($objectType)[$objectId] ?? null; | |
| 547 | + $assignmentInformation = null; | |
| 548 | + $assignedObjects = $this->getAssignedObjects($objectType); | |
| 317 | 549 | |
| 318 | - return $assignmentInformation !== null; | |
| 550 | + if (isset($assignedObjects[$objectId]) === true) { | |
| 551 | + $assignmentInformation = $assignedObjects[$objectId]; | |
| 552 | + return true; | |
| 553 | + } | |
| 554 | + | |
| 555 | + return false; | |
| 319 | 556 | } |
| 320 | 557 | |
| 321 | 558 | /** |
| 559 | + * Returns a single object. | |
| 560 | + * @param string $objectType The object type. | |
| 561 | + * @param int|string $objectId The id of the object which should be checked. | |
| 562 | + * @param null|AssignmentInformation $assignmentInformation The assignment information | |
| 563 | + * @return bool | |
| 322 | 564 | * @throws Exception |
| 323 | 565 | */ |
| 324 | 566 | public function isObjectMember( |
| 325 | 567 | string $objectType, |
| 326 | - int|string|null $objectId, | |
| 327 | - ?AssignmentInformation &$assignmentInformation = null | |
| 568 | + $objectId, | |
| 569 | + &$assignmentInformation = null | |
| 328 | 570 | ): bool { |
| 329 | 571 | if (isset($this->objectMembership[$objectType][$objectId]) === false) { |
| 330 | 572 | try { |
| 331 | 573 | $isMember = $this->objectHandler->getObjectMembershipHandler($objectType)->isMember( |
| @@ -333,57 +575,78 @@ | ||
| 333 | 575 | $this->config->lockRecursive(), |
| 334 | 576 | $objectId, |
| 335 | 577 | $assignmentInformation |
| 336 | 578 | ); |
| 337 | - } catch (MissingObjectMembershipHandlerException) { | |
| 579 | + } catch (MissingObjectMembershipHandlerException $exception) { | |
| 338 | 580 | $isMember = false; |
| 339 | 581 | } |
| 340 | 582 | |
| 341 | - $this->objectMembership[$objectType][$objectId] = ($isMember === true) ? $assignmentInformation : false; | |
| 583 | + $this->objectMembership[$objectType][$objectId] = ($isMember === true) ? | |
| 584 | + $assignmentInformation : false; | |
| 342 | 585 | } |
| 343 | 586 | |
| 344 | - $membership = $this->objectMembership[$objectType][$objectId]; | |
| 345 | - $assignmentInformation = ($membership instanceof AssignmentInformation) ? $membership : null; | |
| 587 | + $assignmentInformation = ($this->objectMembership[$objectType][$objectId] instanceof AssignmentInformation) ? | |
| 588 | + $this->objectMembership[$objectType][$objectId] : null; | |
| 346 | 589 | |
| 347 | - return $membership !== false; | |
| 590 | + return ($this->objectMembership[$objectType][$objectId] !== false); | |
| 348 | 591 | } |
| 349 | 592 | |
| 350 | 593 | /** |
| 594 | + * Checks if the role is a group member. | |
| 595 | + * @param int|string $roleId | |
| 596 | + * @param null $assignmentInformation | |
| 597 | + * @return bool | |
| 351 | 598 | * @throws Exception |
| 352 | 599 | */ |
| 353 | - public function isRoleMember(int|string|null $roleId, ?AssignmentInformation &$assignmentInformation = null): bool | |
| 600 | + public function isRoleMember($roleId, &$assignmentInformation = null): bool | |
| 354 | 601 | { |
| 355 | 602 | return $this->isObjectMember(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, $roleId, $assignmentInformation); |
| 356 | 603 | } |
| 357 | 604 | |
| 358 | 605 | /** |
| 606 | + * Checks if the user is a group member. | |
| 607 | + * @param int|string $userId The user id. | |
| 608 | + * @param null|AssignmentInformation $assignmentInformation The assignment information. | |
| 609 | + * @return bool | |
| 359 | 610 | * @throws Exception |
| 360 | 611 | */ |
| 361 | - public function isUserMember(int|string|null $userId, ?AssignmentInformation &$assignmentInformation = null): bool | |
| 612 | + public function isUserMember($userId, &$assignmentInformation = null): bool | |
| 362 | 613 | { |
| 363 | 614 | return $this->isObjectMember(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId, $assignmentInformation); |
| 364 | 615 | } |
| 365 | 616 | |
| 366 | 617 | /** |
| 618 | + * Checks if the term is a group member. | |
| 619 | + * @param int|string $termId | |
| 620 | + * @param null|AssignmentInformation $assignmentInformation | |
| 621 | + * @return bool | |
| 367 | 622 | * @throws Exception |
| 368 | 623 | */ |
| 369 | - public function isTermMember(int|string|null $termId, ?AssignmentInformation &$assignmentInformation = null): bool | |
| 624 | + public function isTermMember($termId, &$assignmentInformation = null): bool | |
| 370 | 625 | { |
| 371 | 626 | return $this->isObjectMember(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId, $assignmentInformation); |
| 372 | 627 | } |
| 373 | 628 | |
| 374 | 629 | /** |
| 630 | + * Checks if the post is a group member | |
| 631 | + * @param int|string $postId | |
| 632 | + * @param null|AssignmentInformation $assignmentInformation | |
| 633 | + * @return bool | |
| 375 | 634 | * @throws Exception |
| 376 | 635 | */ |
| 377 | - public function isPostMember(int|string|null $postId, ?AssignmentInformation &$assignmentInformation = null): bool | |
| 636 | + public function isPostMember($postId, &$assignmentInformation = null): bool | |
| 378 | 637 | { |
| 379 | 638 | return $this->isObjectMember(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, $assignmentInformation); |
| 380 | 639 | } |
| 381 | 640 | |
| 382 | 641 | /** |
| 642 | + * Returns the recursive membership. | |
| 643 | + * @param string $objectType The object type. | |
| 644 | + * @param int|string $objectId The object id. | |
| 645 | + * @return array | |
| 383 | 646 | * @throws Exception |
| 384 | 647 | */ |
| 385 | - public function getRecursiveMembershipForObject(string $objectType, int|string|null $objectId): array | |
| 648 | + public function getRecursiveMembershipForObject(string $objectType, $objectId): array | |
| 386 | 649 | { |
| 387 | 650 | /** |
| 388 | 651 | * @var AssignmentInformation $assignmentInformation |
| 389 | 652 | */ |
| @@ -394,16 +657,30 @@ | ||
| 394 | 657 | return []; |
| 395 | 658 | } |
| 396 | 659 | |
| 397 | 660 | /** |
| 661 | + * Returns true if the requested object is locked recursive. | |
| 662 | + * @param string $objectType The object type. | |
| 663 | + * @param int|string $objectId The object id. | |
| 664 | + * @return bool | |
| 398 | 665 | * @throws Exception |
| 399 | 666 | */ |
| 400 | - public function isLockedRecursive(string $objectType, int|string|null $objectId): bool | |
| 667 | + public function isLockedRecursive(string $objectType, $objectId): bool | |
| 401 | 668 | { |
| 402 | - return count($this->getRecursiveMembershipForObject($objectType, $objectId)) > 0; | |
| 669 | + /** | |
| 670 | + * @var AssignmentInformation $assignmentInformation | |
| 671 | + */ | |
| 672 | + if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) { | |
| 673 | + return (count($assignmentInformation->getRecursiveMembership()) > 0); | |
| 674 | + } | |
| 675 | + | |
| 676 | + return false; | |
| 403 | 677 | } |
| 404 | 678 | |
| 405 | 679 | /** |
| 680 | + * Returns all objects of the given type. | |
| 681 | + * @param string $objectType The object type. | |
| 682 | + * @return array | |
| 406 | 683 | * @throws Exception |
| 407 | 684 | */ |
| 408 | 685 | public function getAssignedObjectsByType(string $objectType): array |
| 409 | 686 | { |
| @@ -408,15 +685,15 @@ | ||
| 408 | 685 | public function getAssignedObjectsByType(string $objectType): array |
| 409 | 686 | { |
| 410 | 687 | if (isset($this->fullObjectMembership[$objectType]) === false) { |
| 411 | 688 | try { |
| 412 | - $membershipHandler = $this->objectHandler->getObjectMembershipHandler($objectType); | |
| 413 | - $this->fullObjectMembership[$objectType] = $membershipHandler->getFullObjects( | |
| 689 | + $handler = $this->objectHandler->getObjectMembershipHandler($objectType); | |
| 690 | + $this->fullObjectMembership[$objectType] = $handler->getFullObjects( | |
| 414 | 691 | $this, |
| 415 | 692 | $this->config->lockRecursive(), |
| 416 | 693 | ($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType |
| 417 | 694 | ); |
| 418 | - } catch (MissingObjectMembershipHandlerException) { | |
| 695 | + } catch (MissingObjectMembershipHandlerException $exception) { | |
| 419 | 696 | $this->fullObjectMembership[$objectType] = []; |
| 420 | 697 | } |
| 421 | 698 | } |
| 422 | 699 | |
| @@ -423,8 +700,10 @@ | ||
| 423 | 700 | return $this->fullObjectMembership[$objectType]; |
| 424 | 701 | } |
| 425 | 702 | |
| 426 | 703 | /** |
| 704 | + * Returns the roles assigned to the group. | |
| 705 | + * @return array | |
| 427 | 706 | * @throws Exception |
| 428 | 707 | */ |
| 429 | 708 | public function getFullRoles(): array |
| 430 | 709 | { |
| @@ -430,9 +709,12 @@ | ||
| 430 | 709 | { |
| 431 | 710 | return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE); |
| 432 | 711 | } |
| 433 | 712 | |
| 713 | + | |
| 434 | 714 | /** |
| 715 | + * Returns the users assigned to the group. | |
| 716 | + * @return array | |
| 435 | 717 | * @throws Exception |
| 436 | 718 | */ |
| 437 | 719 | public function getFullUsers(): array |
| 438 | 720 | { |
| @@ -439,8 +721,10 @@ | ||
| 439 | 721 | return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_USER_OBJECT_TYPE); |
| 440 | 722 | } |
| 441 | 723 | |
| 442 | 724 | /** |
| 725 | + * Returns the terms assigned to the group. | |
| 726 | + * @return array | |
| 443 | 727 | * @throws Exception |
| 444 | 728 | */ |
| 445 | 729 | public function getFullTerms(): array |
| 446 | 730 | { |
| @@ -447,8 +731,10 @@ | ||
| 447 | 731 | return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_TERM_OBJECT_TYPE); |
| 448 | 732 | } |
| 449 | 733 | |
| 450 | 734 | /** |
| 735 | + * Returns the posts assigned to the group. | |
| 736 | + * @return array | |
| 451 | 737 | * @throws Exception |
| 452 | 738 | */ |
| 453 | 739 | public function getFullPosts(): array |
| 454 | 740 | { |