admin_externalpage_print_footer();
die;
} else if (data_submitted() and !$user->deleted) {
- //following code is also used in auth sync scripts
- $updateuser = new object();
- $updateuser->id = $user->id;
- $updateuser->deleted = 1;
- $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
- $updateuser->email = ''; // Clear this field to free it up
- $updateuser->idnumber = ''; // Clear this field to free it up
- $updateuser->timemodified = time();
- if (update_record('user', $updateuser)) {
- // not sure if this is needed. unenrol_student($user->id); // From all courses
- delete_records('role_assignments', 'userid', $user->id); // unassign all roles
- // remove all context assigned on this user?
+ if (delete_user($user)) {
notify(get_string('deletedactivity', '', fullname($user, true)) );
} else {
notify(get_string('deletednot', '', fullname($user, true)));
return;
}
- // Test si cas activé et paramêtres non remplis
+ // Test si cas activ� et param�tres non remplis
if (empty($this->config->hostname)) {
return;
}
// Connection to CAS server
$this->connectCAS();
- // Gestion de la connection CAS si accès direct d'un ent ou autre
+ // Gestion de la connection CAS si acc�s direct d'un ent ou autre
if (phpCAS::checkAuthentication()) {
$frm->username=phpCAS::getUser();
// if (phpCAS::getUser()=='esup9992')
$remove_users = get_records_sql($sql);
if (!empty($remove_users)) {
print "User entries to remove: ". count($remove_users) . "\n";
- begin_sql();
foreach ($remove_users as $user) {
if ($this->config->removeuser == 2) {
- //following is copy pasted from admin/user.php
- //maybe this should moved to function in lib/datalib.php
- $updateuser = new object();
- $updateuser->id = $user->id;
- $updateuser->deleted = 1;
- $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
- $updateuser->email = ''; // Clear this field to free it up
- $updateuser->idnumber = ''; // Clear this field to free it up
- $updateuser->timemodified = time();
- if (update_record('user', $updateuser)) {
- delete_records('role_assignments', 'userid', $user->id); // unassign all roles
- //copy pasted part ends
+ if (delete_user($user)) {
echo "\t"; print_string('auth_dbdeleteuser', 'auth', array($user->username, $user->id)); echo "\n";
} else {
echo "\t"; print_string('auth_dbdeleteusererror', 'auth', $user->username); echo "\n";
}
}
}
- commit_sql();
} else {
print "No user entries to be removed\n";
}
if (!empty($remove_users)) {
print_string('auth_dbuserstoremove','auth', count($remove_users)); echo "\n";
- begin_sql();
foreach ($remove_users as $user) {
if ($this->config->removeuser == 2) {
- //following is copy pasted from admin/user.php
- //maybe this should moved to function in lib/datalib.php
- $updateuser = new object();
- $updateuser->id = $user->id;
- $updateuser->deleted = 1;
- $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
- $updateuser->email = ''; // Clear this field to free it up
- $updateuser->idnumber = ''; // Clear this field to free it up
- $updateuser->timemodified = time();
- if (update_record('user', $updateuser)) {
- delete_records('role_assignments', 'userid', $user->id); // unassign all roles
- //copy pasted part ends
+ if (delete_user($user)) {
echo "\t"; print_string('auth_dbdeleteuser', 'auth', array($user->username, $user->id)); echo "\n";
} else {
echo "\t"; print_string('auth_dbdeleteusererror', 'auth', $user->username); echo "\n";
}
}
}
- commit_sql();
}
unset($remove_users); // free mem!
}
if (!empty($remove_users)) {
print "User entries to remove: ". count($remove_users) . "\n";
- begin_sql();
foreach ($remove_users as $user) {
if ($this->config->removeuser == 2) {
- //following is copy pasted from admin/user.php
- //maybe this should moved to function in lib/datalib.php
- $updateuser = new object();
- $updateuser->id = $user->id;
- $updateuser->deleted = 1;
- $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
- $updateuser->email = ''; // Clear this field to free it up
- $updateuser->idnumber = ''; // Clear this field to free it up
- $updateuser->timemodified = time();
- if (update_record('user', $updateuser)) {
- delete_records('role_assignments', 'userid', $user->id); // unassign all roles
- //copy pasted part ends
+ if (delete_user($user)) {
echo "\t"; print_string('auth_dbdeleteuser', 'auth', array($user->username, $user->id)); echo "\n";
} else {
echo "\t"; print_string('auth_dbdeleteusererror', 'auth', $user->username); echo "\n";
}
}
}
- commit_sql();
} else {
print "No user entries to be removed\n";
}
return true;
}
+ /**
+ * User delete requested - internal user record is mared as deleted already, username not present anymore.
+ * Do any action in external database.
+ * @param object $user Userobject before delete (without system magic quotes)
+ */
+ function user_delete($olduser) {
+ //override if needed
+ return;
+ }
+
/**
* Returns true if plugin allows resetting of internal password.
*
return $info;
}
+/**
+ * Marks user deleted in internal user database and notifies the auth plugin.
+ * Also unenrols user from all roles and does other cleanup.
+ * @param object $user Userobject before delete (without system magic quotes)
+ * @return boolean success
+ */
+function delete_user($user) {
+ global $CFG;
+ require_once($CFG->libdir.'/grouplib.php');
+
+ begin_sql();
+
+ // delete all grades - backup is kept in grade_grades_history table
+ if ($grades = grade_grade::fetch_all(array('userid'=>$user->id))) {
+ foreach ($grades as $grade) {
+ $grade->delete('userdelete');
+ }
+ }
+
+ // remove from all groups
+ delete_records('groups_members', 'userid', $user->id);
+
+ // unenrol from all roles in all contexts
+ role_unassign(0, $user->id); // this might be slow but it is really needed - modules might do some extra cleanup!
+
+ // now do a final accesslib cleanup - removes all role assingments in user context and context itself
+ delete_context(CONTEXT_USER, $user->id);
+
+ // mark internal user record as "deleted"
+ $updateuser = new object();
+ $updateuser->id = $user->id;
+ $updateuser->deleted = 1;
+ $updateuser->username = addslashes("$user->email.".time()); // Remember it just in case
+ $updateuser->email = ''; // Clear this field to free it up
+ $updateuser->idnumber = ''; // Clear this field to free it up
+ $updateuser->timemodified = time();
+
+ if (update_record('user', $updateuser)) {
+ commit_sql();
+ // notify auth plugin - do not block the delete even when plugin fails
+ $authplugin = get_auth_plugin($user->auth);
+ $authplugin->user_delete($user);
+ return true;
+
+ } else {
+ rollback_sql();
+ return false;
+ }
+}
+
/**
* Retrieve the guest user object
*