391 lines
12 KiB
PHP
391 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* Admin dashboard for 2FA audit overview.
|
|
*
|
|
* @package Robotstxt_2FA
|
|
*/
|
|
|
|
namespace Robotstxt\TwoFA\Admin;
|
|
|
|
use Robotstxt\TwoFA\Attempts_Log;
|
|
use Robotstxt\TwoFA\User\Two_Factor_Config;
|
|
use Robotstxt\TwoFA\User\User_Settings_Repository;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Renders the top-level 2FA admin menu and audit dashboard page.
|
|
*/
|
|
class Dashboard_Page {
|
|
/**
|
|
* Slug for the dashboard admin page.
|
|
*/
|
|
public const PAGE_SLUG = 'robotstxt-2fa-dashboard';
|
|
|
|
/**
|
|
* Failed attempts log.
|
|
*
|
|
* @var Attempts_Log
|
|
*/
|
|
private Attempts_Log $attempts_log;
|
|
|
|
/**
|
|
* Global two-factor configuration.
|
|
*
|
|
* @var Two_Factor_Config
|
|
*/
|
|
private Two_Factor_Config $config;
|
|
|
|
/**
|
|
* User settings repository.
|
|
*
|
|
* @var User_Settings_Repository
|
|
*/
|
|
private User_Settings_Repository $repository;
|
|
|
|
/**
|
|
* Users list table — instantiated lazily inside render_page() after admin
|
|
* includes are loaded. WP_List_Table::__construct() calls convert_to_screen()
|
|
* which is unavailable at plugins_loaded time.
|
|
*
|
|
* @var Users_List_Table|null
|
|
*/
|
|
private ?Users_List_Table $users_table = null;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param Attempts_Log $attempts_log Failed attempts log instance.
|
|
* @param Two_Factor_Config $config Global two-factor configuration.
|
|
* @param User_Settings_Repository $repository User settings repository.
|
|
*/
|
|
public function __construct( Attempts_Log $attempts_log, Two_Factor_Config $config, User_Settings_Repository $repository ) {
|
|
$this->attempts_log = $attempts_log;
|
|
$this->config = $config;
|
|
$this->repository = $repository;
|
|
}
|
|
|
|
/**
|
|
* Return (and lazily create) the users list table instance.
|
|
*
|
|
* @return Users_List_Table
|
|
*/
|
|
private function get_users_table(): Users_List_Table {
|
|
if ( null === $this->users_table ) {
|
|
$this->users_table = new Users_List_Table( $this->config, $this->repository );
|
|
}
|
|
|
|
return $this->users_table;
|
|
}
|
|
|
|
/**
|
|
* Register WordPress hooks.
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register_hooks(): void {
|
|
add_action( 'admin_menu', array( $this, 'register_menu' ) );
|
|
add_filter( 'manage_users_columns', array( $this, 'add_users_column' ) );
|
|
add_filter( 'manage_users_custom_column', array( $this, 'render_users_column' ), 10, 3 );
|
|
add_action( 'admin_post_robotstxt_2fa_export_users', array( $this, 'handle_csv_export' ) );
|
|
}
|
|
|
|
/**
|
|
* Register the top-level and dashboard submenu pages.
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register_menu(): void {
|
|
add_menu_page(
|
|
__( '2FA (by ROBOTSTXT)', 'robotstxt-2fa' ),
|
|
__( '2FA', 'robotstxt-2fa' ),
|
|
'manage_options',
|
|
self::PAGE_SLUG,
|
|
array( $this, 'render_page' ),
|
|
'dashicons-shield'
|
|
);
|
|
|
|
add_submenu_page(
|
|
self::PAGE_SLUG,
|
|
__( 'Dashboard — 2FA', 'robotstxt-2fa' ),
|
|
__( 'Dashboard', 'robotstxt-2fa' ),
|
|
'manage_options',
|
|
self::PAGE_SLUG,
|
|
array( $this, 'render_page' )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Render the audit dashboard page.
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function render_page(): void {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'robotstxt-2fa' ) );
|
|
}
|
|
|
|
$count_result = count_users();
|
|
$total_users = $count_result['total_users'];
|
|
$query_2fa = new \WP_User_Query(
|
|
array(
|
|
'meta_key' => 'robotstxt_2fa_user_settings',
|
|
'meta_compare' => 'EXISTS',
|
|
'fields' => 'ID',
|
|
'number' => -1,
|
|
)
|
|
);
|
|
$users_with_2fa = $query_2fa->get_total();
|
|
|
|
$log = get_option( 'robotstxt_2fa_failed_log', array() );
|
|
$log = is_array( $log ) ? $log : array();
|
|
$week_ago = time() - WEEK_IN_SECONDS;
|
|
|
|
$failed_7d = 0;
|
|
|
|
foreach ( $log as $entry ) {
|
|
if ( is_array( $entry ) && isset( $entry['timestamp'] ) && is_numeric( $entry['timestamp'] ) && (int) $entry['timestamp'] >= $week_ago ) {
|
|
++$failed_7d;
|
|
}
|
|
}
|
|
|
|
$recent_attempts = $this->attempts_log->get_recent( 20 );
|
|
|
|
$card_style = 'background:#fff;border:1px solid #dcdcde;padding:16px 20px;min-width:160px;text-align:center;';
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php esc_html_e( 'Two-Factor Authentication Dashboard', 'robotstxt-2fa' ); ?></h1>
|
|
|
|
<div style="display:flex;gap:16px;flex-wrap:wrap;margin:1.5em 0;">
|
|
<div style="<?php echo esc_attr( $card_style ); ?>">
|
|
<div style="font-size:2em;font-weight:700;"><?php echo absint( $total_users ); ?></div>
|
|
<div style="font-size:0.875em;color:#646970;"><?php esc_html_e( 'Total users', 'robotstxt-2fa' ); ?></div>
|
|
</div>
|
|
<div style="<?php echo esc_attr( $card_style ); ?>">
|
|
<div style="font-size:2em;font-weight:700;"><?php echo absint( $users_with_2fa ); ?></div>
|
|
<div style="font-size:0.875em;color:#646970;"><?php esc_html_e( '2FA enabled', 'robotstxt-2fa' ); ?></div>
|
|
</div>
|
|
<div style="<?php echo esc_attr( $card_style ); ?>">
|
|
<div style="font-size:2em;font-weight:700;"><?php echo absint( $failed_7d ); ?></div>
|
|
<div style="font-size:0.875em;color:#646970;"><?php esc_html_e( 'Failed attempts (7 days)', 'robotstxt-2fa' ); ?></div>
|
|
</div>
|
|
</div>
|
|
|
|
<h2><?php esc_html_e( 'Users', 'robotstxt-2fa' ); ?></h2>
|
|
|
|
<form method="get">
|
|
<input type="hidden" name="page" value="<?php echo esc_attr( self::PAGE_SLUG ); ?>" />
|
|
<?php
|
|
$this->get_users_table()->prepare_items();
|
|
$this->get_users_table()->display();
|
|
?>
|
|
</form>
|
|
|
|
<?php
|
|
// @phpstan-ignore-next-line -- redundant but kept for defence in depth; render_page() already gates on manage_options.
|
|
if ( current_user_can( 'manage_options' ) ) :
|
|
?>
|
|
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top:1em;">
|
|
<input type="hidden" name="action" value="robotstxt_2fa_export_users" />
|
|
<?php wp_nonce_field( 'robotstxt_2fa_export_users' ); ?>
|
|
<?php submit_button( __( 'Export CSV', 'robotstxt-2fa' ), 'secondary', 'submit', false ); ?>
|
|
</form>
|
|
<?php endif; ?>
|
|
|
|
<h2><?php esc_html_e( 'Recent failed attempts', 'robotstxt-2fa' ); ?></h2>
|
|
|
|
<?php if ( empty( $recent_attempts ) ) : ?>
|
|
<p><?php esc_html_e( 'No failed attempts recorded.', 'robotstxt-2fa' ); ?></p>
|
|
<?php else : ?>
|
|
<table class="widefat striped">
|
|
<thead>
|
|
<tr>
|
|
<th><?php esc_html_e( 'Date/Time', 'robotstxt-2fa' ); ?></th>
|
|
<th><?php esc_html_e( 'User', 'robotstxt-2fa' ); ?></th>
|
|
<th><?php esc_html_e( 'Method', 'robotstxt-2fa' ); ?></th>
|
|
<th><?php esc_html_e( 'IP', 'robotstxt-2fa' ); ?></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ( $recent_attempts as $attempt ) : ?>
|
|
<tr>
|
|
<td><?php echo esc_html( gmdate( 'Y-m-d H:i:s', $attempt['timestamp'] ) ); ?></td>
|
|
<td><?php echo esc_html( $attempt['user_login'] ); ?></td>
|
|
<td><?php echo esc_html( $attempt['method'] ); ?></td>
|
|
<td><?php echo esc_html( $attempt['ip'] ); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
|
|
<p style="margin-top:1.5em;">
|
|
<a href="<?php echo esc_url( admin_url( 'users.php' ) ); ?>"><?php esc_html_e( 'View all users →', 'robotstxt-2fa' ); ?></a>
|
|
</p>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Handle the CSV export admin-post action.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle_csv_export(): void {
|
|
check_admin_referer( 'robotstxt_2fa_export_users' );
|
|
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'robotstxt-2fa' ) );
|
|
}
|
|
|
|
$users = get_users(
|
|
array(
|
|
'number' => -1,
|
|
'fields' => 'all',
|
|
)
|
|
);
|
|
$rows = array();
|
|
|
|
foreach ( $users as $user ) {
|
|
if ( ! $user instanceof \WP_User ) {
|
|
continue;
|
|
}
|
|
|
|
$settings = $this->repository->get_user_settings( $user->ID );
|
|
$is_enabled = $settings['enabled'];
|
|
|
|
$required_methods = $this->config->get_required_methods_for_user( $user );
|
|
$is_forced = ! empty( $required_methods );
|
|
|
|
if ( $is_enabled ) {
|
|
$status = 'active';
|
|
} elseif ( $is_forced ) {
|
|
$status = 'required';
|
|
} else {
|
|
$status = 'optional';
|
|
}
|
|
|
|
$last_verified_ts = 0;
|
|
$raw_verifications = get_user_meta( $user->ID, 'robotstxt_2fa_last_verifications', true );
|
|
|
|
if ( is_array( $raw_verifications ) ) {
|
|
foreach ( $raw_verifications as $method_data ) {
|
|
if ( is_array( $method_data ) ) {
|
|
foreach ( $method_data as $context_data ) {
|
|
if ( is_array( $context_data ) && isset( $context_data['verified_at'] ) && is_int( $context_data['verified_at'] ) ) {
|
|
$last_verified_ts = max( $last_verified_ts, $context_data['verified_at'] );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$last_verified_str = $last_verified_ts > 0 ? (string) wp_date( 'Y-m-d H:i', $last_verified_ts ) : '';
|
|
|
|
$unused_codes = 0;
|
|
$raw_codes = get_user_meta( $user->ID, 'robotstxt_2fa_recovery_codes', true );
|
|
|
|
if ( is_array( $raw_codes ) && isset( $raw_codes['codes'] ) && is_array( $raw_codes['codes'] ) ) {
|
|
foreach ( $raw_codes['codes'] as $code_entry ) {
|
|
if ( is_array( $code_entry ) && empty( $code_entry['used_at'] ) ) {
|
|
++$unused_codes;
|
|
}
|
|
}
|
|
}
|
|
|
|
$rows[] = array(
|
|
'ID' => (string) $user->ID,
|
|
'user_login' => $user->user_login,
|
|
'user_email' => $user->user_email,
|
|
'roles' => implode( '|', $user->roles ),
|
|
'status' => $status,
|
|
'methods' => implode( '|', $settings['methods'] ),
|
|
'last_verified' => $last_verified_str,
|
|
'unused_codes' => (string) $unused_codes,
|
|
);
|
|
}
|
|
|
|
header( 'Content-Type: text/csv' );
|
|
header( 'Content-Disposition: attachment; filename="2fa-users-' . gmdate( 'Y-m-d' ) . '.csv"' );
|
|
|
|
$headers = array( 'ID', 'Login', 'Email', 'Roles', 'Status', 'Methods', 'Last Verified', 'Unused Codes' );
|
|
echo implode( ',', array_map( array( $this, 'csv_field' ), $headers ) ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV file download, RFC 4180 escaped.
|
|
|
|
foreach ( $rows as $row ) {
|
|
echo implode( ',', array_map( array( $this, 'csv_field' ), array_values( $row ) ) ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV file download, RFC 4180 escaped.
|
|
}
|
|
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Encode a single value as an RFC 4180-compliant CSV field.
|
|
*
|
|
* Fields containing commas, double-quotes, or line breaks are wrapped in
|
|
* double-quotes; any embedded double-quotes are doubled.
|
|
*
|
|
* @since 1.5.1
|
|
*
|
|
* @param string $value Raw field value.
|
|
*
|
|
* @return string
|
|
*/
|
|
private function csv_field( string $value ): string {
|
|
if ( str_contains( $value, ',' ) || str_contains( $value, '"' ) || str_contains( $value, "\n" ) || str_contains( $value, "\r" ) ) {
|
|
return '"' . str_replace( '"', '""', $value ) . '"';
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* Add a 2FA status column to the users list table.
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @param array<string, string> $columns Existing columns.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function add_users_column( array $columns ): array {
|
|
$columns['robotstxt_2fa'] = __( '2FA', 'robotstxt-2fa' );
|
|
|
|
return $columns;
|
|
}
|
|
|
|
/**
|
|
* Render the 2FA status cell in the users list table.
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @param mixed $output Cell output (typically empty string or existing content).
|
|
* @param string $column_name Current column identifier.
|
|
* @param int $user_id User ID for the current row.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function render_users_column( mixed $output, string $column_name, int $user_id ): string {
|
|
if ( 'robotstxt_2fa' !== $column_name ) {
|
|
return is_string( $output ) ? $output : '';
|
|
}
|
|
|
|
$raw_settings = get_user_meta( $user_id, 'robotstxt_2fa_user_settings', true );
|
|
$enabled = is_array( $raw_settings ) && ! empty( $raw_settings['enabled'] );
|
|
|
|
if ( $enabled ) {
|
|
return '<span style="color:#00a32a;">●</span> ' . esc_html__( 'Active', 'robotstxt-2fa' );
|
|
}
|
|
|
|
return '<span style="color:#999;">○</span> ' . esc_html__( 'Off', 'robotstxt-2fa' );
|
|
}
|
|
}
|