Merge branch 'MDL-59595-master-2' of git://github.com/snake/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2018-08-02 00:21:23 +02:00
commit c965349aad
3 changed files with 29 additions and 3 deletions

View File

@ -7968,9 +7968,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!

View File

@ -712,6 +712,7 @@ class manager {
* @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.
@ -723,6 +724,9 @@ class manager {
// 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();
}

View File

@ -414,4 +414,28 @@ class core_admintree_testcase extends advanced_testcase {
$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);
}
}