PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/User/UserHandler.php +72 -9 2.3.152.2.21 View file →
@@ -20,8 +20,9 @@
20 20 use UserAccessManager\Config\MainConfig;
21 21 use UserAccessManager\Database\Database;
22 22 use UserAccessManager\Object\ObjectHandler;
23 23 use UserAccessManager\UserGroup\AbstractUserGroup;
24 +use UserAccessManager\UserGroup\UserGroup;
24 25 use UserAccessManager\Wrapper\Wordpress;
25 26 use WP_User;
26 27
27 28 /**
@@ -30,19 +31,55 @@
30 31 * @package UserAccessManager\UserHandler
31 32 */
32 33 class UserHandler
33 34 {
34 - public const MANAGE_USER_GROUPS_CAPABILITY = 'manage_user_groups';
35 + const MANAGE_USER_GROUPS_CAPABILITY = 'manage_user_groups';
35 36
37 + /**
38 + * @var Wordpress
39 + */
40 + private $wordpress;
41 +
42 + /**
43 + * @var MainConfig
44 + */
45 + private $config;
46 +
47 + /**
48 + * @var Database
49 + */
50 + private $database;
51 +
52 + /**
53 + * @var ObjectHandler
54 + */
55 + private $objectHandler;
56 +
57 + /**
58 + * The constructor
59 + * @param Wordpress $wordpress
60 + * @param MainConfig $config
61 + * @param Database $database
62 + * @param ObjectHandler $objectHandler
63 + */
36 64 public function __construct(
37 - private Wordpress $wordpress,
38 - private MainConfig $config,
39 - private Database $database,
40 - private ObjectHandler $objectHandler
65 + Wordpress $wordpress,
66 + MainConfig $config,
67 + Database $database,
68 + ObjectHandler $objectHandler
41 69 ) {
70 + $this->wordpress = $wordpress;
71 + $this->config = $config;
72 + $this->database = $database;
73 + $this->objectHandler = $objectHandler;
42 74 }
43 75
44 - private function calculateIp(string $ip): bool|string
76 + /**
77 + * Converts the ip to an integer.
78 + * @param string $ip
79 + * @return string|false
80 + */
81 + private function calculateIp(string $ip)
45 82 {
46 83 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
47 84 return base_convert((string) ip2long($ip), 10, 2);
48 85 } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
@@ -60,8 +97,13 @@
60 97
61 98 return $binaryIp;
62 99 }
63 100
101 + /**
102 + * Calculates the ip range.
103 + * @param string $ipRange
104 + * @return array
105 + */
64 106 private function getCalculatedRange(string $ipRange): array
65 107 {
66 108 $ipRange = explode('-', $ipRange);
67 109 $rangeBegin = $ipRange[0];
@@ -72,8 +114,14 @@
72 114 $this->calculateIp($rangeEnd)
73 115 ];
74 116 }
75 117
118 + /**
119 + * Checks if the given ip matches with the range.
120 + * @param string $currentIp The ip of the current user.
121 + * @param array $ipRanges The ip ranges.
122 + * @return bool
123 + */
76 124 public function isIpInRange(string $currentIp, array $ipRanges): bool
77 125 {
78 126 $currentIp = $this->calculateIp($currentIp);
79 127
@@ -91,9 +139,14 @@
91 139
92 140 return false;
93 141 }
94 142
95 - public function getUserRole(WP_User|bool $user): array
143 + /**
144 + * Return the role of the user.
145 + * @param WP_User|false $user The user.
146 + * @return array
147 + */
148 + public function getUserRole($user): array
96 149 {
97 150 if ($user instanceof WP_User && isset($user->{$this->database->getPrefix() . 'capabilities'}) === true) {
98 151 $capabilities = (array) $user->{$this->database->getPrefix() . 'capabilities'};
99 152 } else {
@@ -102,9 +155,14 @@
102 155
103 156 return (count($capabilities) > 0) ? array_keys($capabilities) : [AbstractUserGroup::NONE_ROLE];
104 157 }
105 158
106 - public function checkUserAccess(bool|string $allowedCapability = false): bool
159 + /**
160 + * Checks the user access by user level.
161 + * @param bool|string $allowedCapability If set check also for the capability.
162 + * @return bool
163 + */
164 + public function checkUserAccess($allowedCapability = false): bool
107 165 {
108 166 $currentUser = $this->wordpress->getCurrentUser();
109 167
110 168 if ($this->wordpress->isSuperAdmin($currentUser->ID) === true
@@ -134,9 +192,14 @@
134 192 || isset($rolesMap['administrator']) === true
135 193 );
136 194 }
137 195
138 - public function userIsAdmin(int|string|null $userId): bool
196 + /**
197 + * Checks if the user is an admin user
198 + * @param int|string $userId The user id.
199 + * @return bool
200 + */
201 + public function userIsAdmin($userId): bool
139 202 {
140 203 $user = $this->objectHandler->getUser($userId);
141 204 $roles = $this->getUserRole($user);
142 205 $rolesMap = array_flip($roles);