Skip to content

Commit

Permalink
chore: add ability to exclude user email
Browse files Browse the repository at this point in the history
this can be used for PII compliance.
i have chosen to keep the existing functionality as it is for compatibility reasons.
  • Loading branch information
iSWORD authored Oct 11, 2024
1 parent c328c8f commit 190d3e5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ return [
'excludePathSegments' => [],
'useRelativePath' => false,
],
Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class,
Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class => ['includeEmail' => true],
// Spatie\SqlCommenter\Commenters\FrameworkVersionCommenter::class,
// Spatie\SqlCommenter\Commenters\DbDriverCommenter::class,
],
Expand Down
2 changes: 1 addition & 1 deletion config/sql-commenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'excludePathSegments' => [],
'useRelativePath' => false,
],
Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class,
Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class => ['includeEmail' => true],
// Spatie\SqlCommenter\Commenters\FrameworkVersionCommenter::class,
// Spatie\SqlCommenter\Commenters\DbDriverCommenter::class,
],
Expand Down
6 changes: 5 additions & 1 deletion src/Commenters/CurrentUserCommenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

class CurrentUserCommenter implements Commenter
{
public function __construct(protected bool $includeEmail = false)
{
}

public function comments(string $query, Connection $connection): Comment|array|null
{
SqlCommenter::disable();
Expand All @@ -23,7 +27,7 @@ public function comments(string $query, Connection $connection): Comment|array|n

return [
Comment::make('user_id', $user->getKey()),
Comment::make('user_email', $user->email ?? ''),
$this->includeEmail ? Comment::make('user_email', $user->email ?? '') : null,
];
}
}
21 changes: 21 additions & 0 deletions tests/Commenters/CurrentUserCommenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\Event;
use Spatie\SqlCommenter\Commenters\CurrentUserCommenter;
use Spatie\SqlCommenter\Tests\TestSupport\TestClasses\User;

it('logs the current user', function () {
Expand All @@ -20,3 +21,23 @@

User::all();
});


it('can exclude the user email', function () {
$user = User::create([
'name' => 'John',
'password' => 'dummy',
'email' => '[email protected]',
]);

auth()->login($user);

$this->addCommenterToConfig(CurrentUserCommenter::class, ['includeEmail' => false]);

Event::listen(QueryExecuted::class, function (QueryExecuted $event) use ($user) {
expect($event->sql)->toContainComment('user_id', $user->id)
->and($event->sql)->notToContainComment('user_email', $user->email);
});

User::all();
});
6 changes: 6 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@

return $this;
});

expect()->extend('notToContainComment', function (string $key, string|int|null $value) {
expect($this->value)->not->toContain((string)Comment::make($key, $value));

return $this;
});

0 comments on commit 190d3e5

Please sign in to comment.