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

API Delete deprecated canArchive method #460

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
15 changes: 5 additions & 10 deletions src/GridFieldArchiveAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace SilverStripe\Versioned;

use SilverStripe\Control\Controller;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_ActionMenuItem;
use SilverStripe\Forms\GridField\GridField_ActionProvider;
Expand All @@ -12,6 +11,7 @@
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException;
use SilverStripe\View\ViewableData;

/**
* This class is a {@link GridField} component that replaces the delete action
Expand Down Expand Up @@ -177,8 +177,7 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
return;
}

$canArchive = Deprecation::withNoReplacement(fn() => $item->canArchive());
if (!$canArchive) {
if (!$item->canDelete()) {
throw new ValidationException(
_t(__CLASS__ . '.ArchivePermissionsFailure', "No archive permissions")
);
Expand All @@ -192,17 +191,13 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
* Returns the GridField_FormAction if archive can be performed
*
* @param GridField $gridField
* @param DataObject $record
* @param ViewableData $record
Comment on lines -195 to +194
Copy link
Member Author

@GuySartorelli GuySartorelli Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semi-unrelated PHPDoc fix - gridfield can accept any viewable data. This class is used to replace the delete button when versioned is installed, regardless of whether the gridfield in use has dataobjects or not.

* @return GridField_FormAction|null
*/
public function getArchiveAction($gridField, $record)
{
/* @var DataObject|Versioned $record */
if (!$record->hasMethod('canArchive')) {
return null;
}
$canArchive = Deprecation::withNoReplacement(fn() => $record->canArchive());
if (!$canArchive) {
/** @var ViewableData|Versioned $record */
if (!$record->has_extension(Versioned::class) || !$record->canDelete()) {
return null;
}

Expand Down
50 changes: 9 additions & 41 deletions src/Versioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -1494,51 +1494,19 @@ protected function extendCanUnpublish()

/**
* Check if the current user is allowed to archive this record.
* If extended, ensure that both canDelete and canUnpublish are extended also
*
* @param Member $member
* @return bool
* @deprecated 5.3.0 Use canDelete() instead.
* We're intentionally using the canDelete check for archiving,
* since there's no concept of "deleting" a versioned record
* and having separate permission checks was confusing and easy
* to forget.
*/
public function canArchive($member = null)
public function canDelete($member = null): ?bool
{
Deprecation::notice('5.3.0', 'Use canDelete() instead.');
if (!$member) {
$member = Security::getCurrentUser();
}

// Standard mechanism for accepting permission changes from extensions
$owner = $this->owner;
$extended = Deprecation::withNoReplacement(fn() => $owner->extendedCan('canArchive', $member));
if ($extended !== null) {
return $extended;
}

// Admin permissions allow
if (Permission::checkMember($member, "ADMIN")) {
return true;
}

// Check if this record can be deleted from stage
if (!$owner->canDelete($member)) {
// If the user isn't allowed to unpublish, they're definitely
// not allowed to archive live content.
if ($this->hasStages() && $this->isPublished() && !$this->getOwner()->canUnpublish($member)) {
return false;
}

// Check if we can delete from live
if (!$owner->canUnpublish($member)) {
return false;
}

return true;
}

/**
* @deprecated 5.3.0 Will be removed without equivalent functionality.
*/
protected function extendCanArchive()
{
Deprecation::notice('5.3.0', 'Will be removed without equivalent functionality.');
// Prevent canArchive() extending itself
return null;
}

Expand Down Expand Up @@ -1827,7 +1795,7 @@ public function publishSingle()
/**
* Removes the record from both live and stage
*
* User code should call {@see canArchive()} prior to invoking this method.
* User code should call {@see canDelete()} prior to invoking this method.
*
* @return bool Success
*/
Expand Down
8 changes: 3 additions & 5 deletions src/VersionedGridFieldItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
Expand Down Expand Up @@ -119,8 +118,7 @@ public function doArchive($data, $form)
{
/** @var Versioned|DataObject $record */
$record = $this->getRecord();
$canArchive = Deprecation::withNoReplacement(fn() => $record->canArchive());
if (!$canArchive) {
if (!$record->canDelete()) {
return $this->httpError(403);
}

Expand Down Expand Up @@ -295,7 +293,7 @@ protected function addVersionedButtons(DataObject $record, FieldList $actions)
$canPublish = $record->canPublish();
$canUnpublish = $record->canUnpublish();
$canEdit = $record->canEdit();
$canArchive = Deprecation::withNoReplacement(fn() => $record->canArchive());
$canDelete = $record->canDelete();

// "save", supports an alternate state that is still clickable, but notifies the user that the action is not needed.
$noChangesClasses = 'btn-outline-primary font-icon-tick';
Expand Down Expand Up @@ -379,7 +377,7 @@ protected function addVersionedButtons(DataObject $record, FieldList $actions)
}

// "archive" action
if (($isOnDraft || $isPublished) && $canArchive) {
if (($isOnDraft || $isPublished) && $canDelete) {
// Replace "delete" action
$actions->removeByName('action_doDelete');
$title = $isPublished
Expand Down
Loading