Skip to content

Commit

Permalink
FIX LeftAndMainSubsites::canAccess() now accepts a Member argument an…
Browse files Browse the repository at this point in the history
…d falls back to the session member
  • Loading branch information
robbieaverill committed Jun 25, 2019
1 parent 1a56661 commit 4fdf2e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Extensions/LeftAndMainSubsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,16 @@ public function shouldChangeSubsite($adminClass, $recordSubsiteID, $currentSubsi

/**
* Check if the current controller is accessible for this user on this subsite.
*
* @param Member $member
*/
public function canAccess()
public function canAccess(Member $member = null)
{
if (!$member) {
$member = Security::getCurrentUser();
}

// Admin can access everything, no point in checking.
$member = Security::getCurrentUser();
if ($member
&& (Permission::checkMember($member, 'ADMIN') // 'Full administrative rights'
|| Permission::checkMember($member, 'CMS_ACCESS_LeftAndMain') // 'Access to all CMS sections'
Expand All @@ -236,10 +241,12 @@ public function canAccess()
/**
* Prevent accessing disallowed resources. This happens after onBeforeInit has executed,
* so all redirections should've already taken place.
*
* @param Member $member
*/
public function alternateAccessCheck()
public function alternateAccessCheck(Member $member = null)
{
return $this->owner->canAccess();
return $this->owner->canAccess($member);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/php/LeftAndMainSubsitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Security\Member;
use SilverStripe\Subsites\Extensions\LeftAndMainSubsites;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\State\SubsiteState;

Expand Down Expand Up @@ -100,4 +101,14 @@ public function testShouldChangeSubsite()
$this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 1, 5));
$this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 1, 1));
}

public function testCanAccessWithPassedMember()
{
$memberID = $this->logInWithPermission('ADMIN');
$member = Member::get()->byID($memberID);

/** @var LeftAndMain&LeftAndMainSubsites $leftAndMain */
$leftAndMain = new LeftAndMain();
$this->assertTrue($leftAndMain->canAccess($member));
}
}

0 comments on commit 4fdf2e2

Please sign in to comment.