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

get_admin() seems to return random admin user instead

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.1.3, 2.2, 2.3
    • Fix Version/s: 2.2.1
    • Component/s: Administration
    • Labels:
    • Database:
      PostgreSQL
    • Testing Instructions:
      Hide

      1/ Go to administrators UI and try some clicking
      2/ make sure the changes are reflected in get_admin() by creating some test script that dumps current admin

      Show
      1/ Go to administrators UI and try some clicking 2/ make sure the changes are reflected in get_admin() by creating some test script that dumps current admin
    • Affected Branches:
      MOODLE_21_STABLE, MOODLE_22_STABLE, MOODLE_23_STABLE
    • Fixed Branches:
      MOODLE_22_STABLE
    • Pull from Repository:
    • Pull Master Branch:
      w50_MDL-30625_m23_adminorder

      Description

      The get_admin() function is supposed to return the main admin of the moodle installation. The comments in the source code say that it returns the admin user with lowest role_assignment id. However, looking at the code, there is no ordering at all.

      I recently configured a new admin user and now get_admin delivers this lastly assigned user instead of the first assigned admin user.

      From /lib/datalib.php:

      /**
       * Returns $user object of the main admin user
       * primary admin = admin with lowest role_assignment id among admins
       *
       * @static stdClass $mainadmin
       * @return stdClass {@link $USER} record from DB, false if not found
       */
      function get_admin() {
          static $mainadmin = null;
       
          if (!isset($mainadmin)) {
              if (! $admins = get_admins()) {
                  return false;
              }
              //TODO: add some admin setting for specifying of THE main admin
              //      for now return the first assigned admin
              $mainadmin = reset($admins);
          }
          // we must clone this otherwise code outside can break the static var
          return clone($mainadmin);
      }
       
      /**
       * Returns list of all admins, using 1 DB query
       *
       * @return array
       */
      function get_admins() {
          global $DB, $CFG;
       
          if (empty($CFG->siteadmins)) {  // Should not happen on an ordinary site
              return array();
          }
       
          $sql = "SELECT u.*
                    FROM {user} u
                   WHERE u.deleted = 0 AND u.id IN ($CFG->siteadmins)";
       
          return $DB->get_records_sql($sql);
      }

      It would be great if there was a admin setting to set the main admin like the source code says. Until that, I think there should be some kind of sorting.

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

                • Votes:
                  0 Vote for this issue
                  Watchers:
                  3 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Fix Release Date:
                    9/Jan/12