PluginProbe
User Access Manager / 2.2.20
User Access Manager v2.2.20
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.20, at src/User/UserHandler.php

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