auth.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. // This file is not 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.
  13. //
  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/>.
  16. /**
  17. * Authentication Plugin: Email RUT Authentication
  18. *
  19. * @author Martin Dougiamas + Hackware Human <hackware.cl>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  21. * @package auth_emailrut
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->libdir.'/authlib.php');
  25. /**
  26. * Email and RUT authentication plugin.
  27. */
  28. class auth_plugin_emailrut extends auth_plugin_base {
  29. /**
  30. * Constructor.
  31. */
  32. public function __construct() {
  33. $this->authtype = 'emailrut';
  34. $this->config = get_config('auth_emailrut');
  35. }
  36. /**
  37. * Old syntax of class constructor. Deprecated in PHP7.
  38. *
  39. * @deprecated since Moodle 3.1
  40. */
  41. public function auth_plugin_emailrut() {
  42. debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  43. self::__construct();
  44. }
  45. /**
  46. * Returns true if the username and password work and false if they are
  47. * wrong or don't exist.
  48. *
  49. * @param string $username The username
  50. * @param string $password The password
  51. * @return bool Authentication success or failure.
  52. */
  53. function user_login ($username, $password) {
  54. global $CFG, $DB;
  55. if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  56. return validate_internal_user_password($user, $password);
  57. }
  58. return false;
  59. }
  60. /**
  61. * Updates the user's password.
  62. *
  63. * called when the user password is updated.
  64. *
  65. * @param object $user User table object (with system magic quotes)
  66. * @param string $newpassword Plaintext password (with system magic quotes)
  67. * @return boolean result
  68. *
  69. */
  70. function user_update_password($user, $newpassword) {
  71. $user = get_complete_user_data('id', $user->id);
  72. // This will also update the stored hash to the latest algorithm
  73. // if the existing hash is using an out-of-date algorithm (or the
  74. // legacy md5 algorithm).
  75. return update_internal_user_password($user, $newpassword);
  76. }
  77. function can_signup() {
  78. return true;
  79. }
  80. /**
  81. * Return a form to capture user details for account creation.
  82. * @return moodle_form A form which edits a record from the user table.
  83. */
  84. function signup_form() {
  85. global $CFG;
  86. require_once($CFG->dirroot . "/auth/emailrut/signup_form.php");
  87. return new login_signup_form(null, null, 'post', '', array('autocomplete'=>'on'));
  88. }
  89. /**
  90. * Sign up a new user ready for confirmation.
  91. * Password is passed in plaintext.
  92. *
  93. * @param object $user new user object
  94. * @param boolean $notify print notice with link and terminate
  95. */
  96. function user_signup($user, $notify=true) {
  97. // Standard signup, without custom confirmatinurl.
  98. return $this->user_signup_with_confirmation($user, $notify);
  99. }
  100. /**
  101. * Sign up a new user ready for confirmation.
  102. *
  103. * Password is passed in plaintext.
  104. * A custom confirmationurl could be used.
  105. *
  106. * @param object $user new user object
  107. * @param boolean $notify print notice with link and terminate
  108. * @param string $confirmationurl user confirmation URL
  109. * @return boolean true if everything well ok and $notify is set to true
  110. * @throws moodle_exception
  111. * @since Moodle 3.2
  112. */
  113. public function user_signup_with_confirmation($user, $notify=true, $confirmationurl = null) {
  114. global $CFG, $DB, $SESSION;
  115. require_once($CFG->dirroot.'/user/profile/lib.php');
  116. require_once($CFG->dirroot.'/user/lib.php');
  117. $plainpassword = $user->password;
  118. $user->password = hash_internal_user_password($user->password);
  119. if (empty($user->calendartype)) {
  120. $user->calendartype = $CFG->calendartype;
  121. }
  122. $user->id = user_create_user($user, false, false);
  123. user_add_password_history($user->id, $plainpassword);
  124. // Save any custom profile field information.
  125. profile_save_data($user);
  126. // Save wantsurl against user's profile, so we can return them there upon confirmation.
  127. if (!empty($SESSION->wantsurl)) {
  128. set_user_preference('auth_emailrut_wantsurl', $SESSION->wantsurl, $user);
  129. }
  130. // Trigger event.
  131. \core\event\user_created::create_from_userid($user->id)->trigger();
  132. if (! send_confirmation_email($user, $confirmationurl)) {
  133. print_error('auth_emailrutnoemail', 'auth_email');
  134. }
  135. if ($notify) {
  136. global $CFG, $PAGE, $OUTPUT;
  137. $emailconfirm = get_string('emailconfirm');
  138. $PAGE->navbar->add($emailconfirm);
  139. $PAGE->set_title($emailconfirm);
  140. $PAGE->set_heading($PAGE->course->fullname);
  141. echo $OUTPUT->header();
  142. notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
  143. } else {
  144. return true;
  145. }
  146. }
  147. /**
  148. * Returns true if plugin allows confirming of new users.
  149. *
  150. * @return bool
  151. */
  152. function can_confirm() {
  153. return true;
  154. }
  155. /**
  156. * Confirm the new user as registered.
  157. *
  158. * @param string $username
  159. * @param string $confirmsecret
  160. */
  161. function user_confirm($username, $confirmsecret) {
  162. global $DB, $SESSION;
  163. $user = get_complete_user_data('username', $username);
  164. if (!empty($user)) {
  165. if ($user->auth != $this->authtype) {
  166. return AUTH_CONFIRM_ERROR;
  167. } else if ($user->secret == $confirmsecret && $user->confirmed) {
  168. return AUTH_CONFIRM_ALREADY;
  169. } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
  170. $DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
  171. if ($wantsurl = get_user_preferences('auth_emailrut_wantsurl', false, $user)) {
  172. // Ensure user gets returned to page they were trying to access before signing up.
  173. $SESSION->wantsurl = $wantsurl;
  174. unset_user_preference('auth_emailrut_wantsurl', $user);
  175. }
  176. return AUTH_CONFIRM_OK;
  177. }
  178. } else {
  179. return AUTH_CONFIRM_ERROR;
  180. }
  181. }
  182. function prevent_local_passwords() {
  183. return false;
  184. }
  185. /**
  186. * Returns true if this authentication plugin is 'internal'.
  187. *
  188. * @return bool
  189. */
  190. function is_internal() {
  191. return true;
  192. }
  193. /**
  194. * Returns true if this authentication plugin can change the user's
  195. * password.
  196. *
  197. * @return bool
  198. */
  199. function can_change_password() {
  200. return true;
  201. }
  202. /**
  203. * Returns the URL for changing the user's pw, or empty if the default can
  204. * be used.
  205. *
  206. * @return moodle_url
  207. */
  208. function change_password_url() {
  209. return null; // use default internal method
  210. }
  211. /**
  212. * Returns true if plugin allows resetting of internal password.
  213. *
  214. * @return bool
  215. */
  216. function can_reset_password() {
  217. return true;
  218. }
  219. /**
  220. * Returns true if plugin can be manually set.
  221. *
  222. * @return bool
  223. */
  224. function can_be_manually_set() {
  225. return true;
  226. }
  227. /**
  228. * Returns whether or not the captcha element is enabled.
  229. * @return bool
  230. */
  231. function is_captcha_enabled() {
  232. return get_config("auth_{$this->authtype}", 'recaptcha');
  233. }
  234. }