feat: swap over to spatie logger

This commit is contained in:
Nicholas Ciechanowski 2023-10-12 22:51:26 +11:00
parent d3864a69f1
commit 7d94630fd6
22 changed files with 485 additions and 269 deletions

View File

@ -1,22 +0,0 @@
<?php
namespace App\Enums;
use App\Traits\EnumOptions;
use App\Traits\EnumValues;
enum LogAction: string
{
use EnumValues;
use EnumOptions;
case CREATE = 'create';
case DELETE = 'delete';
case REQUEST = 'request';
case REJECT = 'reject';
case APPROVE = 'approve';
case SEND = 'send';
case UPDATE = 'update';
case ACCESS = 'access';
}

View File

@ -2,9 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Enums\LogAction;
use App\Http\Controllers\Controller;
use App\Models\Log;
use App\Models\Quote;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
@ -26,14 +24,9 @@ class WebHookController extends Controller
return;
}
Log::create([
'user_id' => auth()?->user()?->id ?? 1,
'loggable_type' => Quote::class,
'loggable_id' => null,
'action' => LogAction::SEND,
'content' => $quote,
'ip' => request()->ip(),
]);
activity()
->event('send')
->log("Manually sent quote: $quote");
$this->webHookSend($quote);
}
@ -42,14 +35,10 @@ class WebHookController extends Controller
{
$quote = Quote::inRandomOrder()->first();
Log::create([
'user_id' => auth()?->user()?->id ?? 1,
'loggable_type' => Quote::class,
'loggable_id' => $quote->id,
'action' => LogAction::REQUEST,
'content' => 'Random quote requested',
'ip' => request()->ip(),
]);
activity()
->performedOn($quote)
->event('send')
->log("Requested quote: $quote->quote");
$this->webHookSend($quote->quote);
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
/**
* @mixin IdeHelperLog
*/
class Log extends Model
{
protected $fillable = [
'user_id',
'loggable_type',
'loggable_id',
'action',
'content',
'ip',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Get the parent loggable model (user, quote or requested quote).
*/
public function loggable(): MorphTo
{
return $this->morphTo();
}
}

View File

@ -2,67 +2,37 @@
namespace App\Models;
use App\Enums\LogAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
/**
* @mixin IdeHelperQuote
*/
class Quote extends Model
{
use SoftDeletes;
public function request(): void
{
// Send the quote
// If success, add it to the transactions
Log::create([
'user_id' => auth()?->user()?->id,
'loggable_type' => self::class,
'loggable_id' => $this->id,
'action' => LogAction::REQUEST,
'content' => $this->quote,
'ip' => request()->ip(),
]);
}
use LogsActivity, SoftDeletes;
protected $fillable = [
'user_id',
'quote',
];
public function logs(): MorphOne
public function request(): void
{
return $this->morphOne(Log::class, 'loggable');
// Send the quote
// If success, add it to the transactions
activity()
->performedOn($this)
->event('send')
->log("Requested quote: $this->quote");
}
public static function boot(): void
public function getActivityLogOptions(): LogOptions
{
parent::boot();
self::created(function (Quote $model) {
Log::create([
'user_id' => auth()?->user()?->id,
'loggable_type' => self::class,
'loggable_id' => $model->id,
'action' => LogAction::CREATE,
'content' => $model->quote,
'ip' => request()->ip(),
]);
});
self::deleted(function (Quote $model) {
Log::create([
'user_id' => auth()?->user()?->id,
'loggable_type' => self::class,
'loggable_id' => $model->id,
'action' => LogAction::DELETE,
'content' => $model->quote,
'ip' => request()->ip(),
]);
});
return LogOptions::defaults()
->logOnly(['quote']);
}
}

View File

@ -2,56 +2,44 @@
namespace App\Models;
use App\Enums\LogAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Facades\LogBatch;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
/**
* @mixin IdeHelperRequestedQuote
*/
class RequestedQuote extends Model
{
use SoftDeletes;
use LogsActivity, SoftDeletes;
protected $fillable = [
'quote',
];
public function logs(): MorphOne
{
return $this->morphOne(Log::class, 'loggable');
}
public function approve(): void
{
Log::create([
'user_id' => auth()?->user()?->id,
'loggable_type' => self::class,
'loggable_id' => $this->id,
'action' => LogAction::APPROVE,
'content' => $this->quote,
'ip' => request()->ip(),
]);
LogBatch::startBatch();
Quote::create([
'quote' => $this->quote,
]);
$this->delete();
LogBatch::endBatch();
}
public function reject(): void
{
Log::create([
'user_id' => auth()?->user()?->id,
'loggable_type' => self::class,
'loggable_id' => $this->id,
'action' => LogAction::REJECT,
'content' => $this->quote,
'ip' => request()->ip(),
]);
$this->delete();
}
public function getActivityLogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['quote']);
}
}

View File

@ -2,20 +2,18 @@
namespace App\Models;
use App\Enums\LogAction;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
/**
* @mixin IdeHelperUser
*/
class User extends Authenticatable
{
use HasFactory, Notifiable;
use LogsActivity, HasFactory, Notifiable;
/**
* The model's default values for attributes.
@ -64,34 +62,14 @@ class User extends Authenticatable
'password' => 'hashed',
];
public function logAction(): HasMany
{
return $this->hasMany(Log::class);
}
public function logs(): MorphOne
{
return $this->morphOne(Log::class, 'loggable');
}
public function getFullNameAttribute(): string
{
return "$this->firstname $this->lastname";
}
public static function boot(): void
public function getActivityLogOptions(): LogOptions
{
parent::boot();
self::created(function ($model) {
Log::create([
'user_id' => auth()?->user()?->id ?? 1,
'loggable_type' => self::class,
'loggable_id' => $model->id,
'action' => LogAction::CREATE,
'content' => $model->full_name,
'ip' => request()->ip(),
]);
});
return LogOptions::defaults()
->logExcept(['password']);
}
}

View File

@ -3,6 +3,7 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Spatie\Activitylog\Models\Activity;
class AppServiceProvider extends ServiceProvider
{
@ -15,6 +16,7 @@ class AppServiceProvider extends ServiceProvider
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
// This is so we can access alpine without livewire on the page.
\Livewire\Livewire::forceAssetInjection();
}
@ -23,6 +25,8 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot(): void
{
//
Activity::saving(function (Activity $activity) {
$activity->properties = $activity->properties->put('ip', request()->ip());
});
}
}

View File

@ -11,7 +11,8 @@
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8",
"livewire/livewire": "^3.0",
"livewire/volt": "^1.0"
"livewire/volt": "^1.0",
"spatie/laravel-activitylog": "^4.7"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.13",

155
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "feae295eec753591e7cf70bf21f57924",
"content-hash": "da777baec27a8032a972c70cc19d114a",
"packages": [
{
"name": "brick/math",
@ -3251,6 +3251,157 @@
],
"time": "2023-04-15T23:01:58+00:00"
},
{
"name": "spatie/laravel-activitylog",
"version": "4.7.3",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-activitylog.git",
"reference": "ec65a478a909b8df1b4f0c3c45de2592ca7639e5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-activitylog/zipball/ec65a478a909b8df1b4f0c3c45de2592ca7639e5",
"reference": "ec65a478a909b8df1b4f0c3c45de2592ca7639e5",
"shasum": ""
},
"require": {
"illuminate/config": "^8.0 || ^9.0 || ^10.0",
"illuminate/database": "^8.69 || ^9.27 || ^10.0",
"illuminate/support": "^8.0 || ^9.0 || ^10.0",
"php": "^8.0",
"spatie/laravel-package-tools": "^1.6.3"
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "^6.23 || ^7.0 || ^8.0",
"pestphp/pest": "^1.20"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\Activitylog\\ActivitylogServiceProvider"
]
}
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Spatie\\Activitylog\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
},
{
"name": "Sebastian De Deyne",
"email": "sebastian@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
},
{
"name": "Tom Witkowski",
"email": "dev.gummibeer@gmail.com",
"homepage": "https://gummibeer.de",
"role": "Developer"
}
],
"description": "A very simple activity logger to monitor the users of your website or application",
"homepage": "https://github.com/spatie/activitylog",
"keywords": [
"activity",
"laravel",
"log",
"spatie",
"user"
],
"support": {
"issues": "https://github.com/spatie/laravel-activitylog/issues",
"source": "https://github.com/spatie/laravel-activitylog/tree/4.7.3"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
},
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2023-01-25T17:04:51+00:00"
},
{
"name": "spatie/laravel-package-tools",
"version": "1.16.1",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-package-tools.git",
"reference": "cc7c991555a37f9fa6b814aa03af73f88026a83d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/cc7c991555a37f9fa6b814aa03af73f88026a83d",
"reference": "cc7c991555a37f9fa6b814aa03af73f88026a83d",
"shasum": ""
},
"require": {
"illuminate/contracts": "^9.28|^10.0",
"php": "^8.0"
},
"require-dev": {
"mockery/mockery": "^1.5",
"orchestra/testbench": "^7.7|^8.0",
"pestphp/pest": "^1.22",
"phpunit/phpunit": "^9.5.24",
"spatie/pest-plugin-test-time": "^1.1"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\LaravelPackageTools\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"role": "Developer"
}
],
"description": "Tools for creating Laravel packages",
"homepage": "https://github.com/spatie/laravel-package-tools",
"keywords": [
"laravel-package-tools",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/laravel-package-tools/issues",
"source": "https://github.com/spatie/laravel-package-tools/tree/1.16.1"
},
"funding": [
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2023-08-23T09:04:39+00:00"
},
{
"name": "symfony/console",
"version": "v6.3.4",
@ -9758,5 +9909,5 @@
"php": "^8.1"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"
"plugin-api-version": "2.6.0"
}

52
config/activitylog.php Normal file
View File

@ -0,0 +1,52 @@
<?php
return [
/*
* If set to false, no activities will be saved to the database.
*/
'enabled' => env('ACTIVITY_LOGGER_ENABLED', true),
/*
* When the clean-command is executed, all recording activities older than
* the number of days specified here will be deleted.
*/
'delete_records_older_than_days' => 365,
/*
* If no log name is passed to the activity() helper
* we use this default log name.
*/
'default_log_name' => 'default',
/*
* You can specify an auth driver here that gets user models.
* If this is null we'll use the current Laravel auth driver.
*/
'default_auth_driver' => null,
/*
* If set to true, the subject returns soft deleted models.
*/
'subject_returns_soft_deleted_models' => true,
/*
* This model will be used to log activity.
* It should implement the Spatie\Activitylog\Contracts\Activity interface
* and extend Illuminate\Database\Eloquent\Model.
*/
'activity_model' => \Spatie\Activitylog\Models\Activity::class,
/*
* This is the name of the table that will be created by the migration and
* used by the Activity model shipped with this package.
*/
'table_name' => 'activity_log',
/*
* This is the database connection that will be used by the migration and
* the Activity model shipped with this package. In case it's not set
* Laravel's database.default will be used instead.
*/
'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION'),
];

View File

@ -1,30 +0,0 @@
<?php
use App\Models\Quote;
use App\Models\RequestedQuote;
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('logs', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(User::class)->nullable();
$table->string('loggable_type');
$table->integer('loggable_id');
$table->string('action');
$table->text('content');
$table->string('ip');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('logs');
}
};

View File

@ -0,0 +1,27 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->create(config('activitylog.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('log_name')->nullable();
$table->text('description');
$table->nullableMorphs('subject', 'subject');
$table->nullableMorphs('causer', 'causer');
$table->json('properties')->nullable();
$table->timestamps();
$table->index('log_name');
});
}
public function down()
{
Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name'));
}
}

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddEventColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->string('event')->nullable()->after('subject_type');
});
}
public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('event');
});
}
}

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddBatchUuidColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->uuid('batch_uuid')->nullable()->after('properties');
});
}
public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('batch_uuid');
});
}
}

View File

@ -0,0 +1,106 @@
@if ($paginator->hasPages())
<nav role="navigation" aria-label="{{ __('Pagination Navigation') }}" class="flex items-center justify-between">
<div class="flex justify-between flex-1 sm:hidden">
@if ($paginator->onFirstPage())
<span class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 rounded-md">
{!! __('pagination.previous') !!}
</span>
@else
<a wire:navigate href="{{ $paginator->previousPageUrl() }}" class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 rounded-md hover:text-gray-500 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150">
{!! __('pagination.previous') !!}
</a>
@endif
@if ($paginator->hasMorePages())
<a wire:navigate href="{{ $paginator->nextPageUrl() }}" class="relative inline-flex items-center px-4 py-2 ml-3 text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 rounded-md hover:text-gray-500 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150">
{!! __('pagination.next') !!}
</a>
@else
<span class="relative inline-flex items-center px-4 py-2 ml-3 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 rounded-md">
{!! __('pagination.next') !!}
</span>
@endif
</div>
<div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
<div>
<p class="text-sm text-nexi-black dark:text-gray-200 leading-5">
{!! __('Showing') !!}
@if ($paginator->firstItem())
<span class="font-medium">{{ $paginator->firstItem() }}</span>
{!! __('to') !!}
<span class="font-medium">{{ $paginator->lastItem() }}</span>
@else
{{ $paginator->count() }}
@endif
{!! __('of') !!}
<span class="font-medium">{{ $paginator->total() }}</span>
{!! __('results') !!}
</p>
</div>
<div>
<span class="relative z-0 inline-flex shadow-sm rounded-md">
{{-- Previous Page Link --}}
@if ($paginator->onFirstPage())
<span aria-disabled="true" aria-label="{{ __('pagination.previous') }}">
<span class="relative inline-flex items-center px-2 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default rounded-l-md leading-5" aria-hidden="true">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" />
</svg>
</span>
</span>
@else
<a wire:navigate href="{{ $paginator->previousPageUrl() }}" rel="prev" class="relative inline-flex items-center px-2 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 rounded-l-md leading-5 hover:text-gray-400 focus:z-10 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-500 transition ease-in-out duration-150" aria-label="{{ __('pagination.previous') }}">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" />
</svg>
</a>
@endif
{{-- Pagination Elements --}}
@foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
@if (is_string($element))
<span aria-disabled="true">
<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 bg-white border border-gray-300 cursor-default leading-5">{{ $element }}</span>
</span>
@endif
{{-- Array Of Links --}}
@if (is_array($element))
@foreach ($element as $page => $url)
@if ($page == $paginator->currentPage())
<span aria-current="page">
<span class="relative bg-nexi-red inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-white border border-nexi-red cursor-default leading-5">{{ $page }}</span>
</span>
@else
<a wire:navigate href="{{ $url }}" class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-nexi-black bg-gray-50 border border-gray-300 leading-5 hover:text-gray-500 focus:z-10 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150" aria-label="{{ __('Go to page :page', ['page' => $page]) }}">
{{ $page }}
</a>
@endif
@endforeach
@endif
@endforeach
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
<a wire:navigate href="{{ $paginator->nextPageUrl() }}" rel="next" class="relative inline-flex items-center px-2 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 rounded-r-md leading-5 hover:text-gray-400 focus:z-10 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-500 transition ease-in-out duration-150" aria-label="{{ __('pagination.next') }}">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" />
</svg>
</a>
@else
<span aria-disabled="true" aria-label="{{ __('pagination.next') }}">
<span class="relative inline-flex items-center px-2 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default rounded-r-md leading-5" aria-hidden="true">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" />
</svg>
</span>
</span>
@endif
</span>
</div>
</div>
</nav>
@endif

View File

@ -1,4 +1,4 @@
<div class="sm:fixed sm:top-0 flex flex-row-reverse w-full sm:right-0 p-6 text-right z-10 bg-nexi-primary dark:bg-nexi-primary-dark transition-colors duration-300">
<div class="sm:fixed sm:top-0 flex flex-row-reverse w-full border-b border-gray-200 sm:right-0 p-6 text-right z-10 bg-nexi-primary dark:bg-nexi-primary-dark transition-colors duration-300">
@auth
<div class="pr-6 flex">
<a href="{{ url('/dashboard') }}"

View File

@ -15,7 +15,7 @@ new class extends Component
}
}; ?>
<nav x-data="{ openProfile : false }" class="sticky top-0 z-40 lg:mx-auto lg:max-w-7xl lg:px-8 bg-nexi-primary dark:bg-nexi-primary-dark transition-colors duration-300">
<nav x-data="{ openProfile : false }" class="sticky top-0 z-40 lg:mx-auto lg:px-8 bg-nexi-primary dark:bg-nexi-primary-dark transition-colors duration-300">
<div class="flex h-16 items-center gap-x-4 border-b border-gray-200 px-4 shadow-sm sm:gap-x-6 sm:px-6 lg:px-0 lg:shadow-none">
<button type="button" class="-m-2.5 p-2.5 text-gray-700 lg:hidden" @click="sidebarOpen = true">
<svg class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">

View File

@ -1,9 +1,12 @@
<?php
use App\Models\Log;
use App\Models\Quote;
use App\Models\RequestedQuote;
use Livewire\Attributes\Layout;
use Livewire\Volt\Component;
use Livewire\WithPagination;
use Spatie\Activitylog\Models\Activity;
new #[Layout('layouts.app')] class extends Component
{
@ -11,8 +14,8 @@ new #[Layout('layouts.app')] class extends Component
public function with(): array
{
$logs = Log::with(['user', 'loggable'])
->orderByDesc('created_at')
$logs = Activity::with(['causer', 'subject'])
->orderByDesc('id')
->paginate(15);
return [
@ -40,7 +43,7 @@ new #[Layout('layouts.app')] class extends Component
</th>
<th scope="col"
class="px-3 py-3.5 text-left text-sm font-semibold text-nexi-black dark:text-gray-200">
Action
Event
</th>
<th scope="col"
class="px-3 py-3.5 text-left text-sm font-semibold text-nexi-black dark:text-gray-200">
@ -56,22 +59,29 @@ new #[Layout('layouts.app')] class extends Component
@foreach ($logs as $log)
<tr>
<td class="relative py-4 px-4 pr-3 text-sm sm:pl-6 border-t text-nexi-black dark:text-gray-200">
{{ $log?->user?->full_name ?? 'Anonymous' }}
{{ $log?->causer?->full_name ?? 'Anonymous' }}
</td>
<td class="relative py-4 pr-3 text-sm sm:pl-6 border-t text-nexi-black dark:text-gray-200">
{{ str_replace('App\Models\\', '', $log->loggable_type) }}
{{ str_replace('App\Models\\', '', $log->subject_type) }}
</td>
<td class="relative py-4 pr-3 text-sm sm:pl-6 border-t text-nexi-black dark:text-gray-200">
{{ $log->loggable_id }}
{{ $log->subject_id }}
</td>
<td class="relative py-4 pr-3 text-sm sm:pl-6 border-t text-nexi-black dark:text-gray-200">
{{ $log->action }}
{{ \Illuminate\Support\Str::headline($log->event) }}
</td>
<td class="relative py-4 pr-3 text-sm sm:pl-6 border-t text-nexi-black dark:text-gray-200">
{{ $log->content }}
@switch($log->subject_type)
@case(Quote::class)
@case(RequestedQuote::class)
{{ $log?->subject?->quote }}
@break
@default
{{ $log->description }}
@endswitch
</td>
<td class="relative py-4 pr-3 text-sm sm:pl-6 border-t text-nexi-black dark:text-gray-200">
{{ $log->ip }}
{{ $log->getExtraProperty('ip') }}
</td>
</tr>
@endforeach
@ -79,7 +89,7 @@ new #[Layout('layouts.app')] class extends Component
</table>
</div>
<div class="mt-4">
{{ $logs->links() }}
{{ $logs->links('components.paginator') }}
</div>
</div>

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\LogAction;
use App\Models\Log;
use Illuminate\Support\Facades\Http;
use Livewire\Attributes\Layout;
@ -19,14 +18,9 @@ new #[Layout('layouts.app')] class extends Component
// TODO: move this to a helper class so we can reuse code between API and FE
$response = Http::post(config('bot.webhook'), ['text' => $validated['quote']]);
Log::create([
'user_id' => auth()->user()->id,
'loggable_type' => Log::class,
'loggable_id' => null,
'action' => LogAction::SEND,
'content' => $validated['quote'],
'ip' => request()->ip(),
]);
activity()
->event('send')
->log("Manually sent quote: {$validated['quote']}");
$this->quote = '';
}
@ -37,7 +31,7 @@ new #[Layout('layouts.app')] class extends Component
<x-text-input
wire:model="quote"
placeholder="{{ __('What did he say this time?') }}"
class="block w-full border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm"
class="block w-full text-nexi-black border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm"
></x-text-input>
<x-input-error :messages="$errors->get('quote')" class="mt-2"/>

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\LogAction;
use App\Models\Log;
use App\Models\User;
use App\Providers\RouteServiceProvider;
@ -42,14 +41,10 @@ new #[Layout('layouts.guest')] class extends Component
session()->regenerate();
Log::create([
'user_id' => auth()->user()->id,
'loggable_type' => User::class,
'loggable_id' => auth()->user()->id,
'action' => LogAction::ACCESS,
'content' => 'User logged in',
'ip' => request()->ip(),
]);
activity()
->performedOn(auth()->user())
->event('access')
->log('login');
$this->redirect(
session('url.intended', RouteServiceProvider::HOME),

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\LogAction;
use App\Models\User;
use App\Models\Log;
use Illuminate\Auth\Events\PasswordReset;
@ -38,6 +37,8 @@ new #[Layout('layouts.guest')] class extends Component
// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
// database. Otherwise we will parse the error and return the response.
auth()->user()->update([
$status = Password::reset(
$this->only('email', 'password', 'password_confirmation', 'token'),
function ($user) {
@ -49,6 +50,7 @@ new #[Layout('layouts.guest')] class extends Component
event(new PasswordReset($user));
}
);
]);
// If the password was successfully reset, we will redirect the user back to
// the application's home authenticated view. If there is an error we can
@ -59,16 +61,11 @@ new #[Layout('layouts.guest')] class extends Component
return;
}
session()->flash('status', __($status));
activity()
->event('updated')
->log('password reset');
Log::create([
'user_id' => auth()->user()->id,
'loggable_type' => User::class,
'loggable_id' => auth()?->user()?->id,
'action' => LogAction::UPDATE,
'content' => 'User reset password via reset',
'ip' => request()->ip(),
]);
session()->flash('status', __($status));
$this->redirectRoute('login', navigate: true);
}

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\LogAction;
use App\Models\Log;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
@ -27,18 +26,16 @@ new class extends Component
throw $e;
}
// We don't want to have a complete log, just something for updating password
activity()->withoutLogs(function () {
auth()->user()->update([
'password' => Hash::make($validated['password']),
]);
});
Log::create([
'user_id' => auth()->user()->id,
'loggable_type' => User::class,
'loggable_id' => auth()?->user()?->id,
'action' => LogAction::UPDATE,
'content' => 'User updated password',
'ip' => request()->ip(),
]);
activity()
->event('updated')
->log('password updated');
$this->reset('current_password', 'password', 'password_confirmation');