362 lines
10 KiB
PHP
362 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* WP_List_Table subclass for the 2FA audit dashboard.
|
|
*
|
|
* @package Robotstxt_2FA
|
|
*/
|
|
|
|
namespace Robotstxt\TwoFA\Admin;
|
|
|
|
use Robotstxt\TwoFA\User\Two_Factor_Config;
|
|
use Robotstxt\TwoFA\User\User_Settings_Repository;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
if ( ! class_exists( 'WP_List_Table' ) ) {
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
|
}
|
|
|
|
/**
|
|
* Displays all users with their 2FA status in a sortable, filterable table.
|
|
*
|
|
* @since 1.5.0
|
|
*/
|
|
class Users_List_Table extends \WP_List_Table {
|
|
/**
|
|
* 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;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param Two_Factor_Config $config Global two-factor configuration.
|
|
* @param User_Settings_Repository $repository User settings repository.
|
|
*/
|
|
public function __construct( Two_Factor_Config $config, User_Settings_Repository $repository ) {
|
|
parent::__construct(
|
|
array(
|
|
'singular' => 'user',
|
|
'plural' => 'users',
|
|
'ajax' => false,
|
|
)
|
|
);
|
|
|
|
$this->config = $config;
|
|
$this->repository = $repository;
|
|
}
|
|
|
|
/**
|
|
* Return the list of columns.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function get_columns(): array {
|
|
return array(
|
|
'cb' => '<input type="checkbox" />',
|
|
'user' => __( 'User', 'robotstxt-2fa' ),
|
|
'roles' => __( 'Role', 'robotstxt-2fa' ),
|
|
'status' => __( '2FA Status', 'robotstxt-2fa' ),
|
|
'methods' => __( 'Methods', 'robotstxt-2fa' ),
|
|
'last_verified' => __( 'Last verified', 'robotstxt-2fa' ),
|
|
'unused_codes' => __( 'Unused codes', 'robotstxt-2fa' ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Return the list of sortable columns.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @return array<string, array{string, bool}>
|
|
*/
|
|
public function get_sortable_columns(): array {
|
|
return array(
|
|
'user' => array( 'user_login', false ),
|
|
'last_verified' => array( 'last_verified', true ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Render the checkbox column.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @param mixed $item Current row data.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function column_cb( mixed $item ): string {
|
|
if ( ! is_array( $item ) ) {
|
|
return '';
|
|
}
|
|
|
|
$raw_id = $item['ID'] ?? 0;
|
|
$item_id = is_numeric( $raw_id ) ? (int) $raw_id : 0;
|
|
|
|
return sprintf(
|
|
'<input type="checkbox" name="users[]" value="%s" />',
|
|
absint( $item_id )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Render the user column with login, email, and edit link.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @param mixed $item Current row data.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function column_user( mixed $item ): string {
|
|
if ( ! is_array( $item ) ) {
|
|
return '';
|
|
}
|
|
|
|
$raw_id = $item['ID'] ?? 0;
|
|
$item_id = is_numeric( $raw_id ) ? (int) $raw_id : 0;
|
|
$edit_link = get_edit_user_link( $item_id );
|
|
$raw_login = $item['user_login'] ?? '';
|
|
$login = esc_html( is_string( $raw_login ) ? $raw_login : '' );
|
|
$raw_email = $item['user_email'] ?? '';
|
|
$email = esc_html( is_string( $raw_email ) ? $raw_email : '' );
|
|
|
|
$output = '<strong>';
|
|
|
|
if ( '' !== $edit_link ) {
|
|
$output .= '<a href="' . esc_url( $edit_link ) . '">' . $login . '</a>';
|
|
} else {
|
|
$output .= $login;
|
|
}
|
|
|
|
$output .= '</strong><br /><span class="description">' . $email . '</span>';
|
|
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* Render a default column value.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @param mixed $item Current row data.
|
|
* @param mixed $column_name Column identifier.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function column_default( mixed $item, mixed $column_name ): string {
|
|
if ( ! is_array( $item ) || ! is_string( $column_name ) ) {
|
|
return '';
|
|
}
|
|
|
|
$raw_value = $item[ $column_name ] ?? '';
|
|
|
|
return esc_html( is_string( $raw_value ) ? $raw_value : '' );
|
|
}
|
|
|
|
/**
|
|
* Return the available bulk actions.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function get_bulk_actions(): array {
|
|
return array(
|
|
'export_csv' => __( 'Export CSV', 'robotstxt-2fa' ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Prepare the items for display: query users, compute 2FA status, sort, paginate.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function prepare_items(): void {
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- GET display params only; no state changes.
|
|
$role = isset( $_REQUEST['role_filter'] ) && is_string( $_REQUEST['role_filter'] ) ? sanitize_key( $_REQUEST['role_filter'] ) : '';
|
|
$search = isset( $_REQUEST['s'] ) && is_string( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : '';
|
|
$status_filter = isset( $_REQUEST['status_filter'] ) && is_string( $_REQUEST['status_filter'] ) ? sanitize_key( $_REQUEST['status_filter'] ) : '';
|
|
$orderby = isset( $_REQUEST['orderby'] ) && is_string( $_REQUEST['orderby'] ) ? sanitize_key( $_REQUEST['orderby'] ) : 'user_login';
|
|
$raw_order = isset( $_REQUEST['order'] ) && is_string( $_REQUEST['order'] ) ? strtolower( sanitize_key( $_REQUEST['order'] ) ) : 'asc';
|
|
$order = in_array( $raw_order, array( 'asc', 'desc' ), true ) ? $raw_order : 'asc';
|
|
$paged = isset( $_REQUEST['paged'] ) && is_numeric( $_REQUEST['paged'] ) ? max( 1, (int) $_REQUEST['paged'] ) : 1;
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
|
|
|
$query_args = array(
|
|
'number' => -1,
|
|
'fields' => 'all',
|
|
);
|
|
|
|
if ( '' !== $role ) {
|
|
$query_args['role'] = $role;
|
|
}
|
|
|
|
if ( '' !== $search ) {
|
|
$query_args['search'] = '*' . $search . '*';
|
|
}
|
|
|
|
$users = get_users( $query_args );
|
|
$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';
|
|
}
|
|
|
|
// Compute last verified timestamp across all methods/contexts.
|
|
$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 ) : '—';
|
|
|
|
// Count unused recovery codes.
|
|
$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' => $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,
|
|
);
|
|
}
|
|
|
|
// Filter by status.
|
|
if ( '' !== $status_filter ) {
|
|
$rows = array_values(
|
|
array_filter(
|
|
$rows,
|
|
static function ( array $row ) use ( $status_filter ): bool {
|
|
return $row['status'] === $status_filter;
|
|
}
|
|
)
|
|
);
|
|
}
|
|
|
|
// Sort.
|
|
usort(
|
|
$rows,
|
|
static function ( array $a, array $b ) use ( $orderby, $order ): int {
|
|
$val_a = (string) ( $a[ $orderby ] ?? '' );
|
|
$val_b = (string) ( $b[ $orderby ] ?? '' );
|
|
$cmp = strcmp( $val_a, $val_b );
|
|
|
|
return 'desc' === $order ? -$cmp : $cmp;
|
|
}
|
|
);
|
|
|
|
$total_items = count( $rows );
|
|
$per_page = 20;
|
|
$offset = ( $paged - 1 ) * $per_page;
|
|
|
|
$this->items = array_slice( $rows, $offset, $per_page );
|
|
|
|
$this->set_pagination_args(
|
|
array(
|
|
'total_items' => $total_items,
|
|
'per_page' => $per_page,
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Render extra table navigation filters.
|
|
*
|
|
* @since 1.5.0
|
|
*
|
|
* @param mixed $which Which tablenav ('top' or 'bottom').
|
|
*
|
|
* @return void
|
|
*/
|
|
public function extra_tablenav( mixed $which ): void {
|
|
if ( 'top' !== $which ) {
|
|
return;
|
|
}
|
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- GET display params only; no state changes.
|
|
$current_role = isset( $_REQUEST['role_filter'] ) && is_string( $_REQUEST['role_filter'] ) ? sanitize_key( $_REQUEST['role_filter'] ) : '';
|
|
$current_status = isset( $_REQUEST['status_filter'] ) && is_string( $_REQUEST['status_filter'] ) ? sanitize_key( $_REQUEST['status_filter'] ) : '';
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
|
|
|
$roles = get_editable_roles();
|
|
?>
|
|
<div class="alignleft actions">
|
|
<select name="role_filter">
|
|
<option value=""><?php esc_html_e( '— All roles —', 'robotstxt-2fa' ); ?></option>
|
|
<?php
|
|
foreach ( $roles as $role_key => $role_info ) :
|
|
$role_name = isset( $role_info['name'] ) && is_string( $role_info['name'] )
|
|
? $role_info['name']
|
|
: $role_key;
|
|
?>
|
|
<option value="<?php echo esc_attr( $role_key ); ?>" <?php selected( $current_role, $role_key ); ?>>
|
|
<?php echo esc_html( translate_user_role( $role_name ) ); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<select name="status_filter">
|
|
<option value=""><?php esc_html_e( '— All statuses —', 'robotstxt-2fa' ); ?></option>
|
|
<option value="active" <?php selected( $current_status, 'active' ); ?>><?php esc_html_e( 'Active', 'robotstxt-2fa' ); ?></option>
|
|
<option value="required" <?php selected( $current_status, 'required' ); ?>><?php esc_html_e( 'Required (not set up)', 'robotstxt-2fa' ); ?></option>
|
|
<option value="optional" <?php selected( $current_status, 'optional' ); ?>><?php esc_html_e( 'Optional (disabled)', 'robotstxt-2fa' ); ?></option>
|
|
</select>
|
|
<?php submit_button( __( 'Filter', 'robotstxt-2fa' ), '', 'filter_action', false ); ?>
|
|
</div>
|
|
<?php
|
|
}
|
|
}
|