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

New disableRestore setting #63

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"weareferal/remote-core": "4.1.2"
},
"require-dev": {
"phpstan/phpstan": "1.8.x-dev",
"phpstan/phpstan": "1.11.x-dev",
"craftcms/phpstan": "dev-main",
"craftcms/rector": "dev-main"
},
Expand Down
10 changes: 8 additions & 2 deletions src/config/remote-sync.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@
'hideDatabases' => false,

// Hide the volume sync panel on the utilities page
'hideVolumes' => false
'hideVolumes' => false,

// Enable pull and restore functionality
'disableRestore' => false,
],
'dev' => [],
'staging' => [],
'production' => [],
'production' => [
// Disable pull and restore only in production environment
'disableRestore' => true,
],
];
3 changes: 3 additions & 0 deletions src/console/controllers/DatabaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public function actionPull($filename)
$this->requirePluginEnabled();
$this->requirePluginConfigured();
$startTime = microtime(true);
if (RemoteSync::getInstance()->getSettings()->disableRestore) {
throw new \Exception(Craft::t('remote-sync', 'Restore not enabled for this environment'));
}
RemoteSync::getInstance()->provider->pullDatabase($filename);
$this->stdout("Pulled and restored remote database:" . PHP_EOL, Console::FG_GREEN);
$this->stdout("- " . $filename . PHP_EOL);
Expand Down
3 changes: 3 additions & 0 deletions src/console/controllers/VolumeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public function actionPull($filename)
$this->requirePluginEnabled();
$this->requirePluginConfigured();
$startTime = microtime(true);
if (RemoteSync::getInstance()->getSettings()->disableRestore) {
throw new \Exception(Craft::t('remote-sync', 'Restore not enabled for this environment'));
}
RemoteSync::getInstance()->provider->pullVolume($filename);
$this->stdout("Pulled and restored remote volume:" . PHP_EOL, Console::FG_GREEN);
$this->stdout("- " . $filename . PHP_EOL);
Expand Down
8 changes: 8 additions & 0 deletions src/controllers/RemoteSyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ public function actionPullDatabase()
$plugin = RemoteSync::getInstance();
$settings = $plugin->getSettings();

if ($settings->disableRestore) {
return $this->asErrorJson(Craft::t('remote-sync', 'Restore not enabled for this environment'));
}

try {
if ($settings->useQueue) {
Craft::$app->queue->push(new PullDatabaseJob([
Expand Down Expand Up @@ -231,6 +235,10 @@ public function actionPullVolume()
$plugin = RemoteSync::getInstance();
$settings = $plugin->getSettings();

if ($settings->disableRestore) {
return $this->asErrorJson(Craft::t('remote-sync', 'Restore not enabled for this environment'));
}

try {
if ($settings->useQueue) {
Craft::$app->queue->push(new PullVolumeJob([
Expand Down
36 changes: 36 additions & 0 deletions src/migrations/m240319_070857_add_disable_restore_setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace weareferal\remotesync\migrations;

use Craft;
use craft\db\Migration;

/**
* m240319_070857_add_disable_restore_setting migration.
*/
class m240319_070857_add_disable_restore_setting extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
// Create the disableRestore setting, if it doesn't already exist
$remoteSync = Craft::$app->plugins->getPlugin("remote-sync");
$settings = $remoteSync->getSettings();
if (!property_exists($settings, 'disableRestore')) {
Craft::$app->getPlugins()->savePluginSettings($remoteSync, [
Copy link
Author

Choose a reason for hiding this comment

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

I'm new to craft plugin development... is this needed?

'disableRestore' => 'boolean',
]);
}
return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
return true;
}
}
3 changes: 2 additions & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class Settings extends BaseSettings
{
public $disableRestore = false;
public $keepEmergencyBackup = true;
public $prune = false;
public $pruneLimit = 10;
Expand All @@ -16,7 +17,7 @@ public function rules(): array
$rules = parent::rules();
return $rules + [
[
['keepEmergencyBackup', 'prune'],
['disableRestore', 'keepEmergencyBackup', 'prune'],
'boolean'
],
[
Expand Down
10 changes: 10 additions & 0 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@
errors: settings.getErrors('keepEmergencyBackup'),
warning: configWarning('keepEmergencyBackup', 'remote-sync')
}) }}

{{ forms.lightswitchField({
label: "Disable pull and restore"|t('remote-sync'),
instructions: "Disable pulling & restoring for this environment."|t('remote-sync'),
name: 'disableRestore',
id: 'disableRestore',
on: settings.disableRestore,
errors: settings.getErrors('disableRestore'),
warning: configWarning('disableRestore', 'remote-sync')
}) }}
{% endblock %}
2 changes: 2 additions & 0 deletions src/templates/utilities/_includes/_remote-sync-section.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ <h2 data-icon="{{ icon }}">{{ "Remote " ~ labelPlural|t('remote-sync') }}</h2>
<td></td>
<td></td>
<td class="thin">
{% if not disableRestore %}
<button class="btn small pull-button" title="{{ 'Pull and restore ' ~ label|lower|t }}">{{ 'Restore'|t }}</button>
{% endif %}
<button class="btn small delete-button" title="{{ 'Delete ' ~ label|lower|t }}">{{ 'Delete'|t }}</button>
</td>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions src/templates/utilities/remote-sync.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
pushAction: 'remote-sync/remote-sync/push-database',
pullAction: 'remote-sync/remote-sync/pull-database',
deleteAction: 'remote-sync/remote-sync/delete-database',
disableRestore: disableRestore,
disabled: false
} %}
{% endif %}
Expand All @@ -62,6 +63,7 @@
pushAction: 'remote-sync/remote-sync/push-volume',
pullAction: 'remote-sync/remote-sync/pull-volume',
deleteAction: 'remote-sync/remote-sync/delete-volume',
disableRestore: disableRestore,
disabled: not haveVolumes
} %}
{% block message %}
Expand Down
1 change: 1 addition & 0 deletions src/utilities/RemoteSyncUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function contentHtml(): string
"hideDatabases" => $settings->hideDatabases,
"hideVolumes" => $settings->hideVolumes,
"haveVolumes" => !$settings->hideVolumes && $haveVolumes,
"disableRestore" => $settings->disableRestore,
"queueActive" => $queueActive
]);
}
Expand Down