Implement superadmin audit log for mutations
This commit is contained in:
@@ -3,11 +3,12 @@
|
||||
namespace App\Filament\Clusters\DailyOps\Resources\Photos\Pages;
|
||||
|
||||
use App\Filament\Clusters\DailyOps\Resources\Photos\PhotoResource;
|
||||
use App\Filament\Resources\Pages\AuditedEditRecord;
|
||||
use App\Services\Audit\SuperAdminAuditLogger;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditPhoto extends EditRecord
|
||||
class EditPhoto extends AuditedEditRecord
|
||||
{
|
||||
protected static string $resource = PhotoResource::class;
|
||||
|
||||
@@ -15,7 +16,12 @@ class EditPhoto extends EditRecord
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
DeleteAction::make(),
|
||||
DeleteAction::make()
|
||||
->after(fn ($record) => app(SuperAdminAuditLogger::class)->recordModelMutation(
|
||||
'deleted',
|
||||
$record,
|
||||
source: static::class
|
||||
)),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Filament\Clusters\DailyOps\Resources\Photos\Tables;
|
||||
use App\Models\Event;
|
||||
use App\Models\Photo;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Audit\SuperAdminAuditLogger;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkAction;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
@@ -208,6 +209,18 @@ class PhotosTable
|
||||
'moderated_at' => now(),
|
||||
'moderated_by' => Filament::auth()->id(),
|
||||
]);
|
||||
|
||||
app(SuperAdminAuditLogger::class)->record(
|
||||
'photo.'.$status,
|
||||
$record,
|
||||
SuperAdminAuditLogger::fieldsMetadata([
|
||||
'status',
|
||||
'moderation_notes',
|
||||
'moderated_at',
|
||||
'moderated_by',
|
||||
]),
|
||||
source: self::class
|
||||
);
|
||||
}
|
||||
|
||||
private static function applyModerationToRecords(Collection $records, string $status, ?string $notes): int
|
||||
@@ -215,7 +228,7 @@ class PhotosTable
|
||||
$moderatedAt = now();
|
||||
$moderatedBy = Filament::auth()->id();
|
||||
|
||||
return Photo::query()
|
||||
$updated = Photo::query()
|
||||
->whereIn('id', $records->pluck('id'))
|
||||
->where('status', 'pending')
|
||||
->update([
|
||||
@@ -224,6 +237,24 @@ class PhotosTable
|
||||
'moderated_at' => $moderatedAt,
|
||||
'moderated_by' => $moderatedBy,
|
||||
]);
|
||||
|
||||
$logger = app(SuperAdminAuditLogger::class);
|
||||
|
||||
foreach ($records as $record) {
|
||||
$logger->record(
|
||||
'photo.'.$status,
|
||||
$record,
|
||||
SuperAdminAuditLogger::fieldsMetadata([
|
||||
'status',
|
||||
'moderation_notes',
|
||||
'moderated_at',
|
||||
'moderated_by',
|
||||
]),
|
||||
source: self::class
|
||||
);
|
||||
}
|
||||
|
||||
return $updated;
|
||||
}
|
||||
|
||||
private static function statusLabels(): array
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Clusters\RareAdmin\Resources\SuperAdminActionLogs\Pages;
|
||||
|
||||
use App\Filament\Clusters\RareAdmin\Resources\SuperAdminActionLogs\SuperAdminActionLogResource;
|
||||
use Filament\Resources\Pages\ManageRecords;
|
||||
|
||||
class ManageSuperAdminActionLogs extends ManageRecords
|
||||
{
|
||||
protected static string $resource = SuperAdminActionLogResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Clusters\RareAdmin\Resources\SuperAdminActionLogs;
|
||||
|
||||
use App\Filament\Clusters\RareAdmin\RareAdminCluster;
|
||||
use App\Filament\Clusters\RareAdmin\Resources\SuperAdminActionLogs\Pages\ManageSuperAdminActionLogs;
|
||||
use App\Models\SuperAdminActionLog;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class SuperAdminActionLogResource extends Resource
|
||||
{
|
||||
protected static ?string $model = SuperAdminActionLog::class;
|
||||
|
||||
protected static ?string $cluster = RareAdminCluster::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedClipboardDocumentList;
|
||||
|
||||
protected static ?int $navigationSort = 95;
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'action';
|
||||
|
||||
public static function getNavigationGroup(): UnitEnum|string|null
|
||||
{
|
||||
return __('admin.nav.infrastructure');
|
||||
}
|
||||
|
||||
public static function getNavigationLabel(): string
|
||||
{
|
||||
return 'Audit log';
|
||||
}
|
||||
|
||||
public static function canCreate(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function canEdit($record): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function canDelete($record): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function canDeleteAny(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
//
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('action')
|
||||
->columns([
|
||||
TextColumn::make('action')
|
||||
->badge()
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('actor.fullName')
|
||||
->label('Actor')
|
||||
->sortable()
|
||||
->searchable(),
|
||||
TextColumn::make('subject_type')
|
||||
->label('Subject')
|
||||
->formatStateUsing(fn (?string $state) => $state ? class_basename($state) : '—')
|
||||
->searchable()
|
||||
->toggleable(),
|
||||
TextColumn::make('subject_id')
|
||||
->label('Subject ID')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
TextColumn::make('metadata')
|
||||
->label('Fields')
|
||||
->formatStateUsing(function ($state): string {
|
||||
if (! is_array($state)) {
|
||||
return '—';
|
||||
}
|
||||
|
||||
$fields = $state['fields'] ?? [];
|
||||
|
||||
return $fields ? implode(', ', $fields) : '—';
|
||||
})
|
||||
->toggleable(isToggledHiddenByDefault: true)
|
||||
->wrap(),
|
||||
TextColumn::make('source')
|
||||
->label('Source')
|
||||
->limit(40)
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('occurred_at')
|
||||
->label('Timestamp')
|
||||
->dateTime()
|
||||
->sortable(),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->recordActions([])
|
||||
->toolbarActions([]);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ManageSuperAdminActionLogs::route('/'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user