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