MDL-58400 auth: New helper methods for identity providers
authorJun Pataleta <jun@moodle.com>
Wed, 5 Apr 2017 07:20:30 +0000 (15:20 +0800)
committerJun Pataleta <jun@moodle.com>
Mon, 10 Apr 2017 03:12:03 +0000 (11:12 +0800)
* get_identity_providers(): Retrieves available auth identity providers
* prepare_identity_providers_for_output(): Prepares auth identity
    provider data for output (e.g. to templates, WS, etc)
* Use these helpers for the login renderer

auth/classes/output/login.php
lib/authlib.php
lib/upgrade.txt

index 421171a..fcb42b7 100644 (file)
@@ -103,12 +103,7 @@ class login implements renderable, templatable {
         }
 
         // Identity providers.
-        $identityproviders = [];
-        foreach ($authsequence as $authname) {
-            $authplugin = get_auth_plugin($authname);
-            $identityproviders = array_merge($identityproviders, $authplugin->loginpage_idp_list($SESSION->wantsurl));
-        }
-        $this->identityproviders = $identityproviders;
+        $this->identityproviders = \auth_plugin_base::get_identity_providers($authsequence);
     }
 
     /**
@@ -121,21 +116,8 @@ class login implements renderable, templatable {
     }
 
     public function export_for_template(renderer_base $output) {
-        global $CFG;
-
-        $identityproviders = array_map(function($idp) use ($output) {
-
-            if (!empty($idp['icon'])) {
-                $idp['iconurl'] = $output->pix_url($idp['icon']->key, $idp['icon']->component);
-            } else if ($idp['iconurl'] instanceof moodle_url) {
-                $idp['iconurl'] = $idp['iconurl']->out(false);
-            }
-            unset($idp['icon']);
-            if ($idp['url'] instanceof moodle_url) {
-                $idp['url'] = $idp['url']->out(false);
-            }
-            return $idp;
-        }, $this->identityproviders);
+
+        $identityproviders = \auth_plugin_base::prepare_identity_providers_for_output($this->identityproviders, $output);
 
         $data = new stdClass();
         $data->autofocusform = $this->autofocusform;
index 9f37803..ced11af 100644 (file)
@@ -610,6 +610,50 @@ class auth_plugin_base {
      */
     public function postlogout_hook($user) {
     }
+
+    /**
+     * Return the list of enabled identity providers.
+     *
+     * Each identity provider data contains the keys 'url' (string), 'name' (string) and 'icon' (pix_icon).
+     *
+     * @param array $authsequence site's auth sequence (list of auth plugins ordered)
+     * @return array an array of enabled identity providers
+     */
+    public static function get_identity_providers($authsequence) {
+        global $SESSION;
+
+        $identityproviders = [];
+        foreach ($authsequence as $authname) {
+            $authplugin = get_auth_plugin($authname);
+            $wantsurl = (isset($SESSION->wantsurl)) ? $SESSION->wantsurl : '';
+            $identityproviders = array_merge($identityproviders, $authplugin->loginpage_idp_list($wantsurl));
+        }
+        return $identityproviders;
+    }
+
+    /**
+     * Prepare a list of identity providers for output.
+     *
+     * @param array $identityproviders
+     * @param renderer_base $output
+     * @return array the identity providers ready for output
+     */
+    public static function prepare_identity_providers_for_output($identityproviders, renderer_base $output) {
+        $data = [];
+        foreach ($identityproviders as $idp) {
+            if (!empty($idp['icon'])) {
+                $idp['iconurl'] = $output->image_url($idp['icon']->key, $idp['icon']->component);
+            } else if ($idp['iconurl'] instanceof moodle_url) {
+                $idp['iconurl'] = $idp['iconurl']->out(false);
+            }
+            unset($idp['icon']);
+            if ($idp['url'] instanceof moodle_url) {
+                $idp['url'] = $idp['url']->out(false);
+            }
+            $data[] = $idp;
+        }
+        return $data;
+    }
 }
 
 /**
@@ -1002,4 +1046,4 @@ function display_auth_lock_options($settings, $auth, $userfields, $helptext, $ma
                     get_string('auth_fieldlock', 'auth'), '', 'unlocked', $lockoptions));
         }
     }
-}
\ No newline at end of file
+}
index f062097..19521a0 100644 (file)
@@ -76,6 +76,9 @@ information provided here is intended especially for developers.
 * Webservices core_course_search_courses and core_course_get_courses_by_field will always return the sortorder field.
 * core_course_external::get_activities_overview has been deprecated. Please do not call this function any more.
 * Changed the pix mustache template helper to accept context variables for the key, component and alt text.
+* New auth_plugin_base helper methods:
+  - get_identity_providers() - Retrieves available auth identity providers.
+  - prepare_identity_providers_for_output() - Prepares auth identity provider data for output (e.g. to templates, WS, etc.).
 
 === 3.2 ===