| 1 |
<?php |
| 2 |
/** |
| 3 |
* Authorizer |
| 4 |
* |
| 5 |
* @license GPL-2.0+ |
| 6 |
* @link https://github.com/uhm-coe/authorizer |
| 7 |
* @package authorizer |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Authorizer; |
| 11 |
|
| 12 |
use Authorizer\Helper; |
| 13 |
use Authorizer\Options; |
| 14 |
use Authorizer\Authentication; |
| 15 |
use Authorizer\Authorization; |
| 16 |
use Authorizer\Admin_Page; |
| 17 |
use Authorizer\Login_Form; |
| 18 |
use Authorizer\Updates; |
| 19 |
use Authorizer\Sync_Userdata; |
| 20 |
use Authorizer\Ajax_Endpoints; |
| 21 |
use Authorizer\Dashboard_Widget; |
| 22 |
|
| 23 |
/** |
| 24 |
* Main plugin class. Activates/deactivates the plugin, and registers all hooks. |
| 25 |
*/ |
| 26 |
class WP_Plugin_Authorizer extends Singleton { |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor. |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
// Installation and uninstallation hooks. |
| 33 |
register_activation_hook( 'authorizer/authorizer.php', array( $this, 'activate' ) ); |
| 34 |
register_deactivation_hook( 'authorizer/authorizer.php', array( $this, 'deactivate' ) ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Register hooks. |
| 38 |
*/ |
| 39 |
|
| 40 |
// Custom wp authentication routine using external service. |
| 41 |
add_filter( 'authenticate', array( Authentication::get_instance(), 'custom_authenticate' ), 1, 3 ); |
| 42 |
|
| 43 |
// Custom logout action using external service. |
| 44 |
add_action( 'clear_auth_cookie', array( Authentication::get_instance(), 'pre_logout' ) ); |
| 45 |
add_action( 'wp_logout', array( Authentication::get_instance(), 'custom_logout' ) ); |
| 46 |
|
| 47 |
// Create settings link on Plugins page. |
| 48 |
add_filter( 'plugin_action_links_' . plugin_basename( plugin_root() ), array( Admin_Page::get_instance(), 'plugin_settings_link' ) ); |
| 49 |
add_filter( 'network_admin_plugin_action_links_' . plugin_basename( plugin_root() ), array( Admin_Page::get_instance(), 'network_admin_plugin_settings_link' ) ); |
| 50 |
|
| 51 |
// Modify login page with a custom password url (if option is set). |
| 52 |
add_filter( 'lostpassword_url', array( Login_Form::get_instance(), 'custom_lostpassword_url' ) ); |
| 53 |
|
| 54 |
// Modify the log in URL (if applicable options are set). |
| 55 |
add_filter( 'login_url', array( Login_Form::get_instance(), 'maybe_add_external_wordpress_to_log_in_links' ) ); |
| 56 |
|
| 57 |
// If we have a custom login error, add the filter to show it. |
| 58 |
$error = get_option( 'auth_settings_advanced_login_error' ); |
| 59 |
if ( $error && strlen( $error ) > 0 ) { |
| 60 |
add_filter( 'login_errors', array( Login_Form::get_instance(), 'show_advanced_login_error' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
// Redirect to wp-login.php?redirect_to=? destination after an Azure login. |
| 64 |
add_filter( 'login_redirect', array( Options\External\OAuth2::get_instance(), 'maybe_redirect_after_azure_login' ), 10, 2 ); |
| 65 |
|
| 66 |
// Enable localization. Translation files stored in /languages. |
| 67 |
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); |
| 68 |
|
| 69 |
// Perform plugin updates if newer version installed. |
| 70 |
add_action( 'plugins_loaded', array( Updates::get_instance(), 'auth_update_check' ) ); |
| 71 |
|
| 72 |
// Update the user meta with this user's failed login attempt. |
| 73 |
add_action( 'wp_login_failed', array( Login_Form::get_instance(), 'update_login_failed_count' ) ); |
| 74 |
|
| 75 |
// Add users who successfully login to the approved list. |
| 76 |
add_action( 'wp_login', array( Sync_Userdata::get_instance(), 'ensure_wordpress_user_in_approved_list_on_login' ), 10, 2 ); |
| 77 |
|
| 78 |
// Create menu item in Settings. |
| 79 |
add_action( 'admin_menu', array( Admin_Page::get_instance(), 'add_plugin_page' ) ); |
| 80 |
|
| 81 |
// Create options page. |
| 82 |
add_action( 'admin_init', array( Admin_Page::get_instance(), 'page_init' ) ); |
| 83 |
|
| 84 |
// Update user role in approved list if it's changed via bulk action on the |
| 85 |
// WordPress list users page (and anywhere else WP_User::set_role() is called). |
| 86 |
add_action( 'set_user_role', array( Sync_Userdata::get_instance(), 'set_user_role_sync_role' ), 10, 3 ); |
| 87 |
|
| 88 |
// Update user role in approved list if it's changed in the WordPress edit user page. |
| 89 |
add_action( 'user_profile_update_errors', array( Sync_Userdata::get_instance(), 'edit_user_profile_update_role' ), 10, 3 ); |
| 90 |
|
| 91 |
// Update user email in approved list if it's changed in the WordPress edit user page. |
| 92 |
add_filter( 'send_email_change_email', array( Sync_Userdata::get_instance(), 'edit_user_profile_update_email' ), 10, 3 ); |
| 93 |
|
| 94 |
// Enqueue javascript and css on the plugin's options page, the |
| 95 |
// dashboard (for the widget), and the network admin. |
| 96 |
add_action( 'load-settings_page_authorizer', array( Admin_Page::get_instance(), 'load_options_page' ) ); |
| 97 |
add_action( 'load-toplevel_page_authorizer', array( Admin_Page::get_instance(), 'load_options_page' ) ); |
| 98 |
add_action( 'admin_head-index.php', array( Admin_Page::get_instance(), 'load_options_page' ) ); |
| 99 |
add_action( 'admin_head-index.php', array( Dashboard_Widget::get_instance(), 'widget_scripts' ) ); |
| 100 |
|
| 101 |
// Add custom css and js to wp-login.php. |
| 102 |
add_action( 'login_enqueue_scripts', array( Login_Form::get_instance(), 'login_enqueue_scripts_and_styles' ) ); |
| 103 |
add_action( 'login_footer', array( Login_Form::get_instance(), 'load_login_footer_js' ) ); |
| 104 |
|
| 105 |
// Modify login page with external auth links (if enabled; e.g., google or cas). |
| 106 |
add_action( 'login_form', array( Login_Form::get_instance(), 'login_form_add_external_service_links' ) ); |
| 107 |
|
| 108 |
// Redirect to CAS login when visiting login page (only if option is |
| 109 |
// enabled, CAS is the only service, and WordPress logins are hidden). |
| 110 |
// Note: hook into wp_login_errors filter so this fires after the |
| 111 |
// authenticate hook (where the redirect to CAS happens), but before html |
| 112 |
// output is started (so the redirect header doesn't complain about data |
| 113 |
// already being sent). |
| 114 |
add_filter( 'wp_login_errors', array( Login_Form::get_instance(), 'wp_login_errors__maybe_redirect_to_cas' ), 10, 2 ); |
| 115 |
|
| 116 |
// Verify current user has access to page they are visiting. |
| 117 |
add_action( 'parse_request', array( Authorization::get_instance(), 'restrict_access' ), 9 ); |
| 118 |
add_action( 'init', array( Sync_Userdata::get_instance(), 'init__maybe_add_network_approved_user' ) ); |
| 119 |
|
| 120 |
// Prevent REST API access if user isn't authenticated and "only logged in |
| 121 |
// users can see the site" is enabled. |
| 122 |
add_action( 'rest_authentication_errors', array( Authorization::get_instance(), 'restrict_rest_api' ), 10, 1 ); |
| 123 |
|
| 124 |
// AJAX: Save options from dashboard widget. |
| 125 |
add_action( 'wp_ajax_update_auth_user', array( Ajax_Endpoints::get_instance(), 'ajax_update_auth_user' ) ); |
| 126 |
|
| 127 |
// AJAX: Save options from multisite options page. |
| 128 |
add_action( 'wp_ajax_save_auth_multisite_settings', array( Ajax_Endpoints::get_instance(), 'ajax_save_auth_multisite_settings' ) ); |
| 129 |
|
| 130 |
// AJAX: Save usermeta from options page. |
| 131 |
add_action( 'wp_ajax_update_auth_usermeta', array( Ajax_Endpoints::get_instance(), 'ajax_update_auth_usermeta' ) ); |
| 132 |
|
| 133 |
// AJAX: Verify google login. |
| 134 |
add_action( 'wp_ajax_process_google_login', array( Ajax_Endpoints::get_instance(), 'ajax_process_google_login' ) ); |
| 135 |
add_action( 'wp_ajax_nopriv_process_google_login', array( Ajax_Endpoints::get_instance(), 'ajax_process_google_login' ) ); |
| 136 |
|
| 137 |
// AJAX: Refresh approved user list. |
| 138 |
add_action( 'wp_ajax_refresh_approved_user_list', array( Ajax_Endpoints::get_instance(), 'ajax_refresh_approved_user_list' ) ); |
| 139 |
|
| 140 |
// AJAX: Test LDAP user. |
| 141 |
add_action( 'wp_ajax_auth_settings_ldap_test_user', array( Ajax_Endpoints::get_instance(), 'ajax_auth_settings_ldap_test_user' ) ); |
| 142 |
|
| 143 |
// Add dashboard widget so instructors can add/edit users with access. |
| 144 |
// Hint: For Multisite Network Admin Dashboard use wp_network_dashboard_setup instead of wp_dashboard_setup. |
| 145 |
add_action( 'wp_dashboard_setup', array( Dashboard_Widget::get_instance(), 'add_dashboard_widgets' ) ); |
| 146 |
|
| 147 |
// If we have a custom admin message, add the action to show it. |
| 148 |
$notice = get_option( 'auth_settings_advanced_admin_notice' ); |
| 149 |
if ( $notice && strlen( $notice ) > 0 ) { |
| 150 |
add_action( 'admin_notices', array( Admin_Page::get_instance(), 'show_advanced_admin_notice' ) ); |
| 151 |
add_action( 'network_admin_notices', array( Admin_Page::get_instance(), 'show_advanced_admin_notice' ) ); |
| 152 |
} |
| 153 |
|
| 154 |
// Add [authorizer_login_form] shortcode to render the login form. |
| 155 |
add_shortcode( 'authorizer_login_form', array( Login_Form::get_instance(), 'shortcode_authorizer_login_form' ) ); |
| 156 |
|
| 157 |
// Load custom javascript for the main site (e.g., for displaying alerts). |
| 158 |
add_action( 'wp_enqueue_scripts', array( Login_Form::get_instance(), 'auth_public_scripts' ), 20 ); |
| 159 |
|
| 160 |
// Multisite-specific actions. |
| 161 |
if ( is_multisite() ) { |
| 162 |
// Add network admin options page (global settings for all sites). |
| 163 |
add_action( 'network_admin_menu', array( Admin_Page::get_instance(), 'network_admin_menu' ) ); |
| 164 |
} |
| 165 |
|
| 166 |
// Remove user from authorizer lists when that user is deleted in WordPress. |
| 167 |
add_action( 'delete_user', array( Sync_Userdata::get_instance(), 'remove_user_from_authorizer_when_deleted' ) ); |
| 168 |
if ( is_multisite() ) { |
| 169 |
// Remove multisite user from authorizer lists when that user is deleted from Network Users. |
| 170 |
add_action( 'remove_user_from_blog', array( Sync_Userdata::get_instance(), 'remove_network_user_from_site_when_removed' ), 10, 2 ); |
| 171 |
add_action( 'wpmu_delete_user', array( Sync_Userdata::get_instance(), 'remove_network_user_from_authorizer_when_deleted' ) ); |
| 172 |
} |
| 173 |
|
| 174 |
// Add user to authorizer approved list when that user is added to a blog from the Users screen. |
| 175 |
// Multisite: invite_user action fired when adding (inviting) an existing network user to the current site (with email confirmation). |
| 176 |
add_action( 'invite_user', array( Sync_Userdata::get_instance(), 'add_existing_user_to_authorizer_when_created' ), 10, 3 ); |
| 177 |
// Multisite: added_existing_user action fired when adding an existing network user to the current site (without email confirmation). |
| 178 |
add_action( 'added_existing_user', array( Sync_Userdata::get_instance(), 'add_existing_user_to_authorizer_when_created_noconfirmation' ), 10, 2 ); |
| 179 |
// Multisite: after_signup_user action fired when adding a new user to the site (with or without email confirmation). |
| 180 |
add_action( 'after_signup_user', array( Sync_Userdata::get_instance(), 'add_new_user_to_authorizer_when_created' ), 10, 4 ); |
| 181 |
// Single site: edit_user_created_user action fired when adding a new user to the site (with or without email notification). |
| 182 |
add_action( 'edit_user_created_user', array( Sync_Userdata::get_instance(), 'add_new_user_to_authorizer_when_created_single_site' ), 10, 2 ); |
| 183 |
|
| 184 |
// Add user to network approved users (and remove from individual sites) |
| 185 |
// when user is elevated to super admin status. |
| 186 |
add_action( 'grant_super_admin', array( Sync_Userdata::get_instance(), 'grant_super_admin__add_to_network_approved' ) ); |
| 187 |
// Remove user from network approved users (and add them to the approved |
| 188 |
// list on sites they are already on) when super admin status is removed. |
| 189 |
add_action( 'revoke_super_admin', array( Sync_Userdata::get_instance(), 'revoke_super_admin__remove_from_network_approved' ) ); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Plugin activation hook. |
| 196 |
* Will also activate the plugin for all sites/blogs if this is a "Network enable." |
| 197 |
* |
| 198 |
* @param bool $network_wide Whether the plugin is being activated for the whole network. |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function activate( $network_wide ) { |
| 202 |
global $wpdb; |
| 203 |
$options = Options::get_instance(); |
| 204 |
$sync_userdata = Sync_Userdata::get_instance(); |
| 205 |
|
| 206 |
// If we're in a multisite environment, run the plugin activation for each |
| 207 |
// site when network enabling. |
| 208 |
// Note: wp-cli does not use nonces, so we skip the nonce check here to |
| 209 |
// allow the "wp plugin activate authorizer" command. |
| 210 |
// phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification |
| 211 |
if ( is_multisite() && $network_wide ) { |
| 212 |
|
| 213 |
// Add super admins to the multisite approved list. |
| 214 |
$auth_multisite_settings_access_users_approved = get_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', array() ); |
| 215 |
$should_update_auth_multisite_settings_access_users_approved = false; |
| 216 |
foreach ( get_super_admins() as $super_admin ) { |
| 217 |
$user = get_user_by( 'login', $super_admin ); |
| 218 |
// Add to approved list if not there. |
| 219 |
if ( ! Helper::in_multi_array( $user->user_email, $auth_multisite_settings_access_users_approved ) ) { |
| 220 |
$approved_user = array( |
| 221 |
'email' => Helper::lowercase( $user->user_email ), |
| 222 |
'role' => count( $user->roles ) > 0 ? $user->roles[0] : 'administrator', |
| 223 |
'date_added' => wp_date( 'M Y', strtotime( $user->user_registered ) ), |
| 224 |
'local_user' => true, |
| 225 |
); |
| 226 |
array_push( $auth_multisite_settings_access_users_approved, $approved_user ); |
| 227 |
$should_update_auth_multisite_settings_access_users_approved = true; |
| 228 |
} |
| 229 |
} |
| 230 |
if ( $should_update_auth_multisite_settings_access_users_approved ) { |
| 231 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved ); |
| 232 |
} |
| 233 |
|
| 234 |
// Run plugin activation on each site in the network. |
| 235 |
$current_blog_id = $wpdb->blogid; |
| 236 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 237 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 238 |
foreach ( $sites as $site ) { |
| 239 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 240 |
switch_to_blog( $blog_id ); |
| 241 |
// Set default plugin options and add current users to approved list. |
| 242 |
$options->set_default_options(); |
| 243 |
$sync_userdata->add_wp_users_to_approved_list(); |
| 244 |
} |
| 245 |
switch_to_blog( $current_blog_id ); |
| 246 |
|
| 247 |
} else { |
| 248 |
// Set default plugin options and add current users to approved list. |
| 249 |
$options->set_default_options(); |
| 250 |
$sync_userdata->add_wp_users_to_approved_list(); |
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
/** |
| 257 |
* Plugin deactivation. |
| 258 |
* |
| 259 |
* @return void |
| 260 |
*/ |
| 261 |
public function deactivate() { |
| 262 |
// Do nothing. Use uninstall.php instead. |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
/** |
| 267 |
* Load translated strings from *.mo files in /languages. |
| 268 |
* |
| 269 |
* Action: plugins_loaded |
| 270 |
*/ |
| 271 |
public function load_textdomain() { |
| 272 |
load_plugin_textdomain( |
| 273 |
'authorizer', |
| 274 |
false, |
| 275 |
basename( dirname( plugin_root() ) ) . '/languages' |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|