PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.3.5
Advanced Access Manager – Access Governance for WordPress v5.3.5
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / Application / Core / Subject / User.php
advanced-access-manager / Application / Core / Subject Last commit date
Default.php 8 years ago Role.php 8 years ago User.php 8 years ago Visitor.php 8 years ago
User.php
372 lines
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * User subject
12 *
13 * @package AAM
14 * @author Vasyl Martyniuk <vasyl@vasyltech.com>
15 */
16 class AAM_Core_Subject_User extends AAM_Core_Subject {
17
18 /**
19 * Subject UID: USER
20 */
21 const UID = 'user';
22
23 /**
24 * AAM Capability Key
25 *
26 * It is very important to have all user capability changes be stored in
27 * separate options from the wp_capabilities usermeta cause if AAM is not
28 * active as a plugin, it reverts back to the default WordPress settings
29 */
30 const AAM_CAPKEY = 'aam_capability';
31
32 /**
33 *
34 * @var type
35 */
36 protected $parent = null;
37
38 /**
39 *
40 * @param type $id
41 */
42 public function __construct($id) {
43 parent::__construct($id);
44
45 if (get_current_user_id() == $id) {
46 //check if user is expired
47 $expired = get_user_option('aam_user_expiration');
48 if (!empty($expired)) {
49 $parts = explode('|', $expired);
50 if ($parts[0] <= date('Y-m-d H:i:s')) {
51 $this->triggerExpiredUserAction($parts);
52 }
53 }
54
55 //check if user's role expired
56 $roleExpire = get_user_option('aam-role-expires');
57 if ($roleExpire && ($roleExpire <= time())) {
58 $this->restoreRoles();
59 }
60 }
61 }
62
63 /**
64 * Expire user
65 *
66 * @param array $config
67 *
68 * @return void
69 *
70 * @access
71 */
72 public function triggerExpiredUserAction($config) {
73 switch($config[1]) {
74 case 'lock':
75 $this->block();
76 break;
77
78 case 'change-role':
79 if (AAM_Core_API::getRoles()->is_role($config[2])) {
80 $this->getSubject()->set_role($config[2]);
81 delete_user_option($this->getSubject()->ID, 'aam_user_expiration');
82 }
83 break;
84
85 case 'delete':
86 require_once(ABSPATH . 'wp-admin/includes/user.php' );
87 wp_delete_user(
88 $this->getId(), AAM_Core_Config::get('core.reasign.ownership.user')
89 );
90 wp_logout();
91 break;
92
93 default:
94 break;
95 }
96 }
97
98 /**
99 * Block User
100 *
101 * @return boolean
102 *
103 * @access public
104 * @global wpdb $wpdb
105 */
106 public function block() {
107 global $wpdb;
108
109 $status = ($this->getSubject()->user_status ? 0 : 1);
110 $result = $wpdb->update(
111 $wpdb->users,
112 array('user_status' => $status),
113 array('ID' => $this->getId())
114 );
115
116 if ($result) {
117 $this->getSubject()->user_status = $status;
118 clean_user_cache($this->getSubject());
119 }
120
121 return $result;
122 }
123
124 /**
125 *
126 */
127 public function restoreRoles() {
128 $roles = get_user_option('aam-original-roles');
129
130 //remove curren roles
131 foreach((array) $this->roles as $role) {
132 $this->remove_role($role);
133 }
134
135 //add original roles
136 foreach(($roles ? $roles : array('subscriber')) as $role) {
137 $this->add_role($role);
138 }
139
140 //delete options
141 delete_user_option($this->getId(), 'aam-role-expires');
142 delete_user_option($this->getId(), 'aam-original-roles');
143 }
144
145 /**
146 * Retrieve User based on ID
147 *
148 * @return WP_Role
149 *
150 * @access protected
151 */
152 protected function retrieveSubject() {
153 $subject = new WP_User($this->getId());
154
155 //retrieve aam capabilities if are not retrieved yet
156 $caps = get_user_option(self::AAM_CAPKEY, $this->getId());
157 if (is_array($caps)) {
158 $caps = array_merge($subject->caps, $caps);
159 $allcaps = array_merge($subject->allcaps, $caps);
160
161 //reset the user capabilities
162 $subject->allcaps = $allcaps;
163 $subject->caps = $caps;
164
165 if (wp_get_current_user()->ID == $subject->ID) {
166 wp_get_current_user()->allcaps = $allcaps;
167 wp_get_current_user()->caps = $caps;
168 }
169 }
170
171 return $subject;
172 }
173
174 /**
175 * Get user capabilities
176 *
177 * @return array
178 *
179 * @access public
180 */
181 public function getCapabilities() {
182 return $this->getSubject()->allcaps;
183 }
184
185 /**
186 * Check if user has a capability
187 *
188 * @param string $capability
189 *
190 * @return boolean
191 *
192 * @access public
193 */
194 public function hasCapability($capability) {
195 return user_can($this->getSubject(), $capability);
196 }
197
198 /**
199 * Add capability
200 *
201 * @param string $capability
202 *
203 * @return boolean
204 *
205 * @access public
206 */
207 public function addCapability($capability) {
208 return $this->updateCapability($capability, true);
209 }
210
211 /**
212 * Remove Capability
213 *
214 * @param string $capability
215 *
216 * @return boolean
217 *
218 * @access public
219 */
220 public function removeCapability($capability) {
221 return $this->updateCapability($capability, false);
222 }
223
224 /**
225 * Update User's Capability Set
226 *
227 * @param string $capability
228 * @param boolean $grand
229 *
230 * @return boolean
231 *
232 * @access public
233 */
234 public function updateCapability($capability, $grand) {
235 //update capability
236 $caps = $this->getSubject()->caps;
237 $caps[$capability] = $grand;
238
239 //save and return the result of operation
240 return update_user_option($this->getId(), self::AAM_CAPKEY, $caps);
241 }
242
243 /**
244 * Undocumented function
245 *
246 * @param string $object
247 * @return void
248 */
249 public function resetObject($object) {
250 if ($object == 'capability') {
251 $result = delete_user_option($this->getId(), self::AAM_CAPKEY);
252 } else {
253 $result = $this->deleteOption($object);
254 }
255
256 return $result;
257 }
258
259 /**
260 * Update user's option
261 *
262 * @param mixed $value
263 * @param string $object
264 * @param string $id
265 *
266 * @return boolean
267 *
268 * @access public
269 */
270 public function updateOption($value, $object, $id = 0) {
271 return update_user_option(
272 $this->getId(), $this->getOptionName($object, $id), $value
273 );
274 }
275
276 /**
277 * Read user's option
278 *
279 * @param string $object
280 * @param string $id
281 *
282 * @return mixed
283 *
284 * @access public
285 */
286 public function readOption($object, $id = '') {
287 return get_user_option(
288 $this->getOptionName($object, $id), $this->getId()
289 );
290 }
291
292 /**
293 * Read user's option
294 *
295 * @param string $object
296 * @param string $id
297 *
298 * @return mixed
299 *
300 * @access public
301 */
302 public function deleteOption($object, $id = 0) {
303 return delete_user_option(
304 $this->getId(), $this->getOptionName($object, $id)
305 );
306 }
307
308 /**
309 * @inheritdoc
310 */
311 public function getParent() {
312 if (is_null($this->parent)) {
313 //try to get this option from the User's Role
314 $roles = $this->getSubject()->roles;
315 //first user role is counted only. AAM does not support multi-roles
316 $parent = array_shift($roles);
317
318 //in case of multisite & current user does not belong to the site
319 if ($parent) {
320 $this->parent = new AAM_Core_Subject_Role($parent);
321 } else {
322 $this->parent = null;
323 }
324 }
325
326 return $this->parent;
327 }
328
329 /**
330 * Prepare option's name
331 *
332 * @param string $object
333 * @param string|int $id
334 *
335 * @return string
336 *
337 * @access public
338 */
339 public function getOptionName($object, $id) {
340 return "aam_{$object}" . ($id ? "_{$id}" : '');
341 }
342
343 /**
344 * Get Subject UID
345 *
346 * @return string
347 *
348 * @access public
349 */
350 public function getUID() {
351 return self::UID;
352 }
353
354 /**
355 *
356 * @return type
357 */
358 public function getName() {
359 $display = $this->display_name;
360
361 return ($display ? $display : $this->user_nicename);
362 }
363
364 /**
365 *
366 * @return type
367 */
368 public function getMaxLevel() {
369 return AAM_Core_API::maxLevel($this->allcaps);
370 }
371
372 }