Implement superadmin audit log for mutations
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Filament\Clusters\DailyOps\DailyOpsCluster;
|
||||
use App\Filament\Resources\PhotoResource\Pages;
|
||||
use App\Models\Event;
|
||||
use App\Models\Photo;
|
||||
use App\Services\Audit\SuperAdminAuditLogger;
|
||||
use BackedEnum;
|
||||
use Filament\Actions;
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
@@ -16,6 +17,7 @@ use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use UnitEnum;
|
||||
|
||||
class PhotoResource extends Resource
|
||||
@@ -78,29 +80,95 @@ class PhotoResource extends Resource
|
||||
])
|
||||
->filters([])
|
||||
->actions([
|
||||
Actions\EditAction::make(),
|
||||
Actions\EditAction::make()
|
||||
->after(fn (array $data, Photo $record) => app(SuperAdminAuditLogger::class)->recordModelMutation(
|
||||
'updated',
|
||||
$record,
|
||||
SuperAdminAuditLogger::fieldsMetadata($data),
|
||||
static::class
|
||||
)),
|
||||
Actions\Action::make('feature')
|
||||
->label(__('admin.photos.actions.feature'))
|
||||
->visible(fn (Photo $record) => ! $record->is_featured)
|
||||
->action(fn (Photo $record) => $record->update(['is_featured' => true]))
|
||||
->action(function (Photo $record): void {
|
||||
$record->update(['is_featured' => true]);
|
||||
|
||||
app(SuperAdminAuditLogger::class)->record(
|
||||
'photo.featured',
|
||||
$record,
|
||||
SuperAdminAuditLogger::fieldsMetadata(['is_featured']),
|
||||
source: static::class
|
||||
);
|
||||
})
|
||||
->icon('heroicon-o-star'),
|
||||
Actions\Action::make('unfeature')
|
||||
->label(__('admin.photos.actions.unfeature'))
|
||||
->visible(fn (Photo $record) => $record->is_featured)
|
||||
->action(fn (Photo $record) => $record->update(['is_featured' => false]))
|
||||
->action(function (Photo $record): void {
|
||||
$record->update(['is_featured' => false]);
|
||||
|
||||
app(SuperAdminAuditLogger::class)->record(
|
||||
'photo.unfeatured',
|
||||
$record,
|
||||
SuperAdminAuditLogger::fieldsMetadata(['is_featured']),
|
||||
source: static::class
|
||||
);
|
||||
})
|
||||
->icon('heroicon-o-star'),
|
||||
Actions\DeleteAction::make(),
|
||||
Actions\DeleteAction::make()
|
||||
->after(fn (Photo $record) => app(SuperAdminAuditLogger::class)->recordModelMutation(
|
||||
'deleted',
|
||||
$record,
|
||||
source: static::class
|
||||
)),
|
||||
])
|
||||
->bulkActions([
|
||||
Actions\BulkAction::make('feature')
|
||||
->label(__('admin.photos.actions.feature_selected'))
|
||||
->icon('heroicon-o-star')
|
||||
->action(fn ($records) => $records->each->update(['is_featured' => true])),
|
||||
->action(function ($records): void {
|
||||
$records->each->update(['is_featured' => true]);
|
||||
|
||||
$logger = app(SuperAdminAuditLogger::class);
|
||||
|
||||
foreach ($records as $record) {
|
||||
$logger->record(
|
||||
'photo.featured',
|
||||
$record,
|
||||
SuperAdminAuditLogger::fieldsMetadata(['is_featured']),
|
||||
source: static::class
|
||||
);
|
||||
}
|
||||
}),
|
||||
Actions\BulkAction::make('unfeature')
|
||||
->label(__('admin.photos.actions.unfeature_selected'))
|
||||
->icon('heroicon-o-star')
|
||||
->action(fn ($records) => $records->each->update(['is_featured' => false])),
|
||||
Actions\DeleteBulkAction::make(),
|
||||
->action(function ($records): void {
|
||||
$records->each->update(['is_featured' => false]);
|
||||
|
||||
$logger = app(SuperAdminAuditLogger::class);
|
||||
|
||||
foreach ($records as $record) {
|
||||
$logger->record(
|
||||
'photo.unfeatured',
|
||||
$record,
|
||||
SuperAdminAuditLogger::fieldsMetadata(['is_featured']),
|
||||
source: static::class
|
||||
);
|
||||
}
|
||||
}),
|
||||
Actions\DeleteBulkAction::make()
|
||||
->after(function (Collection $records): void {
|
||||
$logger = app(SuperAdminAuditLogger::class);
|
||||
|
||||
foreach ($records as $record) {
|
||||
$logger->recordModelMutation(
|
||||
'deleted',
|
||||
$record,
|
||||
source: static::class
|
||||
);
|
||||
}
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user