|
14 | 14 |
|
15 | 15 | namespace Grav\Plugin\Admin; |
16 | 16 |
|
| 17 | +use Grav\Common\Config\Config; |
17 | 18 | use Grav\Common\Filesystem\Folder; |
18 | 19 | use Grav\Common\GPM\Installer; |
19 | 20 | use Grav\Common\GPM\GPM; |
@@ -178,6 +179,14 @@ public function hasSnapshots(): bool |
178 | 179 | */ |
179 | 180 | public function restoreSnapshot(string $snapshotId): array |
180 | 181 | { |
| 182 | + if (!$this->isSafeUpgradeEnabled()) { |
| 183 | + return [ |
| 184 | + 'status' => 'error', |
| 185 | + 'message' => 'Safe upgrade is disabled in configuration.', |
| 186 | + 'manifest' => null, |
| 187 | + ]; |
| 188 | + } |
| 189 | + |
181 | 190 | try { |
182 | 191 | $safeUpgrade = $this->getSafeUpgradeService(); |
183 | 192 | $manifest = $safeUpgrade->rollback($snapshotId); |
@@ -934,6 +943,12 @@ public function runRestore(array $options): array |
934 | 943 |
|
935 | 944 | public function runSnapshot(array $options): array |
936 | 945 | { |
| 946 | + if (!$this->isSafeUpgradeEnabled()) { |
| 947 | + return $this->errorResult('Safe upgrade is disabled in configuration.', [ |
| 948 | + 'operation' => 'snapshot' |
| 949 | + ]); |
| 950 | + } |
| 951 | + |
937 | 952 | $label = isset($options['label']) ? (string)$options['label'] : null; |
938 | 953 | if ($label !== null) { |
939 | 954 | $label = trim($label); |
@@ -1068,14 +1083,37 @@ protected function isSafeUpgradeEnabled(): bool |
1068 | 1083 | { |
1069 | 1084 | try { |
1070 | 1085 | $config = $this->grav['config'] ?? null; |
1071 | | - if ($config === null) { |
1072 | | - return true; |
| 1086 | + return self::configAllowsSafeUpgrade($config); |
| 1087 | + } catch (Throwable $e) { |
| 1088 | + return false; |
| 1089 | + } |
| 1090 | + } |
| 1091 | + |
| 1092 | + /** |
| 1093 | + * @param Config|null $config |
| 1094 | + * @return bool |
| 1095 | + */ |
| 1096 | + public static function configAllowsSafeUpgrade(?Config $config): bool |
| 1097 | + { |
| 1098 | + if ($config === null) { |
| 1099 | + return false; |
| 1100 | + } |
| 1101 | + |
| 1102 | + $value = $config->get('system.updates.safe_upgrade'); |
| 1103 | + if ($value === null) { |
| 1104 | + return false; |
| 1105 | + } |
| 1106 | + |
| 1107 | + if (is_string($value)) { |
| 1108 | + $filtered = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
| 1109 | + if ($filtered === null) { |
| 1110 | + return false; |
1073 | 1111 | } |
1074 | 1112 |
|
1075 | | - return (bool)$config->get('system.updates.safe_upgrade', true); |
1076 | | - } catch (Throwable $e) { |
1077 | | - return true; |
| 1113 | + return $filtered; |
1078 | 1114 | } |
| 1115 | + |
| 1116 | + return (bool)$value; |
1079 | 1117 | } |
1080 | 1118 |
|
1081 | 1119 | /** |
|
0 commit comments