404Redirect.php
7 years ago
Capability.php
7 years ago
GetStarted.php
7 years ago
LoginRedirect.php
7 years ago
LogoutRedirect.php
7 years ago
Menu.php
7 years ago
Metabox.php
7 years ago
Policy.php
7 years ago
Post.php
7 years ago
Redirect.php
7 years ago
Route.php
7 years ago
Toolbar.php
7 years ago
Uri.php
7 years ago
Capability.php
389 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 capability manager |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Backend_Feature_Main_Capability extends AAM_Backend_Feature_Abstract { |
| 17 | |
| 18 | /** |
| 19 | * Capability groups |
| 20 | * |
| 21 | * @var array |
| 22 | * |
| 23 | * @access private |
| 24 | */ |
| 25 | public static $groups = array( |
| 26 | 'system' => array( |
| 27 | 'level_0', 'level_1', 'level_2', 'level_3', 'level_4', 'level_5', |
| 28 | 'level_6', 'level_7', 'level_8', 'level_9', 'level_10' |
| 29 | ), |
| 30 | 'post' => array( |
| 31 | 'delete_others_pages', 'delete_others_posts', 'edit_others_pages', |
| 32 | 'delete_posts', 'delete_private_pages', 'delete_private_posts', |
| 33 | 'delete_published_pages', 'delete_published_posts', 'delete_pages', |
| 34 | 'edit_others_posts', 'edit_pages', 'edit_private_posts', |
| 35 | 'edit_private_pages', 'edit_posts', 'edit_published_pages', |
| 36 | 'edit_published_posts', 'publish_pages', 'publish_posts', 'read', |
| 37 | 'read_private_pages', 'read_private_posts', 'edit_permalink' |
| 38 | ), |
| 39 | 'backend' => array( |
| 40 | 'activate_plugins', 'add_users', 'update_plugins', |
| 41 | 'delete_users', 'delete_themes', 'edit_dashboard', 'edit_files', |
| 42 | 'edit_plugins', 'edit_theme_options', 'edit_themes', 'edit_users', |
| 43 | 'export', 'import', 'install_plugins', 'install_themes', |
| 44 | 'manage_options', 'manage_links', 'manage_categories', 'customize', |
| 45 | 'unfiltered_html', 'unfiltered_upload', 'update_themes', |
| 46 | 'update_core', 'upload_files', 'delete_plugins', 'remove_users', |
| 47 | 'switch_themes', 'list_users', 'promote_users', 'create_users', 'delete_site' |
| 48 | ), |
| 49 | 'aam' => array( |
| 50 | 'aam_manage_admin_menu', 'aam_manage_metaboxes', 'aam_manage_capabilities', |
| 51 | 'aam_manage_posts', 'aam_manage_access_denied_redirect', 'aam_create_roles', |
| 52 | 'aam_manage_login_redirect', 'aam_manage_logout_redirect', 'aam_manager', |
| 53 | 'aam_manage_settings', 'aam_manage_extensions', 'aam_show_notifications', |
| 54 | 'aam_manage_404_redirect', 'aam_manage_ip_check', 'aam_manage_admin_toolbar', |
| 55 | 'aam_manage_default', 'aam_manage_visitors', 'aam_manage_roles', 'aam_manage_users', |
| 56 | 'aam_edit_roles', 'aam_delete_roles', 'aam_toggle_users', 'aam_switch_users', |
| 57 | 'aam_manage_configpress', 'aam_manage_api_routes', 'aam_manage_uri', 'aam_manage_policy', |
| 58 | 'aam_view_help_btn' |
| 59 | ) |
| 60 | ); |
| 61 | |
| 62 | /** |
| 63 | * |
| 64 | * @return type |
| 65 | */ |
| 66 | public function getTable() { |
| 67 | $response = array('data' => $this->retrieveAllCaps()); |
| 68 | |
| 69 | return wp_json_encode($response); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Update capability tag |
| 74 | * |
| 75 | * @return string |
| 76 | * |
| 77 | * @access public |
| 78 | */ |
| 79 | public function update() { |
| 80 | $capability = AAM_Core_Request::post('capability'); |
| 81 | $updated = AAM_Core_Request::post('updated'); |
| 82 | $roles = AAM_Core_API::getRoles(); |
| 83 | |
| 84 | if ($this->isAllowedToEdit($capability) === false) { |
| 85 | $response = array( |
| 86 | 'status' => 'failure', |
| 87 | 'message' => __('Permission denied to update this capability', AAM_KEY) |
| 88 | ); |
| 89 | } elseif (AAM_Core_API::capabilityExists($updated) === false) { |
| 90 | foreach($roles->role_objects as $role) { |
| 91 | //check if capability is present for current role! Note, we |
| 92 | //can not use the native WP_Role::has_cap function because it will |
| 93 | //return false if capability exists but not checked |
| 94 | if (is_array($role->capabilities) |
| 95 | && array_key_exists($capability, $role->capabilities)) { |
| 96 | $role->add_cap($updated, $role->capabilities[$capability]); |
| 97 | $role->remove_cap($capability); |
| 98 | } |
| 99 | } |
| 100 | $response = array('status' => 'success'); |
| 101 | } else { |
| 102 | $response = array( |
| 103 | 'status' => 'failure', |
| 104 | 'message' => __('Capability already exists', AAM_KEY) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | return wp_json_encode($response); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Delete capability |
| 113 | * |
| 114 | * This function delete capability in all roles. |
| 115 | * |
| 116 | * @return string |
| 117 | * |
| 118 | * @access public |
| 119 | */ |
| 120 | public function delete() { |
| 121 | $capability = AAM_Core_Request::post('capability'); |
| 122 | $roles = AAM_Core_API::getRoles(); |
| 123 | |
| 124 | if ($this->isAllowedToEdit($capability) === false) { |
| 125 | $response = array( |
| 126 | 'status' => 'failure', |
| 127 | 'message' => __('Permission denied to delete this capability', AAM_KEY) |
| 128 | ); |
| 129 | } else { |
| 130 | foreach($roles->role_objects as $role) { |
| 131 | $role->remove_cap($capability); |
| 132 | } |
| 133 | $response = array('status' => 'success'); |
| 134 | } |
| 135 | |
| 136 | return wp_json_encode($response); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @inheritdoc |
| 141 | */ |
| 142 | public static function getTemplate() { |
| 143 | return 'main/capability.phtml'; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * |
| 148 | * @param type $cap |
| 149 | * @return type |
| 150 | */ |
| 151 | protected function prepareActionList($cap) { |
| 152 | $subject = AAM_Backend_Subject::getInstance(); |
| 153 | $actions = array(); |
| 154 | |
| 155 | $toggle = ($subject->hasCapability($cap) ? 'checked' : 'unchecked'); |
| 156 | |
| 157 | if (AAM::api()->isAllowed("Capability:{$cap}:AAM:toggle") === false) { |
| 158 | $toggle = 'no-' . $toggle; |
| 159 | } |
| 160 | |
| 161 | $actions[] = $toggle; |
| 162 | |
| 163 | //allow to delete or update capability only for roles! |
| 164 | $edit = 'edit'; |
| 165 | $delete = 'delete'; |
| 166 | |
| 167 | if ($this->isAllowedToEdit($cap) === false) { |
| 168 | $edit = 'no-' . $edit; |
| 169 | } |
| 170 | |
| 171 | if ($this->isAllowedToDelete($cap) === false) { |
| 172 | $delete = 'no-' . $delete; |
| 173 | } |
| 174 | |
| 175 | $actions[] = $edit; |
| 176 | $actions[] = $delete; |
| 177 | |
| 178 | return implode(',', $actions); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * |
| 183 | * @param type $subject |
| 184 | * @param type $cap |
| 185 | * @return boolean |
| 186 | */ |
| 187 | protected function isAllowedToEdit($cap) { |
| 188 | $allowed = false; |
| 189 | |
| 190 | if (AAM_Core_Config::get('core.settings.editCapabilities', true)) { |
| 191 | $allowed = true; |
| 192 | } |
| 193 | |
| 194 | // Access & Security policy has higher priority |
| 195 | if (AAM::api()->isAllowed("Capability:{$cap}:AAM:update") === false) { |
| 196 | $allowed = false; |
| 197 | } |
| 198 | |
| 199 | return $allowed; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * |
| 204 | * @param type $subject |
| 205 | * @param type $cap |
| 206 | * @return boolean |
| 207 | */ |
| 208 | protected function isAllowedToDelete($cap) { |
| 209 | $allowed = false; |
| 210 | |
| 211 | if (AAM_Core_Config::get('core.settings.editCapabilities', true)) { |
| 212 | $allowed = true; |
| 213 | } |
| 214 | |
| 215 | // Access & Security policy has higher priority |
| 216 | if (AAM::api()->isAllowed("Capability:{$cap}:AAM:delete") === false) { |
| 217 | $allowed = false; |
| 218 | } |
| 219 | |
| 220 | return $allowed; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get list of user roles |
| 225 | * |
| 226 | * @param array $roles |
| 227 | * |
| 228 | * @return array |
| 229 | * |
| 230 | * @access protected |
| 231 | */ |
| 232 | protected function getUserRoles($roles) { |
| 233 | $response = array(); |
| 234 | |
| 235 | $names = AAM_Core_API::getRoles()->get_names(); |
| 236 | |
| 237 | if (is_array($roles)) { |
| 238 | foreach($roles as $role) { |
| 239 | if (is_array($names) && array_key_exists($role, $names)) { |
| 240 | $response[] = translate_user_role($names[$role]); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return $response; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * |
| 250 | * @return type |
| 251 | */ |
| 252 | protected function retrieveAllCaps() { |
| 253 | $response = array(); |
| 254 | |
| 255 | $subject = AAM_Backend_Subject::getInstance()->get(); |
| 256 | $subject->initialize(true); |
| 257 | |
| 258 | // Load also capabilities defined in policy |
| 259 | $stms = AAM_Core_Policy_Manager::getInstance()->find( |
| 260 | "/^Capability:/i", $subject |
| 261 | ); |
| 262 | |
| 263 | $policyCaps = array(); |
| 264 | |
| 265 | foreach($stms as $key => $stm) { |
| 266 | $chunks = explode(':', $key); |
| 267 | if (count($chunks) === 2) { |
| 268 | $policyCaps[$chunks[1]] = ($stm['Effect'] === 'allow' ? 1 : 0); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | $caps = array_merge(AAM_Core_API::getAllCapabilities(), $policyCaps); |
| 273 | |
| 274 | foreach (array_keys($caps) as $cap) { |
| 275 | if (AAM::api()->isAllowed("Capability:{$cap}:AAM:list") !== false) { |
| 276 | $response[] = array( |
| 277 | $cap, |
| 278 | $this->getGroup($cap), |
| 279 | $cap, |
| 280 | $this->prepareActionList($cap) |
| 281 | ); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return $response; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Get capability group list |
| 290 | * |
| 291 | * @return array |
| 292 | * |
| 293 | * @access public |
| 294 | */ |
| 295 | public function getGroupList() { |
| 296 | return apply_filters('aam-capability-groups-filter', array( |
| 297 | __('System', AAM_KEY), |
| 298 | __('Posts & Pages', AAM_KEY), |
| 299 | __('Backend', AAM_KEY), |
| 300 | __('AAM Interface', AAM_KEY), |
| 301 | __('Miscellaneous', AAM_KEY) |
| 302 | )); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Add new capability |
| 307 | * |
| 308 | * @return string |
| 309 | * |
| 310 | * @access public |
| 311 | */ |
| 312 | public function add() { |
| 313 | $capability = sanitize_text_field(AAM_Core_Request::post('capability')); |
| 314 | |
| 315 | if ($capability) { |
| 316 | //add the capability to administrator's role as default behavior |
| 317 | AAM_Core_API::getRoles()->add_cap('administrator', $capability); |
| 318 | AAM_Backend_Subject::getInstance()->addCapability($capability); |
| 319 | $response = array('status' => 'success'); |
| 320 | } else { |
| 321 | $response = array('status' => 'failure'); |
| 322 | } |
| 323 | |
| 324 | return wp_json_encode($response); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Get capability group name |
| 329 | * |
| 330 | * @param string $capability |
| 331 | * |
| 332 | * @return string |
| 333 | * |
| 334 | * @access protected |
| 335 | */ |
| 336 | protected function getGroup($capability) { |
| 337 | if (in_array($capability, self::$groups['system'], true)) { |
| 338 | $response = __('System', AAM_KEY); |
| 339 | } elseif (in_array($capability, self::$groups['post'], true)) { |
| 340 | $response = __('Posts & Pages', AAM_KEY); |
| 341 | } elseif (in_array($capability, self::$groups['backend'], true)) { |
| 342 | $response = __('Backend', AAM_KEY); |
| 343 | } elseif (in_array($capability, self::$groups['aam'], true)) { |
| 344 | $response = __('AAM Interface', AAM_KEY); |
| 345 | } else { |
| 346 | $response = __('Miscellaneous', AAM_KEY); |
| 347 | } |
| 348 | |
| 349 | return apply_filters( |
| 350 | 'aam-capability-group-filter', $response, $capability |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Check overwritten status |
| 356 | * |
| 357 | * @return boolean |
| 358 | * |
| 359 | * @access protected |
| 360 | */ |
| 361 | protected function isOverwritten() { |
| 362 | $object = AAM_Backend_Subject::getInstance()->getObject('capability'); |
| 363 | |
| 364 | return $object->isOverwritten(); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Register capability feature |
| 369 | * |
| 370 | * @return void |
| 371 | * |
| 372 | * @access public |
| 373 | */ |
| 374 | public static function register() { |
| 375 | AAM_Backend_Feature::registerFeature((object) array( |
| 376 | 'uid' => 'capability', |
| 377 | 'position' => 15, |
| 378 | 'title' => __('Capabilities', AAM_KEY), |
| 379 | 'capability' => 'aam_manage_capabilities', |
| 380 | 'type' => 'main', |
| 381 | 'subjects' => array( |
| 382 | AAM_Core_Subject_Role::UID, |
| 383 | AAM_Core_Subject_User::UID |
| 384 | ), |
| 385 | 'view' => __CLASS__ |
| 386 | )); |
| 387 | } |
| 388 | |
| 389 | } |