]> git.mjollnir.org Git - moodle.git/commitdiff
mnet: login operations (change pw, forgotpw) now handle multiauth and mnet remote...
authormartinlanghoff <martinlanghoff>
Thu, 4 Jan 2007 03:03:57 +0000 (03:03 +0000)
committermartinlanghoff <martinlanghoff>
Thu, 4 Jan 2007 03:03:57 +0000 (03:03 +0000)
login/change_password.php
login/confirm.php
login/forgot_password.php
login/index.php
login/index_form.html
login/logout.php
login/mnet_email.php [new file with mode: 0644]
login/signup.php
login/signup_form.php

index 6094e1b4edc7e46e825d68beaa3172a66af4b328..00b6e8f69e5571dd9934a11d1613546ad1be3d63 100644 (file)
         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 passwords anymore!
     // TODO: add change password capability so that we can prevent participants to change password
     if (empty($USER->id) or $USER->username=='guest' or has_capability('moodle/legacy:guest', $sitecontext, $USER->id, false)) {
             $user = get_complete_user_data('username', $data->username);
         }
 
-        if (is_internal_auth($user->auth)){
-            if (!update_internal_user_password($user, $data->newpassword1)) {
+        // load the appropriate auth plugin
+        $userauth = get_auth_plugin($user->auth);
+        if ($userauth->can_change_password()){
+            if ($userauth->user_update_password($user, $data->newpassword1)) {
+                // hash the $user->password field (without local db update)
+                update_internal_user_password($user, $frm->newpassword1, false);
+            } else {
                 error('Could not set the new password');
             }
         } else { // external users
-            // the relevant auth libs should be loaded already
-            // as part of form validation in function authenticate_user_login()
-            // check that we allow changes through moodle
-            if (!empty($CFG->{'auth_'. $user->auth.'_stdchangepassword'})) {
-                if (function_exists('auth_user_update_password')){
-                    // note that we pass cleartext password
-                    if (auth_user_update_password($user->username, $data->newpassword1)){
-                        update_internal_user_password($user, $data->newpassword1, false);
-                    } else {
-                        error('Could not set the new password');
-                    }
-                } else {
-                    error('The authentication module is misconfigured (missing auth_user_update_password)');
-                }
-            } else {
-                error('You cannot change your password this way.');
+            $message = 'You cannot change your password this way.';
+            if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
+                $message .= '<br /><br />' . get_string('passwordextlink')
+                    .  '<br /><br />' . '<a href="' . $userauth->change_password_url() . '">'
+                    .  $userauth->change_password_url() . '</a>';            error('You cannot change your password this way.');
             }
+            error($message);
         }
 
         // register success changing password
index 233b7dfddcb41a52e5bda8c43493fe672d699033..51c4b6f3d74f86bef6c0b5c343799f117d2aeb6a 100644 (file)
@@ -1,7 +1,6 @@
 <?php // $Id$
 
     require_once("../config.php");
-    require_once("../auth/$CFG->auth/lib.php");
 
     $data = optional_param('data', '', PARAM_CLEAN);  // Formatted as:  secret/username
 
             $username   = $s;
         }
 
-        $user = get_complete_user_data('username', $username );
+        $authplugin = get_auth_plugin('email');
+        $confirmed = $authplugin->user_confirm($username, $usersecret);
 
-        if (!empty($user)) {
-
-            if ($user->confirmed) {
+        if ($confirmed == AUTH_CONFIRM_ALREADY) {
+                $user = get_complete_user_data('username', $username);
                 print_header(get_string("alreadyconfirmed"), get_string("alreadyconfirmed"), "", "");
                 echo "<center><h3>".get_string("thanks").", ". fullname($user) . "</h3>\n";
                 echo "<h4>".get_string("alreadyconfirmed")."</h4>\n";
                 echo "<h3> -> <a href=\"$CFG->wwwroot/course/\">".get_string("courses")."</a></h3></center>\n";
                 print_footer();
                 exit;
-            }
-
-            if ($user->secret == $usersecret) {   // They have provided the secret key to get in
-
-                if (!set_field("user", "confirmed", 1, "id", $user->id)) {
-                    error("Could not confirm this user!");
-                }
-                if (!set_field("user", "firstaccess", time(), "id", $user->id)) {
-                    error("Could not set this user's first access date!");
-                }
-                if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and function_exists('auth_user_activate') ) {
-                    if (!auth_user_activate($user->username)) {
-                         error("Could not activate this user!");
+        }
+        if ($confirmed == AUTH_CONFIRM_OK) {
+                // Activate new user if necessary
+                $authplugin = get_auth_plugin($CFG->auth);
+                if (isset($CFG->auth_user_create) and $CFG->auth_user_create == 1 and method_exists($authplugin, 'user_activate') ) {
+                    if (!$authplugin->user_activate($username)) {
+                        error('Could not activate this user!');
                     }
                 }
 
                 // The user has confirmed successfully, let's log them in
-                
-                if (!$USER = get_complete_user_data('username', $user->username)) {
+
+                if (!$USER = get_complete_user_data('username', $username)) {
                     error("Something serious is wrong with the database");
                 }
 
                     unset($SESSION->wantsurl);
                     redirect("$goto");
                 }
+
                 print_header(get_string("confirmed"), get_string("confirmed"), "", "");
                 echo "<center><h3>".get_string("thanks").", ". fullname($USER) . "</h3>\n";
                 echo "<h4>".get_string("confirmed")."</h4>\n";
                 echo "<h3> -> <a href=\"$CFG->wwwroot/course/\">".get_string("courses")."</a></h3></center>\n";
                 print_footer();
                 exit;
-
-            } else {
+        } else {
                 error("Invalid confirmation data");
-            }
         }
     } else {
         error(get_string("errorwhenconfirming"));
index c44191053828b995a5c20196e41f5f04de40ca95..aacf5db714417cacb7874811064330922216d477 100644 (file)
@@ -46,15 +46,9 @@ if (isloggedin() && !isguest()) {
     redirect($CFG->wwwroot.'/index.php', $strloginalready, 5);
 }
 
-// changepassword link replaced by individual auth setting
+// instantiate default auth
 $auth = $CFG->auth; // the 'default' authentication method
-if (!empty($CFG->changepassword)) {
-    if (empty($CFG->{'auth_'.$auth.'_changepasswordurl'})) {
-       set_config('auth_'.$auth.'_changepasswordurl', $CFG->changepassword);
-    }
-    set_config('changepassword', '');
-}
-
+$defaultauth = get_auth_plugin($auth);
 
 $mform = new login_forgot_password_form();
 
@@ -114,8 +108,8 @@ if ($action == 'find' and $param = $mform->data_submitted()) {
             $errors[] = $strconfirmednot;
         } else {
             // what to do depends on the authentication method
-            $authmethod = $user->auth;
-            if (is_internal_auth($authmethod) or !empty($CFG->{'auth_'.$authmethod.'_stdchangepassword'})) {
+            $userauth = get_auth_plugin($user->auth);
+            if ($userauth->is_internal() or $userauth->can_change_password()) {
                 // handle internal authentication
 
                 // set 'secret' string
@@ -137,14 +131,13 @@ if ($action == 'find' and $param = $mform->data_submitted()) {
                 // if help text defined then we are going to display another page
                 $strextmessage = '';
                 $continue = false;
-                if (!empty($CFG->{'auth_'.$authmethod.'_changepasswordhelp'})) {
-                    $strextmessage = $CFG->{'auth_'.$authmethod.'_changepasswordhelp'}.'<br /><br />';
+                if (!empty($userauth->config->changepasswordhelp)) {
+                    $txt->extmessage = $userauth->config->changepasswordhelp .'<br /><br />';
                 }
                 // if url defined then add that to the message (with a standard message)
-                if (!empty($CFG->{'auth_'.$authmethod.'_changepasswordurl'})) {
+                if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
                     $strextmessage .= $strpasswordextlink . '<br /><br />';
-                    $link = $CFG->{'auth_'.$authmethod.'_changepasswordurl'};
-                    $strextmessage .= "<a href=\"$link\">$link</a>";
+                    $txt->extmessage .= '<a href="' . $userauth->change_password_url() . '">' . $userauth->change_password_url() . '</a>';
                 }
                 // if nothing to display, just do message that we can't help
                 if (empty($strextmessage)) {
@@ -281,4 +274,3 @@ $mform->display();
 print_footer();
 
 ?>
-
index d4cd256401ecd67c707501027918809bb0760948..35041250f1c8670ed7b9094a3a62edadbf9f3eb4 100644 (file)
@@ -1,5 +1,6 @@
 <?php // $Id$
 
+
     require_once("../config.php");
 
     $loginguest  = optional_param('loginguest', 0, PARAM_BOOL); // determines whether visitors are logged in as guest automatically
 
 /// Load alternative login screens if necessary
 
-    if ($CFG->auth == 'cas' && !empty($CFG->cas_enabled)) {
+
+// check if auth config broken (old config --> multi config)
+if (empty($CFG->auth_plugins_enabled) and ! empty($CFG->auth)) {
+    set_config('auth_plugins_enabled', $CFG->auth);
+}
+$authsequence = explode(',', $CFG->auth_plugins_enabled); // 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');
     }
 
             update_user_login_times();
             set_moodle_cookie($USER->username);
             set_login_session_preferences();
-        
-        
+
             //Select password change url
-            if (is_internal_auth($USER->auth) || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){
+            $userauth = get_auth_plugin($USER->auth);
+            if ($userauth->can_change_password()) {
                 $passwordchangeurl=$CFG->wwwroot.'/login/change_password.php';
-            } elseif($CFG->changepassword) {
-                $passwordchangeurl=$CFG->changepassword;
             } else {
-                $passwordchangeurl = '';
+                $passwordchangeurl = $userauth->change_password_url();
             }
-            
+
             // check whether the user should be changing password
             if (get_user_preferences('auth_forcepasswordchange', false) || $frm->password == 'changeme'){
                 if ($passwordchangeurl != '') {
 
             // check if user password has expired
             // Currently supported only for ldap-authentication module
-            if (isset($CFG->ldap_expiration) && $CFG->ldap_expiration == 1 ) {
-                if (function_exists('auth_password_expire')){
-                    $days2expire = auth_password_expire($USER->username);
+            if (method_exists($userauth, 'password_expire') and !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 align=\"right\">$langmenu</div>"); 
                         notice_yesno(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo); 
                         print_footer();
                         exit;
                     }    
-                }
             }
 
             reset_login_count();
             if (empty($errormsg)) {
                 $errormsg = get_string("invalidlogin");
             }
+
+            // TODO: if the user failed to authenticate, check if the username corresponds to a remote mnet user
+            if ($users = get_records('user', 'username', $frm->username)) {
+                $errormsg .= "<br>If you are a Moodle Network remote user and can <a href=\"mnet_email.php?u=$frm->username\">confirm your email address here</a>, you can be redirected to your login page.<br>";
+            }
         }
     }
 
         set_moodle_cookie('nobody');   // To help search for cookies
     }
     
-    if (empty($frm->username) && $CFG->auth != 'shibboleth') {  // See bug 5184
+if (empty($frm->username) && $authsequence[0] != 'shibboleth') {  // See bug 5184
         $frm->username = get_moodle_cookie() === 'nobody' ? '' : get_moodle_cookie();
         $frm->password = "";
     }
     if (isset($CFG->auth_instructions)) {
         $CFG->auth_instructions = trim($CFG->auth_instructions);
     }
-    if ($CFG->auth == "email" or $CFG->auth == "none" or !empty($CFG->auth_instructions)) {
+if ($authsequence[0] == "email" or $authsequence[0] == "none" or !empty($CFG->auth_instructions)) {
         $show_instructions = true;
     } else {
         $show_instructions = false;
index e7fefb0b7250d2096ca3e69c5213f16cf815607a..971d0634521f637c405999ab7e2ec628783855e7 100644 (file)
@@ -82,7 +82,7 @@
 
 <?php if ($show_instructions) { ?>
     <td width="50%" valign="top" class="content right">
-<?php     switch ($CFG->auth) {
+<?php     switch ($authsequence[0]) {
               case "email":
                  print_string("loginsteps", "", "signup.php");
 ?>
                    </form>
                  </div>
 <?php            break;
+
                case "none":
                  print_string("loginstepsnone");
                  break;
+
                default:
                  echo format_text($CFG->auth_instructions);
-                 if (!function_exists('auth_user_login')) {
-                    require_once("../auth/$CFG->auth/lib.php");
-                 }
-                 if (!empty($CFG->auth_user_create) and function_exists('auth_user_create') ){
+                 // TODO: if !function_exists(auth_user_login) then require_once /auth/$CFG->auth/lib.php
+                 // ..which implies that auth_user_login might have been floating
+                 // about in global namespace. grr
+                 $authplugin = get_auth_plugin($CFG->auth);
+                 if (!empty($CFG->auth_user_create) and method_exists($authplugin, 'user_create') ){
 ?>
                  <div align="center">
                     <form action="signup.php" method="get" id="signup">
index df49f2a3280f9698f70c8ba98e398d0694af158a..c74cfefddd26e70032c2971e8e7fe4338a8c12da 100644 (file)
@@ -3,8 +3,15 @@
 
     require_once("../config.php");
 
+    if ($USER->mnethostid != $CFG->mnet_localhost_id) {
+        $host = get_record('mnet_host', 'id', $USER->mnethostid);
+        $wwwroot = $host->wwwroot;
+    } else {
+        $wwwroot = $CFG->wwwroot;
+    }
+
     require_logout();
 
-    redirect("$CFG->wwwroot/");
+    redirect("$wwwroot/");
 
 ?>
diff --git a/login/mnet_email.php b/login/mnet_email.php
new file mode 100644 (file)
index 0000000..d08a13b
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+require_once dirname(dirname(__FILE__)) . '/config.php';
+httpsrequired();
+
+$username = required_param('u', PARAM_ALPHANUM);
+$sesskey = sesskey();
+
+// if you are logged in then you shouldn't be here
+if (isloggedin() and !isguest()) {
+    redirect( $CFG->wwwroot.'/', get_string('loginalready'), 5);
+}
+
+print_header('MNET ID Provider', 'MNET ID Provider', 'MNET ID Provider', 'form.email' );
+
+if ($form = data_submitted() and confirm_sesskey()) {
+    if ($user = get_record('user', 'username', $username, 'email', $form->email)) {
+        if (!empty($user->mnethostid) and $host = get_record('mnet_host', 'id', $user->mnethostid)) {
+            notice("You should be able to login at your <a href=\"{$host->wwwroot}/login/\">{$host->name}</a> provider.");
+        }
+    }
+}
+
+echo '<p>&nbsp;</p>';
+print_simple_box_start('center','50%','','20');
+
+?>
+  <form method="post">
+    <input type="hidden" name="sesskey" value="<?php echo $sesskey; ?>">
+    <?php echo get_string('email') ?>:
+    <input type="text" name="email" size="" maxlength="100">
+    <input type="submit" value="Find Login">
+  </form>
+<?php
+
+print_simple_box_end();
+print_footer();
+
+?>
index a11a68d1109434614965623ea8fd2060d8d6212e..20fc6077fb7a5c8a93bb34aa26cbab1f8e61cb34 100644 (file)
@@ -1,13 +1,14 @@
 <?php  // $Id$
 
     require_once('../config.php');
-    require_once("../auth/$CFG->auth/lib.php");
     require_once('signup_form.php');
 
     //HTTPS is potentially required in this page
     httpsrequired();
 
-    if ($CFG->auth != 'email' and (empty($CFG->auth_user_create) or !(function_exists('auth_user_create'))) ) {
+    $authplugin = get_auth_plugin($CFG->auth);
+
+    if ($CFG->auth != 'email' and (empty($CFG->auth_user_create) or !(method_exists($authplugin, 'user_create'))) ) {
         error("Sorry, you may not use this page.");
     }
 
         $user->confirmed   = 0;
         $user->lang        = current_language();
         $user->firstaccess = time();
+        $user->mnethostid  = $CFG->mnet_localhost_id;
         $user->secret      = random_string(15);
         $user->auth        = $CFG->auth;
 
-        if (!empty($CFG->auth_user_create) and function_exists('auth_user_create') ){
-            if (! auth_user_exists($user->username)) {
-                if (! auth_user_create($user, $plainpass)) {
+        if (!empty($CFG->auth_user_create) and method_exists($authplugin, 'user_create') ){
+            if (! $authplugin->user_exists($user->username)) {
+                if (! $authplugin->user_create($user, $plainpass)) {
                     error("Could not add user to authentication module!");
                 }
             } else {
             }
         }
 
-        if (! ($user->id = insert_record('user', $user))) {
-            error("Could not add your record to the database!");
-        }
-
-        if (! send_confirmation_email($user)) {
-            error("Tried to send you an email but failed!");
-        }
-
-        $emailconfirm = get_string("emailconfirm");
-        print_header($emailconfirm, $emailconfirm, $emailconfirm);
-        notice(get_string("emailconfirmsent", "", $user->email), "$CFG->wwwroot/index.php");
+        $authplugin = get_auth_plugin($CFG->auth);
+        $signedup = $authplugin->user_signup($user, $notify=true);
         exit;
     }
 
-
     $newaccount = get_string('newaccount');
     $login      = get_string('login');
 
index 40c0cd82c6180cab6824793baeb1b6a4f143dd1d..b2d361d4465ab1fe92b63891e589e737a36227d6 100644 (file)
@@ -64,8 +64,9 @@ class login_signup_form_1 extends moodleform {
                global $CFG;
                $errors = array();
 
+        $authplugin = get_auth_plugin($CFG->auth);
 
-               if (record_exists('user', 'username', $data['username'])){
+               if (record_exists('user', 'username', $data['username'], 'mnethostid', $CFG->mnet_localhost_id))){
                        $errors['username'] = get_string('usernameexists');
                } else {
                        if (empty($CFG->extendedusernamechars)) {
@@ -75,8 +76,8 @@ class login_signup_form_1 extends moodleform {
                                }
                        }
                }
-               if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and function_exists('auth_user_exists') ){
-                       if (auth_user_exists($data['username'])) {
+               if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and method_exists($authplugin, 'user_exists')){
+                       if ($authplugin->user_exists($user->username)) {
                                $errors['username'] = get_string('usernameexists');
                        }
                }