From f643cefa4498ec6ecd76479db6c7de22868e4309 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 17 Oct 2017 09:27:45 +0400 Subject: Initial commit --- auth.php | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 auth.php (limited to 'auth.php') diff --git a/auth.php b/auth.php new file mode 100644 index 0000000..d41a414 --- /dev/null +++ b/auth.php @@ -0,0 +1,167 @@ +. + +/** + * Authorization by direct link. + * + * @package auth_link + * @copyright 2017 Valentin Popov (https://valentineus.link/) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir.'/authlib.php'); + +/** + * Plugin for no authentication. + */ +class auth_plugin_link extends auth_plugin_base { + + /** + * Constructor. + */ + public function __construct() { + $this->authtype = 'link'; + } + + /** + * Old syntax of class constructor. Deprecated in PHP7. + * @deprecated since Moodle 3.1 + */ + public function auth_plugin_link() { + debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); + self::__construct(); + } + + /** + * Returns true if the username and password work or don't exist and false + * if the user exists and the password is wrong. + * @param string $username The username + * @param string $password The password + * @return bool Authentication success or failure. + */ + function user_login($username, $password) { + global $CFG, $DB; + + if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) { + return validate_internal_user_password($user, $password); + } + + return true; + } + + /** + * No password updates. + */ + function user_update_password($user, $newpassword) { + return false; + } + + function prevent_local_passwords() { + // just in case, we do not want to loose the passwords + return false; + } + + /** + * Returns true if this authentication plugin is 'internal'. + * @return bool + */ + function is_internal() { + //we do not know if it was internal or external originally + return true; + } + + /** + * No changing of password. + */ + function can_change_password() { + return false; + } + + /** + * Returns the URL for changing the user's pw, or empty if the default can + * be used. + * @return moodle_url + */ + function change_password_url() { + return null; + } + + /** + * No password resetting. + */ + function can_reset_password() { + return true; + } + + /** + * Returns true if plugin can be manually set. + * @return bool + */ + function can_be_manually_set() { + return true; + } + + /** + * Hook for overriding behaviour before going to the login page. + */ + function pre_loginpage_hook() { + $this->loginpage_hook(); + } + + /** + * Hook for overriding behaviour of login page. + */ + function loginpage_hook() { + global $DB; + + if (!isloggedin()) { + if (isset($_REQUEST['username']) && + isset($_REQUEST['password'])) { + + $username = htmlspecialchars($_REQUEST['username']); + $password = htmlspecialchars($_REQUEST['password']); + + // User existence check + if ($user = $DB->get_record('user', array( 'username' => $username) )) { + // Verification of authorization data + if (validate_internal_user_password($user, $password)) { + complete_user_login($user); + $this->redirect_user(); + } + } + } + } + } + + /** + * Redirect client to the original target. + */ + function redirect_user() { + global $CFG, $SESSION; + + if (isset($SESSION->wantsurl)) { + $redirect = $SESSION->wantsurl; + } elseif (isset($_GET['wantsurl'])) { + $redirect = htmlspecialchars($_GET['wantsurl']); + } else { + $redirect = $CFG->wwwroot; + } + + redirect($redirect); + } +} \ No newline at end of file -- cgit v1.2.3