mnet: user/* now handles remote users and multi-auth

This commit is contained in:
martinlanghoff 2007-01-04 03:01:30 +00:00
parent 03d820c788
commit 56f5274232
4 changed files with 48 additions and 28 deletions

View File

@ -28,9 +28,10 @@
if (has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
$theadmin = get_admin(); // returns false during install
$adminself = (!empty($theadmin) and ($theadmin->id == $USER->id) and ($USER->id == $user->id));
$userauth = get_auth_plugin($user->auth);
echo '<tr>';
echo '<th>'.get_string('username').':</th>';
if ($adminself || is_internal_auth($user->auth) ){
if ($adminself or $userauth->is_internal()) {
echo "<td><input type=\"text\" name=\"username\" size=\"20\" alt=\"".get_string("username")."\" value=\"";
p($user->username);
echo "\" />";
@ -58,7 +59,7 @@ if (has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SI
echo "</td></tr>\n";
}
if ($adminself || is_internal_auth($user->auth) || (!empty($CFG->{'auth_'.$user->auth.'_stdchangepassword'}))) {
if ($adminself or $userauth->can_change_password()) {
echo '<tr>';
echo '<th>'.get_string('newpassword').':</th>';
echo "<td><input type=\"text\" name=\"newpassword\" size=\"20\" alt=\"".get_string("newpassword")."\" value=\"";
@ -73,7 +74,7 @@ if (has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SI
}
echo "</td>";
echo "</tr>\n";
if (!$adminself && (!empty($CFG->{'auth_'.$user->auth.'_stdchangepassword'}) || is_internal_auth())){
if (!$adminself and $userauth->can_change_password()) {
if (get_user_preferences('auth_forcepasswordchange', NULL, $user->id)) {
$checked = ' checked="checked" ';
} else {

View File

@ -35,6 +35,11 @@
require_login($course->id);
}
// remote users cannot be edited
if (is_mnet_remote_user($user)) {
redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
}
if ($USER->id <> $user->id) { // Current user editing someone else's profile
if (has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Current user can update user profiles
if ($mainadmin = get_admin()) {
@ -59,11 +64,15 @@
// load the relevant auth libraries
if (!empty($user->auth)) {
$auth = $user->auth;
if (!file_exists("$CFG->dirroot/auth/$auth/lib.php")) {
trigger_error("Can't find auth module $auth , default to internal.");
$auth = "manual"; // Can't find auth module, default to internal
// TODO: spit dummy if $auth doesn't exist
if (! exists_auth_plugin($auth)) {
trigger_error("Can't find auth module '$auth', default to internal.");
$auth = "manual";
}
require_once("$CFG->dirroot/auth/$auth/lib.php");
$authplugin = get_auth_plugin($auth);
}
else {
$authplugin = get_auth_plugin($CFG->auth);
}
@ -157,14 +166,13 @@
// override locked values
if (!has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
$fields = get_user_fieldnames();
$authconfig = get_config( 'auth/' . $user->auth );
foreach ($fields as $field) {
$configvariable = 'field_lock_' . $field;
if ( empty($authconfig->{$configvariable}) ) {
if ( empty($authplugin->config->{$configvariable}) ) {
continue; //no locking set
}
if ( $authconfig->{$configvariable} === 'locked'
|| ($authconfig->{$configvariable} === 'unlockedifempty' && !empty($user->$field)) ) {
if ( $authplugin->config->{$configvariable} === 'locked'
or ($authplugin->config->{$configvariable} === 'unlockedifempty' and !empty($user->$field))) {
if (!empty( $user->$field)) {
$usernew->$field = addslashes($user->$field);
}
@ -205,9 +213,10 @@
if (!empty($usernew->newpassword)) {
$usernew->password = hash_internal_user_password($usernew->newpassword);
// update external passwords
if (!empty($CFG->{'auth_'. $user->auth.'_stdchangepassword'})) {
if (function_exists('auth_user_update_password')){
if (!auth_user_update_password($user->username, $usernew->newpassword)){
// TODO: this was using $user->auth possibly overriding $authplugin above. Can we guarantee $user->auth being something valid?
if ($authplugin->can_change_password()) {
if (method_exists($authplugin, 'user_update_password')){
if (!$authplugin->user_update_password($user->username, $usernew->newpassword)){
error('Failed to update password on external auth: ' . $user->auth .
'. See the server logs for more details.');
}
@ -233,9 +242,9 @@
$userold = get_record('user','id',$usernew->id);
if (update_record("user", $usernew)) {
if (function_exists("auth_user_update")){
if (method_exists($authplugin, "user_update")){
// pass a true $userold here
if (!auth_user_update($userold, $usernew)) {
if (! $authplugin->user_update($userold, $usernew)) {
// auth update failed, rollback for moodle
update_record("user", $userold);
error('Failed to update user data on external auth: '.$user->auth.
@ -371,13 +380,11 @@
echo '<script type="text/javascript">'."\n";
echo '<!--'."\n";
$authconfig = get_config( 'auth/' . $user->auth );
foreach ($fields as $field) {
$configvariable = 'field_lock_' . $field;
if (isset($authconfig->{$configvariable})) {
if ( $authconfig->{$configvariable} === 'locked'
|| ($authconfig->{$configvariable} === 'unlockedifempty' && !empty($user->$field)) ) {
if (isset($authplugin->config->{$configvariable})) {
if ( $authplugin->config->{$configvariable} === 'locked'
or ($authplugin->config->{$configvariable} === 'unlockedifempty' and !empty($user->$field))) {
echo "eval('document.form.$field.disabled=true');\n";
}
}
@ -412,7 +419,7 @@ function find_form_errors(&$user, &$usernew, &$err, &$um) {
if (empty($usernew->username)) {
$err["username"] = get_string("missingusername");
} else if (record_exists("user", "username", $usernew->username) and $user->username == "changeme") {
} else if (record_exists("user", "username", $usernew->username, 'mnethostid', $CFG->mnet_localhost_id) and $user->username == "changeme") {
$err["username"] = get_string("usernameexists");
} else {
@ -424,9 +431,10 @@ function find_form_errors(&$user, &$usernew, &$err, &$um) {
}
}
if (empty($usernew->newpassword) and empty($user->password) and is_internal_auth() )
// TODO: is_internal_auth() - what, the global auth? the user auth?
if (empty($usernew->newpassword) and empty($user->password) and is_internal_auth()) {
$err["newpassword"] = get_string("missingpassword");
}
if (($usernew->newpassword == "admin") or ($user->password == md5("admin") and empty($usernew->newpassword)) ) {
$err["newpassword"] = get_string("unsafepassword");
}

View File

@ -96,7 +96,11 @@
if (($mainadmin = get_admin()) === false) {
$mainadmin->id = 0; /// Weird - no primary admin!
}
if ((!empty($USER->id) and ($USER->id == $user->id) and !isguest()) or
if (is_mnet_remote_user($user)) {
// cannot edit remote users
}
else if ((!empty($USER->id) and ($USER->id == $user->id) and !isguest()) or
(has_capability('moodle/user:editprofile', $personalcontext) and ($user->id != $mainadmin->id)) ) {
if(empty($CFG->loginhttps)) {

View File

@ -171,6 +171,12 @@
$showroles = 1;
include('tabs.php');
if (is_mnet_remote_user($user)) {
echo "<p class=\"errorboxcontent\">This profile is for a remote user from another Moodle system. <br>\n";
$remotehost = get_record('mnet_host', 'id', $user->mnethostid);
echo "Remote Moodle: <a href=\"{$remotehost->wwwroot}/user/edit.php\">{$remotehost->name}</a> (click here to edit your profile on the remote server) </p>\n";
}
echo "<table width=\"80%\" align=\"center\" border=\"0\" cellspacing=\"0\" class=\"userinfobox\">";
echo "<tr>";
echo "<td width=\"100\" valign=\"top\" class=\"side\">";
@ -332,7 +338,8 @@
echo "</td></tr></table>";
$internalpassword = false;
if (is_internal_auth($user->auth) or (!empty($CFG->{'auth_'.$user->auth.'_stdchangepassword'}))) {
$userauth = get_auth_plugin($user->auth);
if (method_exists($userauth, 'can_change_password') and $userauth->can_change_password()) {
if (empty($CFG->loginhttps)) {
$internalpassword = "$CFG->wwwroot/login/change_password.php";
} else {
@ -354,8 +361,8 @@
echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
}
echo "</form></td>";
} else if ( strlen($CFG->{'auth_'.$user->auth.'_changepasswordurl'}) > 1 ) {
echo "<td nowrap=\"nowrap\"><form action=\"".$CFG->{'auth_'.$user->auth.'_changepasswordurl'}."\" method=\"get\">";
} elseif ( method_exists($userauth, 'change_password_url') and strlen($userauth->change_password_url())) {
echo "<td nowrap=\"nowrap\"><form action=\"".$userauth->change_password_url()."\" method=\"get\">";
echo "<input type=\"submit\" value=\"".get_string("changepassword")."\" />";
echo "</form></td>";
}