MDL-10260 added new user_delete() hook into auth plugins; refactored user delete...
authorskodak <skodak>
Tue, 21 Aug 2007 20:52:36 +0000 (20:52 +0000)
committerskodak <skodak>
Tue, 21 Aug 2007 20:52:36 +0000 (20:52 +0000)
admin/user.php
auth/cas/auth.php
auth/db/auth.php
auth/ldap/auth.php
lib/authlib.php
lib/moodlelib.php

index 8b19695ca2928a4098192eb785535e15102eccef..a68303d912f9ef91a2166182d0341c4ec5ad4f3a 100644 (file)
             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)));
index 43faed08a90a06389e592b3457c4c07f4c7a6ae9..2f89de748b5eebf1dcffd4e3000d251025c0474c 100644 (file)
@@ -97,7 +97,7 @@ class auth_plugin_cas extends auth_plugin_base {
           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;
                  }
@@ -105,7 +105,7 @@ class auth_plugin_cas extends auth_plugin_base {
 // 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')
@@ -699,21 +699,9 @@ if ( !is_object($PHPCAS_CLIENT) ) {
             $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";
@@ -729,7 +717,6 @@ if ( !is_object($PHPCAS_CLIENT) ) {
                         }
                     }
                 }
-                commit_sql();
             } else {
                 print "No user entries to be removed\n";
             }
index 020b3cff6ec8be16a244f1ba6cf5ca461711f10d..e8b2e50c361cf1ec74f3cf54d9411de69b52612c 100644 (file)
@@ -248,21 +248,9 @@ class auth_plugin_db extends auth_plugin_base {
             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";
@@ -278,7 +266,6 @@ class auth_plugin_db extends auth_plugin_base {
                         }
                     }
                 }
-                commit_sql();
             }
             unset($remove_users); // free mem!
         }
index cb2ec2efbf115c9751fe491c0ecd47221787ae55..ec0ceb180905b52cbdfd17a6b01a3b47a5dfc21c 100644 (file)
@@ -614,21 +614,9 @@ class auth_plugin_ldap extends auth_plugin_base {
             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";
@@ -644,7 +632,6 @@ class auth_plugin_ldap extends auth_plugin_base {
                         }
                     }
                 }
-                commit_sql();
             } else {
                 print "No user entries to be removed\n";
             }
index 2c3b58db0e9a34c1b834b4f83cf184a081812271..355fdf3ec5c3490d36d3b6a0f156acf5ac389ff9 100644 (file)
@@ -138,6 +138,16 @@ class auth_plugin_base {
         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.
      *
index 707c4eeb4f42be24c28dc864dfd35409a06df99c..d3419cdea175fa92cb004b4171034a13fa44c7b3 100644 (file)
@@ -2616,6 +2616,56 @@ function truncate_userinfo($info) {
     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
  *