]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12558 modify emails as they are being sent
authorpeterbulmer <peterbulmer>
Sun, 3 Aug 2008 23:24:12 +0000 (23:24 +0000)
committerpeterbulmer <peterbulmer>
Sun, 3 Aug 2008 23:24:12 +0000 (23:24 +0000)
replace urls in-place to direct users to their idp first

auth/mnet/jump.php
lib/db/install.xml
lib/db/upgrade.php
lib/moodlelib.php
mnet/lib.php
version.php

index 2504723de84bcaa80c03848353c2dbd1dab610fb..f7796a5ea5450981e4f543d4bfcdcece064974d8 100644 (file)
@@ -14,7 +14,7 @@
 
 require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
 
-require_login();
+require_login(SITEID,false);
 
 if (!is_enabled_auth('mnet')) {
     print_error('mnetdisable');
@@ -22,9 +22,15 @@ if (!is_enabled_auth('mnet')) {
 
 // grab the GET params - wantsurl could be anything - take it
 // with PARAM_RAW
-$hostid   = required_param('hostid',        PARAM_INT);
+$hostid = optional_param('hostid', '0', PARAM_INT);
+$hostwwwroot = optional_param('hostwwwroot', '', PARAM_URL);
 $wantsurl = optional_param('wantsurl', '', PARAM_RAW);
 
+// If hostid hasn't been specified, try getting it using wwwroot
+if (!$hostid) {
+    $hostid = $DB->get_field('mnet_host', 'id', array('wwwroot' => $hostwwwroot));
+}
+
 // start the mnet session and redirect browser to remote URL
 $mnetauth = get_auth_plugin('mnet');
 $url      = $mnetauth->start_jump_session($hostid, $wantsurl);
index 0154ff5aad0dc7695f8d77bbe852074557e682ce..cc7d1ffc1c9aea9111acf71e1cf5b621238a981b 100644 (file)
         <FIELD NAME="name" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="display_name"/>
         <FIELD NAME="display_name" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="name" NEXT="xmlrpc_server_url"/>
         <FIELD NAME="xmlrpc_server_url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="display_name" NEXT="sso_land_url"/>
-        <FIELD NAME="sso_land_url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="xmlrpc_server_url"/>
+        <FIELD NAME="sso_land_url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="xmlrpc_server_url" NEXT="sso_jump_url"/>
+        <FIELD NAME="sso_jump_url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="sso_land_url"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
   <STATEMENTS>
     <STATEMENT NAME="insert mnet_application" TYPE="insert" TABLE="mnet_application" COMMENT="Initial insert of records on table mnet_application" NEXT="insert log_display">
       <SENTENCES>
-        <SENTENCE TEXT="(name, display_name, xmlrpc_server_url, sso_land_url) VALUES ('moodle','Moodle','/mnet/xmlrpc/server.php', '/auth/mnet/land.php')" />
-        <SENTENCE TEXT="(name, display_name, xmlrpc_server_url, sso_land_url) VALUES ('mahara','Mahara','/api/xmlrpc/server.php',  '/auth/xmlrpc/land.php')" />
+        <SENTENCE TEXT="(name, display_name, xmlrpc_server_url, sso_land_url, sso_jump_url) VALUES ('moodle','Moodle','/mnet/xmlrpc/server.php', '/auth/mnet/land.php', '/auth/mnet/jump.php')" />
+        <SENTENCE TEXT="(name, display_name, xmlrpc_server_url, sso_land_url, sso_jump_url) VALUES ('mahara','Mahara','/api/xmlrpc/server.php',  '/auth/xmlrpc/land.php', '/auth/xmlrpc/jump.php')" />
       </SENTENCES>
     </STATEMENT>
     <STATEMENT NAME="insert log_display" TYPE="insert" TABLE="log_display" COMMENT="Initial insert of records on table log_display" PREVIOUS="insert mnet_application">
index d3cfa033fdfeac4e5002c2c28accbf114aa59937..67ea3ed51481c5c38c0ab160dcee8e4d72ea73d9 100644 (file)
@@ -551,6 +551,21 @@ function xmldb_main_upgrade($oldversion=0) {
         upgrade_main_savepoint($result, 2008073114);
     }
 
+    if ($result && $oldversion < 2008080400) {
+        // Add field ssl_jump_url to mnet application, and populate existing default applications
+        $table = new xmldb_table('mnet_application');
+        $field = new xmldb_field('sso_jump_url');
+        if (!$dbman->field_exists($table, $field)) {
+            $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
+            $result = $result && $dbman->add_field($table, $field);
+            $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
+            $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
+        }
+
+        /// Main savepoint reached
+        upgrade_main_savepoint($result, 2008080400);
+    }
+
     return $result;
 }
 
index fe3080fd1bf687dd4b671587ac18da81a04604dd..b4999efbe2ed230926c5febb68283f9c2b805469 100644 (file)
@@ -3993,7 +3993,8 @@ function &get_mailer($action='get') {
  */
 function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $attachment='', $attachname='', $usetrueaddress=true, $replyto='', $replytoname='', $wordwrapwidth=79) {
 
-    global $CFG, $FULLME;
+    global $CFG, $FULLME, $IDPJUMPURL;
+    static $mnetjumps = array();
 
     if (empty($user)) {
         return false;
@@ -4018,6 +4019,28 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $a
         return false;
     }
 
+    // If the user is a remote mnet user, parse the email text for URL to the
+    // wwwroot and modify the url to direct the user's browser to login at their
+    // home site (identity provider - idp) before hitting the link itself
+    if ($user->mnethostid > 1) {
+        require_once($CFG->dirroot.'/mnet/lib.php');
+        // Form the request url to hit the idp's jump.php
+        if (isset($mnetjumps[$user->mnethostid])) {
+            $IDPJUMPURL = $mnetjumps[$user->mnethostid];
+        } else {
+            $idp = mnet_get_peer_host($user->mnethostid);
+            $idpjumppath = mnet_get_app_jumppath($idp->applicationid);
+            $IDPJUMPURL = $idp->wwwroot . $idpjumppath . '?hostwwwroot=' . $CFG->wwwroot . '&wantsurl=';
+            $mnetjumps[$user->mnethostid] = $IDPJUMPURL;
+        }
+
+        $messagetext = preg_replace_callback("%($CFG->wwwroot[^[:space:]]*)%",
+                'mnet_sso_apply_indirection',
+                $messagetext);
+        $messagehtml = preg_replace_callback("%href=[\"'`]($CFG->wwwroot[\w_:\?=#&@/;.~-]*)[\"'`]%",
+                'mnet_sso_apply_indirection',
+                $messagehtml);
+    }
     $mail =& get_mailer();
 
     if (!empty($mail->SMTPDebug)) {
index 05e955965140f6ff308cdbb389012d50ca556670..9998df0792edf1e5019945ff5adc7ffecffefc57 100644 (file)
@@ -568,4 +568,60 @@ function mnet_update_sso_access_control($username, $mnet_host_id, $accessctrl) {
     }
     return true;
 }
+
+function mnet_get_peer_host ($mnethostid) {
+    global $DB;
+    static $hosts;
+    if (!isset($hosts[$mnethostid])) {
+        $host = $DB->get_record('mnet_host', array('id' => $mnethostid));
+        $hosts[$mnethostid] = $host;
+    }
+    return $hosts[$mnethostid];
+}
+
+/**
+ * Inline function to modify a url string so that mnet users are requested to
+ * log in at their mnet identity provider (if they are not already logged in)
+ * before ultimately being directed to the original url.
+ *
+ * uses global IDPJUMPURL - the url which user should initially be directed to
+ * @param array $url array with 2 elements
+ *     0 - context the url was taken from, possibly just the url, possibly href="url"
+ *     1 - the destination url
+ * @return string the url the remote user should be supplied with.
+ */
+function mnet_sso_apply_indirection ($url) {
+    global $IDPJUMPURL;
+
+    $localpart='';
+    $urlparts = parse_url($url[1]);
+    if($urlparts) {
+        if (isset($urlparts['path'])) {
+            $localpart .= $urlparts['path'];
+        }
+        if (isset($urlparts['query'])) {
+            $localpart .= '?'.$urlparts['query'];
+        }
+        if (isset($urlparts['fragment'])) {
+            $localpart .= '#'.$urlparts['fragment'];
+        }
+    }
+    $indirecturl = $IDPJUMPURL . urlencode($localpart);
+    //If we matched on more than just a url (ie an html link), return the url to an href format
+    if ($url[0] != $url[1]) {
+        $indirecturl = 'href="'.$indirecturl.'"';
+    }
+    return $indirecturl;
+}
+
+function mnet_get_app_jumppath ($applicationid) {
+    global $DB;
+    static $appjumppaths;
+    if (!isset($appjumppaths[$applicationid])) {
+        $ssojumpurl = $DB->get_field('mnet_application', 'sso_jump_url', array('id' => $applicationid));
+        $appjumppaths[$applicationid] = $ssojumpurl;
+    }
+    return $appjumppaths[$applicationid];
+}
+
 ?>
index abe82dc528b89bfff21d7ea3e3d1a30c765e7874..3dc0984a68cf2f4ef0d35d50123b1577f0e1b84e 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2008073114;  // YYYYMMDD   = date of the last version bump
+    $version = 2008080400;  // YYYYMMDD   = date of the last version bump
                             //         XX = daily increments
 
     $release = '2.0 dev (Build: 20080803)';  // Human-friendly version name