PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.8.2
Advanced Access Manager – Access Governance for WordPress v5.8.2
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 / Feature / Subject / Role.php
advanced-access-manager / Application / Backend / Feature / Subject Last commit date
Role.php 7 years ago User.php 7 years ago
Role.php
278 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 * Role view manager
12 *
13 * @package AAM
14 * @author Vasyl Martyniuk <vasyl@vasyltech.com>
15 */
16 class AAM_Backend_Feature_Subject_Role {
17
18 /**
19 * Get role list
20 *
21 * Prepare and return the list of roles for the table view
22 *
23 * @return string JSON Encoded role list
24 *
25 * @access public
26 */
27 public function getTable() {
28 // TODO: The aam_list_roles is legacy and can be removed in Oct 2021
29 if (current_user_can('aam_manage_roles') || current_user_can('aam_list_roles')) {
30 //retrieve list of users
31 $count = count_users();
32 $stats = $count['avail_roles'];
33
34 $filtered = $this->fetchRoleList();
35
36 $response = array(
37 'recordsTotal' => count(get_editable_roles()),
38 'recordsFiltered' => count($filtered),
39 'draw' => AAM_Core_Request::request('draw'),
40 'data' => array(),
41 );
42
43 foreach ($filtered as $id => $data) {
44 $uc = (isset($stats[$id]) ? $stats[$id] : 0);
45
46 $response['data'][] = array(
47 $id,
48 $uc,
49 translate_user_role($data['name']),
50 apply_filters(
51 'aam-role-row-actions-filter',
52 implode(',', $this->prepareRowActions($uc, $id)),
53 $data
54 ),
55 AAM_Core_API::maxLevel($data['capabilities']),
56 AAM_Core_API::getOption("aam-role-{$id}-expiration", '')
57 );
58 }
59 } else {
60 $response = array(
61 'recordsTotal' => 0,
62 'recordsFiltered' => 0,
63 'draw' => AAM_Core_Request::request('draw'),
64 'data' => array(),
65 );
66 }
67
68 return wp_json_encode(apply_filters('aam-get-role-list-filter', $response));
69 }
70
71 /**
72 *
73 * @param type $count
74 * @return string
75 */
76 protected function prepareRowActions($count, $roleId) {
77 $ui = AAM_Core_Request::post('ui', 'main');
78 $id = AAM_Core_Request::post('id');
79
80 if ($ui === 'principal') {
81 $subject = new AAM_Core_Subject_Role($roleId);
82 $object = $subject->getObject('policy');
83
84 $actions = array(($object->has($id) ? 'detach' : 'attach'));
85 } else {
86 $actions = array('manage');
87
88 if (current_user_can('aam_edit_roles')) {
89 $actions[] = 'edit';
90 } else {
91 $actions[] = 'no-edit';
92 }
93 if (current_user_can('aam_create_roles')) {
94 $actions[] = 'clone';
95 } else {
96 $actions[] = 'no-clone';
97 }
98 if (current_user_can('aam_delete_roles') && !$count) {
99 $actions[] = 'delete';
100 } else {
101 $actions[] = 'no-delete';
102 }
103 }
104
105 return $actions;
106 }
107
108 /**
109 * Retrieve Pure Role List
110 *
111 * @return string
112 */
113 public function getList(){
114 return wp_json_encode(
115 apply_filters('aam-get-role-list-filter', $this->fetchRoleList())
116 );
117 }
118
119 /**
120 * Fetch role list
121 *
122 * @return array
123 *
124 * @access protected
125 */
126 protected function fetchRoleList() {
127 $response = array();
128
129 //filter by name
130 $search = trim(AAM_Core_Request::request('search.value'));
131 $exclude = trim(AAM_Core_Request::request('exclude'));
132 $roles = get_editable_roles();
133
134 foreach ($roles as $id => $role) {
135 $match = preg_match('/^' . $search . '/i', $role['name']);
136 if (($exclude !== $id) && (!$search || $match)) {
137 $response[$id] = $role;
138 }
139 }
140
141 return $response;
142 }
143
144 /**
145 * Add New Role
146 *
147 * @return string
148 *
149 * @access public
150 */
151 public function add() {
152 $response = array('status' => 'failure');
153
154 if (current_user_can('aam_create_roles')) {
155 $name = sanitize_text_field(filter_input(INPUT_POST, 'name'));
156 $expire = filter_input(INPUT_POST, 'expire');
157 $roles = AAM_Core_API::getRoles();
158 $role_id = sanitize_key(strtolower($name));
159
160 //if inherited role is set get capabilities from it
161 $parent = $roles->get_role(trim(filter_input(INPUT_POST, 'inherit')));
162 $caps = ($parent ? $parent->capabilities : array());
163
164 if ($role = $roles->add_role($role_id, $name, $caps)) {
165 $response = array(
166 'status' => 'success',
167 'role' => array(
168 'id' => $role_id,
169 'name' => $name,
170 'level' => AAM_Core_API::maxLevel($caps)
171 )
172 );
173 //clone settings if needed
174 if (AAM_Core_Request::post('clone')) {
175 $this->cloneSettings($role, $parent);
176 }
177
178 //save expiration rule if set
179 if ($expire) {
180 AAM_Core_API::updateOption("aam-role-{$role_id}-expiration", $expire);
181 } else {
182 AAM_Core_API::deleteOption("aam-role-{$role_id}-expiration");
183 }
184
185 do_action('aam-post-add-role-action', $role, $parent);
186 } else {
187 $response['reason'] = __("Role with slug [{$role_id}] already exists", AAM_KEY);
188 }
189 }
190
191 return wp_json_encode($response);
192 }
193
194 /**
195 *
196 * @global type $wpdb
197 * @param type $role
198 * @param type $parent
199 */
200 protected function cloneSettings($role, $parent) {
201 global $wpdb;
202
203 //clone _options settings
204 $oquery = "SELECT * FROM {$wpdb->options} WHERE `option_name` LIKE %s";
205 if ($wpdb->query($wpdb->prepare($oquery, 'aam_%_role_' . $parent->name))) {
206 foreach($wpdb->last_result as $setting) {
207 AAM_Core_API::updateOption(
208 str_replace($parent->name, $role->name, $setting->option_name),
209 maybe_unserialize($setting->option_value)
210 );
211 }
212 }
213
214 //clone _postmeta settings
215 $pquery = "SELECT * FROM {$wpdb->postmeta} WHERE `meta_key` LIKE %s";
216 if ($wpdb->query($wpdb->prepare($pquery, 'aam-%-role' . $parent->name))) {
217 foreach($wpdb->last_result as $setting) {
218 add_post_meta(
219 $setting->post_id,
220 str_replace($parent->name, $role->name, $setting->meta_key),
221 maybe_unserialize($setting->meta_value)
222 );
223 }
224 }
225 }
226
227 /**
228 * Edit role name
229 *
230 * @return string
231 *
232 * @access public
233 */
234 public function edit() {
235 if (current_user_can('aam_edit_roles')) {
236 $role = AAM_Backend_Subject::getInstance();
237 $role->update(trim(filter_input(INPUT_POST, 'name')));
238
239 $expire = filter_input(INPUT_POST, 'expire');
240 //save expiration rule if set
241 if ($expire) {
242 AAM_Core_API::updateOption(
243 'aam-role-' . $role->getId() .'-expiration', $expire
244 );
245 } else {
246 AAM_Core_API::deleteOption('aam-role-' . $role->getId() .'-expiration');
247 }
248
249 do_action('aam-post-update-role-action', $role->get());
250
251 $response = array('status' => 'success');
252 } else {
253 $response = array('status' => 'failure');
254 }
255
256 return wp_json_encode($response);
257 }
258
259 /**
260 * Delete role
261 *
262 * @return string
263 *
264 * @access public
265 */
266 public function delete() {
267 $status = 'failure';
268
269 if (current_user_can('aam_delete_roles')) {
270 if (AAM_Backend_Subject::getInstance()->delete()) {
271 $status = 'success';
272 }
273 }
274
275 return wp_json_encode(array('status' => $status));
276 }
277
278 }