-
Notifications
You must be signed in to change notification settings - Fork 0
Add ability to anonymize JSON resources #4
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
63b6ac4
Extract anonymization logic into AnonymizesAttributes trait
stevebauman 04cd07c
Add AnonymizableResource and AnonymizedResource trait
stevebauman c0e32e6
Remove withoutAnonymization
stevebauman 08854fd
Add AnonymizedJsonResource tests
stevebauman be57b83
Tweak test
stevebauman 7678ce5
Re-add withoutAnonymization
stevebauman 6b8db67
Re-add test
stevebauman 09e3d33
Use getKey instead of assuming ID
stevebauman efef4e0
Add missing param doc
stevebauman c3f6d48
Extract Anonymizable key retrieval into method
stevebauman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| namespace DirectoryTree\Anonymize; | ||
|
|
||
| trait AnonymizedResource | ||
| { | ||
| use AnonymizesAttributes; | ||
|
|
||
| /** | ||
| * Transform the resource into an array. | ||
| * | ||
| * @param \Illuminate\Http\Request|null $request | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function resolve($request = null): array | ||
| { | ||
| $attributes = parent::resolve($request); | ||
|
|
||
| if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) { | ||
| return $attributes; | ||
| } | ||
|
|
||
| return $this->addAnonymizedAttributesToArray($attributes); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| namespace DirectoryTree\Anonymize; | ||
|
|
||
| use Illuminate\Support\Facades\App; | ||
|
|
||
| trait AnonymizesAttributes | ||
| { | ||
| /** | ||
| * Whether anonymization is enabled for the current instance. | ||
| */ | ||
| protected bool $anonymizeEnabled = true; | ||
|
|
||
| /** | ||
| * The anonymized attributes for the current instance and seed. | ||
| */ | ||
| protected array $anonymizedAttributeCache; | ||
|
|
||
| /** | ||
| * The seed for the cached anonymized attributes. | ||
| */ | ||
| protected string $anonymizedAttributeCacheSeed; | ||
|
|
||
| /** | ||
| * Execute a callback without anonymization. | ||
| * | ||
| * @template TReturn | ||
| * | ||
| * @param callable($this): TReturn $callback | ||
| * @return TReturn | ||
| */ | ||
| public function withoutAnonymization(callable $callback): mixed | ||
| { | ||
| $previous = $this->anonymizeEnabled; | ||
|
|
||
| $this->anonymizeEnabled = false; | ||
|
|
||
| try { | ||
| return $callback($this); | ||
| } finally { | ||
| $this->anonymizeEnabled = $previous; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the key for the anonymizable instance. | ||
| */ | ||
| public function getAnonymizableKey(): ?string | ||
| { | ||
| return $this->getKey(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the seed for the anonymizable instance. | ||
| */ | ||
| public function getAnonymizableSeed(): string | ||
| { | ||
| return get_class($this).':'.$this->getAnonymizableKey(); | ||
| } | ||
|
|
||
| /** | ||
| * Add the anonymized attributes to the attribute array. | ||
| * | ||
| * @param array<string, mixed> $attributes | ||
| * @return array<string, mixed> | ||
| */ | ||
| protected function addAnonymizedAttributesToArray(array $attributes): array | ||
| { | ||
| foreach ($this->getCachedAnonymizedAttributes() as $key => $value) { | ||
| if (! array_key_exists($key, $attributes)) { | ||
| continue; | ||
| } | ||
|
|
||
| $attributes[$key] = $value; | ||
| } | ||
|
|
||
| return $attributes; | ||
| } | ||
|
|
||
| /** | ||
| * Make the anonymized attributes. | ||
| */ | ||
| protected function getCachedAnonymizedAttributes(): array | ||
| { | ||
| return $this->withoutAnonymization(function (): array { | ||
| $seed = $this->getAnonymizableSeed(); | ||
|
|
||
| if (! isset($this->anonymizedAttributeCache) || $this->anonymizedAttributeCacheSeed !== $seed) { | ||
| $this->anonymizedAttributeCache = $this->getAnonymizedAttributes( | ||
| static::getAnonymizeManager()->faker($seed) | ||
| ); | ||
|
|
||
| $this->anonymizedAttributeCacheSeed = $seed; | ||
| } | ||
|
|
||
| return $this->anonymizedAttributeCache; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Get the anonymize manager instance. | ||
| */ | ||
| protected static function getAnonymizeManager(): AnonymizeManager | ||
| { | ||
| return App::make(AnonymizeManager::class); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| namespace DirectoryTree\Anonymize\Tests; | ||
|
|
||
| use DirectoryTree\Anonymize\Anonymizable; | ||
| use DirectoryTree\Anonymize\AnonymizedResource; | ||
| use Faker\Generator; | ||
| use Illuminate\Http\Resources\Json\JsonResource; | ||
|
|
||
| class AnonymizedJsonResource extends JsonResource implements Anonymizable | ||
| { | ||
| use AnonymizedResource; | ||
|
|
||
| public function getAnonymizedAttributes(Generator $faker): array | ||
| { | ||
| return [ | ||
| 'name' => $faker->name(), | ||
| 'address' => $faker->address(), | ||
| ]; | ||
| } | ||
|
|
||
| public function getAnonymizableKey(): string | ||
| { | ||
| return 1; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| use DirectoryTree\Anonymize\Facades\Anonymize; | ||
| use DirectoryTree\Anonymize\Tests\AnonymizedJsonResource; | ||
|
|
||
| it('anonymizes json resource when anonymization is enabled', function () { | ||
| Anonymize::enable(); | ||
|
|
||
| $resource = new AnonymizedJsonResource([ | ||
| 'name' => 'Foo Bar', | ||
| 'address' => '1600 Pennsylvania Avenue', | ||
| ]); | ||
|
|
||
| expect($resource->resolve())->not->toHaveKey('name', 'Foo Bar') | ||
| ->and($resource->resolve())->not->toHaveKey('address', '1600 Pennsylvania Avenue'); | ||
| }); | ||
|
|
||
| it('does not anonymize json resource when anonymization is disabled', function () { | ||
| Anonymize::disable(); | ||
|
|
||
| $resource = new AnonymizedJsonResource([ | ||
| 'name' => 'Foo Bar', | ||
| 'address' => '1600 Pennsylvania Avenue', | ||
| ]); | ||
|
|
||
| expect($resource->resolve())->toHaveKey('name', 'Foo Bar') | ||
| ->and($resource->resolve())->toHaveKey('address', '1600 Pennsylvania Avenue'); | ||
| }); |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added this method to help prevent possible collisions so developers don't have to remember to provide a unique seed prefix (
get_class($this)) and can instead just worry about the key itself when overriding.