From: skodak <skodak>
Date: Tue, 27 Mar 2007 20:26:05 +0000 (+0000)
Subject: MDL-9053 - adding new function get_enabled_auth_plugins() - this should make the... 
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=c7b10b5f678446a79a3506f677279d3dcbe3ca59;p=moodle.git

MDL-9053 - adding new function get_enabled_auth_plugins() - this should make the code simpler and the bug should be gone forever; merged from MOODLE_18_STABLE
---

diff --git a/admin/auth.php b/admin/auth.php
index ff57cf87c6..d6ab820a1a 100644
--- a/admin/auth.php
+++ b/admin/auth.php
@@ -27,17 +27,11 @@ if (isset($CFG->auth_plugins_enabled)) {
     unset($CFG->auth_plugins_enabled);
 }
 
+get_enabled_auth_plugins(true); // fix the list of enabled auths
 if (empty($CFG->auth)) {
     $authsenabled = array();
 } else {
     $authsenabled = explode(',', $CFG->auth);
-    $authsenabled = array_unique($authsenabled);
-}
-
-$key = array_search('manual', $authsenabled);
-if ($key !== false) {
-    unset($authsenabled[$key]); // manual is always enabled anyway
-    set_config('auth', implode(',', $authsenabled));
 }
 
 if (!isset($CFG->registerauth)) {
diff --git a/admin/cron.php b/admin/cron.php
index 334c6069c7..dbc2a3027b 100644
--- a/admin/cron.php
+++ b/admin/cron.php
@@ -321,11 +321,8 @@
     }
 
 /// Run the auth cron, if any
-    if (empty($CFG->auth)) {
-        $auths = array();
-    } else {
-        $auths = explode(',', $CFG->auth); // only for enabled ones (without manual and nologin)
-    }
+    $auths = get_enabled_auth_plugins();
+
     mtrace("Running auth crons if required...");
     foreach ($auths as $auth) {
         $authplugin = get_auth_plugin($auth);
diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index d7c1f0884a..c399bb0df2 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -1829,7 +1829,7 @@ function require_logout() {
         add_to_log(SITEID, "user", "logout", "view.php?id=$USER->id&course=".SITEID, $USER->id, 0, $USER->id);
 
         //TODO: move following 2 ifs into auth plugins - add new logout hook
-        $authsequence = explode(',', $CFG->auth);
+        $authsequence = get_enabled_auth_plugins();
 
         if (in_array('cas', $authsequence) and $USER->auth == 'cas' and !empty($CFG->cas_enabled)) {
             require($CFG->dirroot.'/auth/cas/logout.php');
@@ -2376,15 +2376,13 @@ function exists_auth_plugin($auth) {
  * @return boolean Whether the plugin is enabled.
  */
 function is_enabled_auth($auth) {
-    global $CFG;
-
     if (empty($auth)) {
         return false;
-    } else if ($auth == 'manual') {
-        return true;
     }
 
-    return in_array($auth, explode(',', $CFG->auth));
+    $enabled = get_enabled_auth_plugins();
+
+    return in_array($auth, $enabled);
 }
 
 /**
@@ -2408,6 +2406,39 @@ function get_auth_plugin($auth) {
     return new $class;
 }
 
+/**
+ * Returns array of active auth plugins.
+ *
+ * @param bool $fix fix $CFG->auth if needed
+ * @return array
+ */
+function get_enabled_auth_plugins($fix=false) {
+    global $CFG;
+
+    $default = array('manual', 'nologin');
+
+    if (empty($CFG->auth)) {
+        $auths = array();
+    } else {
+        $auths = explode(',', $CFG->auth);
+    }
+
+    if ($fix) {
+        $auths = array_unique($auths);
+        foreach($auths as $k=>$authname) {
+            if (!exists_auth_plugin($authname) or in_array($authname, $default)) {
+                unset($auths[$k]);
+            }
+        }
+        $newconfig = implode(',', $auths);
+        if (!isset($CFG->auth) or $newconfig != $CFG->auth) {
+            set_config('auth', $newconfig);
+        }
+    }
+
+    return (array_merge($default, $auths));
+}
+
 /**
  * Returns true if an internal authentication method is being used.
  * if method not specified then, global default is assumed
@@ -2590,11 +2621,7 @@ function authenticate_user_login($username, $password) {
 
     global $CFG;
 
-    if (empty($CFG->auth)) {
-        $authsenabled = array('manual');
-    } else {
-        $authsenabled = explode(',', 'manual,'.$CFG->auth);
-    }
+    $authsenabled = get_enabled_auth_plugins();
 
     if ($user = get_complete_user_data('username', $username)) {
         $auth = empty($user->auth) ? 'manual' : $user->auth;  // use manual if auth not set
diff --git a/login/index.php b/login/index.php
index f1151ca17c..317be9b830 100644
--- a/login/index.php
+++ b/login/index.php
@@ -41,10 +41,7 @@
         }
     }
 
-/// Load alternative login screens if necessary
-
-
-$cfgauthsequence = explode(',', $CFG->auth); // auths, in sequence
+// setup and verify auth settings
 
 if (!isset($CFG->registerauth)) {
     set_config('registerauth', '');
@@ -54,29 +51,12 @@ if (!isset($CFG->auth_instructions)) {
     set_config('auth_instructions', '');
 }
 
-
-$authsequence = array();
-$fixauthseq = false;
-
-// fix auth sequence if needed to prevent fatal errors during login
-foreach($cfgauthsequence as $authname) {
-    if (exists_auth_plugin($authname)) {
-        $authsequence[] = $authname;
-        continue;
-    } else {
-        $fixauthseq = true;
-    }
-}
-if ($fixauthseq) {
-    set_config('auth', implode(',', $authsequence));
-}
-
 // auth plugins may override these - SSO anyone?
 $frm  = false;
 $user = false;
 
+$authsequence = get_enabled_auth_plugins(true); // auths, in sequence
 foreach($authsequence as $authname) {
-    // manual and nologin is not processed here
     $authplugin = get_auth_plugin($authname);
     $authplugin->prelogin_hook();
 }
diff --git a/login/logout.php b/login/logout.php
index 4b7abf69d5..2883a33378 100644
--- a/login/logout.php
+++ b/login/logout.php
@@ -15,7 +15,7 @@
         die;
     }
 
-    $authsequence = explode(',', $CFG->auth); // auths, in sequence
+    $authsequence = get_enabled_auth_plugins(); // auths, in sequence
     foreach($authsequence as $authname) {
         $authplugin = get_auth_plugin($authname);
         $authplugin->prelogout_hook();