113 lines
2.7 KiB
PHP
113 lines
2.7 KiB
PHP
<?php
|
|
/**
|
|
* Resolve two-factor configuration from global settings.
|
|
*
|
|
* @package Robotstxt_2FA
|
|
*/
|
|
|
|
namespace Robotstxt\TwoFA\User;
|
|
|
|
use Robotstxt\TwoFA\Admin\Settings_Page;
|
|
use Robotstxt\TwoFA\Frequency_Options;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Exposes helpers to read plugin-level two-factor configuration.
|
|
*/
|
|
class Two_Factor_Config {
|
|
/**
|
|
* Retrieve global two-factor settings with multisite support.
|
|
*
|
|
* @return array{role_methods: array<string, array<int, string>>, frequency: string, force_frequency: bool, delete_on_uninstall: bool}
|
|
*/
|
|
public function get_settings(): array {
|
|
$settings_page = new Settings_Page();
|
|
return $settings_page->get_settings();
|
|
}
|
|
|
|
/**
|
|
* Retrieve the per-role method map.
|
|
*
|
|
* @return array<string, array<int, string>>
|
|
*/
|
|
public function get_role_methods(): array {
|
|
return $this->get_settings()['role_methods'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the list of roles that have at least one required method configured.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function get_forced_roles(): array {
|
|
return array_keys( $this->get_role_methods() );
|
|
}
|
|
|
|
/**
|
|
* Retrieve the required verification methods for a specific role.
|
|
*
|
|
* @param string $role Role slug.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function get_methods_for_role( string $role ): array {
|
|
$role_methods = $this->get_role_methods();
|
|
return $role_methods[ $role ] ?? array();
|
|
}
|
|
|
|
/**
|
|
* Retrieve the union of required methods across all of a user's roles.
|
|
*
|
|
* Returns an empty array when none of the user's roles have required methods.
|
|
*
|
|
* @param \WP_User $user User object currently being authenticated.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function get_required_methods_for_user( \WP_User $user ): array {
|
|
$role_methods = $this->get_role_methods();
|
|
$required = array();
|
|
|
|
foreach ( $user->roles as $role ) {
|
|
if ( isset( $role_methods[ $role ] ) ) {
|
|
foreach ( $role_methods[ $role ] as $method ) {
|
|
$required[] = $method;
|
|
}
|
|
}
|
|
}
|
|
|
|
return array_values( array_unique( $required ) );
|
|
}
|
|
|
|
/**
|
|
* Determine if two-factor authentication is enforced for the provided user.
|
|
*
|
|
* @param \WP_User $user User object currently being authenticated.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function is_two_factor_forced_for_user( \WP_User $user ): bool {
|
|
return ! empty( $this->get_required_methods_for_user( $user ) );
|
|
}
|
|
|
|
/**
|
|
* Retrieve the globally configured verification frequency.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_frequency(): string {
|
|
return $this->get_settings()['frequency'];
|
|
}
|
|
|
|
/**
|
|
* Determine whether the global frequency must be enforced for all users.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function is_frequency_forced(): bool {
|
|
return $this->get_settings()['force_frequency'];
|
|
}
|
|
}
|