Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/Enums/ActionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Enums;
enum ActionType: string
{
// General
case Create = 'create';
case Update = 'update';
case Delete = 'delete';
case Restore = 'restore';

// Assets/Accessories/Components/Licenses/Consumables
case Checkout = 'checkout';
case CheckinFrom = 'checkin from';
case Requested = 'requested';
case RequestCanceled = 'request canceled';
case Accepted = 'accepted';
case Declined = 'declined';
case Audit = 'audit';
case NoteAdded = 'note added';

// Users
case TwoFactorReset = '2FA reset';
case Merged = 'merged';

// Licenses
case DeleteSeats = 'delete seats';
case AddSeats = 'add seats';

// File Uploads
case Uploaded = 'uploaded';
case UploadDeleted = 'upload deleted';
}
8 changes: 1 addition & 7 deletions app/Http/Controllers/LocationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,7 @@ public function postRestore($id) : RedirectResponse
}

if ($location->restore()) {
$logaction = new Actionlog();
$logaction->item_type = Location::class;
$logaction->item_id = $location->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->created_by = auth()->id();
$logaction->logaction('restore');

//Note - the LogsChanges trait on Locations will automatically do a 'restore' action in the action_log when this fires
return redirect()->route('locations.index')->with('success', trans('admin/locations/message.restore.success'));
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/ViewAssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\CheckoutRequests\CancelCheckoutRequestAction;
use App\Actions\CheckoutRequests\CreateCheckoutRequestAction;
use App\Enums\ActionType;
use App\Exceptions\AssetNotRequestable;
use App\Models\Actionlog;
use App\Models\Asset;
Expand Down Expand Up @@ -201,7 +202,7 @@ public function getRequestItem(Request $request, $itemType, $itemId = null, $can
if (($item_request = $item->isRequestedBy($user)) || $cancel_by_admin) {
$item->cancelRequest($requestingUser);
$data['item_quantity'] = ($item_request) ? $item_request->qty : 1;
$logaction->logaction('request_canceled');
$logaction->logaction(ActionType::RequestCanceled);

if (($settings->alert_email != '') && ($settings->alerts_enabled == '1') && (! config('app.lock_passwords'))) {
$settings->notify(new RequestAssetCancelation($data));
Expand Down
8 changes: 6 additions & 2 deletions app/Models/Actionlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use App\Enums\ActionType;

/**
* Model for the Actionlog (the table that keeps a historical log of
Expand Down Expand Up @@ -335,9 +336,12 @@ public function get_src($type = 'assets', $fieldname = 'filename')
* @since [v3.0]
* @return bool
*/
public function logaction($actiontype)
public function logaction(string|ActionType $actiontype)
{
$this->action_type = $actiontype;
if (is_string($actiontype)) {
$actiontype = ActionType::from($actiontype);
}
$this->action_type = $actiontype->value;
$this->remote_ip = request()->ip();
$this->user_agent = request()->header('User-Agent');
$this->action_source = $this->determineActionSource();
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Watson\Validating\ValidatingTrait;
use App\Helpers\Helper;
use Illuminate\Support\Str;
use App\Models\Traits\LogsChanges;

/**
* Model for Categories. Categories are a higher-level group
Expand All @@ -27,6 +28,7 @@ class Category extends SnipeModel
protected $presenter = \App\Presenters\CategoryPresenter::class;
use Presentable;
use SoftDeletes;
use LogsChanges;

protected $table = 'categories';
protected $hidden = ['created_by', 'deleted_at'];
Expand Down
2 changes: 2 additions & 0 deletions app/Models/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\LogsChanges;
use EasySlugger\Utf8Slugger;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -14,6 +15,7 @@ class CustomField extends Model
use HasFactory;
use ValidatingTrait,
UniqueUndeletedTrait;
use LogsChanges;

/**
*
Expand Down
2 changes: 2 additions & 0 deletions app/Models/CustomFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\LogsChanges;
use App\Rules\AlphaEncrypted;
use App\Rules\BooleanEncrypted;
use App\Rules\DateEncrypted;
Expand All @@ -22,6 +23,7 @@ class CustomFieldset extends Model
{
use HasFactory;
use ValidatingTrait;
use LogsChanges;

protected $guarded = ['id'];

Expand Down
3 changes: 2 additions & 1 deletion app/Models/Department.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\CompanyableTrait;
use App\Models\Traits\LogsChanges;
use App\Models\Traits\Searchable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Watson\Validating\ValidatingTrait;
Expand All @@ -22,7 +23,7 @@ class Department extends SnipeModel
*/
protected $injectUniqueIdentifier = true;

use ValidatingTrait, UniqueUndeletedTrait;
use ValidatingTrait, UniqueUndeletedTrait, LogsChanges;

protected $casts = [
'manager_id' => 'integer',
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Depreciation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\LogsChanges;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -10,6 +11,7 @@
class Depreciation extends SnipeModel
{
use HasFactory;
use LogsChanges;

protected $presenter = \App\Presenters\DepreciationPresenter::class;
use Presentable;
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace App\Models;

use App\Models\Traits\LogsChanges;
use App\Models\Traits\Searchable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Watson\Validating\ValidatingTrait;

class Group extends SnipeModel
{
use HasFactory;
use LogsChanges;

protected $table = 'permission_groups';

Expand Down
3 changes: 2 additions & 1 deletion app/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Traits\CompanyableTrait;
use App\Models\Traits\HasUploads;
use App\Models\Traits\Loggable;
use App\Models\Traits\LogsChanges;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -17,7 +18,7 @@ class Location extends SnipeModel
{
use HasFactory;
use CompanyableTrait;
use Loggable;
use LogsChanges;

protected $presenter = \App\Presenters\LocationPresenter::class;
use Presentable;
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Manufacturer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\LogsChanges;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -16,6 +17,7 @@ class Manufacturer extends SnipeModel
protected $presenter = \App\Presenters\ManufacturerPresenter::class;
use Presentable;
use SoftDeletes;
use LogsChanges;

protected $table = 'manufacturers';

Expand Down
2 changes: 2 additions & 0 deletions app/Models/PredefinedKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\LogsChanges;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -19,6 +20,7 @@ class PredefinedKit extends SnipeModel
protected $presenter = \App\Presenters\PredefinedKitPresenter::class;
use HasFactory;
use Presentable;
use LogsChanges;
protected $table = 'kits';

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Models/ReportTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\LogsChanges;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -14,6 +15,7 @@ class ReportTemplate extends Model
use HasFactory;
use SoftDeletes;
use ValidatingTrait;
use LogsChanges;

protected $casts = [
'options' => 'array',
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\LogsChanges;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -21,7 +22,7 @@
class Setting extends Model
{
use HasFactory;
use Notifiable, ValidatingTrait;
use Notifiable, ValidatingTrait, LogsChanges;

/**
* The cache property so that multiple invocations of this will only load the Settings record from disk only once
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Statuslabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\LogsChanges;
use App\Models\Traits\Searchable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -14,6 +15,7 @@ class Statuslabel extends SnipeModel
use SoftDeletes;
use ValidatingTrait;
use UniqueUndeletedTrait;
use LogsChanges;

protected $injectUniqueIdentifier = true;

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Supplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\LogsChanges;
use App\Models\Traits\Searchable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -12,6 +13,7 @@ class Supplier extends SnipeModel
{
use HasFactory;
use SoftDeletes;
use LogsChanges;

protected $table = 'suppliers';

Expand Down
97 changes: 97 additions & 0 deletions app/Models/Traits/LogsChanges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace App\Models\Traits;

use App\Enums\ActionType;
use App\Models\Actionlog;

trait LogsChanges {

private ?ActionType $action_type = null;
private array $meta = [];
static function bootLogsChanges() {
static::creating(function ($model) {
\Log::error("CREATING!");
$model->action_type = ActionType::Create;
});

static::updating(function ($model) {
\Log::error("UPDATING!");
if(!$model->action_type) {
\Log::error("No action type - so definitely doing 'update'!");
$model->action_type = ActionType::Update;
}
});

// The Settings object does not use soft-deletes, so it has no 'restoring' method
// so to be generic, we just look for that method existing at all, and don't call
// it if it doesn't.
if (method_exists(self::class, 'restoring')) {
static::restoring(function ($model) {
$model->action_type = ActionType::Restore;
});
}

/* The main functionality is here: */
static::saving(function ($model) {
\Log::error("recording changes.......");
$model->record_changes();
});

static::saved(function ($model) {
\Log::error("SAVED!!!!");
$model->add_action_log();
});

static::deleted(function ($model) {
$model->action_type = ActionType::Delete;
$model->add_action_log();
\Log::error("deleted!!!!!!!!!!!");
});
}

function record_changes()
{
$changed = [];

// something here with custom fields is needed? or will getRawOriginal et al just do that for us?
foreach ($this->getRawOriginal() as $key => $value) { //on 'create' this doesn't write down the new attributes
if ($this->getRawOriginal()[$key] != $this->getAttributes()[$key]) {
$changed[$key]['old'] = $this->getRawOriginal()[$key];
$changed[$key]['new'] = $this->getAttributes()[$key];

if (property_exists($this, 'hidden') && in_array($key, $this->hidden)) {
$changed[$key]['old'] = '*************'; //FIXME deleted_at is hidden?!
$changed[$key]['new'] = '*************';
}
}
}
$this->meta = $changed;
}

function add_action_log()
{
if(!$this->action_type && !$this->meta) {
\Log::warning("No action type set, and no changes to record. Not logging.");
return;
}
if($this->action_type == ActionType::Update && !$this->meta) {
\Log::warning("An update with no actual changes to record. Not logging");
return;
}
$logAction = new Actionlog();
$logAction->action_type = $this->action_type->value;
$logAction->item()->associate($this);
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->action_date = date('Y-m-d H:i:s');
// target_id and target_type?
// need IP and user-agent!!!!!
$logAction->created_by = auth()->id();
$logAction->log_meta = $this->meta ? json_encode($this->meta) : null; // this gets weird on 'create'
if($logAction->save()) {
//success! Reset for more actions later...
$this->action_type = null;
$this->meta = [];
}
}
}
Loading
Loading