-
-
Notifications
You must be signed in to change notification settings - Fork 267
Add admin activity logging for CRUD operations #2189
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
Draft
lancepioch
wants to merge
3
commits into
main
Choose a base branch
from
lance/1585
base: main
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.
+298
−0
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| namespace App\Listeners; | ||
|
|
||
| use App\Facades\Activity; | ||
| use Filament\Facades\Filament; | ||
| use Filament\Resources\Events\RecordCreated; | ||
| use Filament\Resources\Events\RecordUpdated; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| class AdminActivityListener | ||
| { | ||
| protected const REDACTED_FIELDS = [ | ||
| 'password', | ||
| 'password_confirmation', | ||
| 'current_password', | ||
| 'token', | ||
| 'secret', | ||
| 'api_key', | ||
| 'daemon_token', | ||
| '_token', | ||
| ]; | ||
|
|
||
| public function handle(RecordCreated|RecordUpdated $event): void | ||
| { | ||
| if (Filament::getCurrentPanel()?->getId() !== 'admin') { | ||
| return; | ||
| } | ||
|
|
||
| $record = $event->getRecord(); | ||
| $page = $event->getPage(); | ||
| $data = $event->getData(); | ||
|
|
||
| $resourceClass = $page::getResource(); | ||
| $modelClass = $resourceClass::getModel(); | ||
| $slug = Str::kebab(class_basename($modelClass)); | ||
|
|
||
| $action = $event instanceof RecordCreated ? 'create' : 'update'; | ||
|
|
||
| $properties = $this->redactSensitiveFields($data); | ||
|
|
||
| Activity::event("admin:$slug.$action") | ||
| ->subject($record) | ||
| ->property($properties) | ||
| ->log(); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $data | ||
| * @return array<string, mixed> | ||
| */ | ||
| protected function redactSensitiveFields(array $data): array | ||
| { | ||
| $redacted = []; | ||
|
|
||
| foreach ($data as $key => $value) { | ||
| if (in_array($key, self::REDACTED_FIELDS, true)) { | ||
| $redacted[$key] = '[REDACTED]'; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (is_array($value)) { | ||
| $redacted[$key] = $this->redactSensitiveFields($value); | ||
| } else { | ||
| $redacted[$key] = $value; | ||
| } | ||
| } | ||
|
|
||
| return $redacted; | ||
| } | ||
| } |
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,158 @@ | ||
| <?php | ||
|
|
||
| use App\Events\ActivityLogged; | ||
| use App\Filament\Admin\Resources\Eggs\Pages\CreateEgg; | ||
| use App\Filament\Admin\Resources\Eggs\Pages\EditEgg; | ||
| use App\Filament\Admin\Resources\Nodes\Pages\CreateNode; | ||
| use App\Filament\Admin\Resources\Nodes\Pages\EditNode; | ||
| use App\Listeners\AdminActivityListener; | ||
| use App\Models\Egg; | ||
| use App\Models\Node; | ||
| use App\Models\Role; | ||
| use Filament\Facades\Filament; | ||
| use Filament\Resources\Events\RecordCreated; | ||
| use Filament\Resources\Events\RecordUpdated; | ||
| use Illuminate\Support\Facades\Event; | ||
|
|
||
| function pageInstance(string $class): object | ||
| { | ||
| return (new ReflectionClass($class))->newInstanceWithoutConstructor(); | ||
| } | ||
|
|
||
| function createEvent(object $record, array $data, object $page): RecordCreated | ||
| { | ||
| return new RecordCreated($record, $data, $page); | ||
| } | ||
|
|
||
| function updateEvent(object $record, array $data, object $page): RecordUpdated | ||
| { | ||
| return new RecordUpdated($record, $data, $page); | ||
| } | ||
|
|
||
| beforeEach(function () { | ||
| [$this->admin] = generateTestAccount([]); | ||
| $this->admin = $this->admin->syncRoles(Role::getRootAdmin()); | ||
| $this->actingAs($this->admin); | ||
|
|
||
| Filament::setCurrentPanel('admin'); | ||
| }); | ||
|
|
||
| it('logs create activity for an egg', function () { | ||
| $egg = Egg::first(); | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(createEvent($egg, ['name' => 'Test Egg'], pageInstance(CreateEgg::class))); | ||
|
|
||
| $this->assertActivityLogged('admin:egg.create'); | ||
| }); | ||
|
|
||
| it('logs update activity for an egg', function () { | ||
| $egg = Egg::first(); | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(updateEvent($egg, ['name' => 'Updated Egg'], pageInstance(EditEgg::class))); | ||
|
|
||
| $this->assertActivityLogged('admin:egg.update'); | ||
| }); | ||
|
|
||
| it('logs create activity for a node', function () { | ||
| $node = Node::first(); | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(createEvent($node, ['name' => 'Test Node'], pageInstance(CreateNode::class))); | ||
|
|
||
| $this->assertActivityLogged('admin:node.create'); | ||
| }); | ||
|
|
||
| it('logs update activity for a node', function () { | ||
| $node = Node::first(); | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(updateEvent($node, ['name' => 'Updated Node'], pageInstance(EditNode::class))); | ||
|
|
||
| $this->assertActivityLogged('admin:node.update'); | ||
| }); | ||
|
|
||
| it('does not log activity for non-admin panels', function () { | ||
| Filament::setCurrentPanel('app'); | ||
|
|
||
| $egg = Egg::first(); | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(createEvent($egg, ['name' => 'Test'], pageInstance(CreateEgg::class))); | ||
|
|
||
| Event::assertNotDispatched(ActivityLogged::class); | ||
| }); | ||
|
|
||
| it('sets the record as the activity subject', function () { | ||
| $egg = Egg::first(); | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(createEvent($egg, ['name' => 'Test'], pageInstance(CreateEgg::class))); | ||
|
|
||
| $this->assertActivityFor('admin:egg.create', $this->admin, $egg); | ||
| }); | ||
|
|
||
| it('redacts sensitive fields from activity properties', function () { | ||
| $egg = Egg::first(); | ||
|
|
||
| $data = [ | ||
| 'name' => 'Visible', | ||
| 'password' => 'should-be-redacted', | ||
| 'password_confirmation' => 'should-be-redacted', | ||
| 'token' => 'should-be-redacted', | ||
| 'secret' => 'should-be-redacted', | ||
| 'api_key' => 'should-be-redacted', | ||
| ]; | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(updateEvent($egg, $data, pageInstance(EditEgg::class))); | ||
|
|
||
| Event::assertDispatched(ActivityLogged::class, function (ActivityLogged $event) { | ||
| $properties = $event->model->properties; | ||
|
|
||
| expect($properties)->toHaveKey('name', 'Visible') | ||
| ->toHaveKey('password', '[REDACTED]') | ||
| ->toHaveKey('password_confirmation', '[REDACTED]') | ||
| ->toHaveKey('token', '[REDACTED]') | ||
| ->toHaveKey('secret', '[REDACTED]') | ||
| ->toHaveKey('api_key', '[REDACTED]'); | ||
|
|
||
| return true; | ||
| }); | ||
| }); | ||
|
|
||
| it('redacts sensitive fields in nested arrays', function () { | ||
| $egg = Egg::first(); | ||
|
|
||
| $data = [ | ||
| 'name' => 'Visible', | ||
| 'nested' => [ | ||
| 'safe' => 'value', | ||
| 'password' => 'should-be-redacted', | ||
| 'token' => 'should-be-redacted', | ||
| ], | ||
| ]; | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(updateEvent($egg, $data, pageInstance(EditEgg::class))); | ||
|
|
||
| Event::assertDispatched(ActivityLogged::class, function (ActivityLogged $event) { | ||
| $properties = $event->model->properties; | ||
|
|
||
| expect($properties['nested'])->toHaveKey('safe', 'value') | ||
| ->toHaveKey('password', '[REDACTED]') | ||
| ->toHaveKey('token', '[REDACTED]'); | ||
|
|
||
| return true; | ||
| }); | ||
| }); | ||
|
|
||
| it('generates kebab-case event names from model class names', function () { | ||
| $node = Node::first(); | ||
|
|
||
| $listener = new AdminActivityListener(); | ||
| $listener->handle(createEvent($node, ['name' => 'Test'], pageInstance(CreateNode::class))); | ||
|
|
||
| $this->assertActivityLogged('admin:node.create'); | ||
| }); |
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.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: pelican-dev/panel
Length of output: 1470
🏁 Script executed:
Repository: pelican-dev/panel
Length of output: 167
🏁 Script executed:
Repository: pelican-dev/panel
Length of output: 141
🌐 Web query:
Filament RecordCreated RecordUpdated event listener signature parameters💡 Result:
Filament doesn’t ship Laravel events named
RecordCreated/RecordUpdatedfor resource CRUD. Instead, you typically hook into:1) Filament resource page hooks (recommended)
Create page (
CreateRecord)After a record is created, you can use the page’s
$recordproperty (the created Eloquent model) and$data(form state). (filamentphp.com)Edit page (
EditRecord)Updates happen via
handleRecordUpdate(Model $record, array $data)(and you can use$this->recordas well). (filamentphp.com)2) Laravel / Eloquent model events (framework-level)
If you want true “created/updated” listeners, use Eloquent events/observers (
created(Model $model),updated(Model $model), etc.). (laravel.com)If you meant a third-party Filament plugin that defines
RecordCreated/RecordUpdated, tell me the package name/namespace and Filament major version (v2/v3/v4), and I’ll pull the exact event class + listener method signature.Citations:
Remove or implement RecordCreated and RecordUpdated events—they do not exist in Filament core or this codebase.
The listener registrations on lines 21-22 reference
RecordCreatedandRecordUpdatedevents that are not defined anywhere in the codebase or Filament's core library. These events are never dispatched, so theAdminActivityListenerwill never be triggered. Either:created,updated) with an observer/listener, orafterCreate(),afterSave()) directly on resource pages.Without dispatching these events, admin activity logging will not function.
🤖 Prompt for AI Agents