Skip to content

Commit 845156f

Browse files
committed
add "Rollback" action
1 parent 6dfbb51 commit 845156f

File tree

12 files changed

+334
-56
lines changed

12 files changed

+334
-56
lines changed

resources/lang/en/resources/content-version.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,21 @@
5757
],
5858
],
5959
],
60+
'rollback' => [
61+
'label' => 'Rollback',
62+
'heading' => 'Rollback version from :from to :to',
63+
'description' => 'Are you sure you want to rollback to this version? This action cannot be undone.',
64+
'invalid_heading' => 'Has error',
65+
'invalid_description' => 'Something went wrong. The selected version is invalid for rollback.',
66+
'messages' => [
67+
'success' => [
68+
'title' => 'The content has been rolled back to the selected version.',
69+
],
70+
'failure' => [
71+
'title' => 'Rollback failed.',
72+
'body' => 'An error occurred while rolling back the content. (Details: :details)',
73+
],
74+
],
75+
],
6076
],
6177
];

resources/lang/zh_TW/resources/content-version.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,21 @@
5757
],
5858
],
5959
],
60+
'rollback' => [
61+
'label' => '恢復',
62+
'heading' => '從 :from 恢復到 :to',
63+
'description' => '您確定要恢復到此版本嗎?此操作無法撤銷。',
64+
'invalid_heading' => '發生錯誤',
65+
'invalid_description' => '發生錯誤。選定的版本無法恢復。',
66+
'messages' => [
67+
'success' => [
68+
'title' => '內容已恢復到選定的版本。',
69+
],
70+
'failure' => [
71+
'title' => '恢復失敗。',
72+
'body' => '恢復內容時發生錯誤。(詳情: :details)',
73+
],
74+
],
75+
],
6076
],
6177
];

resources/views/filament/actions/content-history-detail.blade.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
$propertyData = $diff['propertyData'] ?? [];
44
@endphp
55
<div class="content-history-details flex flex-col gap-4">
6-
<x-inspirecms::version-diff :items="$basicDiff" :heading="__('inspirecms::resources/content-version.content_history_detail.general_info')" />
7-
<x-inspirecms::version-diff :items="$propertyData" class="content-property-data" :heading="__('inspirecms::resources/content-version.content_history_detail.property_data')" />
6+
<x-inspirecms::version-diff
7+
:heading="__('inspirecms::resources/content-version.content_history_detail.general_info')"
8+
:items="$basicDiff"
9+
/>
10+
<x-inspirecms::version-diff
11+
class="content-property-data"
12+
:heading="__('inspirecms::resources/content-version.content_history_detail.property_data')"
13+
:items="$propertyData"
14+
/>
815
</div>

src/Filament/Resources/ContentResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public static function getPermissionPrefixes(): array
7878
'view_history',
7979
'set_as_default',
8080
'lock',
81+
'rollback_version',
8182
];
8283
}
8384

src/Licensing/LicenseManager.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace SolutionForest\InspireCms\Licensing;
44

5-
use Illuminate\Database\Eloquent\SoftDeletingScope;
65
use Illuminate\Support\Arr;
76
use Illuminate\Support\Carbon;
87
use Illuminate\Support\Facades\Cache;
@@ -139,33 +138,22 @@ public function getLimitedRoleCount(): ?int
139138

140139
public function canCreateUser(): bool
141140
{
142-
$limitedUserCount = $this->getLimitedUserCount();
143-
if (is_null($limitedUserCount)) {
144-
return true; // Unlimited users
145-
}
146-
$existingUserCount = InspireCmsConfig::getUserModelClass()::query()
147-
->withoutGlobalScope(SoftDeletingScope::class)
148-
->count();
149-
150-
return $existingUserCount < $limitedUserCount;
141+
return LicenseTierAction::CreateUser->isAllowed();
151142
}
152143

153144
public function canCreateRole(): bool
154145
{
155-
$limitedRoleCount = $this->getLimitedRoleCount();
156-
if (is_null($limitedRoleCount)) {
157-
return true; // Unlimited roles
158-
}
159-
$existingRoleCount = InspireCmsConfig::getRoleModelClass()::query()
160-
->withoutGlobalScope(SoftDeletingScope::class)
161-
->count();
162-
163-
return $existingRoleCount < $limitedRoleCount;
146+
return LicenseTierAction::CreateRole->isAllowed();
164147
}
165148

166149
public function canGlobalSearch(): bool
167150
{
168-
return $this->getLicenseTier() === 'pro';
151+
return LicenseTierAction::GlobalSearch->isAllowed();
152+
}
153+
154+
public function canRollbackVersion(): bool
155+
{
156+
return LicenseTierAction::RollbackContentVersion->isAllowed();
169157
}
170158

171159
public function getLicenseTier(): ?string
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace SolutionForest\InspireCms\Licensing;
4+
5+
use Illuminate\Database\Eloquent\SoftDeletingScope;
6+
use SolutionForest\InspireCms\InspireCmsConfig;
7+
8+
/**
9+
* Enum representing the actions that can be restricted based on the license tier.
10+
*/
11+
enum LicenseTierAction
12+
{
13+
case GlobalSearch;
14+
case CreateUser;
15+
case CreateRole;
16+
case RollbackContentVersion;
17+
18+
private const RESTRICTED_ACTIONS_PRO = [
19+
self::GlobalSearch,
20+
self::CreateUser,
21+
self::CreateRole,
22+
self::RollbackContentVersion,
23+
];
24+
25+
private const RESTRICTED_ACTIONS_FREE = [
26+
self::CreateUser,
27+
self::CreateRole,
28+
];
29+
30+
public function isAllowed($tier = null): bool
31+
{
32+
$licenseManager = app(LicenseManager::class);
33+
34+
$tier ??= $licenseManager->getLicenseTier();
35+
36+
$preCheck = match ($tier) {
37+
'pro' => in_array($this, self::RESTRICTED_ACTIONS_PRO),
38+
'free' => in_array($this, self::RESTRICTED_ACTIONS_FREE),
39+
default => null,
40+
};
41+
42+
if ($preCheck === false) {
43+
return false;
44+
}
45+
46+
// Validate each action based on the tier
47+
switch ($this) {
48+
49+
case self::CreateUser:
50+
$limitedUserCount = $licenseManager->getLimitedUserCount();
51+
if (is_null($limitedUserCount)) {
52+
return true; // Unlimited users
53+
}
54+
$existingUserCount = InspireCmsConfig::getUserModelClass()::query()
55+
->withoutGlobalScope(SoftDeletingScope::class)
56+
->count();
57+
return $existingUserCount < $limitedUserCount;
58+
59+
case self::CreateRole:
60+
$limitedRoleCount = $licenseManager->getLimitedRoleCount();
61+
if (is_null($limitedRoleCount)) {
62+
return true; // Unlimited roles
63+
}
64+
$existingRoleCount = InspireCmsConfig::getRoleModelClass()::query()
65+
->withoutGlobalScope(SoftDeletingScope::class)
66+
->count();
67+
return $existingRoleCount < $limitedRoleCount;
68+
69+
default:
70+
break;
71+
}
72+
73+
return true;
74+
}
75+
}

src/Listeners/Content/ProcessContentVersion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function processContentVersion(HasContentVersions | Model $model)
3838
$contentVersionData['from_data'] = $contentVersionData['from'] ?? [];
3939
$contentVersionData['to_data'] = $contentVersionData['to'] ?? [];
4040
unset($contentVersionData['from'], $contentVersionData['to']);
41-
$contentVersionData['avoid_to_clean'] = $isPublishing;
41+
$contentVersionData['avoid_to_clean'] = $contentVersionData['avoid_to_clean'] ?? $isPublishing;
4242

4343
$contentVersion = $model->contentVersions()->make($contentVersionData);
4444

0 commit comments

Comments
 (0)