Skip to content

Commit

Permalink
[5.x] Always detach localizations when user is missing permissions to…
Browse files Browse the repository at this point in the history
… delete in other sites (#10587)

Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
duncanmcclean and jasonvarga authored Aug 8, 2024
1 parent 6e93d96 commit 3f86e54
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
12 changes: 11 additions & 1 deletion resources/js/components/collections/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ export default {
reorderUrl: { type: String, required: true },
initialSite: { type: String, required: true },
sites: { type: Array },
totalSitesCount: { type: Number },
canChangeLocalizationDeleteBehavior: { type: Boolean },
structurePagesUrl: { type: String },
structureSubmitUrl: { type: String },
structureMaxDepth: { type: Number, default: Infinity },
Expand Down Expand Up @@ -276,11 +278,19 @@ export default {
},
saveTree() {
if (this.sites.length === 1 || this.deletedEntries.length === 0) {
if (this.deletedEntries.length === 0) {
this.performTreeSaving();
return;
}
// When the user doesn't have permission to access the sites the entry is localized in,
// we should use the "copy" behavior to detach the entry from the site.
if (! this.canChangeLocalizationDeleteBehavior) {
this.deleteLocalizationBehavior = 'copy';
this.$nextTick(() => this.performTreeSaving());
return
}
this.showLocalizationDeleteBehaviorConfirmation = true;
this.localizationDeleteBehaviorConfirmCallback = (behavior) => {
this.deleteLocalizationBehavior = behavior;
Expand Down
1 change: 1 addition & 0 deletions resources/views/collections/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
reorder-url="{{ cp_route('collections.entries.reorder', $collection->handle()) }}"
initial-site="{{ $site }}"
:sites="{{ json_encode($sites) }}"
:can-change-localization-delete-behavior="{{ Statamic\Support\Str::bool($canChangeLocalizationDeleteBehavior) }}"

@if ($collection->hasStructure())
:structured="{{ Statamic\Support\Str::bool($user->can('reorder', $collection)) }}"
Expand Down
17 changes: 16 additions & 1 deletion src/Actions/DeleteMultisiteEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Actions;

use Statamic\Contracts\Entries\Entry;
use Statamic\Facades\User;
use Statamic\Statamic;

class DeleteMultisiteEntry extends Delete
Expand All @@ -18,6 +19,10 @@ public function visibleTo($item)

public function fieldItems()
{
if (! $this->canChangeBehavior()) {
return [];
}

return [
'behavior' => [
'display' => __('Localizations'),
Expand All @@ -40,7 +45,7 @@ public function buttonText()

public function run($items, $values)
{
$behavior = $values['behavior'];
$behavior = $this->canChangeBehavior() ? $values['behavior'] : 'copy';

if ($behavior === 'copy') {
$items->each->detachLocalizations();
Expand All @@ -50,4 +55,14 @@ public function run($items, $values)

$items->each->delete();
}

private function canChangeBehavior(): bool
{
return $this->items->every(function ($entry) {
$descendants = $entry->descendants();

return $descendants->isNotEmpty()
&& $descendants->every(fn ($descendant) => User::current()->can('view', $descendant->site()));
});
}
}
2 changes: 2 additions & 0 deletions src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ public function detachLocalizations()
->save();
});

Blink::forget('entry-descendants-'.$this->id());

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ public function show(Request $request, $collection)
'collection' => $collection->handle(),
'blueprints' => $blueprints->pluck('handle')->all(),
]),
'sites' => $this->getAuthorizedSitesForCollection($collection),
'sites' => $authorizedSites = $this->getAuthorizedSitesForCollection($collection),
'createUrls' => $collection->sites()
->mapWithKeys(fn ($site) => [$site => cp_route('collections.entries.create', [$collection->handle(), $site])])
->all(),
'canCreate' => User::current()->can('create', [EntryContract::class, $collection]) && $collection->hasVisibleEntryBlueprint(),
'canChangeLocalizationDeleteBehavior' => count($authorizedSites) > 1 && (count($authorizedSites) == $collection->sites()->count()),
];

if ($collection->queryEntries()->count() === 0) {
Expand Down

0 comments on commit 3f86e54

Please sign in to comment.