Skip to content

Commit

Permalink
Merge branch 'MDL-59595-34-2' of git://github.com/snake/moodle into M…
Browse files Browse the repository at this point in the history
…OODLE_34_STABLE
  • Loading branch information
stronk7 committed Aug 1, 2018
2 parents 71db484 + ab53088 commit 5636f37
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/adminlib.php
Expand Up @@ -7970,9 +7970,7 @@ function admin_externalpage_setup($section, $extrabutton = '', array $extraurlpa
* @return object admin_root object
*/
function admin_get_root($reload=false, $requirefulltree=true) {
global $CFG, $DB, $OUTPUT;

static $ADMIN = NULL;
global $CFG, $DB, $OUTPUT, $ADMIN;

if (is_null($ADMIN)) {
// create the admin tree!
Expand Down
4 changes: 4 additions & 0 deletions lib/classes/session/manager.php
Expand Up @@ -712,6 +712,7 @@ public static function apply_concurrent_login_limit($userid, $sid = null) {
* @param \stdClass $user record
*/
public static function set_user(\stdClass $user) {
global $ADMIN;
$GLOBALS['USER'] = $user;
unset($GLOBALS['USER']->description); // Conserve memory.
unset($GLOBALS['USER']->password); // Improve security.
Expand All @@ -723,6 +724,9 @@ public static function set_user(\stdClass $user) {
// Relink session with global $USER just in case it got unlinked somehow.
$_SESSION['USER'] =& $GLOBALS['USER'];

// Nullify the $ADMIN tree global. If we're changing users, then this is now stale and must be generated again if needed.
$ADMIN = null;

// Init session key.
sesskey();
}
Expand Down
24 changes: 24 additions & 0 deletions lib/tests/admintree_test.php
Expand Up @@ -414,4 +414,28 @@ public function test_mixedhostiplist() {
$this->assertEquals('These entries are invalid: nonvalid site name', $adminsetting->write_setting('nonvalid site name'));
$this->assertEquals('Empty lines are not valid', $adminsetting->write_setting("localhost\n"));
}

/**
* Verifies the $ADMIN global (adminroot cache) is properly reset when changing users, which might occur naturally during cron.
*/
public function test_adminroot_cache_reset() {
$this->resetAfterTest();
global $DB;
// Current user is a manager at site context, which won't have access to the 'debugging' section of the admin tree.
$manageruser = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$managerrole = $DB->get_record('role', array('shortname' => 'manager'));
role_assign($managerrole->id, $manageruser->id, $context->id);
$this->setUser($manageruser);
$adminroot = admin_get_root();
$section = $adminroot->locate('debugging');
$this->assertEmpty($section);

// Now, change the user to an admin user and confirm we get a new copy of the admin tree when next we ask for it.
$adminuser = get_admin();
$this->setUser($adminuser);
$adminroot = admin_get_root();
$section = $adminroot->locate('debugging');
$this->assertInstanceOf('\admin_settingpage', $section);
}
}

0 comments on commit 5636f37

Please sign in to comment.