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