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
user-access-manager / src / User / UserHandler.php

UserHandler.php in User Access Manager 2.2.21, at src/User/UserHandler.php

210 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UserHandler.php
4 *
5 * The UserHandler 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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\User;
19
20 use UserAccessManager\Config\MainConfig;
21 use UserAccessManager\Database\Database;
22 use UserAccessManager\Object\ObjectHandler;
23 use UserAccessManager\UserGroup\AbstractUserGroup;
24 use UserAccessManager\UserGroup\UserGroup;
25 use UserAccessManager\Wrapper\Wordpress;
26 use WP_User;
27
28 /**
29 * Class UserHandler
30 *
31 * @package UserAccessManager\UserHandler
32 */
33 class UserHandler
34 {
35 const MANAGE_USER_GROUPS_CAPABILITY = 'manage_user_groups';
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 */
64 public function __construct(
65 Wordpress $wordpress,
66 MainConfig $config,
67 Database $database,
68 ObjectHandler $objectHandler
69 ) {
70 $this->wordpress = $wordpress;
71 $this->config = $config;
72 $this->database = $database;
73 $this->objectHandler = $objectHandler;
74 }
75
76 /**
77 * Converts the ip to an integer.
78 * @param string $ip
79 * @return string|false
80 */
81 private function calculateIp(string $ip)
82 {
83 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
84 return base_convert((string) ip2long($ip), 10, 2);
85 } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
86 return false;
87 }
88
89 $packedIp = inet_pton($ip);
90 $bits = 15; // 16 x 8 bit = 128bit (ipv6)
91 $binaryIp = '';
92
93 while ($bits >= 0) {
94 $binaryIp = sprintf('%08b', (ord($packedIp[$bits]))) . $binaryIp;
95 $bits--;
96 }
97
98 return $binaryIp;
99 }
100
101 /**
102 * Calculates the ip range.
103 * @param string $ipRange
104 * @return array
105 */
106 private function getCalculatedRange(string $ipRange): array
107 {
108 $ipRange = explode('-', $ipRange);
109 $rangeBegin = $ipRange[0];
110 $rangeEnd = $ipRange[1] ?? $ipRange[0];
111
112 return [
113 $this->calculateIp($rangeBegin),
114 $this->calculateIp($rangeEnd)
115 ];
116 }
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 */
124 public function isIpInRange(string $currentIp, array $ipRanges): bool
125 {
126 $currentIp = $this->calculateIp($currentIp);
127
128 if ($currentIp !== false) {
129 foreach ($ipRanges as $ipRange) {
130 $calculatedIpRange = $this->getCalculatedRange($ipRange);
131
132 if ($calculatedIpRange[0] !== false && $calculatedIpRange[1] !== false
133 && $calculatedIpRange[0] <= $currentIp && $currentIp <= $calculatedIpRange[1]
134 ) {
135 return true;
136 }
137 }
138 }
139
140 return false;
141 }
142
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
149 {
150 if ($user instanceof WP_User && isset($user->{$this->database->getPrefix() . 'capabilities'}) === true) {
151 $capabilities = (array) $user->{$this->database->getPrefix() . 'capabilities'};
152 } else {
153 $capabilities = [];
154 }
155
156 return (count($capabilities) > 0) ? array_keys($capabilities) : [AbstractUserGroup::NONE_ROLE];
157 }
158
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
165 {
166 $currentUser = $this->wordpress->getCurrentUser();
167
168 if ($this->wordpress->isSuperAdmin($currentUser->ID) === true
169 || $allowedCapability !== false && $currentUser->has_cap($allowedCapability) === true
170 ) {
171 return true;
172 }
173
174 $roles = $this->getUserRole($currentUser);
175 $rolesMap = array_flip($roles);
176
177 $orderedRoles = [
178 AbstractUserGroup::NONE_ROLE,
179 'subscriber',
180 'contributor',
181 'author',
182 'editor',
183 'administrator'
184 ];
185 $orderedRolesMap = array_flip($orderedRoles);
186
187 $userRoles = array_intersect_key($orderedRolesMap, $rolesMap);
188 $rightsLevel = (count($userRoles) > 0) ? end($userRoles) : -1;
189 $fullAccessRole = $this->config->getFullAccessRole();
190
191 return (isset($orderedRolesMap[$fullAccessRole]) === true && $rightsLevel >= $orderedRolesMap[$fullAccessRole]
192 || isset($rolesMap['administrator']) === true
193 );
194 }
195
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
202 {
203 $user = $this->objectHandler->getUser($userId);
204 $roles = $this->getUserRole($user);
205 $rolesMap = array_flip($roles);
206
207 return (isset($rolesMap['administrator']) === true || $this->wordpress->isSuperAdmin($userId) === true);
208 }
209 }
210