This commit is contained in:
Javier Casares 2026-03-28 09:59:03 +00:00
commit b8e17df2db
17 changed files with 1145 additions and 732 deletions

View file

@ -39,15 +39,21 @@ class Two_Factor_Extended_CLI_Commands {
*
* @since 0.1.0
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
* @param array<int, string> $args Positional arguments.
* @param array<string, string> $assoc_args Associative arguments.
*/
public function status( array $args, array $assoc_args ): void {
$role = $assoc_args['role'] ?? '';
$format = $assoc_args['format'] ?? 'table';
$compliance = two_factor_extended()->get_compliance_report();
$stats = $compliance->get_compliance_stats( array( 'role' => $role ) );
if ( null === $compliance ) {
WP_CLI::error( 'Compliance report module is not available.' );
return;
}
$stats = $compliance->get_compliance_stats( array( 'role' => $role ) );
if ( 'json' === $format ) {
WP_CLI::line( wp_json_encode( $stats, JSON_PRETTY_PRINT ) );
@ -129,8 +135,8 @@ class Two_Factor_Extended_CLI_Commands {
*
* @since 0.1.0
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
* @param array<int, string> $args Positional arguments.
* @param array<string, string> $assoc_args Associative arguments.
*/
public function enforce( array $args, array $assoc_args ): void {
if ( empty( $assoc_args['role'] ) ) {
@ -150,7 +156,13 @@ class Two_Factor_Extended_CLI_Commands {
}
$enforcement = two_factor_extended()->get_enforcement();
$processed = 0;
if ( null === $enforcement ) {
WP_CLI::error( 'Enforcement module is not available.' );
return;
}
$processed = 0;
$progress = WP_CLI\Utils\make_progress_bar( 'Enforcing 2FA requirements', count( $users ) );
@ -170,15 +182,19 @@ class Two_Factor_Extended_CLI_Commands {
}
// Log the action.
two_factor_extended()->get_audit_log()->log_event(
'cli_enforce',
sprintf( 'WP-CLI: Enforced 2FA for user %s', $user->user_login ),
$user->ID,
array(
'role' => $role,
'required_providers' => $required,
)
);
$audit_log = two_factor_extended()->get_audit_log();
if ( null !== $audit_log ) {
$audit_log->log_event(
'cli_enforce',
sprintf( 'WP-CLI: Enforced 2FA for user %s', $user->user_login ),
$user->ID,
array(
'role' => $role,
'required_providers' => $required,
)
);
}
$processed++;
$progress->tick();
@ -217,16 +233,21 @@ class Two_Factor_Extended_CLI_Commands {
*
* @since 0.1.0
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
* @param array<int, string> $args Positional arguments.
* @param array<string, string> $assoc_args Associative arguments.
*/
public function report( array $args, array $assoc_args ): void {
$role = $assoc_args['role'] ?? '';
$format = $assoc_args['format'] ?? 'table';
$role = $assoc_args['role'] ?? '';
$format = $assoc_args['format'] ?? 'table';
$non_compliant_only = isset( $assoc_args['non-compliant-only'] );
$compliance = two_factor_extended()->get_compliance_report();
if ( null === $compliance ) {
WP_CLI::error( 'Compliance report module is not available.' );
return;
}
if ( $non_compliant_only ) {
$users = $compliance->get_non_compliant_users( array( 'role' => $role ) );
@ -237,12 +258,18 @@ class Two_Factor_Extended_CLI_Commands {
$items = array();
foreach ( $users as $user_data ) {
$roles_raw = isset( $user_data['roles'] ) && is_array( $user_data['roles'] ) ? $user_data['roles'] : array();
$missing_providers_raw = isset( $user_data['missing_providers'] ) && is_array( $user_data['missing_providers'] ) ? $user_data['missing_providers'] : array();
$roles = array_filter( $roles_raw, 'is_string' );
$missing_providers = array_filter( $missing_providers_raw, 'is_string' );
$items[] = array(
'User ID' => $user_data['user_id'],
'Username' => $user_data['user_login'],
'Email' => $user_data['user_email'],
'Roles' => implode( ', ', $user_data['roles'] ),
'Missing Providers' => implode( ', ', $user_data['missing_providers'] ),
'Roles' => implode( ', ', $roles ),
'Missing Providers' => implode( ', ', $missing_providers ),
'Grace Period' => $user_data['in_grace_period'] ? 'Yes' : 'No',
'Days Remaining' => $user_data['grace_remaining'],
);
@ -275,8 +302,8 @@ class Two_Factor_Extended_CLI_Commands {
*
* @since 0.1.0
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
* @param array<int, string> $args Positional arguments.
* @param array<string, string> $assoc_args Associative arguments.
*/
public function reset( array $args, array $assoc_args ): void {
if ( empty( $assoc_args['user'] ) ) {
@ -288,7 +315,7 @@ class Two_Factor_Extended_CLI_Commands {
$user_identifier = $assoc_args['user'];
if ( is_numeric( $user_identifier ) ) {
$user = get_userdata( $user_identifier );
$user = get_userdata( (int) $user_identifier );
} elseif ( is_email( $user_identifier ) ) {
$user = get_user_by( 'email', $user_identifier );
} else {
@ -307,11 +334,15 @@ class Two_Factor_Extended_CLI_Commands {
delete_user_meta( $user->ID, Two_Factor_Extended_Enforcement::META_GRACE_NOTIFIED );
// Log the action.
two_factor_extended()->get_audit_log()->log_event(
'cli_reset_grace',
sprintf( 'WP-CLI: Reset grace period for user %s', $user->user_login ),
$user->ID
);
$audit_log = two_factor_extended()->get_audit_log();
if ( null !== $audit_log ) {
$audit_log->log_event(
'cli_reset_grace',
sprintf( 'WP-CLI: Reset grace period for user %s', $user->user_login ),
$user->ID
);
}
WP_CLI::success(
sprintf(