Skip to content

Commit d61f64f

Browse files
committed
Support multiple environments that require a prompt before anonymizing.
By default the listed blocked environments are forcibly blocked for extra safety.
1 parent b103d61 commit d61f64f

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

config/anonymizer.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,17 @@
33
return [
44
'locale' => 'en_US',
55
'chunk_size' => 1000,
6-
'models_path' => app_path('Models')
6+
'models_path' => app_path('Models'),
7+
8+
/*
9+
|--------------------------------------------------------------------------
10+
| Blocked Environments
11+
|--------------------------------------------------------------------------
12+
|
13+
| Support multiple environments that require a prompt before anonymizing.
14+
| By default the listed blocked environments are forcibly blocked for extra safety.
15+
|
16+
*/
17+
'blocked_env' => ['production'],
18+
'force_blocked_env' => true,
719
];

src/Anonymizer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function __construct()
1919
);
2020
}
2121

22+
public function isBlockedEnvironment(): bool
23+
{
24+
return in_array(config('app.env'), config('anonymizer.blocked_env', []));
25+
}
26+
2227
private function getAllModels(): array
2328
{
2429
return File::allFiles(config('anonymizer.models_path', app_path('Models')));
@@ -63,6 +68,10 @@ public function getChunk(Model $model, callable $call): bool
6368

6469
public function changeData(Model $model): bool
6570
{
71+
if ($this->isBlockedEnvironment() && config('anonymizer.force_blocked_env', true)) {
72+
throw new \Exception(sprintf("Environment '%s' has blocking enforced.", config('app.env')));
73+
}
74+
6675
return $model
6776
->setTouchedRelations([]) // disable touch owners
6877
->updateQuietly( // disable events handling

src/Commands/AnonymizerCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public function __construct()
3535

3636
public function handle(): int
3737
{
38-
if (! $this->confirmToProceed()) {
38+
if (!$this->confirmToProceed('Environment "'.config('app.env').'" blocked.', function () {
39+
return $this->service->isBlockedEnvironment();
40+
})) {
3941
return 0;
4042
}
4143

0 commit comments

Comments
 (0)