]> git.mjollnir.org Git - moodle.git/commitdiff
(MDL-8973) Fix OOP model of new multi auth plugins + some other auth related fixes...
authorskodak <skodak>
Thu, 22 Mar 2007 12:27:52 +0000 (12:27 +0000)
committerskodak <skodak>
Thu, 22 Mar 2007 12:27:52 +0000 (12:27 +0000)
37 files changed:
admin/auth.php
admin/auth_config.php
auth/authlib.php [deleted file]
auth/cas/auth.php
auth/db/auth.php
auth/email/auth.php
auth/fc/auth.php
auth/imap/auth.php
auth/ldap/auth.php
auth/manual/auth.php
auth/mnet/auth.php
auth/nntp/auth.php
auth/nologin/auth.php
auth/none/auth.php
auth/pam/auth.php
auth/pop3/auth.php
auth/radius/auth.php
auth/shibboleth/auth.php
blocks/login/block_login.php
lang/en_utf8/auth.php
lib/accesslib.php
lib/authlib.php [new file with mode: 0644]
lib/db/mysql.php
lib/db/postgres7.php
lib/moodlelib.php
login/change_password.php
login/change_password_form.php
login/confirm.php
login/forgot_password.php
login/index.php
login/index_form.html
login/logout.php
login/signup.php
login/signup_form.php
user/edit.php
user/editadvanced.php
user/view.php

index c30ed576eea8e894d372f651025a43d638d1856d..ff57cf87c640ef33477eb69aedccad2302a2307b 100644 (file)
@@ -147,7 +147,7 @@ $registrationauths[''] = $txt->disable;
 foreach ($authsenabled as $auth) {
     $authplugin = get_auth_plugin($auth);
     $displayauths[$auth] = get_string("auth_{$auth}title", 'auth');
-    if (method_exists($authplugin, 'user_signup')) {
+    if ($authplugin->can_signup()) {
         $registrationauths[$auth] = get_string("auth_{$auth}title", 'auth');
     }
 }
@@ -158,7 +158,7 @@ foreach ($authsavailable as $auth) {
     }
     $authplugin = get_auth_plugin($auth);
     $displayauths[$auth] = get_string("auth_{$auth}title", 'auth');
-    if (method_exists($authplugin, 'user_signup')) {
+    if ($authplugin->can_signup()) {
         $registrationauths[$auth] = get_string("auth_{$auth}title", 'auth');
     }
 }
index 710af729252bed3a00de6a027fe480dd6a685416..99459f094b64131a8f7b677707b6afae6b00f089 100644 (file)
@@ -19,9 +19,7 @@ if ($frm = data_submitted()) {
         error(get_string('confirmsesskeybad', 'error'));
     }
 
-    if (method_exists($authplugin, 'validate_form')) {
-        $authplugin->validate_form($frm, $err);
-    }
+    $authplugin->validate_form($frm, $err);
 
     if (count($err) == 0) {
 
diff --git a/auth/authlib.php b/auth/authlib.php
deleted file mode 100644 (file)
index 9aa2e75..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-<?php
-/**
- * @author Martin Dougiamas
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package moodle multiauth
- *
- * Multiple plugin authentication
- * Support library
- *
- * 2006-08-28  File created, AUTH return values defined.
- */
-
-/**
- * Returned when the login was successful.
- */
-define('AUTH_OK',     0);
-
-/**
- * Returned when the login was unsuccessful.
- */
-define('AUTH_FAIL',   1);
-
-/**
- * Returned when the login was denied (a reason for AUTH_FAIL).
- */
-define('AUTH_DENIED', 2);
-
-/**
- * Returned when some error occurred (a reason for AUTH_FAIL).
- */
-define('AUTH_ERROR',  4);
-
-?>
index 61ac669ceae7b857e5a73d96bc03a0f2ca566f6e..4278b39e0c86adfe5d4f1196eecf3de454ca4111 100644 (file)
@@ -15,20 +15,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * CAS authentication plugin.
  */
-class auth_plugin_cas {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_cas extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_cas() {
+        $this->authtype = 'cas';
         $this->config = get_config('auth/cas');
     }
 
@@ -222,6 +220,17 @@ class auth_plugin_cas {
         return !empty($this->config->changepasswordurl);
     }
 
+    function prelogin_hook() {
+        // Load alternative login screens if necessary
+        // TODO: fix the cas login screen
+        return;
+
+        if(!empty($CFG->cas_enabled)) {
+            require($CFG->dirroot.'/auth/cas/login.php');
+        }
+    }
+
+
     /**
      * Prints a form for configuring this authentication plugin.
      *
index babface7c1a309f07c1d031e5585cace7901be5f..1bc28a580e4ff3613790612efa8b38ee4f4fd46a 100644 (file)
@@ -16,20 +16,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * External database authentication plugin.
  */
-class auth_plugin_db {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_db extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_db() {
+        $this->authtype = 'db';
         $this->config = get_config('auth/db');
         if (empty($this->config->extencoding)) {
             $this->config->extencoding = 'utf-8';
@@ -572,7 +570,6 @@ class auth_plugin_db {
             $this->config->changepasswordurl = '';
             set_config('changepasswordurl', '', 'auth/db');
         }
-        return true;
     }
 
     /**
index 4f39c169d34d24bd6858070928653c969582fbc3..20ad9232efaa48107d020ebef2f6ac9e172bca92 100644 (file)
@@ -16,21 +16,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
 
 /**
  * Email authentication plugin.
  */
-class auth_plugin_email {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_email extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_email() {
+        $this->authtype = 'email';
         $this->config = get_config('auth/email');
     }
 
@@ -65,6 +62,10 @@ class auth_plugin_email {
         return update_internal_user_password($user, $newpassword);
     }
 
+    function can_signup() {
+        return true;
+    }
+
     /**
      * Sign up a new user ready for confirmation.
      * Password is passed in plaintext.
@@ -72,7 +73,7 @@ class auth_plugin_email {
      * @param object $user new user object (with system magic quotes)
      * @param boolean $notify print notice with link and terminate
      */
-    function user_signup($user, $notify = true) {
+    function user_signup($user, $notify=true) {
         $user->password = hash_internal_user_password($user->password);
 
         if (! ($user->id = insert_record('user', $user)) ) {
@@ -92,6 +93,15 @@ class auth_plugin_email {
         }
     }
 
+    /**
+     * Returns true if plugin allows confirming of new users.
+     *
+     * @return bool
+     */
+    function can_confirm() {
+        return true;
+    }
+
     /**
      * Confirm the new user as registered.
      *
index f61b25c8f7da77c6968736375c88f187f42d8ef8..45049ad9f2a7b7def50ced63466f872946565c79 100644 (file)
@@ -15,22 +15,20 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 require_once 'fcFPP.php';
 
 /**
  * FirstClass authentication plugin.
  */
-class auth_plugin_fc {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_fc extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_fc() {
+        $this->authtype = 'fc';
         $this->config = get_config('auth/fc');
     }
 
@@ -116,14 +114,9 @@ class auth_plugin_fc {
      * Get users group membership from the FirstClass server user and check if
      * user is member of one of the groups of creators.
      */
-    function iscreator($username = 0) {
-        global $USER;
-
+    function iscreator($username) {
         if (! $this->config->creators) {
-            return false;
-        }
-        if (! $username) {
-            $username = $USER->username;
+            return null;
         }
 
         $fcgroups = array();
@@ -143,7 +136,9 @@ class auth_plugin_fc {
         $creators = explode(";", $this->config->creators);
 
         foreach($creators as $creator) {
-            If (in_array($creator, $fcgroups)) return true;
+            if (in_array($creator, $fcgroups)) {
+                return true;
+            }
         }
 
         return false;
@@ -168,6 +163,30 @@ class auth_plugin_fc {
         return false;
     }
 
+    /**
+     * Sync roles for this user
+     *
+     * @param $user object user object (without system magic quotes)
+     */
+    function sync_roles($user) {
+        $iscreator = $this->iscreator($user->username);
+        if ($iscreator === null) {
+            return; //nothing to sync - creators not configured
+        }
+
+        if ($roles = get_roles_with_capability('moodle/legacy:coursecreator', CAP_ALLOW)) {
+            $creatorrole = array_shift($roles);      // We can only use one, let's use the first one
+            $systemcontext = get_context_instance(CONTEXT_SYSTEM);
+
+            if ($iscreator) { // Following calls will not create duplicates
+                role_assign($creatorrole->id, $user->id, 0, $systemcontext->id, 0, 0, 0, 'fc');
+            } else {
+                //unassign only if previously assigned by this plugin!
+                role_unassign($creatorrole->id, $user->id, 0, $systemcontext->id, 'fc');
+            }
+        }
+    }
+
     /**
      * Prints a form for configuring this authentication plugin.
      *
index 3c1c0f7e2253d3835eacd431b8a5132ad3897018..c2c3cf0ae69de288a3d7c17cf8ff82fb7001f1af 100644 (file)
@@ -16,20 +16,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * IMAP authentication plugin.
  */
-class auth_plugin_imap {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_imap extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_imap() {
+        $this->authtype = 'imap';
         $this->config = get_config('auth/imap');
     }
 
index 96d932705234ccd717cf7fe86d0d4906ed2943ba..8f4b09a7569ea28ff1556c900c1d0b0c4ab27995 100644 (file)
@@ -16,20 +16,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * LDAP authentication plugin.
  */
-class auth_plugin_ldap {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_ldap extends auth_plugin_base {
 
     /**
      * Constructor with initialisation.
      */
     function auth_plugin_ldap() {
+        $this->authtype = 'ldap';
         $this->config = get_config('auth/ldap');
         if (empty($this->config->ldapencoding)) {
             $this->config->ldapencoding = 'utf-8';
@@ -274,7 +272,7 @@ class auth_plugin_ldap {
      * If userpassword does not expire it should return 0. If password is already expired
      * it should return negative value.
      *
-     * @param mixed $username username
+     * @param mixed $username username (with system magic quotes)
      * @return integer
      */
     function password_expire($username) {
@@ -584,7 +582,7 @@ class auth_plugin_ldap {
                         if ($this->iscreator($user->username)) {
                             role_assign($creatorrole->id, $user->id, 0, $sitecontext->id, 0, 0, 0, 'ldap');
                         } else {
-                            role_unassign($creatorrole->id, $user->id, 0, $sitecontext->id);
+                            role_unassign($creatorrole->id, $user->id, 0, $sitecontext->id, 'ldap');
                         }
                     }
 
@@ -783,26 +781,18 @@ class auth_plugin_ldap {
     /**
      * Returns true if user should be coursecreator.
      *
-     * @param mixed $username    username (with system magic quotes)
+     * @param mixed $username    username (without system magic quotes)
      * @return boolean result
      */
-    function iscreator($username = false) {
-        global $USER;
-
+    function iscreator($username) {
         if (empty($this->config->creators) or empty($this->config->memberattribute)) {
-            return false;
-        }
-
-        if ($username === false) {
-            $username = $USER->username;
-        } else {
-            $username = stripslashes($username);
+            return null;
         }
 
         $textlib = textlib_get_instance();
         $extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
 
-        return $this->ldap_isgroupmember($extusername, $this->config->creators);
+        return (boolean)$this->ldap_isgroupmember($extusername, $this->config->creators);
     }
 
     /**
@@ -824,7 +814,7 @@ class auth_plugin_ldap {
             return false;
         }
 
-        if (isset($olduser->auth) and $olduser->auth == 'ldap') {
+        if (isset($olduser->auth) and $olduser->auth != 'ldap') {
             return true; // just change auth and skip update
         }
 
@@ -1512,6 +1502,30 @@ class auth_plugin_ldap {
         }
     }
 
+    /**
+     * Sync roles for this user
+     *
+     * @param $user object user object (without system magic quotes)
+     */
+    function sync_roles($user) {
+        $iscreator = $this->iscreator($user->username);
+        if ($iscreator === null) {
+            return; //nothing to sync - creators not configured
+        }
+
+        if ($roles = get_roles_with_capability('moodle/legacy:coursecreator', CAP_ALLOW)) {
+            $creatorrole = array_shift($roles);      // We can only use one, let's use the first one
+            $systemcontext = get_context_instance(CONTEXT_SYSTEM);
+
+            if ($iscreator) { // Following calls will not create duplicates
+                role_assign($creatorrole->id, $user->id, 0, $systemcontext->id, 0, 0, 0, 'ldap');
+            } else {
+                //unassign only if previously assigned by this plugin!
+                role_unassign($creatorrole->id, $user->id, 0, $systemcontext->id, 'ldap');
+            }
+        }
+    }
+
     /**
      * Prints a form for configuring this authentication plugin.
      *
index 23ebf4b1d4747ecb80cbacf6a2dcc695948be4c4..6f1e7ca620198e19945f7193d10c30724abe1cd4 100644 (file)
@@ -15,23 +15,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * Manual authentication plugin.
  */
-class auth_plugin_manual
-{
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
-
-    var $canchangepassword = true;
-    var $isinternal = true;
+class auth_plugin_manual extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_manual() {
+        $this->authtype = 'manual';
         $this->config = get_config('auth/manual');
     }
 
index 338a9b259bde0b430c22bfd5f3ad9f1a7cb1275c..4061fac508beb58f716b5af4d169c3bd272db30e 100644 (file)
@@ -16,21 +16,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * Moodle Network authentication plugin.
  */
-class auth_plugin_mnet
-{
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_mnet extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_mnet() {
+        $this->authtype = 'mnet';
         $this->config = get_config('auth/mnet');
     }
 
@@ -1336,6 +1333,16 @@ class auth_plugin_mnet
         }
         return $accessctrl == 'allow';
     }
+
+    function prelogout_hook() {
+        global $USER, $CFG, $redirect;
+
+        if (!empty($USER->mnethostid) and $USER->mnethostid != $CFG->mnet_localhost_id) {
+            $host = get_record('mnet_host', 'id', $USER->mnethostid);
+            $redirect = $host->wwwroot.'/';
+        }
+    }
+
 }
 
 ?>
index 9e236ee68d3793fd854b3ad3a05a4b5360efc8bf..da549b915ea8c8ac79ba448ba76e6ee1ec2e8352 100644 (file)
@@ -16,20 +16,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * NNTP authentication plugin.
  */
-class auth_plugin_nntp {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_nntp extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_nntp() {
+        $this->authtype = 'nntp';
         $this->config = get_config('auth/nntp');
     }
 
index baf920622826bee437028785c805871b051bf5c7..f91ec9c8a7fa5f0968c0321de1d72a3d95386f90 100644 (file)
@@ -16,16 +16,19 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * Plugin for no authentication.
  */
-class auth_plugin_nologin {
+class auth_plugin_nologin extends auth_plugin_base {
 
 
     /**
      * Constructor.
      */
     function auth_plugin_nologin() {
+        $this->authtype = 'nologin';
     }
 
     /**
index 76211ba548f2e2ce750b2a5dc9d0167e7ccff64f..9b2da1e04b14942437d71cd7addb0fc9008b33f1 100644 (file)
@@ -16,23 +16,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * Plugin for no authentication.
  */
-class auth_plugin_none {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
-
-    var $canchangepassword = true;
-    var $isinternal = true;
+class auth_plugin_none extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_none() {
+        $this->authtype = 'none';
         $this->config = get_config('auth/none');
     }
 
index 28d3c75034b19d7102f0ada1fe2e3ecba1e51ab3..dbb2eb1cee50c6187e991258189367e9c898aa2a 100644 (file)
@@ -30,15 +30,12 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * PAM authentication plugin.
  */
-class auth_plugin_pam {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_pam extends auth_plugin_base {
 
     /**
      * Store error messages from pam authentication attempts.
@@ -49,6 +46,7 @@ class auth_plugin_pam {
      * Constructor.
      */
     function auth_plugin_pam() {
+        $this->authtype = 'pam';
         $this->config = get_config('auth/pam');
         $this->errormessage = '';
     }
index 431d20796a1ed2d053e874fa22ed6ac84ffff1f2..888b0d62965bcadc788ba7a668406ecf7ade0bf5 100644 (file)
@@ -16,20 +16,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * POP3 authentication plugin.
  */
-class auth_plugin_pop3 {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_pop3 extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_pop3() {
+        $this->authtype = 'pop3';
         $this->config = get_config('auth/pop3');
     }
 
index 7f1776aeafb40ed7009d3e7dc0aabcb8fa7b343e..e01fe2c3d1e31bdf847d75e8d7c9fc598db3629f 100644 (file)
@@ -17,20 +17,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * RADIUS authentication plugin.
  */
-class auth_plugin_radius {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_radius extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_radius() {
+        $this->authtype = 'radius';
         $this->config = get_config('auth/radius');
     }
 
index 0a49ae81bb7c5ba401f8e2fbba2191ae8a550305..4d1fd8a1c6aba8cc14ba7add656ece13ed0bc665 100644 (file)
@@ -24,20 +24,18 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
+require_once($CFG->libdir.'/authlib.php');
+
 /**
  * Shibboleth authentication plugin.
  */
-class auth_plugin_shibboleth {
-
-    /**
-     * The configuration details for the plugin.
-     */
-    var $config;
+class auth_plugin_shibboleth extends auth_plugin_base {
 
     /**
      * Constructor.
      */
     function auth_plugin_shibboleth() {
+        $this->authtype = 'shibboleth';
         $this->config = get_config('auth/shibboleth');
     }
 
@@ -148,6 +146,22 @@ class auth_plugin_shibboleth {
         return false;
     }
 
+    function prelogin_hook() {
+        global $SESSION, $CFG;
+
+        //TODO: fix the code
+        return;
+
+    //  See http://moodle.org/mod/forum/discuss.php?d=39918#187611
+    //    if ($CFG->auth == 'shibboleth') {
+    //        if (!empty($SESSION->shibboleth_checked) ) {  // Just come from there
+    //            unset($SESSION->shibboleth_checked);
+    //        } else if (empty($_POST)) {                   // No incoming data, so redirect
+    //            redirect($CFG->wwwroot.'/auth/shibboleth/index.php');
+    //        }
+    //    }
+    }
+
     /**
      * Prints a form for configuring this authentication plugin.
      *
index 417d213941392e2e41d8849a9bd4fa414afa562e..65fa64b2dfda59996d901ecdac77af43441cae01 100644 (file)
@@ -29,7 +29,7 @@ class block_login extends block_base {
         
         if (!empty($CFG->registerauth)) {
             $authplugin = get_auth_plugin($CFG->registerauth);
-            if (method_exists($authplugin, 'user_signup')) {
+            if ($authplugin->can_signup()) {
                 $signup = $wwwroot . '/login/signup.php';
             }
         }
index fd7d1a1df91e6d3ffa3021e92256b4011243b5a9..bdaadd50424a4df15e75bb96bebe9b497c9ffb64 100644 (file)
@@ -304,6 +304,7 @@ $string['changepassword'] = 'Change password URL';
 $string['changepasswordhelp'] = 'Here you can specify a location at which your users can recover or change their username/password if they\'ve forgotten it.  This will be provided to users as a button on the login page and their user page.  if you leave this blank the button will not be printed.';
 $string['chooseauthmethod'] = 'Choose an authentication method';
 $string['createpasswordifneeded'] = 'Create password if needed';
+$string['errorpasswordupdate'] = 'Error updating password, password not changed';
 $string['infilefield'] = 'Field required in file';
 $string['forcechangepassword'] = 'Force change password';
 $string['forcechangepassword_help'] = 'Force users to change password on their next login to Moodle.';
index 09e849eef4756b98a5f344b8ccbd848887bb0897..9f4c2eebe6c7850b1828916aae1e93d47005eb81 100755 (executable)
@@ -2194,9 +2194,10 @@ function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $time
  * @param $userid
  * @param $groupid
  * @param $contextid
+ * @param $enrol unassign only if enrolment type matches, NULL means anything
  * @return boolean - success or failure
  */
-function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) {
+function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0, $enrol=NULL) {
 
     global $USER, $CFG;
 
@@ -2209,6 +2210,9 @@ function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) {
             $select[] = $arg.' = '.$$arg;
         }
     }
+    if (!empty($enrol)) {
+        $select[] = "enrol='$enrol'";
+    }
 
     if ($select) {
         if ($ras = get_records_select('role_assignments', implode(' AND ', $select))) {
diff --git a/lib/authlib.php b/lib/authlib.php
new file mode 100644 (file)
index 0000000..d701a2d
--- /dev/null
@@ -0,0 +1,281 @@
+<?php
+/**
+ * @author Martin Dougiamas
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package moodle multiauth
+ *
+ * Multiple plugin authentication
+ * Support library
+ *
+ * 2006-08-28  File created, AUTH return values defined.
+ */
+
+/**
+ * Returned when the login was successful.
+ */
+define('AUTH_OK',     0);
+
+/**
+ * Returned when the login was unsuccessful.
+ */
+define('AUTH_FAIL',   1);
+
+/**
+ * Returned when the login was denied (a reason for AUTH_FAIL).
+ */
+define('AUTH_DENIED', 2);
+
+/**
+ * Returned when some error occurred (a reason for AUTH_FAIL).
+ */
+define('AUTH_ERROR',  4);
+
+/**
+ * Authentication - error codes for user confirm
+ */
+define('AUTH_CONFIRM_FAIL', 0);
+define('AUTH_CONFIRM_OK', 1);
+define('AUTH_CONFIRM_ALREADY', 2);
+define('AUTH_CONFIRM_ERROR', 3);
+
+
+
+/**
+ * Abstract authentication plugin.
+ */
+class auth_plugin_base {
+
+    /**
+     * The configuration details for the plugin.
+     */
+    var $config;
+
+    /**
+     * Authentication plugin type - the same as db field.
+     */
+    var $authtype;
+
+    /**
+     * Returns true if the username and password work and false if they are
+     * wrong or don't exist.
+     *
+     * @param string $username The username (with system magic quotes)
+     * @param string $password The password (with system magic quotes)
+     *
+     * @return bool Authentication success or failure.
+     */
+    function user_login($username, $password) {
+        error('Abstract user_login() method must be overriden.');
+    }
+
+    /**
+     * Returns true if this authentication plugin can change the user's
+     * password.
+     *
+     * @return bool
+     */
+    function can_change_password() {
+        //override if needed
+        return false;
+    }
+
+    /**
+     * Returns the URL for changing the user's pw, or empty if the default can
+     * be used.
+     *
+     * @return string
+     */
+    function change_password_url() {
+        //override if needed
+        return '';
+    }
+
+    /**
+     * Returns true if this authentication plugin is 'internal'.
+     *
+     * @return bool
+     */
+    function is_internal() {
+        //override if needed
+        return true;
+    }
+
+    /**
+     * Change a user's password
+     *
+     * @param  object  $user        User table object  (with system magic quotes)
+     * @param  string  $newpassword Plaintext password (with system magic quotes)
+     *
+     * @return bool                  True on success
+     */
+    function user_update_password($user, $newpassword) {
+        //override if needed
+        return true;
+    }
+
+    /**
+     * Called when the user record is updated.
+     * Modifies user in external database. It takes olduser (before changes) and newuser (after changes)
+     * conpares information saved modified information to external db.
+     *
+     * @param mixed $olduser     Userobject before modifications    (without system magic quotes)
+     * @param mixed $newuser     Userobject new modified userobject (without system magic quotes)
+     * @return boolean true if updated or update ignored; false if error
+     *
+     */
+    function user_update($olduser, $newuser) {
+        //override if needed
+        return true;
+    }
+
+    /**
+     * Returns true if plugin allows resetting of internal password.
+     *
+     * @return bool
+     */
+    function can_reset_password() {
+        //override if needed
+        return false;
+    }
+
+    /**
+     * Returns true if plugin allows resetting of internal password.
+     *
+     * @return bool
+     */
+    function can_signup() {
+        //override if needed
+        return false;
+    }
+
+    /**
+     * Sign up a new user ready for confirmation.
+     * Password is passed in plaintext.
+     *
+     * @param object $user new user object (with system magic quotes)
+     * @param boolean $notify print notice with link and terminate
+     */
+    function user_signup($user, $notify=true) {
+        //override when can signup
+        error('user_signup method must be overriden if signup enabled');
+    }
+
+    /**
+     * Returns true if plugin allows confirming of new users.
+     *
+     * @return bool
+     */
+    function can_confirm() {
+        //override if needed
+        return false;
+    }
+
+    /**
+     * Confirm the new user as registered.
+     *
+     * @param string $username (with system magic quotes)
+     * @param string $confirmsecret (with system magic quotes)
+     */
+    function user_confirm($username, $confirmsecret) {
+        //override when can confirm
+        error('user_confirm method must be overriden if confirm enabled');
+    }
+
+    /**
+     * Checks if user exists in external db
+     *
+     * @param string $username (with system magic quotes)
+     */
+    function user_exists() {
+        //override if needed
+        return false;
+    }
+
+    /**
+     * Activates (enables) user in external db so user can login using username/password from external db
+     *
+     * @param mixed $username    username (with system magic quotes)
+     * @return boolen result
+     */
+    function user_activate($username) {
+        //override if needed
+        return true;
+    }
+
+    /**
+     * return number of days to user password expires
+     *
+     * If userpassword does not expire it should return 0. If password is already expired
+     * it should return negative value.
+     *
+     * @param mixed $username username (with system magic quotes)
+     * @return integer
+     */
+    function password_expire($username) {
+        return 0;
+    }
+    /**
+     * Sync roles for this user - usually creator
+     *
+     * @param $user object user object (without system magic quotes)
+     */
+    function sync_roles($user) {
+        //override if needed
+    }
+
+    /**
+     * Read user information from external database and returns it as array().
+     * Function should return all information available. If you are saving
+     * this information to moodle user-table you should honor syncronization flags
+     *
+     * @param string $username username (with system magic quotes)
+     *
+     * @return mixed array with no magic quotes or false on error
+     */
+    function get_userinfo($username) {
+        //override if needed
+        return array();
+    }
+
+    /**
+     * A chance to validate form data, and last chance to
+     * do stuff before it is inserted in config_plugin
+     */
+     function validate_form(&$form, &$err) {
+        //override if needed
+    }
+
+    /**
+     * Prelogin actions.
+     */
+    function prelogin_hook() {
+        //override if needed
+    }
+
+    /**
+     * Post authentication hook.
+     */
+    function user_authenticated_hook($user, $username, $password) {
+    /// TODO: review following code - looks hackish :-( mnet should obsole this, right?
+    /// Log in to a second system if necessary
+        global $CFG;
+
+        if (!empty($CFG->sso)) {
+            include_once($CFG->dirroot .'/sso/'. $CFG->sso .'/lib.php');
+            if (function_exists('sso_user_login')) {
+                if (!sso_user_login($username, $password)) {   // Perform the signon process
+                    notify('Second sign-on failed');
+                }
+            }
+        }
+    }
+
+    /**
+     * Prelogout actions.
+     */
+    function prelogout_hook() {
+        //override if needed
+    }
+}
+
+?>
index d42ba5eb71a5b377fbc17a057e58e6e8cd4a2ef7..3ef47adfe4357581c88b6b4239d77f8cb31e8412 100644 (file)
@@ -826,14 +826,15 @@ function main_upgrade($oldversion=0) {
 
     if ($oldversion < 2004082600) {
         //update auth-fields for external users
-        include_once ($CFG->dirroot."/auth/".$CFG->auth."/lib.php");
+        // following code would not work in 1.8
+/*        include_once ($CFG->dirroot."/auth/".$CFG->auth."/lib.php");
         if (function_exists('auth_get_userlist')) {
             $externalusers = auth_get_userlist();
             if (!empty($externalusers)){
                 $externalusers = '\''. implode('\',\'',$externalusers).'\'';
                 execute_sql("UPDATE {$CFG->prefix}user SET auth = '$CFG->auth' WHERE username  IN ($externalusers)");
             }
-        }
+        }*/
     }
 
     if ($oldversion < 2004082900) {  // Make sure guest is "manual" too.
index 22fe33aa2549b344625e312a4f8b8941b58939ea..850d1ac148439a4e23becf69dd9d2bffeb1962c9 100644 (file)
@@ -559,14 +559,15 @@ function main_upgrade($oldversion=0) {
     
     if ($oldversion < 2004082600) {
         //update auth-fields for external users
-        include_once ($CFG->dirroot."/auth/".$CFG->auth."/lib.php");
+        // following code would not work in 1.8
+/*        include_once ($CFG->dirroot."/auth/".$CFG->auth."/lib.php");
         if (function_exists('auth_get_userlist')) {
             $externalusers = auth_get_userlist();
             if (!empty($externalusers)){
                 $externalusers = '\''. implode('\',\'',$externalusers).'\'';
                 execute_sql("UPDATE {$CFG->prefix}user SET auth = '$CFG->auth' WHERE username  IN ($externalusers)");
             }
-        }
+        }*/
     }
         
     if ($oldversion < 2004082900) {  // Make sure guest is "manual" too.
index a822ba8333b2addaf13526525b4587d298e01aa9..d8cc0bf7f40fe410cc336fa401e1688a62bb8598 100644 (file)
@@ -261,14 +261,6 @@ define ('BLOG_COURSE_LEVEL', 3);
 define ('BLOG_SITE_LEVEL', 4);
 define ('BLOG_GLOBAL_LEVEL', 5);
 
-/**
- * Authentication - error codes for user confirm
- */
-define('AUTH_CONFIRM_FAIL', 0);
-define('AUTH_CONFIRM_OK', 1);
-define('AUTH_CONFIRM_ALREADY', 2);
-define('AUTH_CONFIRM_ERROR', 3);
-
 
 
 /// PARAMETER HANDLING ////////////////////////////////////////////////////
@@ -1673,7 +1665,7 @@ function require_login($courseorid=0, $autologinguest=true, $cm=null) {
     if (get_user_preferences('auth_forcepasswordchange') && empty($USER->realuser)) {
         if ($userauth->can_change_password()) {
             $SESSION->wantsurl = $FULLME;
-            if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
+            if ($userauth->change_password_url()) {
                 //use plugin custom url
                 redirect($userauth->change_password_url());
             } else {
@@ -2462,12 +2454,10 @@ function create_user_record($username, $password, $auth='') {
 
     $authplugin = get_auth_plugin($auth);
 
-    if (method_exists($authplugin, 'get_userinfo')) {
-        if ($newinfo = $authplugin->get_userinfo($username)) {
-            $newinfo = truncate_userinfo($newinfo);
-            foreach ($newinfo as $key => $value){
-                $newuser->$key = addslashes($value);
-            }
+    if ($newinfo = $authplugin->get_userinfo($username)) {
+        $newinfo = truncate_userinfo($newinfo);
+        foreach ($newinfo as $key => $value){
+            $newuser->$key = addslashes($value);
         }
     }
 
@@ -2511,24 +2501,23 @@ function create_user_record($username, $password, $auth='') {
  * @return user A {@link $USER} object
  */
 function update_user_record($username, $authplugin) {
-    if (method_exists($authplugin, 'get_userinfo')) {
-        $username = trim(moodle_strtolower($username)); /// just in case check text case
-
-        $oldinfo = get_record('user', 'username', $username, '','','','', 'username, auth');
-        $userauth = get_auth_plugin($oldinfo->auth);
-
-        if ($newinfo = $authplugin->get_userinfo($username)) {
-            $newinfo = truncate_userinfo($newinfo);
-            foreach ($newinfo as $key => $value){
-                $confkey = 'field_updatelocal_' . $key;
-                if (!empty($userauth->config->$confkey) and $userauth->config->$confkey === 'onlogin') {
-                    $value = addslashes(stripslashes($value));   // Just in case
-                    set_field('user', $key, $value, 'username', $username)
-                        or error_log("Error updating $key for $username");
-                }
+    $username = trim(moodle_strtolower($username)); /// just in case check text case
+
+    $oldinfo = get_record('user', 'username', $username, '','','','', 'username, auth');
+    $userauth = get_auth_plugin($oldinfo->auth);
+
+    if ($newinfo = $userauth->get_userinfo($username)) {
+        $newinfo = truncate_userinfo($newinfo);
+        foreach ($newinfo as $key => $value){
+            $confkey = 'field_updatelocal_' . $key;
+            if (!empty($userauth->config->$confkey) and $userauth->config->$confkey === 'onlogin') {
+                $value = addslashes(stripslashes($value));   // Just in case
+                set_field('user', $key, $value, 'username', $username)
+                    or error_log("Error updating $key for $username");
             }
         }
     }
+
     return get_complete_user_data('username', $username);
 }
 
@@ -2649,29 +2638,10 @@ function authenticate_user_login($username, $password) {
             // if user not found, create him
             $user = create_user_record($username, $password, $auth);
         }
-        // fix for MDL-6928
-        if (method_exists($authplugin, 'iscreator')) {
-            $sitecontext = get_context_instance(CONTEXT_SYSTEM);
-            if ($creatorroles = get_roles_with_capability('moodle/legacy:coursecreator', CAP_ALLOW)) {
-                $creatorrole = array_shift($creatorroles); // We can only use one, let's use the first one
-                // Check if the user is a creator
-                if ($authplugin->iscreator($username)) { // Following calls will not create duplicates
-                    role_assign($creatorrole->id, $user->id, 0, $sitecontext->id, 0, 0, 0, $auth);
-                } else {
-                    role_unassign($creatorrole->id, $user->id, 0, $sitecontext->id);
-                }
-            }
-        }
 
-    /// Log in to a second system if necessary
-        if (!empty($CFG->sso)) {
-            include_once($CFG->dirroot .'/sso/'. $CFG->sso .'/lib.php');
-            if (function_exists('sso_user_login')) {
-                if (!sso_user_login($username, $password)) {   // Perform the signon process
-                    notify('Second sign-on failed');
-                }
-            }
-        }
+        $authplugin->sync_roles($user);
+
+        $authplugin->user_authenticated_hook($user, $username, $password);
 
         return $user;
 
@@ -3631,7 +3601,7 @@ function send_password_change_info($user) {
     $data->admin = fullname($from).' ('. $from->email .')';
 
      $userauth = get_auth_plugin($user->auth);
-    if ($userauth->can_change_password() and method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
+    if ($userauth->can_change_password() and $userauth->change_password_url()) {
         // we have some external url for password cahnging
         $data->link .= $userauth->change_password_url();
 
index 7bf7c6b5974393b6dae9fdc340cb1468eedd99c2..0e8c1fd2968cb7b2b0cba259dd6e46c47f4ef2dc 100644 (file)
@@ -3,28 +3,20 @@
     require_once('../config.php');
     require_once('change_password_form.php');
 
-    $id = optional_param('id', SITEID, PARAM_INT);
+    $id = optional_param('id', SITEID, PARAM_INT); // current course
 
     //HTTPS is potentially required in this page
     httpsrequired();
 
-    $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
+    $systemcontext = get_context_instance(CONTEXT_SYSTEM);
 
     if (!$course = get_record('course', 'id', $id)) {
         error('No such course!');
     }
 
-    if (is_mnet_remote_user($USER)) {
-        $message = get_string('usercannotchangepassword', 'mnet');
-        if ($idprovider = get_record('mnet_host', 'id', $USER->mnethostid)) {
-            $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
-        }
-        error($message);
-    }
-
     // require proper login; guest can not change password
-    // TODO: add change password capability so that we can prevent participants to change password
-    if (empty($USER->id) or isguestuser() or has_capability('moodle/legacy:guest', $sitecontext, $USER->id, false)) {
+    // TODO: add change password capability so that we can prevent participants from changing password
+    if (empty($USER->id) or isguestuser() or has_capability('moodle/legacy:guest', $systemcontext, $USER->id, false)) {
         if (empty($SESSION->wantsurl)) {
             $SESSION->wantsurl = $CFG->httpswwwroot.'/login/change_password.php';
         }
         error('Can not use this script when "Logged in as"!');
     }
 
+    if (is_mnet_remote_user($USER)) {
+        $message = get_string('usercannotchangepassword', 'mnet');
+        if ($idprovider = get_record('mnet_host', 'id', $USER->mnethostid)) {
+            $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
+        }
+        error($message);
+    }
+
     // load the appropriate auth plugin
     $userauth = get_auth_plugin($USER->auth);
 
         error(get_string('nopasswordchange', 'auth'));
     }
 
-    if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
+    if ($userauth->change_password_url()) {
         // this internal scrip not used
         redirect($userauth->change_password_url());
     }
 
     $mform = new login_change_password_form();
-    $mform->set_data(array('id'=>$course->id, 'username'=>$USER->username));
+    $mform->set_data(array('id'=>$course->id));
 
     if ($mform->is_cancelled()) {
         redirect($CFG->wwwroot.'/user/view.php?id='.$USER->id.'&amp;course='.$course->id);
     } else if ($data = $mform->get_data()) {
 
-        if (!has_capability('moodle/user:update', $sitecontext)) {
-            //ignore submitted username - the same is done in form validation
-            $data->username = $USER->username;
-        }
-
-        if ($data->username == $USER->username) {
-            $user =& $USER;
-        } else {
-            $user = get_complete_user_data('username', $data->username);
+        if (!$userauth->user_update_password(addslashes_recursive($USER), $data->newpassword1)) {
+            error(get_string('errorpasswordupdate', 'auth'));
         }
 
         // register success changing password
-        unset_user_preference('auth_forcepasswordchange', $user->id);
+        unset_user_preference('auth_forcepasswordchange', $USER->id);
 
         $strpasswordchanged = get_string('passwordchanged');
 
-        add_to_log($course->id, 'user', 'change password', "view.php?id=$user->id&amp;course=$course->id", "$user->id");
+        add_to_log($course->id, 'user', 'change password', "view.php?id=$USER->id&amp;course=$course->id", "$USER->id");
 
         $fullname = fullname($USER, true);
 
index 92952f113686a479eccea3f40aa0489fda03ef72..685200219e1489c29b2cd858b84effbfdbc9d556 100644 (file)
@@ -7,29 +7,16 @@ class login_change_password_form extends moodleform {
     function definition() {
         global $USER;
 
-        $mform    =& $this->_form;
+        $mform =& $this->_form;
 
         $mform->addElement('header', '', get_string('changepassword'), '');
-        $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
 
         // visible elements
-        if (has_capability('moodle/user:update', $sitecontext)) {
-            $mform->addElement('text', 'username', get_string('username'));
-            $mform->addRule('username', get_string('required'), 'required', null, 'client');
-            $mform->setType('username', PARAM_RAW);
-        } else {
-            $mform->addElement('hidden', 'username');
-            $mform->setType('username', PARAM_RAW);
-        }
+        $mform->addElement('static', 'username', get_string('username'));
 
-        if (has_capability('moodle/user:update', $sitecontext)) {
-            $mform->addElement('hidden', 'password');
-            $mform->setType('username', PARAM_RAW);
-        } else {
-            $mform->addElement('password', 'password', get_string('oldpassword'));
-            $mform->addRule('password', get_string('required'), 'required', null, 'client');
-            $mform->setType('password', PARAM_RAW);
-        }
+        $mform->addElement('password', 'password', get_string('oldpassword'));
+        $mform->addRule('password', get_string('required'), 'required', null, 'client');
+        $mform->setType('password', PARAM_RAW);
 
         $mform->addElement('password', 'newpassword1', get_string('newpassword'));
         $mform->addRule('newpassword1', get_string('required'), 'required', null, 'client');
@@ -55,44 +42,25 @@ class login_change_password_form extends moodleform {
 /// perform extra password change validation
     function validation($data){
         global $USER;
-        $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
         $errors = array();
 
-        if (has_capability('moodle/user:update', $sitecontext)) {
-            if (!$user = get_record('user', 'username', $data['username'])) {
-                $errors['username'] = get_string('invalidlogin');
-                return $errors;
-            }
-        } else {
-            update_login_count();
-
-            // ignore submitted username
-            if (!$user = authenticate_user_login($USER->username, $data['password'])) {
-                $errors['password'] = get_string('invalidlogin');
-                return $errors;
-            }
+        update_login_count();
 
-            reset_login_count();
-        }
-
-        // can not change guest user password
-        if ($user->username == 'guest') {
-            $errors['username'] = get_string('invalidlogin');
+        // ignore submitted username
+        if (!$user = authenticate_user_login($USER->username, $data['password'])) {
+            $errors['password'] = get_string('invalidlogin');
             return $errors;
         }
 
-        // can not change password of primary admin
-        $mainadmin = get_admin();
-        if ($user->id == $mainadmin->id and $USER->id != $mainadmin->id) {
-            $errors['username'] = get_string('adminprimarynoedit');
-            return $errors;
-        }
+        reset_login_count();
 
         if ($data['newpassword1'] <> $data['newpassword2']) {
             $errors['newpassword1'] = get_string('passwordsdiffer');
             $errors['newpassword2'] = get_string('passwordsdiffer');
             return $errors;
-        } else if (!has_capability('moodle/user:update', $sitecontext) and ($data['password'] == $data['newpassword1'])){
+        }
+
+        if ($data['password'] == $data['newpassword1']){
             $errors['newpassword1'] = get_string('mustchangepassword');
             $errors['newpassword2'] = get_string('mustchangepassword');
             return $errors;
index 72fba41cf6d9909b7f9d5a61459d1b88d6d142a3..fcfdeb6082c911984201dcb7db39ff1c85d4597f 100644 (file)
@@ -12,7 +12,7 @@
     }
     $authplugin = get_auth_plugin($CFG->registerauth);
 
-    if (!method_exists($authplugin, 'user_confirm')) {
+    if (!$authplugin->can_confirm()) {
         error("Sorry, you may not use this page.");
     }
 
 
         } else if ($confirmed == AUTH_CONFIRM_OK) {
             // Activate new user if necessary
-            if (method_exists($authplugin, 'user_activate')) {
-                if (!$authplugin->user_activate($username)) {
-                    error('Could not activate this user!');
-                }
+            if (!$authplugin->user_activate($username)) {
+                error('Could not activate this user!');
             }
 
             // The user has confirmed successfully, let's log them in
index 2d6e75b1f6ff2283714ef24dbd0bd681cc0c2685..4de719922459da33ebb31ba9ad481bd1e1538780 100644 (file)
@@ -98,7 +98,7 @@ if ($mform->is_cancelled()) {
 
         $userauth = get_auth_plugin($user->auth);
 
-        if (method_exists($userauth, 'can_reset_password') and $userauth->can_reset_password()) {
+        if ($userauth->can_reset_password()) {
             // reset internal password and notify user
 
             // set 'secret' string
index 569f038956a2bb01acca2d5e50102aff0cb49a24..43eebe1f3e4b78c8c0a7cc99c6d5bf5b9add0605 100644 (file)
@@ -23,9 +23,6 @@
         $session_has_timed_out = false;
     }
 
-    //HTTPS is potentially required in this page
-    httpsrequired();
-
 /// Check if the guest user exists.  If not, create one.
     if (! record_exists('user', 'username', 'guest')) {
         $guest->auth        = 'manual'; 
 
 $authsequence = explode(',', $CFG->auth); // auths, in sequence
 
-// Load alternative login screens if necessary
-if ($authsequence[0] == 'cas' and !empty($CFG->cas_enabled)) {
-        require($CFG->dirroot.'/auth/cas/login.php');
-    }
-
 if (!isset($CFG->registerauth)) {
     set_config('registerauth', '');
 }
@@ -62,17 +54,19 @@ if (!isset($CFG->auth_instructions)) {
     set_config('auth_instructions', '');
 }
 
-//  See http://moodle.org/mod/forum/discuss.php?d=39918#187611
-//    if ($CFG->auth == 'shibboleth') {
-//        if (!empty($SESSION->shibboleth_checked) ) {  // Just come from there
-//            unset($SESSION->shibboleth_checked);
-//        } else if (empty($_POST)) {                   // No incoming data, so redirect
-//            redirect($CFG->wwwroot.'/auth/shibboleth/index.php');
-//        }
-//    }
-    
 
-    
+// auth plugins can override these - SSO anyone?
+$frm  = false;
+$user = false;
+
+foreach($authsequence as $authname) {
+    $authplugin = get_auth_plugin($authname);
+    $authplugin->prelogin_hook();
+}
+
+//HTTPS is potentially required in this page
+httpsrequired();
+
 /// Define variables used in page
     if (!$site = get_site()) {
         error("No site found!");
@@ -91,16 +85,18 @@ if (!isset($CFG->auth_instructions)) {
 
     $loginurl = (!empty($CFG->alternateloginurl)) ? $CFG->alternateloginurl : '';
 
-    $frm = false;
-    $user = false;
 
+    if ($user !== false or $frm !== false) {
+        // some auth plugin already supplied these
 
-    if ((!empty($SESSION->wantsurl) and strstr($SESSION->wantsurl,'username=guest')) or $loginguest) {
+    } else if ((!empty($SESSION->wantsurl) and strstr($SESSION->wantsurl,'username=guest')) or $loginguest) {
         /// Log in as guest automatically (idea from Zbigniew Fiedorowicz)
         $frm->username = 'guest';
         $frm->password = 'guest';
+
     } else if (!empty($SESSION->wantsurl) && file_exists($CFG->dirroot.'/login/weblinkauth.php')) {
         // Handles the case of another Moodle site linking into a page on this site
+        //TODO: move weblink into own auth plugin
         include($CFG->dirroot.'/login/weblinkauth.php');
         if (function_exists(weblink_auth)) {
             $user = weblink_auth($SESSION->wantsurl);
@@ -110,6 +106,7 @@ if (!isset($CFG->auth_instructions)) {
         } else {
             $frm = data_submitted($loginurl);
         }
+
     } else {
         $frm = data_submitted($loginurl);
     }
@@ -120,7 +117,7 @@ if (!isset($CFG->auth_instructions)) {
 
         $errormsg = get_string("cookiesnotenabled");
 
-    } else  if ($frm) {                             // Login WITH cookies
+    } else if ($frm) {                             // Login WITH cookies
 
         $frm->username = trim(moodle_strtolower($frm->username));
 
@@ -132,10 +129,12 @@ if (!isset($CFG->auth_instructions)) {
             }
         }
 
-        if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) {
+        if ($user) {
+            //user already supplied by aut plugin prelogin hook
+        } else if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) {
             $user = false;    /// Can't log in as guest if guest button is disabled
             $frm = false;
-        } else if (!$user) {
+        } else {
             if (empty($errormsg)) {
                 $user = authenticate_user_login($frm->username, $frm->password);
             }
@@ -178,7 +177,7 @@ if (!isset($CFG->auth_instructions)) {
             //Select password change url
             $userauth = get_auth_plugin($USER->auth);
             if ($userauth->can_change_password()) {
-                if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
+                if ($userauth->change_password_url()) {
                     $passwordchangeurl = $userauth->change_password_url();
                 } else {
                     $passwordchangeurl = $CFG->httpswwwroot.'/login/change_password.php';
@@ -213,7 +212,7 @@ if (!isset($CFG->auth_instructions)) {
             }
 
           /// Go to my-moodle page instead of homepage if mymoodleredirect enabled
-            if (!has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->mymoodleredirect) and !isguest()) {
+            if (!has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM)) and !empty($CFG->mymoodleredirect) and !isguest()) {
                 if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') {
                     $urltogo = $CFG->wwwroot.'/my/';
                 }
@@ -222,7 +221,7 @@ if (!isset($CFG->auth_instructions)) {
 
             // check if user password has expired
             // Currently supported only for ldap-authentication module
-            if (method_exists($userauth, 'password_expire') and !empty($userauth->config->expiration) and $userauth->config->expiration == 1) {
+            if (!empty($userauth->config->expiration) and $userauth->config->expiration == 1) {
                     $days2expire = $userauth->password_expire($USER->username);
                     if (intval($days2expire) > 0 && intval($days2expire) < intval($CFG->{$USER->auth.'_expiration_warning'})) {
                         print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div class=\"langmenu\">$langmenu</div>"); 
index 0bcecc985494954a1526bec4ca46e139209ad872..012075adf0e41592bee6a133130aab19dd305da5 100644 (file)
@@ -90,16 +90,13 @@ if ($show_instructions) {
                    </form>
                  </div>
 <?php     } else if (!empty($CFG->registerauth)) {
-              echo format_text($CFG->auth_instructions);
-              $authplugin = get_auth_plugin($CFG->registerauth);
-              if (method_exists($authplugin, 'user_create')) { ?>
-                <div class="signupform">
-                  <form action="signup.php" method="get" id="signup">
-                  <div><input type="submit" value="<?php print_string("startsignup") ?>" /></div>
-                  </form>
-                </div>
-<?php         }
-          } else {
+              echo format_text($CFG->auth_instructions); ?>
+              <div class="signupform">
+                <form action="signup.php" method="get" id="signup">
+                <div><input type="submit" value="<?php print_string("startsignup") ?>" /></div>
+                </form>
+              </div>
+<?php     } else {
               echo format_text($CFG->auth_instructions);
           } ?>
       </div>
index e4e90d2f12aa10362fbd337688ad42bf8ef7516f..41c3039abad6a41237851d15aa00406f57817d7a 100644 (file)
@@ -3,11 +3,13 @@
 
     require_once("../config.php");
 
-    if (!empty($USER->mnethostid) and $USER->mnethostid != $CFG->mnet_localhost_id) {
-        $host = get_record('mnet_host', 'id', $USER->mnethostid);
-        $wwwroot = $host->wwwroot;
-    } else {
-        $wwwroot = $CFG->wwwroot;
+    // can be overriden by auth plugins
+    $redirect = $CFG->wwwroot.'/';
+
+    $authsequence = explode(',', $CFG->auth); // auths, in sequence
+    foreach($authsequence as $authname) {
+        $authplugin = get_auth_plugin($authname);
+        $authplugin->prelogin_hook();
     }
 
     $sesskey = optional_param('sesskey', '__notpresent__', PARAM_RAW); // we want not null default to prevent required sesskey warning
@@ -21,6 +23,6 @@
 
     require_logout();
 
-    redirect("$wwwroot/");
+    redirect($redirect);
 
 ?>
index 2aecde05c54e358eb5630190f4faf96d28b679dd..be1f6000c101972c08860696c1428e485756c161 100644 (file)
@@ -8,7 +8,7 @@
     }
     $authplugin = get_auth_plugin($CFG->registerauth);
 
-    if (!method_exists($authplugin, 'user_signup')) {
+    if (!$authplugin->can_signup()) {
         error("Sorry, you may not use this page.");
     }
 
@@ -28,7 +28,7 @@
         $user->secret      = random_string(15);
         $user->auth        = $CFG->registerauth;
 
-        $authplugin->user_signup($user, $notify=true); // prints notice and link to login/index.php
+        $authplugin->user_signup($user, true); // prints notice and link to login/index.php
         exit; //never reached
     }
 
index 7a7081ab3831421e5b5ee8b5f38e97d81ff8582d..20176bae5216c3eab98f491d512bfeb997c73292 100644 (file)
 require_once($CFG->libdir.'/formslib.php');
 
 class login_signup_form extends moodleform {
-       function definition() {
-               global $USER, $CFG;
+    function definition() {
+        global $USER, $CFG;
 
-               $mform =& $this->_form;
+        $mform =& $this->_form;
 
-               $mform->addElement('header', '', get_string('createuserandpass'), '');
+        $mform->addElement('header', '', get_string('createuserandpass'), '');
 
 
-               $mform->addElement('text', 'username', get_string('username'), 'size="12"');
-               $mform->setType('username', PARAM_NOTAGS);
-               $mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
+        $mform->addElement('text', 'username', get_string('username'), 'size="12"');
+        $mform->setType('username', PARAM_NOTAGS);
+        $mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
 
-               $mform->addElement('password', 'password', get_string('password'), 'size="12"');
-               $mform->setType('password', PARAM_RAW);
-               $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
+        $mform->addElement('password', 'password', get_string('password'), 'size="12"');
+        $mform->setType('password', PARAM_RAW);
+        $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
 
-               $mform->addElement('header', '', get_string('supplyinfo'),'');
+        $mform->addElement('header', '', get_string('supplyinfo'),'');
 
-               $mform->addElement('text', 'email', get_string('email'), 'size="25"');
-               $mform->setType('email', PARAM_NOTAGS);
-               $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
+        $mform->addElement('text', 'email', get_string('email'), 'size="25"');
+        $mform->setType('email', PARAM_NOTAGS);
+        $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
 
-               $mform->addElement('text', 'email2', get_string('emailagain'), 'size="25"');
-               $mform->setType('email2', PARAM_NOTAGS);
-               $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
+        $mform->addElement('text', 'email2', get_string('emailagain'), 'size="25"');
+        $mform->setType('email2', PARAM_NOTAGS);
+        $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
 
-               $mform->addElement('text', 'firstname', get_string('firstname'), 'size="25"');
-               $mform->setType('firstname', PARAM_TEXT);
-               $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'client');
+        $mform->addElement('text', 'firstname', get_string('firstname'), 'size="25"');
+        $mform->setType('firstname', PARAM_TEXT);
+        $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'client');
 
-               $mform->addElement('text', 'lastname', get_string('lastname'), 'size="25"');
-               $mform->setType('lastname', PARAM_TEXT);
-               $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'client');
+        $mform->addElement('text', 'lastname', get_string('lastname'), 'size="25"');
+        $mform->setType('lastname', PARAM_TEXT);
+        $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'client');
 
-               $mform->addElement('text', 'city', get_string('city'), 'size="20"');
-               $mform->setType('city', PARAM_TEXT);
-               $mform->addRule('city', get_string('missingcity'), 'required', null, 'client');
+        $mform->addElement('text', 'city', get_string('city'), 'size="20"');
+        $mform->setType('city', PARAM_TEXT);
+        $mform->addRule('city', get_string('missingcity'), 'required', null, 'client');
 
-               $country = get_list_of_countries();
-               $default_country[''] = get_string('selectacountry');
-               $country = array_merge($default_country, $country);
-               $mform->addElement('select', 'country', get_string('country'), $country);
-               $mform->addRule('country', get_string('missingcountry'), 'required', null, 'client');
+        $country = get_list_of_countries();
+        $default_country[''] = get_string('selectacountry');
+        $country = array_merge($default_country, $country);
+        $mform->addElement('select', 'country', get_string('country'), $country);
+        $mform->addRule('country', get_string('missingcountry'), 'required', null, 'client');
         $mform->setDefault('country', '');
 
         // buttons
         $this->add_action_buttons(true, get_string('createaccount'));
 
-       }
+    }
 
-       function definition_after_data(){
-               $mform =& $this->_form;
+    function definition_after_data(){
+        $mform =& $this->_form;
 
-               $mform->applyFilter('username', 'moodle_strtolower');
-               $mform->applyFilter('username', 'trim');
-       }
+        $mform->applyFilter('username', 'moodle_strtolower');
+        $mform->applyFilter('username', 'trim');
+    }
 
-       function validation($data) {
-               global $CFG;
-               $errors = array();
+    function validation($data) {
+        global $CFG;
+        $errors = array();
 
         $authplugin = get_auth_plugin($CFG->registerauth);
 
-               if (record_exists('user', 'username', $data['username'], 'mnethostid', $CFG->mnet_localhost_id)) {
-                       $errors['username'] = get_string('usernameexists');
-               } else {
-                       if (empty($CFG->extendedusernamechars)) {
-                               $string = eregi_replace("[^(-\.[:alnum:])]", '', $data['username']);
-                               if (strcmp($data['username'], $string)) {
-                                       $errors['username'] = get_string('alphanumerical');
-                               }
-                       }
-               }
-               if (method_exists($authplugin, 'user_exists')){
-                       if ($authplugin->user_exists($user->username)) {
-                               $errors['username'] = get_string('usernameexists');
-                       }
-               }
-
-
-               if (! validate_email($data['email'])) {
-                       $errors['email'] = get_string('invalidemail');
-
-               } else if (record_exists('user', 'email', $data['email'])) {
-                       $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
-               }
+        if (record_exists('user', 'username', $data['username'], 'mnethostid', $CFG->mnet_localhost_id)) {
+            $errors['username'] = get_string('usernameexists');
+        } else {
+            if (empty($CFG->extendedusernamechars)) {
+                $string = eregi_replace("[^(-\.[:alnum:])]", '', $data['username']);
+                if (strcmp($data['username'], $string)) {
+                    $errors['username'] = get_string('alphanumerical');
+                }
+            }
+        }
+
+        //check if user exists in external db
+        //TODO: maybe we should check all enabled plugins instead
+        if ($authplugin->user_exists($user->username)) {
+            $errors['username'] = get_string('usernameexists');
+        }
+
+
+        if (! validate_email($data['email'])) {
+            $errors['email'] = get_string('invalidemail');
+
+        } else if (record_exists('user', 'email', $data['email'])) {
+            $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
+        }
         if (empty($data['email2'])) {
             $errors['email2'] = get_string('missingemail');
 
         } else if ($data['email2'] != $data['email']) {
             $errors['email2'] = get_string('invalidemail');
         }
-               if (!isset($errors['email'])) {
-                       if ($err = email_is_not_allowed($data['email'])) {
-                               $errors['email'] = $err;
-                       }
+        if (!isset($errors['email'])) {
+            if ($err = email_is_not_allowed($data['email'])) {
+                $errors['email'] = $err;
+            }
 
-               }
+        }
 
 
-               if (0 == count($errors)){
-                       return true;
-               } else {
-                       return $errors;
-               }
+        if (0 == count($errors)){
+            return true;
+        } else {
+            return $errors;
+        }
 
 
-       }
+    }
 }
 
 ?>
index 4bb0f52e1a10c478e3e3134157621a84d2642fc2..a34776e2c9e74eace12556e50a4d7891cbd9dae4 100644 (file)
 
         $usernew->timemodified = time();
 
-        if (update_record('user', $usernew)) {
-            if (method_exists($authplugin, 'user_update')){
-                // pass a true $userold here
-                if (! $authplugin->user_update($user, $userform->get_data(false))) {
-                    // auth update failed, rollback for moodle
-                    update_record('user', addslashes_object($user));
-                    error('Failed to update user data on external auth: '.$usernew->auth.
-                            '. See the server logs for more details.');
-                }
-            };
-        } else {
+        if (!update_record('user', $usernew)) {
             error('Error updating user record');
         }
 
+        // pass a true $userold here
+        if (! $authplugin->user_update($user, $userform->get_data(false))) {
+            // auth update failed, rollback for moodle
+            update_record('user', addslashes_object($user));
+            error('Failed to update user data on external auth: '.$usernew->auth.
+                    '. See the server logs for more details.');
+        }
+
         //update preferences
         useredit_update_user_preference($usernew);
 
index b94045da39e4e1927dddca0903986d246ff735e1..0f3c291eda2a518a271f7c0f277e32e518a8458d 100644 (file)
             if (!update_record('user', $usernew)) {
                 error('Error updating user record');
             }
-            if (method_exists($authplugin, 'user_update')){
-                // pass a true $userold here
-                if (! $authplugin->user_update($user, $userform->get_data(false))) {
-                    // auth update failed, rollback for moodle
-                    update_record('user', addslashes_object($user));
-                    error('Failed to update user data on external auth: '.$usernew->auth.
-                            '. See the server logs for more details.');
-                }
-            };
+            // pass a true $userold here
+            if (! $authplugin->user_update($user, $userform->get_data(false))) {
+                // auth update failed, rollback for moodle
+                update_record('user', addslashes_object($user));
+                error('Failed to update user data on external auth: '.$usernew->auth.
+                        '. See the server logs for more details.');
+            }
 
             //set new password if specified
             if (!empty($usernew->newpassword)) {
                 if ($authplugin->can_change_password()) {
-                    if (method_exists($authplugin, 'user_update_password')){
-                        if (!$authplugin->user_update_password($usernew, $usernew->newpassword)){
-                            error('Failed to update password on external auth: ' . $usernew->auth .
-                                    '. See the server logs for more details.');
-                        }
-                    } else {
-                        error('Your external authentication module is misconfigued!');
+                    if (!$authplugin->user_update_password($usernew, $usernew->newpassword)){
+                        error('Failed to update password on external auth: ' . $usernew->auth .
+                                '. See the server logs for more details.');
                     }
                 }
             }
index 69a1075a776fae0522343885936c730d5e7ae2d2..71eb5204b7678f0090036764c400f51d8c59a0e9 100644 (file)
 
     $passwordchangeurl = false;
     if ($userauth->can_change_password()) {
-        if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
+        if ($userauth->change_password_url()) {
             $passwordchangeurl = $userauth->change_password_url();
         } else {
             if (empty($CFG->loginhttps)) {