Skip to content

Commit

Permalink
MDL-58032 core_user: Fix case when acting user is a visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitagarwal committed Feb 22, 2017
1 parent 0767770 commit 3450210
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,11 @@ function user_can_view_profile($user, $course = null, $usercontext = null) {
} else {
$sharedcourses = enrol_get_shared_courses($USER->id, $user->id, true);
}

if (empty($sharedcourses)) {
return false;
}

foreach ($sharedcourses as $sharedcourse) {
$coursecontext = context_course::instance($sharedcourse->id);
if (has_capability('moodle/user:viewdetails', $coursecontext)) {
Expand Down
20 changes: 20 additions & 0 deletions user/tests/userlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ public function test_user_can_view_profile() {
$user5 = $this->getDataGenerator()->create_user();
$user6 = $this->getDataGenerator()->create_user(array('deleted' => 1));
$user7 = $this->getDataGenerator()->create_user();
$user8 = $this->getDataGenerator()->create_user();
$user8->id = 0; // Visitor.

$studentrole = $DB->get_record('role', array('shortname' => 'student'));
// Add the course creator role to the course contact and assign a user to that role.
Expand Down Expand Up @@ -574,6 +576,24 @@ public function test_user_can_view_profile() {
$this->assertTrue(user_can_view_profile($user4));

$CFG->coursecontact = null;

// Visitor (Not a guest user, userid=0).
$CFG->forceloginforprofiles = 1;
$this->setUser($user8);

// By default guest has 'moodle/user:viewdetails' cap.
$this->assertTrue(user_can_view_profile($user1));
$CFG->forceloginforprofiles = 0;
$this->assertTrue(user_can_view_profile($user1));

// Let us remove this cap.
$allroles = $DB->get_records_menu('role', array(), 'id', 'archetype, id');
assign_capability('moodle/user:viewdetails', CAP_PROHIBIT, $allroles['guest'], context_system::instance()->id, true);
reload_all_capabilities();
$CFG->forceloginforprofiles = 1;
$this->assertFalse(user_can_view_profile($user1));
$CFG->forceloginforprofiles = 0;
$this->assertTrue(user_can_view_profile($user1));
}

/**
Expand Down

0 comments on commit 3450210

Please sign in to comment.