Skip to content

Commit df3fd4d

Browse files
authored
Merge pull request #2 from theodson/feature/safer_environments
Support multiple environments that require a prompt before anonymizing.
2 parents 70c5132 + f879069 commit df3fd4d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

config/anonymizer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77

88
/*
99
|--------------------------------------------------------------------------
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,
19+
20+
/*
1021
| Model Ordering
1122
|--------------------------------------------------------------------------
1223
|
1324
| Optionally specify the order of anonymization, these Models will be anonymized first.
1425
|
1526
*/
1627
'ordered_models' => []
17-
1828
];

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)