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