| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access.'); |
| 4 |
|
| 5 |
/** |
| 6 |
* This class contains code for integrating within the WP admin dashboard |
| 7 |
*/ |
| 8 |
|
| 9 |
class Simba_TFA_Admin_Dashboard_Integration { |
| 10 |
|
| 11 |
// @var Simba_Two_Factor_Authentication |
| 12 |
// Main class |
| 13 |
private $tfa; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class constructor |
| 17 |
* |
| 18 |
* @param Simba_Two_Factor_Authentication |
| 19 |
*/ |
| 20 |
public function __construct($tfa) { |
| 21 |
$this->tfa = $tfa; |
| 22 |
|
| 23 |
if (!is_admin()) return; |
| 24 |
|
| 25 |
// Add to Settings menu on sites |
| 26 |
add_action('admin_menu', array($this, 'menu_entry_for_admin')); |
| 27 |
|
| 28 |
// Add settings link in plugin list |
| 29 |
$plugin = plugin_basename(SIMBA_TFA_PLUGIN_FILE); |
| 30 |
add_filter("plugin_action_links_".$plugin, array($this, 'add_plugin_settings_link')); |
| 31 |
add_filter('network_admin_plugin_action_links_'.$plugin, array($this, 'add_plugin_settings_link')); |
| 32 |
|
| 33 |
// Entry that everybody gets |
| 34 |
add_action('network_admin_menu', array($this, 'admin_menu')); |
| 35 |
add_action('admin_menu', array($this, 'admin_menu')); |
| 36 |
|
| 37 |
// Add TFA column on users list |
| 38 |
add_action('manage_users_columns', array($this, 'manage_users_columns_tfa')); |
| 39 |
add_action('wpmu_users_columns', array($this, 'manage_users_columns_tfa')); |
| 40 |
add_action('manage_users_custom_column', array($this, 'manage_users_custom_column_tfa'), 10, 3); |
| 41 |
|
| 42 |
// Needed users.php CSS. |
| 43 |
add_action('admin_print_styles-users.php', array($this, 'load_users_css'), 10, 0); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Enqueue CSS styling on the users page |
| 48 |
*/ |
| 49 |
public function load_users_css() { |
| 50 |
wp_enqueue_style( |
| 51 |
'tfa-users-css', |
| 52 |
SIMBA_TFA_PLUGIN_URL.'/css/users.css', |
| 53 |
array(), |
| 54 |
$this->tfa->version, |
| 55 |
'screen' |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Runs upon the WP filters plugin_action_links_(plugin) and network_plugin_action_links_(plugin) |
| 61 |
* |
| 62 |
* @param Array $links |
| 63 |
* |
| 64 |
* @return Array |
| 65 |
*/ |
| 66 |
public function add_plugin_settings_link($links) { |
| 67 |
if (!is_network_admin()) { |
| 68 |
$link = '<a href="options-general.php?page=two-factor-auth">'.__('Plugin settings', 'two-factor-authentication').'</a>'; |
| 69 |
array_unshift($links, $link); |
| 70 |
} else { |
| 71 |
switch_to_blog(1); |
| 72 |
$link = '<a href="'.admin_url('options-general.php').'?page=two-factor-auth">'.__('Plugin settings', 'two-factor-authentication').'</a>'; |
| 73 |
restore_current_blog(); |
| 74 |
array_unshift($links, $link); |
| 75 |
} |
| 76 |
|
| 77 |
$link2 = '<a href="admin.php?page=two-factor-auth-user">'.__('User settings', 'two-factor-authentication').'</a>'; |
| 78 |
array_unshift($links, $link2); |
| 79 |
|
| 80 |
return $links; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Runs upon the WP action admin_menu |
| 85 |
*/ |
| 86 |
public function admin_menu() { |
| 87 |
$this->tfa->get_totp_controller()->potentially_port_private_keys(); |
| 88 |
|
| 89 |
global $current_user; |
| 90 |
if(!$this->tfa->is_activated_for_user($current_user->ID)) return; |
| 91 |
|
| 92 |
add_menu_page(__('Two Factor Authentication', 'two-factor-authentication'), __('Two Factor Auth', 'two-factor-authentication'), 'read', 'two-factor-auth-user', array($this, 'show_user_settings_page'), SIMBA_TFA_PLUGIN_URL.'/img/tfa_admin_icon_16x16.png', 72); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Include the user settings page code |
| 97 |
*/ |
| 98 |
public function show_user_settings_page() { |
| 99 |
|
| 100 |
global $current_user; |
| 101 |
|
| 102 |
$totp_controller = $this->tfa->get_totp_controller(); |
| 103 |
|
| 104 |
if (!empty($_REQUEST['_tfa_activate_nonce']) && !empty($_POST['tfa_enable_tfa']) && wp_verify_nonce($_REQUEST['_tfa_activate_nonce'], 'tfa_activate') && !empty($_GET['settings-updated'])) { |
| 105 |
$this->tfa->change_tfa_enabled_status($current_user->ID, $_POST['tfa_enable_tfa']); |
| 106 |
$tfa_settings_saved = true; |
| 107 |
} |
| 108 |
|
| 109 |
// TODO: This should go in the TOTP class |
| 110 |
if (!empty($_REQUEST['_tfa_algorithm_nonce']) && !empty($_POST['tfa_algorithm_type']) && !empty($_GET['settings-updated']) && wp_verify_nonce($_REQUEST['_tfa_algorithm_nonce'], 'tfa_algorithm')) { |
| 111 |
|
| 112 |
$old_algorithm = $totp_controller->get_user_otp_algorithm($current_user->ID); |
| 113 |
|
| 114 |
if ($old_algorithm != $_POST['tfa_algorithm_type']) { |
| 115 |
$totp_controller->changeUserAlgorithmTo($current_user->ID, $_POST['tfa_algorithm_type']); |
| 116 |
} |
| 117 |
|
| 118 |
$tfa_settings_saved = true; |
| 119 |
} |
| 120 |
|
| 121 |
if (!empty($_GET['warning_button_clicked']) && !empty($_REQUEST['resyncnonce']) && wp_verify_nonce($_REQUEST['resyncnonce'], 'tfaresync')) { |
| 122 |
delete_user_meta($current_user->ID, 'tfa_hotp_off_sync'); |
| 123 |
} |
| 124 |
|
| 125 |
include SIMBA_TFA_PLUGIN_DIR.'/includes/user_settings.php'; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Runs upon the WP action admin_menu |
| 130 |
*/ |
| 131 |
public function menu_entry_for_admin() { |
| 132 |
|
| 133 |
if (is_multisite() && (!is_super_admin() || !is_main_site())) return; |
| 134 |
|
| 135 |
add_action('admin_init', array($this, 'register_two_factor_auth_settings')); |
| 136 |
|
| 137 |
add_options_page( |
| 138 |
__('Two Factor Authentication', 'two-factor-authentication'), |
| 139 |
__('Two Factor Authentication', 'two-factor-authentication'), |
| 140 |
$this->tfa->get_management_capability(), |
| 141 |
'two-factor-auth', |
| 142 |
array($this, 'show_admin_settings_page') |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Include the admin settings page code |
| 148 |
*/ |
| 149 |
public function show_admin_settings_page() { |
| 150 |
$simba_two_factor_authentication = $this->tfa; |
| 151 |
$totp_controller = $this->tfa->get_totp_controller(); |
| 152 |
$totp_controller->setUserHMACTypes(); |
| 153 |
if (!is_admin() || !current_user_can($this->tfa->get_management_capability())) return; |
| 154 |
require_once(SIMBA_TFA_PLUGIN_DIR.'/includes/admin_settings.php'); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Add the 2FA label to the users list table header. |
| 159 |
* |
| 160 |
* @param Array $columns Table columns. |
| 161 |
* |
| 162 |
* @return Array |
| 163 |
*/ |
| 164 |
public function manage_users_columns_tfa($columns = array()) { |
| 165 |
$columns['tfa-status'] = __('2FA', 'two-factor-authentication'); |
| 166 |
return $columns; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Add status into TFA column. |
| 171 |
* |
| 172 |
* @param String $value String. |
| 173 |
* @param String $column_name Column name. |
| 174 |
* @param Integer $user_id User ID. |
| 175 |
* |
| 176 |
* @return String |
| 177 |
*/ |
| 178 |
public function manage_users_custom_column_tfa($value = '', $column_name = '', $user_id = 0) { |
| 179 |
|
| 180 |
// Only for this column name. |
| 181 |
if ('tfa-status' === $column_name) { |
| 182 |
|
| 183 |
if (!$this->tfa->is_activated_for_user($user_id)) { |
| 184 |
$value = '—'; |
| 185 |
} elseif ($this->tfa->is_activated_by_user($user_id)) { |
| 186 |
// Use value. |
| 187 |
$value = '<span title="' . __( 'Enabled', 'two-factor-authentication' ) . '" class="dashicons dashicons-yes"></span>'; |
| 188 |
} else { |
| 189 |
// No group. |
| 190 |
$value = '<span title="' . __( 'Disabled', 'two-factor-authentication' ) . '" class="dashicons dashicons-no"></span>'; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return $value; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Runs upon the WP action admin_init |
| 199 |
*/ |
| 200 |
public function register_two_factor_auth_settings() { |
| 201 |
global $wp_roles; |
| 202 |
if (!isset($wp_roles)) $wp_roles = new WP_Roles(); |
| 203 |
|
| 204 |
foreach ($wp_roles->role_names as $id => $name) { |
| 205 |
register_setting('tfa_user_roles_group', 'tfa_'.$id); |
| 206 |
register_setting('tfa_user_roles_trusted_group', 'tfa_trusted_'.$id); |
| 207 |
register_setting('tfa_user_roles_required_group', 'tfa_required_'.$id); |
| 208 |
} |
| 209 |
|
| 210 |
if (is_multisite()) { |
| 211 |
register_setting('tfa_user_roles_group', 'tfa__super_admin'); |
| 212 |
register_setting('tfa_user_roles_trusted_group', 'tfa_trusted__super_admin'); |
| 213 |
register_setting('tfa_user_roles_required_group', 'tfa_required__super_admin'); |
| 214 |
} |
| 215 |
|
| 216 |
register_setting('tfa_user_roles_required_group', 'tfa_requireafter'); |
| 217 |
register_setting('tfa_user_roles_required_group', 'tfa_if_required_redirect_to'); |
| 218 |
register_setting('tfa_user_roles_required_group', 'tfa_hide_turn_off'); |
| 219 |
register_setting('tfa_user_roles_trusted_group', 'tfa_trusted_for'); |
| 220 |
register_setting('simba_tfa_woocommerce_group', 'tfa_wc_add_section'); |
| 221 |
register_setting('simba_tfa_woocommerce_group', 'tfa_bot_protection'); |
| 222 |
register_setting('simba_tfa_default_hmac_group', 'tfa_default_hmac'); |
| 223 |
register_setting('tfa_xmlrpc_status_group', 'tfa_xmlrpc_on'); |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|