Skip to content

Commit 64491a6

Browse files
authored
Merge pull request #27 from Automattic/add/PLTFRM-973-notice-admins
Show the MFA notice only for users with `manage_options` capabilities
2 parents b9d1250 + 24aa150 commit 64491a6

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

modules/highlight-mfa-users/class-highlight-mfa-users.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public static function display_mfa_disabled_notice() {
5555
return;
5656
}
5757

58+
// Only show the notice to admins
59+
if ( ! current_user_can( 'manage_options' ) ) {
60+
return;
61+
}
62+
5863
// Only show on the main users list table
5964
$screen = get_current_screen();
6065
if ( ! $screen || 'users' !== $screen->id ) {

tests/phpunit/test-highlight-mfa-users.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function setUp(): void {
6363

6464
// Set skipped users option
6565
update_option( Highlight_MFA_Users::MFA_SKIP_USER_IDS_OPTION_KEY, [ $this->admin_user_mfa_skipped_id ] );
66-
66+
wp_set_current_user( $this->admin_user_mfa_enabled_id );
6767
Highlight_MFA_Users::init();
6868
}
6969

@@ -85,7 +85,7 @@ public function tearDown(): void {
8585
$_GET = $this->original_get;
8686
$GLOBALS['current_screen'] = $this->original_current_screen;
8787
unset( $GLOBALS['current_screen'] ); // Ensure it's fully removed if it wasn't set before
88-
88+
wp_set_current_user( 0 );
8989
parent::tearDown();
9090
}
9191

@@ -195,6 +195,22 @@ public function test_filter_users_by_mfa_status_does_nothing_on_wrong_page() {
195195
unset( $_GET['filter_mfa_disabled'] );
196196
}
197197

198+
199+
/**
200+
* Test that the admin notice is not displayed when we're an editor
201+
*/
202+
public function test_display_mfa_disabled_notice_does_not_show_when_not_admin() {
203+
$this->set_admin_screen_users();
204+
// Set a non-admin user
205+
wp_set_current_user( $this->editor_user_id );
206+
207+
ob_start();
208+
Highlight_MFA_Users::display_mfa_disabled_notice();
209+
$output = ob_get_clean();
210+
211+
$this->assertEquals( '', $output );
212+
}
213+
198214
/**
199215
* Test that the admin notice is displayed correctly when MFA-disabled admins exist.
200216
*/

0 commit comments

Comments
 (0)