PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.0
Advanced Access Manager – Access Governance for WordPress v5.0
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
336 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 locked
47 if ($this->user_status == 1) {
48 wp_logout();
49 }
50
51 //check if user's role expired
52 $expire = get_user_option('aam-role-expires');
53 if ($expire && ($expire <= time())) {
54 $this->restoreRoles();
55 }
56 }
57 }
58
59 /**
60 * Block User
61 *
62 * @return boolean
63 *
64 * @access public
65 * @global wpdb $wpdb
66 */
67 public function block() {
68 global $wpdb;
69
70 $response = false;
71 if (current_user_can('edit_users')) {
72 $status = ($this->getSubject()->user_status ? 0 : 1);
73 $result = $wpdb->update(
74 $wpdb->users,
75 array('user_status' => $status),
76 array('ID' => $this->getId())
77 );
78 if ($result) {
79 $this->getSubject()->user_status = $status;
80 clean_user_cache($this->getSubject());
81 $response = true;
82 }
83 }
84
85 return $response;
86 }
87
88 /**
89 *
90 */
91 public function restoreRoles() {
92 $roles = get_user_option('aam-original-roles');
93
94 //remove curren roles
95 foreach((array) $this->roles as $role) {
96 $this->remove_role($role);
97 }
98
99 //add original roles
100 foreach(($roles ? $roles : array('subscriber')) as $role) {
101 $this->add_role($role);
102 }
103
104 //delete options
105 delete_user_option($this->getId(), 'aam-role-expires');
106 delete_user_option($this->getId(), 'aam-original-roles');
107 }
108
109 /**
110 * Retrieve User based on ID
111 *
112 * @return WP_Role
113 *
114 * @access protected
115 */
116 protected function retrieveSubject() {
117 $subject = new WP_User($this->getId());
118
119 //retrieve aam capabilities if are not retrieved yet
120 $caps = get_user_option(self::AAM_CAPKEY, $this->getId());
121 if (is_array($caps)) {
122 $caps = array_merge($subject->caps, $caps);
123 $allcaps = array_merge($subject->allcaps, $caps);
124
125 //reset the user capabilities
126 $subject->allcaps = $allcaps;
127 $subject->caps = $caps;
128
129 if (wp_get_current_user()->ID == $subject->ID) {
130 wp_get_current_user()->allcaps = $allcaps;
131 wp_get_current_user()->caps = $caps;
132 }
133 }
134
135 return $subject;
136 }
137
138 /**
139 * Get user capabilities
140 *
141 * @return array
142 *
143 * @access public
144 */
145 public function getCapabilities() {
146 return $this->getSubject()->allcaps;
147 }
148
149 /**
150 * Check if user has a capability
151 *
152 * @param string $capability
153 *
154 * @return boolean
155 *
156 * @access public
157 */
158 public function hasCapability($capability) {
159 return user_can($this->getSubject(), $capability);
160 }
161
162 /**
163 * Add capability
164 *
165 * @param string $capability
166 *
167 * @return boolean
168 *
169 * @access public
170 */
171 public function addCapability($capability) {
172 return $this->updateCapability($capability, true);
173 }
174
175 /**
176 * Remove Capability
177 *
178 * @param string $capability
179 *
180 * @return boolean
181 *
182 * @access public
183 */
184 public function removeCapability($capability) {
185 return $this->updateCapability($capability, false);
186 }
187
188 /**
189 * Update User's Capability Set
190 *
191 * @param string $capability
192 * @param boolean $grand
193 *
194 * @return boolean
195 *
196 * @access public
197 */
198 public function updateCapability($capability, $grand) {
199 //update capability
200 $caps = $this->getSubject()->caps;
201 $caps[$capability] = $grand;
202
203 //save and return the result of operation
204 return update_user_option($this->getId(), self::AAM_CAPKEY, $caps);
205 }
206
207 /**
208 * Undocumented function
209 *
210 * @param string $object
211 * @return void
212 */
213 public function resetObject($object) {
214 if ($object == 'capability') {
215 $result = delete_user_option($this->getId(), self::AAM_CAPKEY);
216 } else {
217 $result = $this->deleteOption($object);
218 }
219
220 return $result;
221 }
222
223 /**
224 * Update user's option
225 *
226 * @param mixed $value
227 * @param string $object
228 * @param string $id
229 *
230 * @return boolean
231 *
232 * @access public
233 */
234 public function updateOption($value, $object, $id = 0) {
235 return update_user_option(
236 $this->getId(), $this->getOptionName($object, $id), $value
237 );
238 }
239
240 /**
241 * Read user's option
242 *
243 * @param string $object
244 * @param string $id
245 *
246 * @return mixed
247 *
248 * @access public
249 */
250 public function readOption($object, $id = '') {
251 return get_user_option(
252 $this->getOptionName($object, $id), $this->getId()
253 );
254 }
255
256 /**
257 * Read user's option
258 *
259 * @param string $object
260 * @param string $id
261 *
262 * @return mixed
263 *
264 * @access public
265 */
266 public function deleteOption($object, $id = 0) {
267 return delete_user_option(
268 $this->getId(), $this->getOptionName($object, $id)
269 );
270 }
271
272 /**
273 * @inheritdoc
274 */
275 public function getParent() {
276 if (is_null($this->parent)) {
277 //try to get this option from the User's Role
278 $roles = $this->getSubject()->roles;
279 //first user role is counted only. AAM does not support multi-roles
280 $parent = array_shift($roles);
281
282 //in case of multisite & current user does not belong to the site
283 if ($parent) {
284 $this->parent = new AAM_Core_Subject_Role($parent);
285 } else {
286 $this->parent = null;
287 }
288 }
289
290 return $this->parent;
291 }
292
293 /**
294 * Prepare option's name
295 *
296 * @param string $object
297 * @param string|int $id
298 *
299 * @return string
300 *
301 * @access public
302 */
303 public function getOptionName($object, $id) {
304 return "aam_{$object}" . ($id ? "_{$id}" : '');
305 }
306
307 /**
308 * Get Subject UID
309 *
310 * @return string
311 *
312 * @access public
313 */
314 public function getUID() {
315 return self::UID;
316 }
317
318 /**
319 *
320 * @return type
321 */
322 public function getName() {
323 $display = $this->display_name;
324
325 return ($display ? $display : $this->user_nicename);
326 }
327
328 /**
329 *
330 * @return type
331 */
332 public function getMaxLevel() {
333 return AAM_Core_API::maxLevel($this->allcaps);
334 }
335
336 }