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