Merge branch 'MDL-63303_master-deleteduserfix' of https://github.com/markn86/moodle
[moodle.git] / auth / manual / auth.php
blob21a34d75099633cc462d85a0c437e3a80cae25b3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Authentication Plugin: Manual Authentication
19 * Just does a simple check against the moodle database.
21 * @package auth_manual
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir.'/authlib.php');
30 /**
31 * Manual authentication plugin.
33 * @package auth
34 * @subpackage manual
35 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class auth_plugin_manual extends auth_plugin_base {
40 /**
41 * The name of the component. Used by the configuration.
43 const COMPONENT_NAME = 'auth_manual';
44 const LEGACY_COMPONENT_NAME = 'auth/manual';
46 /**
47 * Constructor.
49 public function __construct() {
50 $this->authtype = 'manual';
51 $config = get_config(self::COMPONENT_NAME);
52 $legacyconfig = get_config(self::LEGACY_COMPONENT_NAME);
53 $this->config = (object)array_merge((array)$legacyconfig, (array)$config);
56 /**
57 * Old syntax of class constructor. Deprecated in PHP7.
59 * @deprecated since Moodle 3.1
61 public function auth_plugin_manual() {
62 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
63 self::__construct();
66 /**
67 * Returns true if the username and password work and false if they are
68 * wrong or don't exist. (Non-mnet accounts only!)
70 * @param string $username The username
71 * @param string $password The password
72 * @return bool Authentication success or failure.
74 function user_login($username, $password) {
75 global $CFG, $DB, $USER;
76 if (!$user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
77 return false;
79 if (!validate_internal_user_password($user, $password)) {
80 return false;
82 if ($password === 'changeme') {
83 // force the change - this is deprecated and it makes sense only for manual auth,
84 // because most other plugins can not change password easily or
85 // passwords are always specified by users
86 set_user_preference('auth_forcepasswordchange', true, $user->id);
88 return true;
91 /**
92 * Updates the user's password.
94 * Called when the user password is updated.
96 * @param object $user User table object
97 * @param string $newpassword Plaintext password
98 * @return boolean result
100 function user_update_password($user, $newpassword) {
101 $user = get_complete_user_data('id', $user->id);
102 set_user_preference('auth_manual_passwordupdatetime', time(), $user->id);
103 // This will also update the stored hash to the latest algorithm
104 // if the existing hash is using an out-of-date algorithm (or the
105 // legacy md5 algorithm).
106 return update_internal_user_password($user, $newpassword);
109 function prevent_local_passwords() {
110 return false;
114 * Returns true if this authentication plugin is 'internal'.
116 * @return bool
118 function is_internal() {
119 return true;
123 * Returns true if this authentication plugin can change the user's
124 * password.
126 * @return bool
128 function can_change_password() {
129 return true;
133 * Returns the URL for changing the user's pw, or empty if the default can
134 * be used.
136 * @return moodle_url
138 function change_password_url() {
139 return null;
143 * Returns true if plugin allows resetting of internal password.
145 * @return bool
147 function can_reset_password() {
148 return true;
152 * Returns true if plugin can be manually set.
154 * @return bool
156 function can_be_manually_set() {
157 return true;
161 * Return number of days to user password expires.
163 * If user password does not expire, it should return 0 or a positive value.
164 * If user password is already expired, it should return negative value.
166 * @param mixed $username username (with system magic quotes)
167 * @return integer
169 public function password_expire($username) {
170 $result = 0;
172 if (!empty($this->config->expirationtime)) {
173 $user = core_user::get_user_by_username($username, 'id,timecreated');
174 $lastpasswordupdatetime = get_user_preferences('auth_manual_passwordupdatetime', $user->timecreated, $user->id);
175 $expiretime = $lastpasswordupdatetime + $this->config->expirationtime * DAYSECS;
176 $now = time();
177 $result = ($expiretime - $now) / DAYSECS;
178 if ($expiretime > $now) {
179 $result = ceil($result);
180 } else {
181 $result = floor($result);
185 return $result;
189 * Confirm the new user as registered. This should normally not be used,
190 * but it may be necessary if the user auth_method is changed to manual
191 * before the user is confirmed.
193 * @param string $username
194 * @param string $confirmsecret
196 function user_confirm($username, $confirmsecret = null) {
197 global $DB;
199 $user = get_complete_user_data('username', $username);
201 if (!empty($user)) {
202 if ($user->confirmed) {
203 return AUTH_CONFIRM_ALREADY;
204 } else {
205 $DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
206 return AUTH_CONFIRM_OK;
208 } else {
209 return AUTH_CONFIRM_ERROR;