Controllers
4 years ago
Helpers
4 years ago
SettingsPages
4 years ago
Views
4 years ago
class-help-contact-us.php
4 years ago
class-premium-features.php
4 years ago
class-settings-page.php
4 years ago
class-settingspage.php
4 years ago
class-setup-wizard.php
4 years ago
class-user-listing.php
4 years ago
class-user-notices.php
4 years ago
class-user-profile.php
4 years ago
class-user-registered.php
4 years ago
class-user.php
4 years ago
index.php
5 years ago
class-user-notices.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for WP2FA user's notifying. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage user-utils |
| 7 | * @copyright 2021 WP White Security |
| 8 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 9 | * @link https://wordpress.org/plugins/wp-2fa/ |
| 10 | */ |
| 11 | |
| 12 | namespace WP2FA\Admin; |
| 13 | |
| 14 | use \WP2FA\WP2FA as WP2FA; |
| 15 | use WP2FA\Utils\Date_Time_Utils; |
| 16 | use WP2FA\Admin\Helpers\WP_Helper; |
| 17 | use WP2FA\Admin\Helpers\User_Helper; |
| 18 | use WP2FA\Admin\Controllers\Settings; |
| 19 | |
| 20 | /** |
| 21 | * User_Notices - Class for displaying notices to our users. |
| 22 | */ |
| 23 | class User_Notices { |
| 24 | /** |
| 25 | * The WP User |
| 26 | * |
| 27 | * @var User |
| 28 | */ |
| 29 | private $wp2fa_user; |
| 30 | |
| 31 | /** |
| 32 | * Lets set things up |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | $enforcement_policy = WP2FA::get_wp2fa_setting( 'enforcement-policy' ); |
| 36 | if ( ! empty( $enforcement_policy ) ) { |
| 37 | // Check we are supposed to, before adding action to show nag. |
| 38 | if ( in_array( $enforcement_policy, array( 'all-users', 'certain-roles-only', 'certain-users-only', 'superadmins-only', 'superadmins-siteadmins-only', 'enforce-on-multisite', true ), true ) ) { |
| 39 | add_action( 'admin_notices', array( $this, 'user_setup_2fa_nag' ) ); |
| 40 | add_action( 'network_admin_notices', array( $this, 'user_setup_2fa_nag' ) ); |
| 41 | } elseif ( 'do-not-enforce' === WP2FA::get_wp2fa_setting( 'enforcement-policy' ) ) { |
| 42 | add_action( 'admin_notices', array( $this, 'user_reconfigure_2fa_nag' ) ); |
| 43 | add_action( 'network_admin_notices', array( $this, 'user_setup_2fa_nag' ) ); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * The nag content |
| 50 | * |
| 51 | * @param string $is_shortcode - Is that a call from shortcode. |
| 52 | * @param string $configure_2fa_url - The configuration url. |
| 53 | * |
| 54 | * @return void |
| 55 | */ |
| 56 | public function user_setup_2fa_nag( $is_shortcode = '', $configure_2fa_url = '' ) { |
| 57 | |
| 58 | $this->ensure_user(); |
| 59 | |
| 60 | if ( isset( $_GET['user_id'] ) ) { // phpcs:ignore |
| 61 | $current_profile_user_id = (int) $_GET['user_id']; // phpcs:ignore |
| 62 | } elseif ( ! is_null( $this->wp2fa_user->get_2fa_wp_user() ) ) { |
| 63 | $current_profile_user_id = $this->wp2fa_user->get_2fa_wp_user()->ID; |
| 64 | } else { |
| 65 | $current_profile_user_id = false; |
| 66 | } |
| 67 | |
| 68 | if ( ! $current_profile_user_id || |
| 69 | isset( $_GET['user_id'] ) && // phpcs:ignore |
| 70 | $_GET['user_id'] !== $this->wp2fa_user->get_2fa_wp_user()->ID || // phpcs:ignore |
| 71 | User_Helper::get_user_enforced_instantly( $this->wp2fa_user->get_2fa_wp_user() ) ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $grace_expiry = $this->wp2fa_user->get_grace_period_expiration(); |
| 76 | |
| 77 | $class = 'notice notice-info wp-2fa-nag'; |
| 78 | |
| 79 | if ( User_Helper::get_user_needs_to_reconfigure_2fa( $this->wp2fa_user->get_2fa_wp_user() ) ) { |
| 80 | $message = esc_html__( 'The 2FA method you were using is no longer allowed on this website. Please reconfigure 2FA using one of the supported methods within', 'wp-2fa' ); |
| 81 | } else { |
| 82 | $message = esc_html__( 'This website’s administrator requires you to enable 2FA authentication', 'wp-2fa' ); |
| 83 | } |
| 84 | |
| 85 | $is_nag_dismissed = $this->wp2fa_user->get_dismissed_nag(); |
| 86 | $is_nag_needed = User::is_enforced( $this->wp2fa_user->get_2fa_wp_user()->ID ); |
| 87 | $is_user_excluded = User::is_excluded( $this->wp2fa_user->get_2fa_wp_user()->ID ); |
| 88 | $enabled_methods = User_Helper::get_enabled_method_for_user( $this->wp2fa_user->get_2fa_wp_user() ); |
| 89 | $new_page_id = WP2FA::get_wp2fa_setting( 'custom-user-page-id' ); |
| 90 | $new_page_permalink = get_permalink( $new_page_id ); |
| 91 | |
| 92 | $setup_url = Settings::get_setup_page_link(); |
| 93 | |
| 94 | // Allow setup URL to be customized if outputting via shortcode. |
| 95 | if ( isset( $is_shortcode ) && 'output_shortcode' === $is_shortcode && ! empty( $configure_2fa_url ) ) { |
| 96 | $setup_url = $configure_2fa_url; |
| 97 | } |
| 98 | |
| 99 | // Stop the page from being a link to a page this user cant access if needed. |
| 100 | if ( WP_Helper::is_multisite() && ! is_user_member_of_blog( $this->wp2fa_user->get_2fa_wp_user()->ID ) ) { |
| 101 | $new_page_id = false; |
| 102 | } |
| 103 | |
| 104 | // If we have a custom page generated, lets use it. |
| 105 | if ( ! empty( $new_page_id ) && $new_page_permalink ) { |
| 106 | $setup_url = $new_page_permalink; |
| 107 | } |
| 108 | |
| 109 | // If the nag has not already been dismissed, and of course if the user is eligible, lets show them something. |
| 110 | if ( ! $is_nag_dismissed && $is_nag_needed && empty( $enabled_methods ) && ! $is_user_excluded && ! empty( $grace_expiry ) ) { |
| 111 | echo '<div class="' . esc_attr( $class ) . '">'; |
| 112 | echo '<p>' . esc_html( $message ); |
| 113 | echo ' <span class="grace-period-countdown">' . esc_attr( Date_Time_Utils::format_grace_period_expiration_string( null, $grace_expiry ) ) . '</span>'; |
| 114 | echo ' <a href="' . esc_url( $setup_url ) . '" class="button button-primary">' . esc_html__( 'Configure 2FA now', 'wp-2fa' ) . '</a>'; |
| 115 | echo ' <a href="#" class="button button-secondary dismiss-user-configure-nag">' . esc_html__( 'Remind me on next login', 'wp-2fa' ) . '</a></p>'; |
| 116 | echo '</div>'; |
| 117 | } else { |
| 118 | $this->user_reconfigure_2fa_nag(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * The nag content |
| 124 | */ |
| 125 | public function user_reconfigure_2fa_nag() { |
| 126 | |
| 127 | $this->ensure_user(); |
| 128 | |
| 129 | // If the nag has not already been dismissed, and of course if the user is eligible, lets show them something. |
| 130 | if ( $this->wp2fa_user->needs_to_reconfigure_method() ) { |
| 131 | $class = 'notice notice-info wp-2fa-nag'; |
| 132 | |
| 133 | $message = esc_html__( 'The 2FA method you were using is no longer allowed on this website. Please reconfigure 2FA using one of the supported methods.', 'wp-2fa' ); |
| 134 | |
| 135 | echo '<div class="' . esc_attr( $class ) . '"><p>' . esc_html( $message ); |
| 136 | echo ' <a href="' . esc_url( Settings::get_setup_page_link() ) . '" class="button button-primary">' . esc_html__( 'Configure 2FA now', 'wp-2fa' ) . '</a>'; |
| 137 | echo ' <a href="#" class="button button-secondary dismiss-user-reconfigure-nag">' . esc_html__( 'I\'ll do it later', 'wp-2fa' ) . '</a></p>'; |
| 138 | echo '</div>'; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Dismiss notice and setup a user meta value so we know its been dismissed |
| 144 | */ |
| 145 | public function dismiss_nag() { |
| 146 | $this->ensure_user(); |
| 147 | $this->wp2fa_user->set_dismissed_nag(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Reset the nag when the user logs out, so they get it again next time. |
| 152 | * |
| 153 | * @param [type] $user_id - The ID of the user. |
| 154 | * |
| 155 | * @return void |
| 156 | */ |
| 157 | public function reset_nag( $user_id ) { |
| 158 | $this->wp2fa_user = User::get_instance( $user_id ); |
| 159 | $this->wp2fa_user->delete_user_meta( 'wp_2fa_update_nag_dismissed' ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Sets user variable |
| 164 | * |
| 165 | * @return void |
| 166 | */ |
| 167 | private function ensure_user() { |
| 168 | if ( ! isset( $this->wp2fa_user ) ) { |
| 169 | $this->wp2fa_user = User::get_instance(); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 |