[ Index ]

PHP Cross Reference of moodle-2.8

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


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