Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Check canArchive() permission instead of canDelete() #315

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,20 @@ private function getLinkData(Link $link): array
*/
public function linkDelete(): HTTPResponse
{
$link = $this->linkFromRequest();
if (!$link->canDelete()) {
$this->jsonError(403);
}
// Check security token on destructive operation
if (!SecurityToken::inst()->checkRequest($this->getRequest())) {
$this->jsonError(400);
}
$link = $this->linkFromRequest();
if ($link->hasExtension(Versioned::class)) {
if (!$link->canArchive()) {
$this->jsonError(403);
}
$link->doArchive();
} else {
if (!$link->canDelete()) {
$this->jsonError(403);
}
$link->delete();
}
// Send response
Expand Down
21 changes: 18 additions & 3 deletions tests/php/Controllers/LinkFieldControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SilverStripe\LinkField\Controllers\LinkFieldController;
use SilverStripe\LinkField\Tests\Models\LinkTest\LinkOwner;
use SilverStripe\Versioned\Versioned;
use SilverStripe\LinkField\Models\Link;

class LinkFieldControllerTest extends FunctionalTest
{
Expand Down Expand Up @@ -528,14 +529,20 @@ public function provideLinkData(): array
}

/**
* @dataProvider provideLinkDelete
* @dataProvider provideLinkArchive
*/
public function testLinkDelete(
public function testLinkArchive(
string $idType,
string $fail,
int $expectedCode
): void {
TestPhoneLink::$fail = $fail;
if ($fail == 'can-delete') {
// Remove the Versioned extension because there's logic in LinkFieldController
// to use either canArchive() or canDelete() based on the presence of the Versioned extension
// Note that you must remove the extension on the base class rather than a TestOnly subclass
Link::remove_extension(Versioned::class);
}
$owner = $this->getFixtureLinkOwner();
$ownerID = $owner->ID;
$ownerClass = urlencode($owner->ClassName);
Expand All @@ -560,16 +567,24 @@ public function testLinkDelete(
} else {
$this->assertNull(TestPhoneLink::get()->byID($fixtureID));
}
if ($fail == 'can-delete') {
Link::add_extension(Versioned::class);
}
}

public function provideLinkDelete(): array
public function provideLinkArchive(): array
{
return [
'Valid' => [
'idType' => 'existing',
'fail' => '',
'expectedCode' => 204,
],
'Reject fail canArchive()' => [
'idType' => 'existing',
'fail' => 'can-archive',
'expectedCode' => 403,
],
'Reject fail canDelete()' => [
'idType' => 'existing',
'fail' => 'can-delete',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function canDelete($member = null)
return TestPhoneLink::$fail !== 'can-delete';
}

public function canArchive($member = null)
{
return TestPhoneLink::$fail !== 'can-archive';
}

public function validate(): ValidationResult
{
$validationResult = parent::validate();
Expand Down
Loading