PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / trunk
Advanced Access Manager – Access Governance for WordPress vtrunk
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 / Backend / AccessLevel.php
advanced-access-manager / application / Backend Last commit date
Feature 1 year ago View 1 year ago Widget 1 year ago tmpl 3 months ago AccessLevel.php 1 year ago Feature.php 1 year ago Manager.php 3 months ago View.php 1 year ago
AccessLevel.php
298 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 * Backend access level instance
12 *
13 * Currently managed access level. Based on the HTTP request data, define which
14 * access level is currently managed with AAM UI.
15 *
16 * @package AAM
17 * @version 7.0.0
18 */
19 class AAM_Backend_AccessLevel
20 {
21
22 /**
23 * Single instance of itself
24 *
25 * @var AAM_Backend_AccessLevel
26 * @access private
27 *
28 * @version 7.0.0
29 */
30 private static $_instance = null;
31
32 /**
33 * Access Level
34 *
35 * @var AAM_Framework_AccessLevel_Interface
36 * @access private
37 *
38 * @version 7.0.0
39 */
40 private $_access_level = null;
41
42 /**
43 * Constructor
44 *
45 * @return void
46 * @access protected
47 *
48 * @version 7.0.0
49 */
50 protected function __construct()
51 {
52 $access_level_type = strtolower(AAM::api()->misc->get(
53 $_POST, 'access_level', ''
54 ));
55
56 if ($access_level_type === AAM_Framework_Type_AccessLevel::ROLE) {
57 $access_level_id = AAM::api()->misc->get($_POST, 'role_id');
58 } elseif ($access_level_type === AAM_Framework_Type_AccessLevel::USER) {
59 $access_level_id = AAM::api()->misc->get($_POST, 'user_id');
60 } else {
61 $access_level_id = null;
62 }
63
64 if (empty($access_level_type)) {
65 $al = $this->_get_last_managed_access_level();
66
67 if (!empty($al)) {
68 $access_level_type = $al['type'];
69 $access_level_id = !empty($al['id']) ? $al['id']: null;
70 }
71 } else { // Persist the last managed access level
72 AAM::api()->cache->set(
73 'managed_access_level_by_' . get_current_user_id(),
74 [
75 'type' => $access_level_type,
76 'id' => $access_level_id
77 ],
78 2592000 // 30 days
79 );
80 }
81
82 if ($access_level_type) {
83 $this->_init_access_level($access_level_type, $access_level_id);
84 } else {
85 $this->_init_fallback_access_level();
86 }
87 }
88
89 /**
90 * Check if current access level is role
91 *
92 * @return boolean
93 * @access public
94 *
95 * @version 7.0.0
96 */
97 public function is_role()
98 {
99 return $this->_access_level->type === AAM_Framework_Type_AccessLevel::ROLE;
100 }
101
102 /**
103 * Check if current access level is user
104 *
105 * @return boolean
106 * @access public
107 *
108 * @version 7.0.0
109 */
110 public function is_user()
111 {
112 return $this->_access_level->type === AAM_Framework_Type_AccessLevel::USER;
113 }
114
115 /**
116 * Check if current access level is visitor
117 *
118 * @return boolean
119 * @access public
120 *
121 * @version 7.0.0
122 */
123 public function is_visitor()
124 {
125 return $this->_access_level->type === AAM_Framework_Type_AccessLevel::VISITOR;
126 }
127
128 /**
129 * Check if current access level is default
130 *
131 * @return boolean
132 * @access public
133 *
134 * @version 7.0.0
135 */
136 public function is_default()
137 {
138 return $this->_access_level->type === AAM_Framework_Type_AccessLevel::ALL;
139 }
140
141 /**
142 * Get last managed access level
143 *
144 * @return array|null
145 * @access private
146 *
147 * @version 7.0.0
148 */
149 private function _get_last_managed_access_level()
150 {
151 $level = AAM::api()->cache->get(
152 'managed_access_level_by_' . get_current_user_id()
153 );
154
155 if (!is_null($level)) {
156 // Verifying that access level exists and is accessible
157 if ($level['type'] === 'role') {
158 if (!AAM::api()->roles->is_editable_role($level['id'])) {
159 $level = null;
160 }
161 } elseif ($level['type'] === 'user') {
162 $user = apply_filters('aam_get_user', get_user_by('id', $level['id']));
163
164 if ($user === false
165 || is_wp_error($user)
166 || !current_user_can('edit_user', $user->ID)
167 ) {
168 $level = null;
169 }
170 }
171 }
172
173 return $level;
174 }
175
176 /**
177 * Initialize requested access level
178 *
179 * @param string $type
180 * @param int|string $id
181 *
182 * @return void
183 * @access private
184 *
185 * @version 7.0.0
186 */
187 private function _init_access_level($type, $id = null)
188 {
189 $this->_access_level = AAM::api()->access_levels->get($type, $id);
190 }
191
192 /**
193 * Initialize fallback access level
194 *
195 * Based on user permissions, pick the first available access level that current
196 * user can manage
197 *
198 * @return void
199 * @access private
200 *
201 * @version 7.0.0
202 */
203 private function _init_fallback_access_level()
204 {
205 if (current_user_can('aam_manage_roles')) {
206 $roles = array_keys(get_editable_roles());
207 $this->_init_access_level(
208 AAM_Framework_Type_AccessLevel::ROLE, array_pop($roles)
209 );
210 } elseif (current_user_can('aam_manage_users')) {
211 $this->_init_access_level(
212 AAM_Framework_Type_AccessLevel::USER, get_current_user_id()
213 );
214 } elseif (current_user_can('aam_manage_visitors')) {
215 $this->_init_access_level(AAM_Framework_Type_AccessLevel::VISITOR);
216 } elseif (current_user_can('aam_manage_default')) {
217 $this->_init_access_level(AAM_Framework_Type_AccessLevel::ALL);
218 } else {
219 AAM::api()->redirect->do_redirect([
220 'type' => 'custom_message',
221 'message' => __('You do not have permission to manage any access levels', 'advanced-access-manager')
222 ]);
223 }
224 }
225
226 /**
227 * Get access level property
228 *
229 * @return mixed
230 * @access public
231 *
232 * @version 7.0.0
233 */
234 public function __get($name)
235 {
236 return $this->_access_level->$name;
237 }
238
239 /**
240 * Call access level method
241 *
242 * @param string $name
243 * @param array $args
244 *
245 * @return mixed
246 * @access public
247 *
248 * @version 7.0.0
249 */
250 public function __call($name, $args)
251 {
252 return call_user_func_array(array($this->_access_level, $name), $args);
253 }
254
255 /**
256 * Get AAM core subject
257 *
258 * @return AAM_Framework_AccessLevel_Interface
259 * @access public
260 *
261 * @version 7.0.0
262 */
263 public function get_access_level()
264 {
265 return $this->_access_level;
266 }
267
268 /**
269 * Bootstrap the object
270 *
271 * @return AAM_Backend_AccessLevel
272 * @access public
273 *
274 * @version 7.0.0
275 */
276 public static function bootstrap()
277 {
278 if (is_null(self::$_instance)) {
279 self::$_instance = new self;
280 }
281
282 return self::$_instance;
283 }
284
285 /**
286 * Get single instance of itself
287 *
288 * @return AAM_Backend_AccessLevel
289 * @access public
290 *
291 * @version 7.0.0
292 */
293 public static function get_instance()
294 {
295 return self::bootstrap();
296 }
297
298 }