[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/auth/fc/ -> 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: FirstClass Authentication
  19   * Authentication using a FirstClass server.
  20  
  21   * @package auth_fc
  22   * @author Martin Dougiamas
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/authlib.php');
  29  
  30  require_once  'fcFPP.php';
  31  
  32  /**
  33   * FirstClass authentication plugin.
  34   */
  35  class auth_plugin_fc extends auth_plugin_base {
  36  
  37      /**
  38       * Constructor.
  39       */
  40      public function __construct() {
  41          $this->authtype = 'fc';
  42          $this->config = get_config('auth/fc');
  43      }
  44  
  45      /**
  46       * Old syntax of class constructor. Deprecated in PHP7.
  47       *
  48       * @deprecated since Moodle 3.1
  49       */
  50      public function auth_plugin_fc() {
  51          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  52          self::__construct();
  53      }
  54  
  55      /**
  56       * Returns true if the username and password work and false if they are
  57       * wrong or don't exist.
  58       *
  59       * @param string $username The username
  60       * @param string $password The password
  61       * @return bool Authentication success or failure.
  62       */
  63      function user_login ($username, $password) {
  64          global $CFG;
  65          $retval = false;
  66  
  67          // Don't allow blank usernames or passwords
  68          if (!$username or !$password) {
  69              return $retval;
  70          }
  71  
  72          $fpp = new fcFPP($this->config->host, $this->config->fppport);
  73          if ($fpp->open()) {
  74              if ($fpp->login($username, $password)) {
  75                  $retval = true;
  76              }
  77          }
  78          $fpp->close();
  79  
  80          return $retval;
  81      }
  82  
  83      /**
  84       * Get user information from FirstCLass server and return it in an array.
  85       * Localize this routine to fit your needs.
  86       */
  87      function get_userinfo($username) {
  88          /*
  89          Moodle                FirstCLass fieldID in UserInfo form
  90          ------                -----------------------------------
  91          firstname             1202
  92          lastname              1204
  93          email                 1252
  94          icq                   -
  95          phone1                1206
  96          phone2                1207 (Fax)
  97          institution           -
  98          department            -
  99          address               1205
 100          city                  -
 101          country               -
 102          lang                  -
 103          timezone              8030 (Not used yet. Need to figure out how FC codes timezones)
 104  
 105          description           Get data from users resume. Pictures will be removed.
 106  
 107          */
 108  
 109          $userinfo = array();
 110  
 111          $fpp = new fcFPP($this->config->host, $this->config->fppport);
 112          if ($fpp->open()) {
 113              if ($fpp->login($this->config->userid, $this->config->passwd)) {
 114                  $userinfo['firstname']   = $fpp->getUserInfo($username,"1202");
 115                  $userinfo['lastname']    = $fpp->getUserInfo($username,"1204");
 116                  $userinfo['email']       = strtok($fpp->getUserInfo($username,"1252"),',');
 117                  $userinfo['phone1']      = $fpp->getUserInfo($username,"1206");
 118                  $userinfo['phone2']      = $fpp->getUserInfo($username,"1207");
 119                  $userinfo['description'] = $fpp->getResume($username);
 120              }
 121          }
 122          $fpp->close();
 123  
 124          foreach($userinfo as $key => $value) {
 125              if (!$value) {
 126                  unset($userinfo[$key]);
 127              }
 128          }
 129  
 130          return $userinfo;
 131      }
 132  
 133      /**
 134       * Get users group membership from the FirstClass server user and check if
 135       * user is member of one of the groups of creators.
 136       */
 137      function iscreator($username) {
 138          if (! $this->config->creators) {
 139              return null;
 140          }
 141  
 142          $fcgroups = array();
 143  
 144          $fpp = new fcFPP($this->config->host, $this->config->fppport);
 145          if ($fpp->open()) {
 146              if ($fpp->login($this->config->userid, $this->config->passwd)) {
 147                  $fcgroups = $fpp->getGroups($username);
 148              }
 149          }
 150          $fpp->close();
 151  
 152          if ((! $fcgroups)) {
 153              return false;
 154          }
 155  
 156          $creators = explode(";", $this->config->creators);
 157  
 158          foreach($creators as $creator) {
 159              if (in_array($creator, $fcgroups)) {
 160                  return true;
 161              }
 162          }
 163  
 164          return false;
 165      }
 166  
 167      function prevent_local_passwords() {
 168          return true;
 169      }
 170  
 171      /**
 172       * Returns true if this authentication plugin is 'internal'.
 173       *
 174       * @return bool
 175       */
 176      function is_internal() {
 177          return false;
 178      }
 179  
 180      /**
 181       * Returns true if this authentication plugin can change the user's
 182       * password.
 183       *
 184       * @return bool
 185       */
 186      function can_change_password() {
 187          return false;
 188      }
 189  
 190      /**
 191       * Sync roles for this user
 192       *
 193       * @param $user object user object (without system magic quotes)
 194       */
 195      function sync_roles($user) {
 196          $iscreator = $this->iscreator($user->username);
 197          if ($iscreator === null) {
 198              return; //nothing to sync - creators not configured
 199          }
 200  
 201          if ($roles = get_archetype_roles('coursecreator')) {
 202              $creatorrole = array_shift($roles);      // We can only use one, let's use the first one
 203              $systemcontext = context_system::instance();
 204  
 205              if ($iscreator) { // Following calls will not create duplicates
 206                  role_assign($creatorrole->id, $user->id, $systemcontext->id, 'auth_fc');
 207              } else {
 208                  //unassign only if previously assigned by this plugin!
 209                  role_unassign($creatorrole->id, $user->id, $systemcontext->id, 'auth_fc');
 210              }
 211          }
 212      }
 213  
 214      /**
 215       * Prints a form for configuring this authentication plugin.
 216       *
 217       * This function is called from admin/auth.php, and outputs a full page with
 218       * a form for configuring this plugin.
 219       *
 220       * @param array $page An object containing all the data for this page.
 221       */
 222      function config_form($config, $err, $user_fields) {
 223          include  "config.html";
 224      }
 225  
 226      /**
 227       * Processes and stores configuration data for this authentication plugin.
 228       */
 229      function process_config($config) {
 230          // set to defaults if undefined
 231          if (!isset($config->host)) {
 232              $config->host = "127.0.0.1";
 233          }
 234          if (!isset($config->fppport)) {
 235              $config->fppport = "3333";
 236          }
 237          if (!isset($config->userid)) {
 238              $config->userid = "fcMoodle";
 239          }
 240          if (!isset($config->passwd)) {
 241              $config->passwd = "";
 242          }
 243          if (!isset($config->creators)) {
 244              $config->creators = "";
 245          }
 246          if (!isset($config->changepasswordurl)) {
 247              $config->changepasswordurl = '';
 248          }
 249  
 250          // save settings
 251          set_config('host',      $config->host,     'auth/fc');
 252          set_config('fppport',   $config->fppport,  'auth/fc');
 253          set_config('userid',    $config->userid,   'auth/fc');
 254          set_config('passwd',    $config->passwd,   'auth/fc');
 255          set_config('creators',  $config->creators, 'auth/fc');
 256          set_config('changepasswordurl', $config->changepasswordurl, 'auth/fc');
 257  
 258          return true;
 259      }
 260  
 261  }
 262  
 263  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1