v1.0.1
This commit is contained in:
parent
212d64abfa
commit
b8e17df2db
17 changed files with 1145 additions and 732 deletions
|
|
@ -25,9 +25,9 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $args Optional arguments (role, blog_id).
|
||||
* @param array<string, mixed> $args Optional arguments (role, blog_id).
|
||||
*
|
||||
* @return array Compliance statistics.
|
||||
* @return array{total_users: int, compliant_users: int, non_compliant: int, grace_period: int, no_requirements: int, by_role: array<string, array{total: int, compliant: int, non_compliant: int}>} Compliance statistics.
|
||||
*/
|
||||
public function get_compliance_stats( array $args = array() ): array {
|
||||
$defaults = array(
|
||||
|
|
@ -63,6 +63,10 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
|
||||
$enforcement = two_factor_extended()->get_enforcement();
|
||||
|
||||
if ( null === $enforcement ) {
|
||||
return $stats;
|
||||
}
|
||||
|
||||
foreach ( $users as $user ) {
|
||||
$required = $enforcement->get_required_providers_for_user( $user->ID );
|
||||
|
||||
|
|
@ -72,7 +76,7 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
}
|
||||
|
||||
$compliant = $enforcement->user_meets_requirements( $user->ID, $required );
|
||||
$in_grace = $enforcement->is_in_grace_period( $user->ID );
|
||||
$in_grace = $enforcement->is_in_grace_period( $user->ID );
|
||||
|
||||
if ( $compliant ) {
|
||||
$stats['compliant_users']++;
|
||||
|
|
@ -112,9 +116,9 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $args Optional arguments.
|
||||
* @param array<string, mixed> $args Optional arguments.
|
||||
*
|
||||
* @return array Array of non-compliant user data.
|
||||
* @return array<int, array<string, mixed>> Array of non-compliant user data.
|
||||
*/
|
||||
public function get_non_compliant_users( array $args = array() ): array {
|
||||
$defaults = array(
|
||||
|
|
@ -136,9 +140,13 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
$user_args['blog_id'] = $args['blog_id'];
|
||||
}
|
||||
|
||||
$users = get_users( $user_args );
|
||||
$users = get_users( $user_args );
|
||||
$non_compliant = array();
|
||||
$enforcement = two_factor_extended()->get_enforcement();
|
||||
$enforcement = two_factor_extended()->get_enforcement();
|
||||
|
||||
if ( null === $enforcement ) {
|
||||
return $non_compliant;
|
||||
}
|
||||
|
||||
foreach ( $users as $user ) {
|
||||
$required = $enforcement->get_required_providers_for_user( $user->ID );
|
||||
|
|
@ -150,24 +158,23 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
$compliant = $enforcement->user_meets_requirements( $user->ID, $required );
|
||||
|
||||
if ( ! $compliant ) {
|
||||
$enabled = Two_Factor_Extended_Provider_Detector::get_user_enabled_providers( $user->ID );
|
||||
$missing = array_diff( $required, array_keys( $enabled ) );
|
||||
|
||||
$enabled = Two_Factor_Extended_Provider_Detector::get_user_enabled_providers( $user->ID );
|
||||
$missing = array_diff( $required, array_keys( $enabled ) );
|
||||
$provider_names = Two_Factor_Extended_Provider_Detector::get_provider_names();
|
||||
$missing_names = array();
|
||||
$missing_names = array();
|
||||
|
||||
foreach ( $missing as $class ) {
|
||||
$missing_names[] = $provider_names[ $class ] ?? $class;
|
||||
}
|
||||
|
||||
$non_compliant[] = array(
|
||||
'user_id' => $user->ID,
|
||||
'user_login' => $user->user_login,
|
||||
'user_email' => $user->user_email,
|
||||
'roles' => Two_Factor_Extended_Role_Manager::get_user_roles( $user->ID ),
|
||||
'user_id' => $user->ID,
|
||||
'user_login' => $user->user_login,
|
||||
'user_email' => $user->user_email,
|
||||
'roles' => Two_Factor_Extended_Role_Manager::get_user_roles( $user->ID ),
|
||||
'missing_providers' => $missing_names,
|
||||
'in_grace_period' => $enforcement->is_in_grace_period( $user->ID ),
|
||||
'grace_remaining' => $enforcement->get_grace_period_remaining_days( $user->ID ),
|
||||
'in_grace_period' => $enforcement->is_in_grace_period( $user->ID ),
|
||||
'grace_remaining' => $enforcement->get_grace_period_remaining_days( $user->ID ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -180,7 +187,7 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return array Network compliance report.
|
||||
* @return array<string, mixed> Network compliance report.
|
||||
*/
|
||||
public function get_network_report(): array {
|
||||
if ( ! is_multisite() ) {
|
||||
|
|
@ -197,7 +204,7 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
);
|
||||
|
||||
foreach ( $sites as $site ) {
|
||||
switch_to_blog( $site->blog_id );
|
||||
switch_to_blog( (int) $site->blog_id );
|
||||
|
||||
$site_stats = $this->get_compliance_stats( array( 'blog_id' => $site->blog_id ) );
|
||||
|
||||
|
|
@ -224,7 +231,7 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $args Optional arguments.
|
||||
* @param array<string, mixed> $args Optional arguments.
|
||||
*
|
||||
* @return string CSV content.
|
||||
*/
|
||||
|
|
@ -235,27 +242,41 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
$csv[] = array( 'User ID', 'Username', 'Email', 'Roles', 'Missing Providers', 'Grace Period', 'Days Remaining' );
|
||||
|
||||
foreach ( $non_compliant 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' );
|
||||
$user_id_val = isset( $user_data['user_id'] ) && is_int( $user_data['user_id'] ) ? (string) $user_data['user_id'] : '';
|
||||
$user_login_val = isset( $user_data['user_login'] ) && is_string( $user_data['user_login'] ) ? $user_data['user_login'] : '';
|
||||
$user_email_val = isset( $user_data['user_email'] ) && is_string( $user_data['user_email'] ) ? $user_data['user_email'] : '';
|
||||
$grace_remaining_val = isset( $user_data['grace_remaining'] ) && is_scalar( $user_data['grace_remaining'] ) ? (string) $user_data['grace_remaining'] : '';
|
||||
$in_grace_val = ! empty( $user_data['in_grace_period'] ) ? 'Yes' : 'No';
|
||||
|
||||
$csv[] = array(
|
||||
$user_data['user_id'],
|
||||
$user_data['user_login'],
|
||||
$user_data['user_email'],
|
||||
implode( ', ', $user_data['roles'] ),
|
||||
implode( ', ', $user_data['missing_providers'] ),
|
||||
$user_data['in_grace_period'] ? 'Yes' : 'No',
|
||||
$user_data['grace_remaining'],
|
||||
$user_id_val,
|
||||
$user_login_val,
|
||||
$user_email_val,
|
||||
implode( ', ', $roles ),
|
||||
implode( ', ', $missing_providers ),
|
||||
$in_grace_val,
|
||||
$grace_remaining_val,
|
||||
);
|
||||
}
|
||||
|
||||
ob_start();
|
||||
$handle = fopen( 'php://output', 'w' );
|
||||
|
||||
foreach ( $csv as $row ) {
|
||||
fputcsv( $handle, $row );
|
||||
if ( false !== $handle ) {
|
||||
foreach ( $csv as $row ) {
|
||||
fputcsv( $handle, $row );
|
||||
}
|
||||
|
||||
fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Simple CSV export
|
||||
}
|
||||
|
||||
fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Simple CSV export
|
||||
$output = ob_get_clean();
|
||||
|
||||
return ob_get_clean();
|
||||
return false !== $output ? $output : '';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -263,8 +284,8 @@ class Two_Factor_Extended_Compliance_Report {
|
|||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $to Recipient email address.
|
||||
* @param array $args Optional arguments for report.
|
||||
* @param string $to Recipient email address.
|
||||
* @param array<string, mixed> $args Optional arguments for report.
|
||||
*
|
||||
* @return bool True on success, false on failure.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue