-
Notifications
You must be signed in to change notification settings - Fork 0
Improve Higlight-MFA Module notice to specify the affected roles #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,9 @@ | |
use function Automattic\VIP\Security\Utils\get_module_configs; | ||
|
||
class Highlight_MFA_Users { | ||
const MFA_SKIP_USER_IDS_OPTION_KEY = 'vip_security_mfa_skip_user_ids'; | ||
const ROLE_COLUMN_KEY = 'role'; | ||
const MFA_SKIP_USER_IDS_OPTION_KEY = 'vip_security_mfa_skip_user_ids'; | ||
const ROLE_COLUMN_KEY = 'role'; | ||
const DEFAULT_ADMIN_EDITOR_ROLE_SLUGS = [ 'administrator', 'editor' ]; | ||
|
||
/** | ||
* The roles used to highlight users without MFA. | ||
|
@@ -17,15 +18,15 @@ class Highlight_MFA_Users { | |
public static function init() { | ||
// Feature is always active unless specific users are skipped via option. | ||
$highlight_mfa_configs = get_module_configs( 'highlight-mfa-users' ); | ||
self::$roles = $highlight_mfa_configs['roles'] ?? [ 'administrator', 'editor' ]; // Default to administrator and editor if not configured | ||
self::$roles = $highlight_mfa_configs['roles'] ?? self::DEFAULT_ADMIN_EDITOR_ROLE_SLUGS; // Default to administrator and editor if not configured | ||
|
||
if ( ! is_array( self::$roles ) ) { | ||
self::$roles = [ self::$roles ]; | ||
} | ||
self::$roles = array_filter( self::$roles ); | ||
// If after filtering, the array is empty, default back to administrator and editor | ||
if ( empty( self::$roles ) ) { | ||
self::$roles = [ 'administrator', 'editor' ]; | ||
self::$roles = self::DEFAULT_ADMIN_EDITOR_ROLE_SLUGS; | ||
} | ||
|
||
add_action( 'admin_notices', [ __CLASS__, 'display_mfa_disabled_notice' ] ); | ||
|
@@ -61,6 +62,14 @@ public static function display_mfa_disabled_notice() { | |
return; | ||
} | ||
|
||
// Determine notice text based on role configuration | ||
// self::$roles is already an array and populated from init(). | ||
$configured_roles_for_comparison = self::$roles; | ||
\sort( $configured_roles_for_comparison ); // Use global sort | ||
|
||
// unordered array check with == | ||
$is_default_config = ( self::DEFAULT_ADMIN_EDITOR_ROLE_SLUGS === $configured_roles_for_comparison || empty( $configured_roles_for_comparison ) ); | ||
|
||
$skipped_user_ids = get_option( self::MFA_SKIP_USER_IDS_OPTION_KEY, [] ); | ||
if ( ! is_array( $skipped_user_ids ) ) { | ||
$skipped_user_ids = []; | ||
|
@@ -95,45 +104,104 @@ public static function display_mfa_disabled_notice() { | |
$is_filtered = isset( $_GET['filter_mfa_disabled'] ) && '1' === $_GET['filter_mfa_disabled']; | ||
|
||
if ( $is_filtered ) { | ||
// Display notice for when the list IS filtered | ||
$show_all_url = remove_query_arg( 'filter_mfa_disabled', admin_url( 'users.php' ) ); | ||
// Display info notice for when the list IS filtered | ||
$show_all_url = remove_query_arg( 'filter_mfa_disabled', admin_url( 'users.php' ) ); | ||
$notice_message_text = self::get_missing_mfa_notice_message_text( $mfa_disabled_count, $is_default_config ); | ||
|
||
printf( | ||
'<div class="notice notice-info"><p>%s <a href="%s">%s</a></p></div>', | ||
esc_html( sprintf( | ||
/* Translators: %d is the number of users without 2FA enabled being shown in the filtered list. */ | ||
_n( | ||
'Showing %d user without Two-Factor Authentication enabled.', | ||
'Showing %d users without Two-Factor Authentication enabled.', | ||
$mfa_disabled_count, | ||
'wpvip' | ||
), | ||
number_format_i18n( $mfa_disabled_count ) | ||
) ), | ||
esc_html( $notice_message_text ), | ||
esc_url( $show_all_url ), | ||
esc_html__( 'Show all users.', 'wpvip' ) | ||
); | ||
} else { | ||
// Display the original notice when the list is NOT filtered | ||
$filter_url = add_query_arg( 'filter_mfa_disabled', '1', admin_url( 'users.php' ) ); | ||
$filter_url = add_query_arg( 'filter_mfa_disabled', '1', admin_url( 'users.php' ) ); | ||
$notice_message_text = self::get_filtering_mfa_info_message_text( $mfa_disabled_count, $is_default_config ); | ||
|
||
printf( | ||
'<div class="notice notice-error"><p>%s <a href="%s">%s</a></p></div>', | ||
esc_html( sprintf( | ||
/* Translators: %d is the number of users without 2FA enabled. */ | ||
_n( | ||
'There is %d user with Two-Factor Authentication disabled.', | ||
'There are %d users with Two-Factor Authentication disabled.', | ||
$mfa_disabled_count, | ||
'wpvip' | ||
), | ||
number_format_i18n( $mfa_disabled_count ) | ||
) ), | ||
esc_html( $notice_message_text ), | ||
esc_url( $filter_url ), | ||
esc_html__( 'Filter list to show these users.', 'wpvip' ) | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get the notice message text for when users with missing MFA are shown. | ||
* | ||
* @param int $mfa_disabled_count The number of users with missing MFA. | ||
* @param bool $is_default_config Whether the default roles are being used. | ||
* @return string The notice message text. | ||
*/ | ||
protected static function get_missing_mfa_notice_message_text( $mfa_disabled_count, $is_default_config ) { | ||
$notice_message_text = ''; | ||
if ( $is_default_config ) { | ||
// Default roles: Administrator, Editor | ||
$notice_message_text = sprintf( | ||
/* Translators: %d is the number of users with Administrator or Editor roles without 2FA enabled being shown. */ | ||
_n( | ||
'Showing %d user with Administrator or Editor roles without Two-Factor Authentication enabled.', | ||
'Showing %d users with Administrator or Editor roles without Two-Factor Authentication enabled.', | ||
$mfa_disabled_count, | ||
'wpvip' | ||
), | ||
number_format_i18n( $mfa_disabled_count ) | ||
); | ||
} else { | ||
// Custom roles | ||
$notice_message_text = sprintf( | ||
/* Translators: %d is the number of users with high-privileges without 2FA enabled being shown. */ | ||
_n( | ||
'Showing %d user with high-privileges without Two-Factor Authentication enabled.', | ||
'Showing %d users with high-privileges without Two-Factor Authentication enabled.', | ||
$mfa_disabled_count, | ||
'wpvip' | ||
), | ||
number_format_i18n( $mfa_disabled_count ) | ||
); | ||
} | ||
|
||
return $notice_message_text; | ||
} | ||
|
||
/** | ||
* Get the notice message text for when users with missing MFA are filtered. | ||
* | ||
* @param int $mfa_disabled_count The number of users with missing MFA. | ||
* @param bool $is_default_config Whether the default roles are being used. | ||
* @return string The notice message text. | ||
*/ | ||
protected static function get_filtering_mfa_info_message_text( $mfa_disabled_count, $is_default_config ) { | ||
$notice_message_text = ''; | ||
if ( $is_default_config ) { | ||
$notice_message_text = sprintf( | ||
/* Translators: %d is the number of users with Administrator or Editor roles and 2FA disabled. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Indentation looks off here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brunobasto Agree. I wonder why it wasn't caught by the linting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed indentation in cf48a0d |
||
_n( | ||
'There is %d user with Administrator or Editor roles with Two-Factor Authentication disabled.', | ||
'There are %d users with Administrator or Editor roles with Two-Factor Authentication disabled.', | ||
$mfa_disabled_count, | ||
'wpvip' | ||
), | ||
number_format_i18n( $mfa_disabled_count ) | ||
); | ||
} else { | ||
$notice_message_text = sprintf( | ||
/* Translators: %d is the number of high-privilege users with 2FA disabled. */ | ||
_n( | ||
'There is %d user with high-privileges with Two-Factor Authentication disabled.', | ||
'There are %d users with high-privileges with Two-Factor Authentication disabled.', | ||
$mfa_disabled_count, | ||
'wpvip' | ||
), | ||
number_format_i18n( $mfa_disabled_count ) | ||
); | ||
} | ||
return $notice_message_text; | ||
} | ||
|
||
/** | ||
* Modify the user query on the Users page to filter by MFA status if requested. | ||
* @param \WP_User_Query $query The WP_User_Query instance (passed by reference). | ||
|
Uh oh!
There was an error while loading. Please reload this page.