Skip to content

Commit

Permalink
Merge pull request #10 from mitydigital/feature/new-events
Browse files Browse the repository at this point in the history
Support new events
  • Loading branch information
martyf authored Jul 18, 2024
2 parents 52be9db + 7bda40c commit a0448c9
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 58 deletions.
1 change: 1 addition & 0 deletions resources/lang/en/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
'render' => 'Render Error',
'unknown_user' => 'Unknown user',
'view_not_found' => 'View ":view" not found for :handler',
'handler_not_found' => 'Handler ":handler" not found',
];
3 changes: 3 additions & 0 deletions resources/lang/en/listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

'entry' => ":action ':name' in the ':collection_name' Collection",

'impersonated' => 'Impersonated',
'impersonator' => 'Impersonator',

'nav_tree' => ":action ':id' Nav order in the ':site' Site",

'term' => ":action ':name' in the ':taxonomy_name' Taxonomy",
Expand Down
13 changes: 13 additions & 0 deletions resources/lang/en/utilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'glide' => 'Image Cache',

'license' => 'License',

'search' => 'Search Index',

'stache' => 'Content Stache',

'static' => 'Static Page Cache',
];
10 changes: 10 additions & 0 deletions resources/lang/en/verbs.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
<?php

return [
'cleared' => 'Cleared',
'created' => 'Created',

'deleted' => 'Deleted',

'impersonation_started' => 'Started impersonating',
'impersonation_ended' => 'Ended impersonating',

'logged_in' => 'Logged In',
'logged_out' => 'Logged Out',
'login_failed' => 'Attempted (but failed) to log in',

'password_changed' => 'Had their password changed',
'password_reset' => 'Requested a password reset',

'refreshed' => 'Refreshed',
'replaced' => 'Replaced',
'reuploaded' => 'Re-uploaded',

'saved' => 'Saved',
'set' => 'Set',

'updated' => 'Updated',
'uploaded' => 'Uploaded',

'warmed' => 'Warmed',
];
5 changes: 5 additions & 0 deletions resources/views/listeners/impersonation.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>{{ $data->impersonator->name }} {{ strtolower($handler->action()) }} {{ $data->impersonated->name }}</div>
<div class="text-xs text-gray-500">{{ __('statamic-logger::listeners.impersonator') }}
: {{ $data->impersonator->id }}</div>
<div class="text-xs text-gray-500">{{ __('statamic-logger::listeners.impersonated') }}
: {{ $data->impersonated->id }}</div>
2 changes: 2 additions & 0 deletions resources/views/listeners/site.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div>{{ $data->name }} {{ strtolower($handler->action()) }}</div>
<div class="text-xs text-gray-500">{{ __('statamic-logger::listeners.id') }}: {{ $data->id }}</div>
1 change: 1 addition & 0 deletions resources/views/listeners/utility.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="h-10 flex items-center">{{ $data->utility }} {{ strtolower($handler->action()) }}</div>
7 changes: 6 additions & 1 deletion src/Abstracts/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function handle(mixed $event): void
protected function buildLogEntry(mixed $event): array
{
// if we have a user, get their details
$user = $event->authenticatedUser ?? auth()->user();
$user = $this->getAuthenticatedUser($event);
if ($user) {
$user = [
'id' => $user->id,
Expand All @@ -47,6 +47,11 @@ protected function buildLogEntry(mixed $event): array
];
}

protected function getAuthenticatedUser($event)
{
return $event->authenticatedUser ?? auth()->user();
}

abstract protected function data(mixed $event): array;

protected function supplement(mixed $event): array
Expand Down
123 changes: 67 additions & 56 deletions src/Http/Resources/LogResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MityDigital\StatamicLogger\Http\Resources;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\View;
use Illuminate\View\ViewException;
Expand Down Expand Up @@ -46,71 +47,81 @@ public function toArray($request)
'json' => $matches['message'],
];
} else {
//
// get the handler
//
$handler = app()->make($message->handler);

//
// set the type
//
$type = $handler->type();

//
// convert to a statamic logger entry
//
$entry = new Entry();
foreach ($message->data as $key => $value) {
$entry->$key = $value;
}
try {
//
// get the handler
//
$handler = app()->make($message->handler);

//
// render the message
//
if (View::exists($handler->view())) {
$view = $handler->view();
//
// set the type
//
$type = $handler->type();

//
// convert to a statamic logger entry
//
$entry = new Entry();
foreach ($message->data as $key => $value) {
$entry->$key = $value;
}

// add the event to the handler (just makes it easier)
$handler->setActionEvent($message->event);
//
// render the message
//
if (View::exists($handler->view())) {
$view = $handler->view();

// add the event to the handler (just makes it easier)
$handler->setActionEvent($message->event);

$viewData = [
'data' => $entry,
'event' => $message->event,
'handler' => $handler,
];
} else {
// no view
$view = 'statamic-logger::error';
$viewData = [
'error' => __('statamic-logger::errors.view_not_found', [
':view' => $handler->view(),
':handler' => get_class($handler),
]),
'json' => $matches['message'],
];
}

$viewData = [
'data' => $entry,
'event' => $message->event,
'handler' => $handler,
];
} else {
// no view
if ($message->user) {
//
// prepare the user
//
$user = [
'id' => $message->user?->id,
'name' => $message->user->name ?? null,
'initials' => $message->user->name ? collect(explode(' ', $message->user->name))->map(fn (
string $name
) => mb_substr($name, 0, 1))->join('') : null,
'avatar' => $this->getUserAvatar($message->user),
];

}

//
// raw message
//
if (LogResource::$includeRawMessage) {
$debug = view('statamic-logger::raw', ['data' => $entry])->render();
}
} catch (BindingResolutionException $e) {
$view = 'statamic-logger::error';
$viewData = [
'error' => __('statamic-logger::errors.view_not_found', [
':view' => $handler->view(),
':handler' => get_class($handler),
'error' => __('statamic-logger::errors.handler_not_found', [
'handler' => $message->handler,
]),
'json' => $matches['message'],
];
}

if ($message->user) {
//
// prepare the user
//
$user = [
'id' => $message->user?->id,
'name' => $message->user->name ?? null,
'initials' => $message->user->name ? collect(explode(' ', $message->user->name))->map(fn (
string $name
) => mb_substr($name, 0, 1))->join('') : null,
'avatar' => $this->getUserAvatar($message->user),
];

}

//
// raw message
//
if (LogResource::$includeRawMessage) {
$debug = view('statamic-logger::raw', ['data' => $entry])->render();
}
}

//
Expand Down
58 changes: 58 additions & 0 deletions src/Listeners/Impersonation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace MityDigital\StatamicLogger\Listeners;

use MityDigital\StatamicLogger\Abstracts\EventListener;
use Statamic\Events\ImpersonationEnded;
use Statamic\Events\ImpersonationStarted;

class Impersonation extends EventListener
{
public function view(): string
{
return 'statamic-logger::listeners.impersonation';
}

protected function data($event): array
{
return [
'impersonated' => $this->getUser($event->impersonated),
'impersonator' => $this->getUser($event->impersonator),
];
}

protected function getUser($user = null): array
{
if (! $user) {
return [
'id' => null,
'name' => __('statamic-logger::errors.unknown_user'),
];
}

$name = $user->id;
if (method_exists($user, 'name')) {
$name = $user->name();
} elseif ($user->name) {
$name = $user->name;
}

return [
'id' => $user->id,
'name' => $name,
];
}

protected function getAuthenticatedUser($event)
{
return $event->impersonator;
}

protected function verb($event): string
{
return match ($event) {
ImpersonationEnded::class => __('statamic-logger::verbs.impersonation_ended'),
ImpersonationStarted::class => __('statamic-logger::verbs.impersonation_started'),
};
}
}
33 changes: 33 additions & 0 deletions src/Listeners/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace MityDigital\StatamicLogger\Listeners;

use MityDigital\StatamicLogger\Abstracts\EventListener;
use Statamic\Events\SiteCreated;
use Statamic\Events\SiteDeleted;
use Statamic\Events\SiteSaved;

class Site extends EventListener
{
public function view(): string
{
return 'statamic-logger::listeners.site';
}

protected function data($event): array
{
return [
'id' => $event->site->handle(),
'name' => $event->site->name(),
];
}

protected function verb($event): string
{
return match ($event) {
SiteCreated::class => __('statamic-logger::verbs.created'),
SiteDeleted::class => __('statamic-logger::verbs.deleted'),
SiteSaved::class => __('statamic-logger::verbs.saved')
};
}
}
5 changes: 4 additions & 1 deletion src/Listeners/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MityDigital\StatamicLogger\Abstracts\EventListener;
use Statamic\Events\UserCreated;
use Statamic\Events\UserDeleted;
use Statamic\Events\UserPasswordChanged;
use Statamic\Events\UserSaved;

class User extends EventListener
Expand Down Expand Up @@ -50,7 +51,9 @@ protected function verb($event): string

UserCreated::class => __('statamic-logger::verbs.created'),
UserDeleted::class => __('statamic-logger::verbs.deleted'),
UserSaved::class => __('statamic-logger::verbs.saved')
UserSaved::class => __('statamic-logger::verbs.saved'),

UserPasswordChanged::class => __('statamic-logger::verbs.password_changed'),
};
}
}
Loading

0 comments on commit a0448c9

Please sign in to comment.