Capability.php
| 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 capability manager |
| 12 | * |
| 13 | * @since 6.8.5 https://github.com/aamplugin/advanced-access-manager/issues/218 |
| 14 | * @since 6.8.0 https://github.com/aamplugin/advanced-access-manager/issues/195 |
| 15 | * @since 6.0.0 Initial implementation of the class |
| 16 | * |
| 17 | * @package AAM |
| 18 | * @version 6.8.5 |
| 19 | */ |
| 20 | class AAM_Backend_Feature_Main_Capability |
| 21 | extends AAM_Backend_Feature_Abstract implements AAM_Backend_Feature_ISubjectAware |
| 22 | { |
| 23 | |
| 24 | /** |
| 25 | * Default access capability to the service |
| 26 | * |
| 27 | * @version 6.0.0 |
| 28 | */ |
| 29 | const ACCESS_CAPABILITY = 'aam_manage_capabilities'; |
| 30 | |
| 31 | /** |
| 32 | * HTML template to render |
| 33 | * |
| 34 | * @version 6.0.0 |
| 35 | */ |
| 36 | const TEMPLATE = 'service/capability.php'; |
| 37 | |
| 38 | /** |
| 39 | * Capability groups |
| 40 | * |
| 41 | * @var array |
| 42 | * |
| 43 | * @access public |
| 44 | * @version 6.0.0 |
| 45 | */ |
| 46 | public static $groups = array( |
| 47 | 'system' => array( |
| 48 | 'level_0', 'level_1', 'level_2', 'level_3', 'level_4', 'level_5', |
| 49 | 'level_6', 'level_7', 'level_8', 'level_9', 'level_10' |
| 50 | ), |
| 51 | 'post' => array( |
| 52 | 'delete_others_pages', 'delete_others_posts', 'edit_others_pages', |
| 53 | 'delete_posts', 'delete_private_pages', 'delete_private_posts', |
| 54 | 'delete_published_pages', 'delete_published_posts', 'delete_pages', |
| 55 | 'edit_others_posts', 'edit_pages', 'edit_private_posts', |
| 56 | 'edit_private_pages', 'edit_posts', 'edit_published_pages', |
| 57 | 'edit_published_posts', 'publish_pages', 'publish_posts', 'read', |
| 58 | 'read_private_pages', 'read_private_posts', 'edit_permalink' |
| 59 | ), |
| 60 | 'backend' => array( |
| 61 | 'activate_plugins', 'add_users', 'update_plugins', |
| 62 | 'delete_users', 'delete_themes', 'edit_dashboard', 'edit_files', |
| 63 | 'edit_plugins', 'edit_theme_options', 'edit_themes', 'edit_users', |
| 64 | 'export', 'import', 'install_plugins', 'install_themes', |
| 65 | 'manage_options', 'manage_links', 'manage_categories', 'customize', |
| 66 | 'unfiltered_html', 'unfiltered_upload', 'update_themes', |
| 67 | 'update_core', 'upload_files', 'delete_plugins', 'remove_users', |
| 68 | 'switch_themes', 'list_users', 'promote_users', 'create_users', |
| 69 | 'delete_site' |
| 70 | ) |
| 71 | ); |
| 72 | |
| 73 | /** |
| 74 | * Save capability status |
| 75 | * |
| 76 | * @return string |
| 77 | * |
| 78 | * @since 6.8.0 https://github.com/aamplugin/advanced-access-manager/issues/195 |
| 79 | * @since 6.0.0 Initial implementation of the method |
| 80 | * |
| 81 | * @access public |
| 82 | * @version 6.8.0 |
| 83 | */ |
| 84 | public function save() |
| 85 | { |
| 86 | $result = false; |
| 87 | $cap = sanitize_text_field($this->getFromPost('capability')); |
| 88 | $effect = $this->getFromPost('effect', FILTER_VALIDATE_BOOLEAN); |
| 89 | $assign = $this->getFromPost('assignToMe', FILTER_VALIDATE_BOOLEAN); |
| 90 | |
| 91 | if ($cap && $this->isAllowedToToggle($cap)) { |
| 92 | $result = $this->getSubject()->addCapability($cap, $effect); |
| 93 | |
| 94 | // Add capability to current user if checkbox checked |
| 95 | if ($result && $assign === true) { |
| 96 | AAM::getUser()->addCapability($cap); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return wp_json_encode(array( |
| 101 | 'status' => ($result ? 'success' : 'failure') |
| 102 | )); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Update capability slug |
| 107 | * |
| 108 | * @return string |
| 109 | * |
| 110 | * @access public |
| 111 | * @version 6.0.0 |
| 112 | */ |
| 113 | public function update() |
| 114 | { |
| 115 | $capability = $this->getFromPost('capability'); |
| 116 | $updated = sanitize_text_field($this->getFromPost('updated')); |
| 117 | $subject = $this->getSubject(); |
| 118 | |
| 119 | if ($this->isAllowedToEdit($capability) === false) { |
| 120 | $response = array( |
| 121 | 'status' => 'failure', |
| 122 | 'message' => __('Permission denied to update this capability', AAM_KEY) |
| 123 | ); |
| 124 | } else { |
| 125 | // First we need to get the current grant status for updating capability |
| 126 | $status = $subject->hasCapability($capability); |
| 127 | // Remove updating capability |
| 128 | if ($subject->removeCapability($capability)) { |
| 129 | // Add new capability with the original grant status |
| 130 | $result = $subject->addCapability($updated, $status); |
| 131 | } |
| 132 | |
| 133 | $response = array('status' => (!empty($result) ? 'success' : 'failure')); |
| 134 | } |
| 135 | |
| 136 | return wp_json_encode($response); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Delete capability |
| 141 | * |
| 142 | * This function delete capability in all roles or only for very specific subject. |
| 143 | * It all depends on the "subjectOnly" POST param. |
| 144 | * |
| 145 | * @return string |
| 146 | * |
| 147 | * @access public |
| 148 | * @version 6.0.0 |
| 149 | */ |
| 150 | public function delete() |
| 151 | { |
| 152 | $capability = $this->getFromPost('capability'); |
| 153 | $subjectOnly = $this->getFromPost('subjectOnly', FILTER_VALIDATE_BOOLEAN); |
| 154 | |
| 155 | if ($this->isAllowedToDelete($capability) === false) { |
| 156 | $response = array( |
| 157 | 'status' => 'failure', |
| 158 | 'message' => __('Permission denied to delete this capability', AAM_KEY) |
| 159 | ); |
| 160 | } else { |
| 161 | if ($subjectOnly === true) { |
| 162 | $this->getSubject()->removeCapability($capability); |
| 163 | } else { |
| 164 | $roles = AAM_Core_API::getRoles(); |
| 165 | foreach (array_keys($roles->roles) as $roleId) { |
| 166 | $roles->remove_cap($roleId, $capability); |
| 167 | } |
| 168 | } |
| 169 | $response = array('status' => 'success'); |
| 170 | } |
| 171 | |
| 172 | return wp_json_encode($response); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get list of capabilities for table view |
| 177 | * |
| 178 | * @return string |
| 179 | * |
| 180 | * @since 6.8.5 https://github.com/aamplugin/advanced-access-manager/issues/218 |
| 181 | * @since 6.0.0 Initial implementation of the method |
| 182 | * |
| 183 | * @access public |
| 184 | * @version 6.8.5 |
| 185 | */ |
| 186 | public function getTable() |
| 187 | { |
| 188 | $data = array(); |
| 189 | |
| 190 | // Compile the complete list of capabilities |
| 191 | $caps = AAM_Core_API::getAllCapabilities(); |
| 192 | |
| 193 | // Add also subject specific capabilities |
| 194 | $caps = array_merge($caps, $this->getSubject()->getCapabilities()); |
| 195 | |
| 196 | foreach (array_keys($caps) as $cap) { |
| 197 | if (apply_filters('aam_cap_can_filter', true, $cap, 'list') !== false) { |
| 198 | $data[] = array( |
| 199 | $cap, |
| 200 | $this->getGroup($cap), |
| 201 | $cap, |
| 202 | $this->prepareActionList($cap), |
| 203 | $this->getSubject()->hasCapability($cap) |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return wp_json_encode(array('data' => $data)); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Prepare row actions |
| 213 | * |
| 214 | * Based on current user permissions and subject's capability ownership, prepare |
| 215 | * the correct list of actions |
| 216 | * |
| 217 | * @param string $cap |
| 218 | * |
| 219 | * @return string |
| 220 | * |
| 221 | * @access protected |
| 222 | * @version 6.0.0 |
| 223 | */ |
| 224 | protected function prepareActionList($cap) |
| 225 | { |
| 226 | $actions = array(); |
| 227 | $subject = $this->getSubject(); |
| 228 | |
| 229 | $toggle = ($subject->hasCapability($cap) ? 'checked' : 'unchecked'); |
| 230 | |
| 231 | if ($this->isAllowedToToggle($cap) === false) { |
| 232 | $toggle = 'no-' . $toggle; |
| 233 | } |
| 234 | |
| 235 | $actions[] = $toggle; |
| 236 | |
| 237 | $edit = 'edit'; |
| 238 | $delete = 'delete'; |
| 239 | |
| 240 | if ($this->isAllowedToEdit($cap) === false) { |
| 241 | $edit = 'no-' . $edit; |
| 242 | } |
| 243 | |
| 244 | if ($this->isAllowedToDelete($cap) === false) { |
| 245 | $delete = 'no-' . $delete; |
| 246 | } |
| 247 | |
| 248 | $actions[] = $edit; |
| 249 | $actions[] = $delete; |
| 250 | |
| 251 | return implode(',', $actions); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Check if current user is allowed to toggle capability |
| 256 | * |
| 257 | * @param string $cap |
| 258 | * |
| 259 | * @return boolean |
| 260 | * |
| 261 | * @access protected |
| 262 | * @version 6.0.0 |
| 263 | */ |
| 264 | protected function isAllowedToToggle($cap) |
| 265 | { |
| 266 | return apply_filters('aam_cap_can_filter', true, $cap, 'toggle'); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Check if current user can edit capability |
| 271 | * |
| 272 | * @param string $cap |
| 273 | * |
| 274 | * @return boolean |
| 275 | * |
| 276 | * @access protected |
| 277 | * @version 6.0.0 |
| 278 | */ |
| 279 | protected function isAllowedToEdit($cap) |
| 280 | { |
| 281 | $allowed = false; |
| 282 | |
| 283 | if (AAM_Core_Config::get('core.settings.editCapabilities', true)) { |
| 284 | $allowed = true; |
| 285 | } |
| 286 | |
| 287 | // Access & Security policy has higher priority |
| 288 | if (apply_filters('aam_cap_can_filter', true, $cap, 'update') === false) { |
| 289 | $allowed = false; |
| 290 | } |
| 291 | |
| 292 | // Check if current subject contains the capability and if so, allow to |
| 293 | // edit it |
| 294 | if ($allowed) { |
| 295 | $allowed = array_key_exists($cap, $this->getSubject()->getCapabilities()); |
| 296 | } |
| 297 | |
| 298 | return $allowed; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Check if current user can delete capability |
| 303 | * |
| 304 | * @param string $cap |
| 305 | * |
| 306 | * @return boolean |
| 307 | * |
| 308 | * @access protected |
| 309 | * @version 6.0.0 |
| 310 | */ |
| 311 | protected function isAllowedToDelete($cap) |
| 312 | { |
| 313 | $allowed = false; |
| 314 | |
| 315 | if (AAM_Core_Config::get('core.settings.editCapabilities', true)) { |
| 316 | $allowed = true; |
| 317 | } |
| 318 | |
| 319 | // Access & Security policy has higher priority |
| 320 | if (apply_filters('aam_cap_can_filter', true, $cap, 'delete') === false) { |
| 321 | $allowed = false; |
| 322 | } |
| 323 | |
| 324 | // Check if current subject contains the capability and if so, allow to |
| 325 | // delete it |
| 326 | if ($allowed) { |
| 327 | $allowed = array_key_exists($cap, $this->getSubject()->getCapabilities()); |
| 328 | } |
| 329 | |
| 330 | return $allowed; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Get capability group list |
| 335 | * |
| 336 | * @return array |
| 337 | * |
| 338 | * @access public |
| 339 | * @version 6.0.0 |
| 340 | */ |
| 341 | public function getGroupList() |
| 342 | { |
| 343 | return apply_filters('aam_capability_groups_filter', array( |
| 344 | __('System', AAM_KEY), |
| 345 | __('Posts & Pages', AAM_KEY), |
| 346 | __('Backend', AAM_KEY), |
| 347 | __('AAM Interface', AAM_KEY), |
| 348 | __('Miscellaneous', AAM_KEY) |
| 349 | )); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Get capability group name |
| 354 | * |
| 355 | * @param string $capability |
| 356 | * |
| 357 | * @return string |
| 358 | * |
| 359 | * @access protected |
| 360 | * @version 6.0.0 |
| 361 | */ |
| 362 | protected function getGroup($capability) |
| 363 | { |
| 364 | if (in_array($capability, self::$groups['system'], true)) { |
| 365 | $response = __('System', AAM_KEY); |
| 366 | } elseif (in_array($capability, self::$groups['post'], true)) { |
| 367 | $response = __('Posts & Pages', AAM_KEY); |
| 368 | } elseif (in_array($capability, self::$groups['backend'], true)) { |
| 369 | $response = __('Backend', AAM_KEY); |
| 370 | } elseif (strpos($capability, 'aam_') === 0) { |
| 371 | $response = __('AAM Interface', AAM_KEY); |
| 372 | } else { |
| 373 | $response = __('Miscellaneous', AAM_KEY); |
| 374 | } |
| 375 | |
| 376 | return apply_filters('aam_capability_group_filter', $response, $capability); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Register Capability service UI |
| 381 | * |
| 382 | * @return void |
| 383 | * |
| 384 | * @access public |
| 385 | * @version 6.0.0 |
| 386 | */ |
| 387 | public static function register() |
| 388 | { |
| 389 | AAM_Backend_Feature::registerFeature((object) array( |
| 390 | 'uid' => 'capability', |
| 391 | 'position' => 15, |
| 392 | 'title' => __('Capabilities', AAM_KEY), |
| 393 | 'capability' => self::ACCESS_CAPABILITY, |
| 394 | 'type' => 'main', |
| 395 | 'subjects' => array( |
| 396 | AAM_Core_Subject_Role::UID, |
| 397 | AAM_Core_Subject_User::UID |
| 398 | ), |
| 399 | 'view' => __CLASS__ |
| 400 | )); |
| 401 | } |
| 402 | |
| 403 | } |