spatie / laravel-activitylog

Log activity inside your Laravel app
https://docs.spatie.be/laravel-activitylog
MIT License
5.35k stars 714 forks source link

Add more detail to manipulating changes #1268

Closed tonypartridge closed 8 months ago

tonypartridge commented 9 months ago

We were storing sensitive encrypted information and the pipe is fantastic approach and works well, but there wasn't an obvious easy way to apply it to the model for new comers to pipes etc.

Also, for anyone interested I use this pipe class to encrypt the values too

<?php

namespace App\Pipelines\Loggable;

use Closure;
use Spatie\Activitylog\Contracts\LoggablePipe;
use Spatie\Activitylog\EventLogBag;

class EncryptAttributesPipe implements LoggablePipe
{
    public function handle(EventLogBag $event, Closure $next): EventLogBag
    {
        foreach (['attributes', 'old'] as $type) {
            foreach ($event->changes[$type] ?? [] as $key => $value) {
                if ($event->model->hasCast($key, 'encrypted')) {
                    $event->changes[$type][$key] = encrypt($value);
                }
            }
        }

        return $next($event);
    }
}
tonypartridge commented 8 months ago

Hmm...

Gummibeer commented 8 months ago

Changed a little detail - switch to booted() method as otherwise you will have to call parent::boot() and doing so is the same as using booting() or booted().

tonypartridge commented 8 months ago

Fantastic, thanks!

On Thu, 29 Feb 2024 at 09:56, Tom Herrmann @.***> wrote:

Merged #1268 https://github.com/spatie/laravel-activitylog/pull/1268 into main.

— Reply to this email directly, view it on GitHub https://github.com/spatie/laravel-activitylog/pull/1268#event-11963511233, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKWBFU22WTP4MCL2QJWUB3YV35MRAVCNFSM6AAAAABCL36L3CVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJRHE3DGNJRGEZDGMY . You are receiving this because you authored the thread.Message ID: @.***>