-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
732 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
33 changes: 33 additions & 0 deletions
33
app/Extensions/Auditing/Resolvers/CustomIpAddressResolver.php
This file contains 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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Extensions\Auditing\Resolvers; | ||
|
||
use Illuminate\Support\Facades\Request; | ||
use OwenIt\Auditing\Contracts\Auditable; | ||
use OwenIt\Auditing\Contracts\Resolver; | ||
|
||
class CustomIpAddressResolver implements Resolver | ||
{ | ||
private static function anonymizeIpAddress(string $ipAddress): string | ||
{ | ||
/** @source https://stackoverflow.com/a/48777412 */ | ||
return preg_replace( | ||
['/\.\d*$/', '/[\da-f]*:[\da-f]*$/'], | ||
['.0', '0:0'], | ||
$ipAddress | ||
); | ||
} | ||
|
||
public static function resolve(Auditable $auditable): string | ||
{ | ||
$ip = $auditable->preloadedResolverData['ip_address'] ?? Request::ip(); | ||
|
||
if ($ip !== null) { | ||
$ip = self::anonymizeIpAddress($ip); | ||
} | ||
|
||
return $ip; | ||
} | ||
} |
This file contains 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,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Filament\Resources\AuditResource\Pages; | ||
use App\Models\Audit; | ||
use Filament\Forms; | ||
use Filament\Forms\Form; | ||
use Filament\Resources\Resource; | ||
use Filament\Tables; | ||
use Filament\Tables\Columns\IconColumn; | ||
use Filament\Tables\Table; | ||
use Illuminate\Support\Str; | ||
use Novadaemon\FilamentPrettyJson\PrettyJson; | ||
|
||
class AuditResource extends Resource | ||
{ | ||
protected static ?string $model = Audit::class; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-archive-box'; | ||
|
||
protected static ?string $navigationGroup = 'System'; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
Forms\Components\TextInput::make('user_type') | ||
->maxLength(255), | ||
Forms\Components\TextInput::make('user_id'), | ||
Forms\Components\TextInput::make('event') | ||
->required() | ||
->maxLength(255), | ||
Forms\Components\TextInput::make('auditable_type') | ||
->required() | ||
->maxLength(255), | ||
Forms\Components\TextInput::make('auditable_id') | ||
->required(), | ||
PrettyJson::make('old_values'), | ||
PrettyJson::make('new_values'), | ||
Forms\Components\Textarea::make('url'), | ||
Forms\Components\TextInput::make('ip_address'), | ||
Forms\Components\TextInput::make('user_agent') | ||
->maxLength(1023), | ||
Forms\Components\TextInput::make('tags') | ||
->maxLength(255), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
Tables\Columns\TextColumn::make('user.name'), | ||
Tables\Columns\TextColumn::make('event'), | ||
Tables\Columns\TextColumn::make('auditable_type'), | ||
Tables\Columns\TextColumn::make('auditable_id'), | ||
IconColumn::make('was_command') | ||
->getStateUsing(fn (Audit $record) => Str::startsWith($record->url, 'artisan ')) | ||
->boolean(), | ||
Tables\Columns\TextColumn::make('created_at') | ||
->sortable() | ||
->dateTime(), | ||
Tables\Columns\TextColumn::make('updated_at') | ||
->sortable() | ||
->dateTime(), | ||
]) | ||
->filters([ | ||
// | ||
]) | ||
->actions([ | ||
Tables\Actions\ViewAction::make(), | ||
]) | ||
->bulkActions([ | ||
]) | ||
->defaultSort('created_at', 'desc'); | ||
} | ||
|
||
public static function getRelations(): array | ||
{ | ||
return [ | ||
]; | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListAudits::route('/'), | ||
'create' => Pages\CreateAudit::route('/create'), | ||
'view' => Pages\ViewAudit::route('/{record}'), | ||
]; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/Filament/Resources/AuditResource/Pages/CreateAudit.php
This file contains 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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\AuditResource\Pages; | ||
|
||
use App\Filament\Resources\AuditResource; | ||
use Filament\Resources\Pages\CreateRecord; | ||
|
||
class CreateAudit extends CreateRecord | ||
{ | ||
protected static string $resource = AuditResource::class; | ||
} |
This file contains 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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\AuditResource\Pages; | ||
|
||
use App\Filament\Resources\AuditResource; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListAudits extends ListRecords | ||
{ | ||
protected static string $resource = AuditResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains 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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\AuditResource\Pages; | ||
|
||
use App\Filament\Resources\AuditResource; | ||
use Filament\Resources\Pages\ViewRecord; | ||
|
||
class ViewAudit extends ViewRecord | ||
{ | ||
protected static string $resource = AuditResource::class; | ||
} |
This file contains 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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Database\Factories\AuditFactory; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Support\Carbon; | ||
use OwenIt\Auditing\Models\Audit as PackageAuditModel; | ||
|
||
/** | ||
* @property int $id | ||
* @property string|null $user_type | ||
* @property string|null $user_id | ||
* @property string $event | ||
* @property string $auditable_type | ||
* @property string $auditable_id | ||
* @property array|null $old_values | ||
* @property array|null $new_values | ||
* @property string|null $url | ||
* @property string|null $ip_address | ||
* @property string|null $user_agent | ||
* @property string|null $tags | ||
* @property Carbon|null $created_at | ||
* @property Carbon|null $updated_at | ||
* | ||
* @method static AuditFactory factory() | ||
*/ | ||
class Audit extends PackageAuditModel | ||
{ | ||
use HasFactory; | ||
} |
This file contains 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 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 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 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 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 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 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 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 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
Oops, something went wrong.