| 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\Util\Util; |
| 12 |
use UserAccessManager\Wrapper\Php; |
| 13 |
use UserAccessManager\Wrapper\Wordpress; |
| 14 |
|
| 15 |
class DynamicUserGroup extends AbstractUserGroup |
| 16 |
{ |
| 17 |
public const USER_TYPE = 'user'; |
| 18 |
public const ROLE_TYPE = 'role'; |
| 19 |
public const NOT_LOGGED_IN_USER_ID = 0; |
| 20 |
|
| 21 |
/** |
| 22 |
* @throws UserGroupTypeException |
| 23 |
*/ |
| 24 |
public function __construct( |
| 25 |
Php $php, |
| 26 |
Wordpress $wordpress, |
| 27 |
Database $database, |
| 28 |
MainConfig $config, |
| 29 |
Util $util, |
| 30 |
ObjectHandler $objectHandler, |
| 31 |
AssignmentInformationFactory $assignmentInformationFactory, |
| 32 |
protected ?string $type, |
| 33 |
int|string $id |
| 34 |
) { |
| 35 |
parent::__construct( |
| 36 |
$php, |
| 37 |
$wordpress, |
| 38 |
$database, |
| 39 |
$config, |
| 40 |
$util, |
| 41 |
$objectHandler, |
| 42 |
$assignmentInformationFactory, |
| 43 |
$id |
| 44 |
); |
| 45 |
|
| 46 |
if ($this->type !== self::USER_TYPE && $this->type !== self::ROLE_TYPE) { |
| 47 |
throw new UserGroupTypeException('Invalid dynamic group type.'); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public function getId(): string |
| 52 |
{ |
| 53 |
return $this->type . '|' . $this->id; |
| 54 |
} |
| 55 |
|
| 56 |
public function getName(): string |
| 57 |
{ |
| 58 |
if ($this->name === null) { |
| 59 |
$this->name = ''; |
| 60 |
|
| 61 |
if ($this->type === self::USER_TYPE && (int) $this->id === self::NOT_LOGGED_IN_USER_ID) { |
| 62 |
$this->name = TXT_UAM_ADD_DYNAMIC_NOT_LOGGED_IN_USERS; |
| 63 |
} elseif ($this->type === self::USER_TYPE) { |
| 64 |
$userData = $this->wordpress->getUserData($this->id); |
| 65 |
$userName = $userData !== false ? "$userData->display_name ($userData->user_login)" : ''; |
| 66 |
$this->name = TXT_UAM_USER . ": $userName"; |
| 67 |
} elseif ($this->type === self::ROLE_TYPE) { |
| 68 |
$roles = $this->wordpress->getRoles()->roles; |
| 69 |
$this->name = TXT_UAM_ROLE . ': '; |
| 70 |
$this->name .= (isset($roles[$this->id]['name']) === true) ? $roles[$this->id]['name'] : $this->id; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $this->name; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @throws UserGroupAssignmentException |
| 79 |
* @throws Exception |
| 80 |
*/ |
| 81 |
public function addObject(string $objectType, int|string $objectId, $fromDate = null, $toDate = null): bool |
| 82 |
{ |
| 83 |
if ($this->objectHandler->getGeneralObjectType($objectType) === ObjectHandler::GENERAL_USER_OBJECT_TYPE) { |
| 84 |
throw new UserGroupAssignmentException('Dynamic user groups can\'t be assigned to user.'); |
| 85 |
} |
| 86 |
|
| 87 |
return parent::addObject($objectType, $objectId, $fromDate, $toDate); |
| 88 |
} |
| 89 |
} |
| 90 |
|