AuditCheckTrait.php
1 year ago
CoreUserRoleOptionIntegrityCheck.php
1 year ago
EditableFileSystemCheck.php
7 months ago
ElevatedCoreRoleCheck.php
7 months ago
EmptyUnusedRoleCheck.php
7 months ago
HighPrivilegeContentModeratorCheck.php
7 months ago
HighPrivilegeOrElevatedUserCheck.php
7 months ago
HighPrivilegeRoleCheck.php
7 months ago
HighPrivilegeUserCountCheck.php
7 months ago
RestfulAutoDiscoverEndpointCheck.php
7 months ago
RoleCapabilityNamingConventionCheck.php
7 months ago
RoleIntegrityCheck.php
7 months ago
RoleTransparencyCheck.php
7 months ago
XmlRpcEndpointCheck.php
7 months ago
RoleIntegrityCheck.php
319 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 integrity audit check |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Audit_RoleIntegrityCheck |
| 17 | { |
| 18 | |
| 19 | use AAM_Audit_AuditCheckTrait; |
| 20 | |
| 21 | /** |
| 22 | * Step ID |
| 23 | * |
| 24 | * @version 7.0.0 |
| 25 | */ |
| 26 | const ID = 'core_roles_integrity'; |
| 27 | |
| 28 | /** |
| 29 | * Default collection of roles & capabilities |
| 30 | * |
| 31 | * @version 7.0.0 |
| 32 | */ |
| 33 | const DEFAULT_STATE = [ |
| 34 | 'administrator' => [ |
| 35 | 'name' => 'Administrator', |
| 36 | 'capabilities' => [ |
| 37 | 'switch_themes', |
| 38 | 'edit_themes', |
| 39 | 'activate_plugins', |
| 40 | 'edit_plugins', |
| 41 | 'edit_users', |
| 42 | 'edit_files', |
| 43 | 'manage_options', |
| 44 | 'moderate_comments', |
| 45 | 'manage_categories', |
| 46 | 'manage_links', |
| 47 | 'upload_files', |
| 48 | 'import', |
| 49 | 'unfiltered_html', |
| 50 | 'edit_posts', |
| 51 | 'edit_others_posts', |
| 52 | 'edit_published_posts', |
| 53 | 'publish_posts', |
| 54 | 'edit_pages', |
| 55 | 'read', |
| 56 | 'level_10', |
| 57 | 'level_9', |
| 58 | 'level_8', |
| 59 | 'level_7', |
| 60 | 'level_6', |
| 61 | 'level_5', |
| 62 | 'level_4', |
| 63 | 'level_3', |
| 64 | 'level_2', |
| 65 | 'level_1', |
| 66 | 'level_0', |
| 67 | 'edit_others_pages', |
| 68 | 'edit_published_pages', |
| 69 | 'publish_pages', |
| 70 | 'delete_pages', |
| 71 | 'delete_others_pages', |
| 72 | 'delete_published_pages', |
| 73 | 'delete_posts', |
| 74 | 'delete_others_posts', |
| 75 | 'delete_published_posts', |
| 76 | 'delete_private_posts', |
| 77 | 'edit_private_posts', |
| 78 | 'read_private_posts', |
| 79 | 'delete_private_pages', |
| 80 | 'edit_private_pages', |
| 81 | 'read_private_pages', |
| 82 | 'delete_users', |
| 83 | 'create_users', |
| 84 | 'unfiltered_upload', |
| 85 | 'edit_dashboard', |
| 86 | 'update_plugins', |
| 87 | 'delete_plugins', |
| 88 | 'install_plugins', |
| 89 | 'update_themes', |
| 90 | 'install_themes', |
| 91 | 'update_core', |
| 92 | 'list_users', |
| 93 | 'remove_users', |
| 94 | 'promote_users', |
| 95 | 'edit_theme_options', |
| 96 | 'delete_themes', |
| 97 | 'export' |
| 98 | ] |
| 99 | ], |
| 100 | 'editor' => [ |
| 101 | 'name' => 'Editor', |
| 102 | 'capabilities' => [ |
| 103 | 'moderate_comments', |
| 104 | 'manage_categories', |
| 105 | 'manage_links', |
| 106 | 'upload_files', |
| 107 | // 'unfiltered_html', - Disabling this as it is very dangerous cap |
| 108 | 'edit_posts', |
| 109 | 'edit_others_posts', |
| 110 | 'edit_published_posts', |
| 111 | 'publish_posts', |
| 112 | 'edit_pages', |
| 113 | 'read', |
| 114 | 'level_7', |
| 115 | 'level_6', |
| 116 | 'level_5', |
| 117 | 'level_4', |
| 118 | 'level_3', |
| 119 | 'level_2', |
| 120 | 'level_1', |
| 121 | 'level_0', |
| 122 | 'edit_others_pages', |
| 123 | 'edit_published_pages', |
| 124 | 'publish_pages', |
| 125 | 'delete_pages', |
| 126 | 'delete_others_pages', |
| 127 | 'delete_published_pages', |
| 128 | 'delete_posts', |
| 129 | 'delete_others_posts', |
| 130 | 'delete_published_posts', |
| 131 | 'delete_private_posts', |
| 132 | 'edit_private_posts', |
| 133 | 'read_private_posts', |
| 134 | 'delete_private_pages', |
| 135 | 'edit_private_pages', |
| 136 | 'read_private_pages' |
| 137 | ] |
| 138 | ], |
| 139 | 'author' => [ |
| 140 | 'name' => 'Author', |
| 141 | 'capabilities' => [ |
| 142 | 'upload_files', |
| 143 | 'edit_posts', |
| 144 | 'edit_published_posts', |
| 145 | 'publish_posts', |
| 146 | 'read', |
| 147 | 'level_2', |
| 148 | 'level_1', |
| 149 | 'level_0', |
| 150 | 'delete_posts', |
| 151 | 'delete_published_posts' |
| 152 | ] |
| 153 | ], |
| 154 | 'contributor' => [ |
| 155 | 'name' => 'Contributor', |
| 156 | 'capabilities' => [ |
| 157 | 'edit_posts', |
| 158 | 'read', |
| 159 | 'level_1', |
| 160 | 'level_0', |
| 161 | 'delete_posts' |
| 162 | ] |
| 163 | ], |
| 164 | 'subscriber' => [ |
| 165 | 'name' => 'Subscriber', |
| 166 | 'capabilities' => [ |
| 167 | 'read', |
| 168 | 'level_0' |
| 169 | ] |
| 170 | ] |
| 171 | ]; |
| 172 | |
| 173 | /** |
| 174 | * Run the check |
| 175 | * |
| 176 | * @return array |
| 177 | * |
| 178 | * @access public |
| 179 | * @static |
| 180 | * |
| 181 | * @version 7.0.0 |
| 182 | */ |
| 183 | public static function run() |
| 184 | { |
| 185 | $issues = []; |
| 186 | $response = [ 'is_completed' => true ]; |
| 187 | |
| 188 | try { |
| 189 | $db_roles = self::_read_role_key_option(); |
| 190 | |
| 191 | // Step #1. Insuring all WordPress core roles exist |
| 192 | array_push($issues, ...self::_validate_core_role_existence($db_roles)); |
| 193 | |
| 194 | // Step #2. Insuring all WordPress core roles have necessary caps |
| 195 | array_push( |
| 196 | $issues, |
| 197 | ...self::_validate_core_role_capabilities($db_roles) |
| 198 | ); |
| 199 | } catch (Exception $e) { |
| 200 | array_push($issues, self::_format_issue( |
| 201 | 'APPLICATION_ERROR', |
| 202 | [ |
| 203 | 'message' => $e->getMessage() |
| 204 | ], |
| 205 | 'error' |
| 206 | )); |
| 207 | } |
| 208 | |
| 209 | if (count($issues) > 0) { |
| 210 | $response['issues'] = $issues; |
| 211 | } |
| 212 | |
| 213 | // Determine final status for the check |
| 214 | self::_determine_check_status($response); |
| 215 | |
| 216 | return $response; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get a collection of error messages for current step |
| 221 | * |
| 222 | * @return array |
| 223 | * @access private |
| 224 | * @static |
| 225 | * |
| 226 | * @version 7.0.0 |
| 227 | */ |
| 228 | private static function _get_message_templates() |
| 229 | { |
| 230 | return [ |
| 231 | 'MISSING_CORE_ROLES' => __( |
| 232 | 'Detected missing WordPress core role(s): %s', |
| 233 | 'advanced-access-manager' |
| 234 | ), |
| 235 | 'MISSING_CORE_CAPS' => __( |
| 236 | 'Detected missing WordPress core caps for role %s (%s): %s', |
| 237 | 'advanced-access-manager' |
| 238 | ) |
| 239 | ]; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Validate that core WP roles exist |
| 244 | * |
| 245 | * @param array $db_roles |
| 246 | * |
| 247 | * @return array |
| 248 | * |
| 249 | * @access private |
| 250 | * @static |
| 251 | * |
| 252 | * @version 7.0.0 |
| 253 | */ |
| 254 | private static function _validate_core_role_existence($db_roles) |
| 255 | { |
| 256 | $response = []; |
| 257 | |
| 258 | $existing_roles = array_keys($db_roles); |
| 259 | $complete_roles = array_keys(self::DEFAULT_STATE); |
| 260 | $diff_roles = array_diff($complete_roles, $existing_roles); |
| 261 | |
| 262 | if (!empty($diff_roles)) { |
| 263 | array_push($response, self::_format_issue( |
| 264 | 'MISSING_CORE_ROLES', |
| 265 | [ |
| 266 | 'roles' => $diff_roles |
| 267 | ], |
| 268 | 'warning' |
| 269 | )); |
| 270 | } |
| 271 | |
| 272 | return $response; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Validate that WP core capabilities assigned to core roles |
| 277 | * |
| 278 | * @param array $db_roles |
| 279 | * |
| 280 | * @return array |
| 281 | * |
| 282 | * @access private |
| 283 | * @static |
| 284 | * |
| 285 | * @version 7.0.0 |
| 286 | */ |
| 287 | private static function _validate_core_role_capabilities($db_roles) |
| 288 | { |
| 289 | $response = []; |
| 290 | |
| 291 | foreach($db_roles as $role_id => $role) { |
| 292 | // Take into consideration only core roles |
| 293 | if (array_key_exists($role_id, self::DEFAULT_STATE)) { |
| 294 | $existing_caps = array_keys( |
| 295 | array_filter($role['capabilities'], function($v) { |
| 296 | return !empty($v); |
| 297 | }) |
| 298 | ); |
| 299 | $complete_caps = self::DEFAULT_STATE[$role_id]['capabilities']; |
| 300 | $diff_caps = array_diff($complete_caps, $existing_caps); |
| 301 | |
| 302 | if (!empty($diff_caps)) { |
| 303 | array_push($response, self::_format_issue( |
| 304 | 'MISSING_CORE_CAPS', |
| 305 | [ |
| 306 | 'role_name' => translate_user_role($role['name']), |
| 307 | 'role_slug' => $role_id, |
| 308 | 'caps' => $diff_caps |
| 309 | ], |
| 310 | 'warning' |
| 311 | )); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return $response; |
| 317 | } |
| 318 | |
| 319 | } |