| @@ -19,9 +19,9 @@ | ||
| 19 | 19 | |
| 20 | 20 | use UserAccessManager\Config\MainConfig; |
| 21 | 21 | use UserAccessManager\Database\Database; |
| 22 | 22 | use UserAccessManager\Object\ObjectHandler; |
| 23 | -use UserAccessManager\UserGroup\AbstractUserGroup; | |
| 23 | +use UserAccessManager\UserGroup\UserGroup; | |
| 24 | 24 | use UserAccessManager\Wrapper\Wordpress; |
| 25 | 25 | use WP_User; |
| 26 | 26 | |
| 27 | 27 | /** |
| @@ -30,19 +30,55 @@ | ||
| 30 | 30 | * @package UserAccessManager\UserHandler |
| 31 | 31 | */ |
| 32 | 32 | class UserHandler |
| 33 | 33 | { |
| 34 | - public const MANAGE_USER_GROUPS_CAPABILITY = 'manage_user_groups'; | |
| 34 | + const MANAGE_USER_GROUPS_CAPABILITY = 'manage_user_groups'; | |
| 35 | 35 | |
| 36 | + /** | |
| 37 | + * @var Wordpress | |
| 38 | + */ | |
| 39 | + private $wordpress; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * @var MainConfig | |
| 43 | + */ | |
| 44 | + private $config; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * @var Database | |
| 48 | + */ | |
| 49 | + private $database; | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * @var ObjectHandler | |
| 53 | + */ | |
| 54 | + private $objectHandler; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * The constructor | |
| 58 | + * @param Wordpress $wordpress | |
| 59 | + * @param MainConfig $config | |
| 60 | + * @param Database $database | |
| 61 | + * @param ObjectHandler $objectHandler | |
| 62 | + */ | |
| 36 | 63 | public function __construct( |
| 37 | - private Wordpress $wordpress, | |
| 38 | - private MainConfig $config, | |
| 39 | - private Database $database, | |
| 40 | - private ObjectHandler $objectHandler | |
| 64 | + Wordpress $wordpress, | |
| 65 | + MainConfig $config, | |
| 66 | + Database $database, | |
| 67 | + ObjectHandler $objectHandler | |
| 41 | 68 | ) { |
| 69 | + $this->wordpress = $wordpress; | |
| 70 | + $this->config = $config; | |
| 71 | + $this->database = $database; | |
| 72 | + $this->objectHandler = $objectHandler; | |
| 42 | 73 | } |
| 43 | 74 | |
| 44 | - private function calculateIp(string $ip): bool|string | |
| 75 | + /** | |
| 76 | + * Converts the ip to an integer. | |
| 77 | + * @param string $ip | |
| 78 | + * @return string|false | |
| 79 | + */ | |
| 80 | + private function calculateIp(string $ip) | |
| 45 | 81 | { |
| 46 | 82 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) { |
| 47 | 83 | return base_convert((string) ip2long($ip), 10, 2); |
| 48 | 84 | } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { |
| @@ -60,13 +96,18 @@ | ||
| 60 | 96 | |
| 61 | 97 | return $binaryIp; |
| 62 | 98 | } |
| 63 | 99 | |
| 100 | + /** | |
| 101 | + * Calculates the ip range. | |
| 102 | + * @param string $ipRange | |
| 103 | + * @return array | |
| 104 | + */ | |
| 64 | 105 | private function getCalculatedRange(string $ipRange): array |
| 65 | 106 | { |
| 66 | 107 | $ipRange = explode('-', $ipRange); |
| 67 | 108 | $rangeBegin = $ipRange[0]; |
| 68 | - $rangeEnd = $ipRange[1] ?? $ipRange[0]; | |
| 109 | + $rangeEnd = isset($ipRange[1]) ? $ipRange[1] : $ipRange[0]; | |
| 69 | 110 | |
| 70 | 111 | return [ |
| 71 | 112 | $this->calculateIp($rangeBegin), |
| 72 | 113 | $this->calculateIp($rangeEnd) |
| @@ -72,8 +113,14 @@ | ||
| 72 | 113 | $this->calculateIp($rangeEnd) |
| 73 | 114 | ]; |
| 74 | 115 | } |
| 75 | 116 | |
| 117 | + /** | |
| 118 | + * Checks if the given ip matches with the range. | |
| 119 | + * @param string $currentIp The ip of the current user. | |
| 120 | + * @param array $ipRanges The ip ranges. | |
| 121 | + * @return bool | |
| 122 | + */ | |
| 76 | 123 | public function isIpInRange(string $currentIp, array $ipRanges): bool |
| 77 | 124 | { |
| 78 | 125 | $currentIp = $this->calculateIp($currentIp); |
| 79 | 126 | |
| @@ -91,9 +138,14 @@ | ||
| 91 | 138 | |
| 92 | 139 | return false; |
| 93 | 140 | } |
| 94 | 141 | |
| 95 | - public function getUserRole(WP_User|bool $user): array | |
| 142 | + /** | |
| 143 | + * Return the role of the user. | |
| 144 | + * @param WP_User|false $user The user. | |
| 145 | + * @return array | |
| 146 | + */ | |
| 147 | + public function getUserRole($user): array | |
| 96 | 148 | { |
| 97 | 149 | if ($user instanceof WP_User && isset($user->{$this->database->getPrefix() . 'capabilities'}) === true) { |
| 98 | 150 | $capabilities = (array) $user->{$this->database->getPrefix() . 'capabilities'}; |
| 99 | 151 | } else { |
| @@ -99,12 +151,17 @@ | ||
| 99 | 151 | } else { |
| 100 | 152 | $capabilities = []; |
| 101 | 153 | } |
| 102 | 154 | |
| 103 | - return (count($capabilities) > 0) ? array_keys($capabilities) : [AbstractUserGroup::NONE_ROLE]; | |
| 155 | + return (count($capabilities) > 0) ? array_keys($capabilities) : [UserGroup::NONE_ROLE]; | |
| 104 | 156 | } |
| 105 | 157 | |
| 106 | - public function checkUserAccess(bool|string $allowedCapability = false): bool | |
| 158 | + /** | |
| 159 | + * Checks the user access by user level. | |
| 160 | + * @param bool|string $allowedCapability If set check also for the capability. | |
| 161 | + * @return bool | |
| 162 | + */ | |
| 163 | + public function checkUserAccess($allowedCapability = false): bool | |
| 107 | 164 | { |
| 108 | 165 | $currentUser = $this->wordpress->getCurrentUser(); |
| 109 | 166 | |
| 110 | 167 | if ($this->wordpress->isSuperAdmin($currentUser->ID) === true |
| @@ -115,16 +172,9 @@ | ||
| 115 | 172 | |
| 116 | 173 | $roles = $this->getUserRole($currentUser); |
| 117 | 174 | $rolesMap = array_flip($roles); |
| 118 | 175 | |
| 119 | - $orderedRoles = [ | |
| 120 | - AbstractUserGroup::NONE_ROLE, | |
| 121 | - 'subscriber', | |
| 122 | - 'contributor', | |
| 123 | - 'author', | |
| 124 | - 'editor', | |
| 125 | - 'administrator' | |
| 126 | - ]; | |
| 176 | + $orderedRoles = [UserGroup::NONE_ROLE, 'subscriber', 'contributor', 'author', 'editor', 'administrator']; | |
| 127 | 177 | $orderedRolesMap = array_flip($orderedRoles); |
| 128 | 178 | |
| 129 | 179 | $userRoles = array_intersect_key($orderedRolesMap, $rolesMap); |
| 130 | 180 | $rightsLevel = (count($userRoles) > 0) ? end($userRoles) : -1; |
| @@ -134,9 +184,14 @@ | ||
| 134 | 184 | || isset($rolesMap['administrator']) === true |
| 135 | 185 | ); |
| 136 | 186 | } |
| 137 | 187 | |
| 138 | - public function userIsAdmin(int|string|null $userId): bool | |
| 188 | + /** | |
| 189 | + * Checks if the user is an admin user | |
| 190 | + * @param int|string $userId The user id. | |
| 191 | + * @return bool | |
| 192 | + */ | |
| 193 | + public function userIsAdmin($userId): bool | |
| 139 | 194 | { |
| 140 | 195 | $user = $this->objectHandler->getUser($userId); |
| 141 | 196 | $roles = $this->getUserRole($user); |
| 142 | 197 | $rolesMap = array_flip($roles); |