v0.2.0
This commit is contained in:
commit
f936aeb807
1 changed files with 15710 additions and 0 deletions
130
includes/user/class-two-factor-config.php
Normal file
130
includes/user/class-two-factor-config.php
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<?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<string, mixed>
|
||||
*/
|
||||
public function get_settings(): array {
|
||||
$settings = array();
|
||||
|
||||
if ( is_multisite() ) {
|
||||
$network_settings = get_site_option( Settings_Page::OPTION_NAME, array() );
|
||||
|
||||
if ( is_array( $network_settings ) && ! empty( $network_settings ) ) {
|
||||
$settings = $network_settings;
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $settings ) ) {
|
||||
$site_settings = get_option( Settings_Page::OPTION_NAME, array() );
|
||||
|
||||
if ( is_array( $site_settings ) ) {
|
||||
$settings = $site_settings;
|
||||
}
|
||||
}
|
||||
|
||||
return wp_parse_args(
|
||||
$settings,
|
||||
array(
|
||||
'force_roles' => array(),
|
||||
'default_method' => 'email',
|
||||
'frequency' => Frequency_Options::FREQUENCY_SESSION,
|
||||
'force_frequency' => false,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the list of roles that must enable two-factor authentication.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function get_forced_roles(): array {
|
||||
$settings = $this->get_settings();
|
||||
|
||||
if ( empty( $settings['force_roles'] ) || ! is_array( $settings['force_roles'] ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return array_values(
|
||||
array_filter(
|
||||
array_map( 'sanitize_key', $settings['force_roles'] )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
$forced_roles = $this->get_forced_roles();
|
||||
|
||||
if ( empty( $forced_roles ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $user->roles as $role ) {
|
||||
if ( in_array( $role, $forced_roles, true ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the default two-factor authentication method.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_default_method(): string {
|
||||
$settings = $this->get_settings();
|
||||
|
||||
return sanitize_key( $settings['default_method'] ?? 'email' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the globally configured verification frequency.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_frequency(): string {
|
||||
$settings = $this->get_settings();
|
||||
|
||||
return Frequency_Options::sanitize( (string) ( $settings['frequency'] ?? Frequency_Options::FREQUENCY_SESSION ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the global frequency must be enforced for all users.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_frequency_forced(): bool {
|
||||
$settings = $this->get_settings();
|
||||
|
||||
return ! empty( $settings['force_frequency'] );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue