-
-
Notifications
You must be signed in to change notification settings - Fork 144
feat(mapper): add #[Hidden] attribute for sensitive properties
#1931
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
Open
xHeaven
wants to merge
8
commits into
tempestphp:3.x
Choose a base branch
from
xHeaven:hidden-attrib
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+286
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c20477a
feat(mapper): add Hidden attribute
xHeaven 7ec9dde
feat(mapper): skip hidden properties in serialization
xHeaven 8870eb2
feat(database): exclude hidden fields from SELECT
xHeaven b1f18c5
feat(database): add include() method for hidden fields
xHeaven 97b2298
fix(database): deduplicate fields in select()
xHeaven 7f7e959
test(database): add Hidden attribute integration tests
xHeaven 9b81168
docs: add Hidden attribute documentation
xHeaven 4f291a2
refactor(database): use #[Test] attributes
xHeaven 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
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,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Mapper; | ||
|
|
||
| use Attribute; | ||
|
|
||
| /** | ||
| * Hidden properties are excluded from SELECT queries and serialization. | ||
| */ | ||
| #[Attribute(Attribute::TARGET_PROPERTY)] | ||
| final readonly class Hidden | ||
| { | ||
| } |
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
153 changes: 153 additions & 0 deletions
153
tests/Integration/Database/ModelInspector/HiddenTest.php
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,153 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Tempest\Integration\Database\ModelInspector; | ||
|
|
||
| use PHPUnit\Framework\Attributes\Test; | ||
| use Tempest\Database\IsDatabaseModel; | ||
| use Tempest\Database\PrimaryKey; | ||
| use Tempest\Database\Table; | ||
| use Tempest\Mapper\Hidden; | ||
| use Tests\Tempest\Integration\FrameworkIntegrationTestCase; | ||
|
|
||
| use function Tempest\Database\inspect; | ||
| use function Tempest\Database\query; | ||
| use function Tempest\Mapper\map; | ||
|
|
||
| final class HiddenTest extends FrameworkIntegrationTestCase | ||
| { | ||
| #[Test] | ||
| public function hidden_property_is_excluded_from_select_fields(): void | ||
| { | ||
| $model = inspect(HiddenTestModel::class); | ||
| $selectFields = $model->getSelectFields(); | ||
|
|
||
| $this->assertContains('id', $selectFields->toArray()); | ||
| $this->assertContains('name', $selectFields->toArray()); | ||
| $this->assertNotContains('password', $selectFields->toArray()); | ||
| $this->assertNotContains('secret', $selectFields->toArray()); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function hidden_property_is_included_in_property_values(): void | ||
| { | ||
| $instance = new HiddenTestModel(); | ||
| $instance->id = new PrimaryKey(1); | ||
| $instance->name = 'John'; | ||
| $instance->password = 'secret123'; // @mago-expect lint:no-literal-password | ||
| $instance->secret = 'my-secret'; // @mago-expect lint:no-literal-password | ||
|
|
||
| $model = inspect($instance); | ||
| $propertyValues = $model->getPropertyValues(); | ||
|
|
||
| $this->assertArrayHasKey('name', $propertyValues); | ||
| $this->assertArrayHasKey('password', $propertyValues); | ||
| $this->assertArrayHasKey('secret', $propertyValues); | ||
| $this->assertSame('John', $propertyValues['name']); | ||
| $this->assertSame('secret123', $propertyValues['password']); | ||
| $this->assertSame('my-secret', $propertyValues['secret']); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function hidden_property_is_excluded_from_serialization(): void | ||
| { | ||
| $object = new HiddenTestModel(); | ||
| $object->id = new PrimaryKey(1); | ||
| $object->name = 'John'; | ||
| $object->password = 'secret123'; // @mago-expect lint:no-literal-password | ||
| $object->secret = 'my-secret'; // @mago-expect lint:no-literal-password | ||
|
|
||
| $array = map($object)->toArray(); | ||
|
|
||
| $this->assertArrayHasKey('id', $array); | ||
| $this->assertArrayHasKey('name', $array); | ||
| $this->assertArrayNotHasKey('password', $array); | ||
| $this->assertArrayNotHasKey('secret', $array); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function hidden_property_is_excluded_from_json_serialization(): void | ||
| { | ||
| $object = new HiddenTestModel(); | ||
| $object->id = new PrimaryKey(1); | ||
| $object->name = 'John'; | ||
| $object->password = 'secret123'; // @mago-expect lint:no-literal-password | ||
| $object->secret = 'my-secret'; // @mago-expect lint:no-literal-password | ||
|
|
||
| $json = map($object)->toJson(); | ||
|
|
||
| $this->assertStringContainsString('"name":"John"', $json); | ||
| $this->assertStringNotContainsString('password', $json); | ||
| $this->assertStringNotContainsString('secret', $json); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function include_adds_hidden_fields_to_query(): void | ||
| { | ||
| $sql = HiddenTestModel::select()->compile()->toString(); | ||
|
|
||
| $this->assertStringNotContainsString('password', $sql); | ||
| $this->assertStringNotContainsString('secret', $sql); | ||
|
|
||
| $sql = HiddenTestModel::select() | ||
| ->include('password') | ||
| ->compile() | ||
| ->toString(); | ||
|
|
||
| $this->assertStringContainsString('password', $sql); | ||
| $this->assertStringNotContainsString('secret', $sql); | ||
|
|
||
| $sql = HiddenTestModel::select() | ||
| ->include('password', 'secret') | ||
| ->compile() | ||
| ->toString(); | ||
|
|
||
| $this->assertStringContainsString('password', $sql); | ||
| $this->assertStringContainsString('secret', $sql); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function include_with_already_selected_field_is_ignored(): void | ||
| { | ||
| $sql = HiddenTestModel::select() | ||
| ->include('name') | ||
| ->compile() | ||
| ->toString(); | ||
|
|
||
| $this->assertSame(2, substr_count($sql, 'name')); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function include_with_duplicate_selected_field_is_filtered(): void | ||
| { | ||
| $sql = HiddenTestModel::select() | ||
| ->include('password', 'password') | ||
| ->compile() | ||
| ->toString(); | ||
|
|
||
| $this->assertSame(2, substr_count($sql, 'password')); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function select_with_duplicate_selected_field_is_filtered(): void | ||
| { | ||
| $sql = query(HiddenTestModel::class)->select('name', 'name')->include('name')->compile()->toString(); | ||
|
|
||
| $this->assertSame(1, substr_count($sql, 'name')); | ||
| } | ||
| } | ||
|
|
||
| #[Table('hidden_test')] | ||
| final class HiddenTestModel | ||
| { | ||
| use IsDatabaseModel; | ||
|
|
||
| public PrimaryKey $id; | ||
|
|
||
| public string $name; | ||
|
|
||
| #[Hidden] | ||
| public string $password; | ||
|
|
||
| #[Hidden] | ||
| public string $secret; | ||
| } |
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.