mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-16 01:06:46 +00:00
Add audit logs for rules.
This commit is contained in:
53
app/Events/TriggeredAuditLog.php
Normal file
53
app/Events/TriggeredAuditLog.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* TriggeredAuditLog.php
|
||||||
|
* Copyright (c) 2022 james@firefly-iii.org
|
||||||
|
*
|
||||||
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FireflyIII\Events;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TriggeredAuditLog
|
||||||
|
*/
|
||||||
|
class TriggeredAuditLog extends Event
|
||||||
|
|
||||||
|
{
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
public Model $changer;
|
||||||
|
public Model $auditable;
|
||||||
|
public string $field;
|
||||||
|
public mixed $before;
|
||||||
|
public mixed $after;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*/
|
||||||
|
public function __construct(Model $changer, Model $auditable, string $field, mixed $before = null, mixed $after = null)
|
||||||
|
{
|
||||||
|
$this->changer = $changer;
|
||||||
|
$this->auditable = $auditable;
|
||||||
|
$this->field = $field;
|
||||||
|
$this->before = $before;
|
||||||
|
$this->after = $after;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
45
app/Handlers/Events/AuditEventHandler.php
Normal file
45
app/Handlers/Events/AuditEventHandler.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* AuditEventHandler.php
|
||||||
|
* Copyright (c) 2022 james@firefly-iii.org
|
||||||
|
*
|
||||||
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FireflyIII\Handlers\Events;
|
||||||
|
|
||||||
|
use FireflyIII\Events\TriggeredAuditLog;
|
||||||
|
use FireflyIII\Models\AuditLogEntry;
|
||||||
|
|
||||||
|
class AuditEventHandler
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param TriggeredAuditLog $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function storeAuditEvent(TriggeredAuditLog $event)
|
||||||
|
{
|
||||||
|
$auditLogEntry = new AuditLogEntry;
|
||||||
|
$auditLogEntry->auditable()->associate($event->auditable);
|
||||||
|
$auditLogEntry->changer()->associate($event->changer);
|
||||||
|
$auditLogEntry->action = $event->field;
|
||||||
|
$auditLogEntry->before = $event->before;
|
||||||
|
$auditLogEntry->after = $event->after;
|
||||||
|
$auditLogEntry->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
58
app/Models/AuditLogEntry.php
Normal file
58
app/Models/AuditLogEntry.php
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* AuditLogEntry.php
|
||||||
|
* Copyright (c) 2022 james@firefly-iii.org
|
||||||
|
*
|
||||||
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FireflyIII\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class AuditLogEntry
|
||||||
|
*/
|
||||||
|
class AuditLogEntry extends Model
|
||||||
|
{
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'before' => 'array',
|
||||||
|
'after' => 'array',
|
||||||
|
'created_at' => 'datetime',
|
||||||
|
'updated_at' => 'datetime',
|
||||||
|
'deleted_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
*/
|
||||||
|
public function auditable(): MorphTo
|
||||||
|
{
|
||||||
|
return $this->morphTo();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
*/
|
||||||
|
public function changer(): MorphTo
|
||||||
|
{
|
||||||
|
return $this->morphTo();
|
||||||
|
}
|
||||||
|
}
|
@@ -78,7 +78,7 @@ class Note extends Model
|
|||||||
/**
|
/**
|
||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
*
|
*
|
||||||
* Get all of the owning noteable models.
|
* Get all the owning noteable models.
|
||||||
*/
|
*/
|
||||||
public function noteable(): MorphTo
|
public function noteable(): MorphTo
|
||||||
{
|
{
|
||||||
|
@@ -36,6 +36,7 @@ use FireflyIII\Events\RequestedSendWebhookMessages;
|
|||||||
use FireflyIII\Events\RequestedVersionCheckStatus;
|
use FireflyIII\Events\RequestedVersionCheckStatus;
|
||||||
use FireflyIII\Events\StoredAccount;
|
use FireflyIII\Events\StoredAccount;
|
||||||
use FireflyIII\Events\StoredTransactionGroup;
|
use FireflyIII\Events\StoredTransactionGroup;
|
||||||
|
use FireflyIII\Events\TriggeredAuditLog;
|
||||||
use FireflyIII\Events\UpdatedAccount;
|
use FireflyIII\Events\UpdatedAccount;
|
||||||
use FireflyIII\Events\UpdatedTransactionGroup;
|
use FireflyIII\Events\UpdatedTransactionGroup;
|
||||||
use FireflyIII\Events\UserChangedEmail;
|
use FireflyIII\Events\UserChangedEmail;
|
||||||
@@ -157,6 +158,11 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
WarnUserAboutBill::class => [
|
WarnUserAboutBill::class => [
|
||||||
'FireflyIII\Handlers\Events\BillEventHandler@warnAboutBill',
|
'FireflyIII\Handlers\Events\BillEventHandler@warnAboutBill',
|
||||||
],
|
],
|
||||||
|
|
||||||
|
// audit log events:
|
||||||
|
TriggeredAuditLog::class => [
|
||||||
|
'FireflyIII\Handlers\Events\AuditEventHandler@storeAuditEvent',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -23,8 +23,10 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\TransactionRules\Actions;
|
namespace FireflyIII\TransactionRules\Actions;
|
||||||
|
|
||||||
use DB;
|
use DB;
|
||||||
|
use FireflyIII\Events\TriggeredAuditLog;
|
||||||
use FireflyIII\Factory\TagFactory;
|
use FireflyIII\Factory\TagFactory;
|
||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Log;
|
use Log;
|
||||||
|
|
||||||
@@ -71,6 +73,10 @@ class AddTag implements ActionInterface
|
|||||||
// add to journal:
|
// add to journal:
|
||||||
DB::table('tag_transaction_journal')->insert(['tag_id' => $tag->id, 'transaction_journal_id' => $journal['transaction_journal_id']]);
|
DB::table('tag_transaction_journal')->insert(['tag_id' => $tag->id, 'transaction_journal_id' => $journal['transaction_journal_id']]);
|
||||||
Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal['transaction_journal_id']));
|
Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal['transaction_journal_id']));
|
||||||
|
$journal = TransactionJournal::find($journal['transaction_journal_id']);
|
||||||
|
// event for audit log entry
|
||||||
|
//// changer, auditable, field, value before, value after
|
||||||
|
event(new TriggeredAuditLog($this->action->rule, $journal, 'add_tag', null, $tag->tag));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
42
database/migrations/2022_10_01_210238_audit_log_entries.php
Normal file
42
database/migrations/2022_10_01_210238_audit_log_entries.php
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('audit_log_entries', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->timestamps();
|
||||||
|
$table->softDeletes();
|
||||||
|
|
||||||
|
$table->integer('auditable_id', false, true);
|
||||||
|
$table->string('auditable_type');
|
||||||
|
|
||||||
|
$table->integer('changer_id', false, true);
|
||||||
|
$table->string('changer_type');
|
||||||
|
|
||||||
|
$table->string('action', 255);
|
||||||
|
$table->text('before')->nullable();
|
||||||
|
$table->text('after')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('audit_log_entries');
|
||||||
|
}
|
||||||
|
};
|
Reference in New Issue
Block a user