Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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.
  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  /**
  18   * Login renderable.
  19   *
  20   * @package    core_auth
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_auth\output;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir . '/externallib.php');
  29  
  30  use context_system;
  31  use help_icon;
  32  use moodle_url;
  33  use renderable;
  34  use renderer_base;
  35  use stdClass;
  36  use templatable;
  37  
  38  /**
  39   * Login renderable class.
  40   *
  41   * @package    core_auth
  42   * @copyright  2016 Frédéric Massart - FMCorz.net
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class login implements renderable, templatable {
  46  
  47      /** @var bool Whether to auto focus the form fields. */
  48      public $autofocusform;
  49      /** @var bool Whether we can login as guest. */
  50      public $canloginasguest;
  51      /** @var bool Whether we can login by e-mail. */
  52      public $canloginbyemail;
  53      /** @var bool Whether we can sign-up. */
  54      public $cansignup;
  55      /** @var help_icon The cookies help icon. */
  56      public $cookieshelpicon;
  57      /** @var string The error message, if any. */
  58      public $error;
  59      /** @var moodle_url Forgot password URL. */
  60      public $forgotpasswordurl;
  61      /** @var array Additional identify providers, contains the keys 'url', 'name' and 'icon'. */
  62      public $identityproviders;
  63      /** @var string Login instructions, if any. */
  64      public $instructions;
  65      /** @var moodle_url The form action login URL. */
  66      public $loginurl;
  67      /** @var moodle_url The sign-up URL. */
  68      public $signupurl;
  69      /** @var string The user name to pre-fill the form with. */
  70      public $username;
  71      /** @var string The language selector menu. */
  72      public $languagemenu;
  73      /** @var string The csrf token to limit login to requests that come from the login form. */
  74      public $logintoken;
  75      /** @var string Maintenance message, if Maintenance is enabled. */
  76      public $maintenance;
  77  
  78      /**
  79       * Constructor.
  80       *
  81       * @param array $authsequence The enabled sequence of authentication plugins.
  82       * @param string $username The username to display.
  83       */
  84      public function __construct(array $authsequence, $username = '') {
  85          global $CFG, $OUTPUT, $PAGE;
  86  
  87          $this->username = $username;
  88  
  89          $languagedata = new \core\output\language_menu($PAGE);
  90  
  91          $this->languagemenu = $languagedata->export_for_action_menu($OUTPUT);
  92          $this->canloginasguest = $CFG->guestloginbutton && !isguestuser();
  93          $this->canloginbyemail = !empty($CFG->authloginviaemail);
  94          $this->cansignup = $CFG->registerauth == 'email' || !empty($CFG->registerauth);
  95          if ($CFG->rememberusername == 0) {
  96              $this->cookieshelpicon = new help_icon('cookiesenabledonlysession', 'core');
  97          } else {
  98              $this->cookieshelpicon = new help_icon('cookiesenabled', 'core');
  99          }
 100  
 101          $this->autofocusform = !empty($CFG->loginpageautofocus);
 102  
 103          $this->forgotpasswordurl = new moodle_url('/login/forgot_password.php');
 104          $this->loginurl = new moodle_url('/login/index.php');
 105          $this->signupurl = new moodle_url('/login/signup.php');
 106  
 107          // Authentication instructions.
 108          $this->instructions = $CFG->auth_instructions;
 109          if (is_enabled_auth('none')) {
 110              $this->instructions = get_string('loginstepsnone');
 111          } else if ($CFG->registerauth == 'email' && empty($this->instructions)) {
 112              $this->instructions = get_string('loginsteps', 'core', 'signup.php');
 113          }
 114  
 115          if ($CFG->maintenance_enabled == true) {
 116              if (!empty($CFG->maintenance_message)) {
 117                  $this->maintenance = $CFG->maintenance_message;
 118              } else {
 119                  $this->maintenance = get_string('sitemaintenance', 'admin');
 120              }
 121          }
 122  
 123          // Identity providers.
 124          $this->identityproviders = \auth_plugin_base::get_identity_providers($authsequence);
 125          $this->logintoken = \core\session\manager::get_login_token();
 126      }
 127  
 128      /**
 129       * Set the error message.
 130       *
 131       * @param string $error The error message.
 132       */
 133      public function set_error($error) {
 134          $this->error = $error;
 135      }
 136  
 137      public function export_for_template(renderer_base $output) {
 138  
 139          $identityproviders = \auth_plugin_base::prepare_identity_providers_for_output($this->identityproviders, $output);
 140  
 141          $data = new stdClass();
 142          $data->autofocusform = $this->autofocusform;
 143          $data->canloginasguest = $this->canloginasguest;
 144          $data->canloginbyemail = $this->canloginbyemail;
 145          $data->cansignup = $this->cansignup;
 146          $data->cookieshelpicon = $this->cookieshelpicon->export_for_template($output);
 147          $data->error = $this->error;
 148          $data->forgotpasswordurl = $this->forgotpasswordurl->out(false);
 149          $data->hasidentityproviders = !empty($this->identityproviders);
 150          $data->hasinstructions = !empty($this->instructions) || $this->cansignup;
 151          $data->identityproviders = $identityproviders;
 152          list($data->instructions, $data->instructionsformat) = external_format_text($this->instructions, FORMAT_MOODLE,
 153              context_system::instance()->id);
 154          $data->loginurl = $this->loginurl->out(false);
 155          $data->signupurl = $this->signupurl->out(false);
 156          $data->username = $this->username;
 157          $data->logintoken = $this->logintoken;
 158          $data->maintenance = format_text($this->maintenance, FORMAT_MOODLE);
 159          $data->languagemenu = $this->languagemenu;
 160  
 161          return $data;
 162      }
 163  }