Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-70857

fix_deleted_users.php doesn't actually work

    XMLWordPrintable

    Details

    • Affected Branches:
      MOODLE_310_STABLE

      Description

      The script admin/cli/fix_deleted_users.php claims to clean up users where someone has manually set "deleted = 1" through SQL instead of clicking the actual Delete button.

      However, when actually run, it produces an exception because delete_user() -> context_user::instance() has an explicit check for "deleted = 0", so it's unable to find the user ID.

      We use auth_saml2, so users are automatically created but there's no way to also have them accordingly deleted and suspended. So I use a custom tool to directly set suspended=1 for locked users, deleted=1 for users who no longer exist, and so on.

      Now in my initial tests this appeared to work because I had visited the test user's profile page immediately before "deleting" it, so the context was still cached.

      But when I actually wanted to use this method to bulk delete users which hadn't been active for many years (so they are not in cache), the problem is revealed: 
       


      == Looking for sloppy user deletes ==
      Redeleting user 3968: testuser1 (xxx@yyy.zzz)
      Default exception handler: Invalid user Debug: SELECT id FROM {user} WHERE id = ? AND deleted = ?
      [array (
      {{ 0 => '3968',}}
      {{ 1 => 0,}}
      )]
      Error code: invaliduser
      * line 1646 of /lib/dml/moodle_database.php: dml_missing_record_exception thrown
      * line 1622 of /lib/dml/moodle_database.php: call to moodle_database->get_record_select()
      * line 6558 of /lib/accesslib.php: call to moodle_database->get_record()
      * line 4249 of /lib/moodlelib.php: call to context_user::instance()
      * line 74 of /admin/cli/fix_deleted_users.php: call to delete_user()!!! Invalid user !!!


        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            grawity Mantas Mikulėnas
            Participants:
            Component watchers:
            Andrew Nicols, Dongsheng Cai, Huong Nguyen, Jun Pataleta, Michael Hawkins, Shamim Rezaie, Simey Lameze, Amaia Anabitarte, Carlos Escobedo, Ferran Recio, Ilya Tregubov, Sara Arjona (@sarjona)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated: