Merge branch 'MDL-33509-master' of git://github.com/mihailges/moodle
[moodle.git] / auth / webservice / auth.php
blob479b7fd37cda824a88f4013f086ac5ba179f1820
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 * Web service auth plugin, reserves username, prevents normal login.
19 * TODO: add IP restrictions and some other features - MDL-17135
21 * @package auth_webservice
22 * @copyright 2008 Petr Skoda (http://skodak.org)
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 * Web service auth plugin.
33 class auth_plugin_webservice extends auth_plugin_base {
35 /**
36 * Constructor.
38 public function __construct() {
39 $this->authtype = 'webservice';
40 $this->config = get_config('auth_webservice');
43 /**
44 * Old syntax of class constructor. Deprecated in PHP7.
46 * @deprecated since Moodle 3.1
48 public function auth_plugin_webservice() {
49 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
50 self::__construct();
53 /**
54 * Returns true if the username and password work and false if they are
55 * wrong or don't exist.
57 * @param string $username The username (with system magic quotes)
58 * @param string $password The password (with system magic quotes)
60 * @return bool Authentication success or failure.
62 function user_login($username, $password) {
63 // normla logins not allowed!
64 return false;
67 /**
68 * Custom auth hook for web services.
69 * @param string $username
70 * @param string $password
71 * @return bool success
73 function user_login_webservice($username, $password) {
74 global $CFG, $DB;
75 // special web service login
76 if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
77 return validate_internal_user_password($user, $password);
79 return false;
82 /**
83 * Updates the user's password.
85 * called when the user password is updated.
87 * @param object $user User table object (with system magic quotes)
88 * @param string $newpassword Plaintext password (with system magic quotes)
89 * @return boolean result
92 function user_update_password($user, $newpassword) {
93 $user = get_complete_user_data('id', $user->id);
94 // This will also update the stored hash to the latest algorithm
95 // if the existing hash is using an out-of-date algorithm (or the
96 // legacy md5 algorithm).
97 return update_internal_user_password($user, $newpassword);
101 * Returns true if this authentication plugin is 'internal'.
103 * Webserice auth doesn't use password fields, it uses only tokens.
105 * @return bool
107 function is_internal() {
108 return false;
112 * Returns true if this authentication plugin can change the user's
113 * password.
115 * @return bool
117 function can_change_password() {
118 return false;
122 * Returns the URL for changing the user's pw, or empty if the default can
123 * be used.
125 * @return moodle_url
127 function change_password_url() {
128 return null;
132 * Returns true if plugin allows resetting of internal password.
134 * @return bool
136 function can_reset_password() {
137 return false;
141 * Confirm the new user as registered. This should normally not be used,
142 * but it may be necessary if the user auth_method is changed to manual
143 * before the user is confirmed.
145 function user_confirm($username, $confirmsecret = null) {
146 return AUTH_CONFIRM_ERROR;