| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * DynamicUserGroup.php | |
| 4 | + * | |
| 5 | + * The DynamicUserGroup 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,69 +20,115 @@ | ||
| 7 | 20 | use Exception; |
| 8 | 21 | use UserAccessManager\Config\MainConfig; |
| 9 | 22 | use UserAccessManager\Database\Database; |
| 10 | 23 | use UserAccessManager\Object\ObjectHandler; |
| 24 | +use UserAccessManager\Util\Util; | |
| 25 | +use UserAccessManager\Wrapper\Php; | |
| 11 | 26 | use UserAccessManager\Wrapper\Wordpress; |
| 12 | 27 | |
| 28 | +/** | |
| 29 | + * Class DynamicUserGroup | |
| 30 | + * | |
| 31 | + * @package UserAccessManager\UserGroup | |
| 32 | + */ | |
| 13 | 33 | class DynamicUserGroup extends AbstractUserGroup |
| 14 | 34 | { |
| 15 | - public const USER_TYPE = 'user'; | |
| 16 | - public const ROLE_TYPE = 'role'; | |
| 17 | - public const NOT_LOGGED_IN_USER_ID = 0; | |
| 35 | + const USER_TYPE = 'user'; | |
| 36 | + const ROLE_TYPE = 'role'; | |
| 37 | + const NOT_LOGGED_IN_USER_ID = 0; | |
| 18 | 38 | |
| 19 | 39 | /** |
| 40 | + * @var string | |
| 41 | + */ | |
| 42 | + protected $type = null; | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * DynamicUserGroup constructor. | |
| 46 | + * @param Php $php | |
| 47 | + * @param Wordpress $wordpress | |
| 48 | + * @param Database $database | |
| 49 | + * @param MainConfig $config | |
| 50 | + * @param Util $util | |
| 51 | + * @param ObjectHandler $objectHandler | |
| 52 | + * @param AssignmentInformationFactory $assignmentInformationFactory | |
| 53 | + * @param string $type | |
| 54 | + * @param int|string $id | |
| 20 | 55 | * @throws UserGroupTypeException |
| 21 | 56 | */ |
| 22 | 57 | public function __construct( |
| 58 | + Php $php, | |
| 23 | 59 | Wordpress $wordpress, |
| 24 | 60 | Database $database, |
| 25 | 61 | MainConfig $config, |
| 62 | + Util $util, | |
| 26 | 63 | ObjectHandler $objectHandler, |
| 27 | - AssignedObjectsLoader $assignedObjectsLoader, | |
| 28 | - protected ?string $type, | |
| 29 | - int|string $id | |
| 64 | + AssignmentInformationFactory $assignmentInformationFactory, | |
| 65 | + string $type, | |
| 66 | + $id | |
| 30 | 67 | ) { |
| 31 | - parent::__construct($wordpress, $database, $config, $objectHandler, $assignedObjectsLoader, $id); | |
| 68 | + $this->type = $type; | |
| 32 | 69 | |
| 33 | - if ($this->type !== self::USER_TYPE && $this->type !== self::ROLE_TYPE) { | |
| 70 | + parent::__construct( | |
| 71 | + $php, | |
| 72 | + $wordpress, | |
| 73 | + $database, | |
| 74 | + $config, | |
| 75 | + $util, | |
| 76 | + $objectHandler, | |
| 77 | + $assignmentInformationFactory, | |
| 78 | + $id | |
| 79 | + ); | |
| 80 | + | |
| 81 | + if ($type !== self::USER_TYPE && $type !== self::ROLE_TYPE) { | |
| 34 | 82 | throw new UserGroupTypeException('Invalid dynamic group type.'); |
| 35 | 83 | } |
| 36 | 84 | } |
| 37 | 85 | |
| 86 | + /** | |
| 87 | + * Returns the dynamic user group id. | |
| 88 | + * @return string | |
| 89 | + */ | |
| 38 | 90 | public function getId(): string |
| 39 | 91 | { |
| 40 | 92 | return $this->type . '|' . $this->id; |
| 41 | 93 | } |
| 42 | 94 | |
| 95 | + /** | |
| 96 | + * Returns the dynamic group name. | |
| 97 | + * @return string | |
| 98 | + */ | |
| 43 | 99 | public function getName(): string |
| 44 | 100 | { |
| 45 | - return $this->name ??= ($this->type === self::ROLE_TYPE) ? $this->createRoleName() : $this->createUserName(); | |
| 46 | - } | |
| 101 | + if ($this->name === null) { | |
| 102 | + $this->name = ''; | |
| 47 | 103 | |
| 48 | - private function createRoleName(): string | |
| 49 | - { | |
| 50 | - $roles = $this->wordpress->getRoles()->roles; | |
| 51 | - | |
| 52 | - return TXT_UAM_ROLE . ': ' . ($roles[$this->id]['name'] ?? $this->id); | |
| 53 | - } | |
| 54 | - | |
| 55 | - private function createUserName(): string | |
| 56 | - { | |
| 57 | - if ((int) $this->id === self::NOT_LOGGED_IN_USER_ID) { | |
| 58 | - return TXT_UAM_ADD_DYNAMIC_NOT_LOGGED_IN_USERS; | |
| 104 | + if ($this->type === self::USER_TYPE && (int) $this->id === self::NOT_LOGGED_IN_USER_ID) { | |
| 105 | + $this->name = TXT_UAM_ADD_DYNAMIC_NOT_LOGGED_IN_USERS; | |
| 106 | + } elseif ($this->type === self::USER_TYPE) { | |
| 107 | + $userData = $this->wordpress->getUserData($this->id); | |
| 108 | + $userName = $userData !== false ? "$userData->display_name ($userData->user_login)" : ''; | |
| 109 | + $this->name = TXT_UAM_USER . ": $userName"; | |
| 110 | + } elseif ($this->type === self::ROLE_TYPE) { | |
| 111 | + $roles = $this->wordpress->getRoles()->roles; | |
| 112 | + $this->name = TXT_UAM_ROLE . ': '; | |
| 113 | + $this->name .= (isset($roles[$this->id]['name']) === true) ? $roles[$this->id]['name'] : $this->id; | |
| 114 | + } | |
| 59 | 115 | } |
| 60 | 116 | |
| 61 | - $userData = $this->wordpress->getUserData($this->id); | |
| 62 | - $userName = ($userData !== false) ? "$userData->display_name ($userData->user_login)" : ''; | |
| 63 | - | |
| 64 | - return TXT_UAM_USER . ": $userName"; | |
| 117 | + return $this->name; | |
| 65 | 118 | } |
| 66 | 119 | |
| 67 | 120 | /** |
| 121 | + * Checks if the user group is assigned to a user. | |
| 122 | + * @param string $objectType | |
| 123 | + * @param int|string $objectId | |
| 124 | + * @param null $fromDate | |
| 125 | + * @param null $toDate | |
| 126 | + * @return bool | |
| 68 | 127 | * @throws UserGroupAssignmentException |
| 69 | 128 | * @throws Exception |
| 70 | 129 | */ |
| 71 | - public function addObject(string $objectType, int|string|null $objectId, $fromDate = null, $toDate = null): bool | |
| 130 | + public function addObject(string $objectType, $objectId, $fromDate = null, $toDate = null): bool | |
| 72 | 131 | { |
| 73 | 132 | if ($this->objectHandler->getGeneralObjectType($objectType) === ObjectHandler::GENERAL_USER_OBJECT_TYPE) { |
| 74 | 133 | throw new UserGroupAssignmentException('Dynamic user groups can\'t be assigned to user.'); |
| 75 | 134 | } |