Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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