[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/auth/manual/ -> auth.php (source)

   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   * Authentication Plugin: Manual Authentication
  19   * Just does a simple check against the moodle database.
  20   *
  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
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/authlib.php');
  29  
  30  /**
  31   * Manual authentication plugin.
  32   *
  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
  37   */
  38  class auth_plugin_manual extends auth_plugin_base {
  39  
  40      /**
  41       * The name of the component. Used by the configuration.
  42       */
  43      const COMPONENT_NAME = 'auth_manual';
  44      const LEGACY_COMPONENT_NAME = 'auth/manual';
  45  
  46      /**
  47       * Constructor.
  48       */
  49      function auth_plugin_manual() {
  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);
  54      }
  55  
  56      /**
  57       * Returns true if the username and password work and false if they are
  58       * wrong or don't exist. (Non-mnet accounts only!)
  59       *
  60       * @param string $username The username
  61       * @param string $password The password
  62       * @return bool Authentication success or failure.
  63       */
  64      function user_login($username, $password) {
  65          global $CFG, $DB, $USER;
  66          if (!$user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  67              return false;
  68          }
  69          if (!validate_internal_user_password($user, $password)) {
  70              return false;
  71          }
  72          if ($password === 'changeme') {
  73              // force the change - this is deprecated and it makes sense only for manual auth,
  74              // because most other plugins can not change password easily or
  75              // passwords are always specified by users
  76              set_user_preference('auth_forcepasswordchange', true, $user->id);
  77          }
  78          return true;
  79      }
  80  
  81      /**
  82       * Updates the user's password.
  83       *
  84       * Called when the user password is updated.
  85       *
  86       * @param  object  $user        User table object
  87       * @param  string  $newpassword Plaintext password
  88       * @return boolean result
  89       */
  90      function user_update_password($user, $newpassword) {
  91          $user = get_complete_user_data('id', $user->id);
  92          set_user_preference('auth_manual_passwordupdatetime', time(), $user->id);
  93          // This will also update the stored hash to the latest algorithm
  94          // if the existing hash is using an out-of-date algorithm (or the
  95          // legacy md5 algorithm).
  96          return update_internal_user_password($user, $newpassword);
  97      }
  98  
  99      function prevent_local_passwords() {
 100          return false;
 101      }
 102  
 103      /**
 104       * Returns true if this authentication plugin is 'internal'.
 105       *
 106       * @return bool
 107       */
 108      function is_internal() {
 109          return true;
 110      }
 111  
 112      /**
 113       * Returns true if this authentication plugin can change the user's
 114       * password.
 115       *
 116       * @return bool
 117       */
 118      function can_change_password() {
 119          return true;
 120      }
 121  
 122      /**
 123       * Returns the URL for changing the user's pw, or empty if the default can
 124       * be used.
 125       *
 126       * @return moodle_url
 127       */
 128      function change_password_url() {
 129          return null;
 130      }
 131  
 132      /**
 133       * Returns true if plugin allows resetting of internal password.
 134       *
 135       * @return bool
 136       */
 137      function can_reset_password() {
 138          return true;
 139      }
 140  
 141      /**
 142       * Returns true if plugin can be manually set.
 143       *
 144       * @return bool
 145       */
 146      function can_be_manually_set() {
 147          return true;
 148      }
 149  
 150      /**
 151       * Prints a form for configuring this authentication plugin.
 152       *
 153       * This function is called from admin/auth.php, and outputs a full page with
 154       * a form for configuring this plugin.
 155       *
 156       * @param array $config An object containing all the data for this page.
 157       * @param string $error
 158       * @param array $user_fields
 159       * @return void
 160       */
 161      function config_form($config, $err, $user_fields) {
 162          include  'config.html';
 163      }
 164  
 165      /**
 166       * Return number of days to user password expires.
 167       *
 168       * If user password does not expire, it should return 0 or a positive value.
 169       * If user password is already expired, it should return negative value.
 170       *
 171       * @param mixed $username username (with system magic quotes)
 172       * @return integer
 173       */
 174      public function password_expire($username) {
 175          $result = 0;
 176  
 177          if (!empty($this->config->expirationtime)) {
 178              $user = core_user::get_user_by_username($username, 'id,timecreated');
 179              $lastpasswordupdatetime = get_user_preferences('auth_manual_passwordupdatetime', $user->timecreated, $user->id);
 180              $expiretime = $lastpasswordupdatetime + $this->config->expirationtime * DAYSECS;
 181              $now = time();
 182              $result = ($expiretime - $now) / DAYSECS;
 183              if ($expiretime > $now) {
 184                  $result = ceil($result);
 185              } else {
 186                  $result = floor($result);
 187              }
 188          }
 189  
 190          return $result;
 191      }
 192  
 193      /**
 194       * Processes and stores configuration data for this authentication plugin.
 195       *
 196       * @param stdClass $config
 197       * @return void
 198       */
 199      function process_config($config) {
 200          // Set to defaults if undefined.
 201          if (!isset($config->expiration)) {
 202              $config->expiration = '';
 203          }
 204          if (!isset($config->expiration_warning)) {
 205              $config->expiration_warning = '';
 206          }
 207          if (!isset($config->expirationtime)) {
 208              $config->expirationtime = '';
 209          }
 210  
 211          // Save settings.
 212          set_config('expiration', $config->expiration, self::COMPONENT_NAME);
 213          set_config('expiration_warning', $config->expiration_warning, self::COMPONENT_NAME);
 214          set_config('expirationtime', $config->expirationtime, self::COMPONENT_NAME);
 215          return true;
 216      }
 217  
 218     /**
 219      * Confirm the new user as registered. This should normally not be used,
 220      * but it may be necessary if the user auth_method is changed to manual
 221      * before the user is confirmed.
 222      *
 223      * @param string $username
 224      * @param string $confirmsecret
 225      */
 226      function user_confirm($username, $confirmsecret = null) {
 227          global $DB;
 228  
 229          $user = get_complete_user_data('username', $username);
 230  
 231          if (!empty($user)) {
 232              if ($user->confirmed) {
 233                  return AUTH_CONFIRM_ALREADY;
 234              } else {
 235                  $DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
 236                  if ($user->firstaccess == 0) {
 237                      $DB->set_field("user", "firstaccess", time(), array("id"=>$user->id));
 238                  }
 239                  return AUTH_CONFIRM_OK;
 240              }
 241          } else  {
 242              return AUTH_CONFIRM_ERROR;
 243          }
 244      }
 245  
 246  }
 247  
 248  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1