mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
Move Loggable to Traits directory; use trait boot method for observers
This commit is contained in:
parent
afa7391080
commit
233ad92112
|
@ -4,6 +4,7 @@ namespace App\Models;
|
||||||
|
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Models\Traits\Acceptable;
|
use App\Models\Traits\Acceptable;
|
||||||
|
use App\Models\Traits\Loggable;
|
||||||
use App\Models\Traits\Searchable;
|
use App\Models\Traits\Searchable;
|
||||||
use App\Presenters\Presentable;
|
use App\Presenters\Presentable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
|
@ -7,19 +7,17 @@ use App\Exceptions\CheckoutNotAllowed;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Http\Traits\UniqueUndeletedTrait;
|
use App\Http\Traits\UniqueUndeletedTrait;
|
||||||
use App\Models\Traits\Acceptable;
|
use App\Models\Traits\Acceptable;
|
||||||
|
use App\Models\Traits\Loggable;
|
||||||
use App\Models\Traits\Searchable;
|
use App\Models\Traits\Searchable;
|
||||||
use App\Presenters\Presentable;
|
|
||||||
use App\Presenters\AssetPresenter;
|
use App\Presenters\AssetPresenter;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use App\Presenters\Presentable;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Watson\Validating\ValidatingTrait;
|
use Watson\Validating\ValidatingTrait;
|
||||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model for Assets.
|
* Model for Assets.
|
||||||
|
|
|
@ -2,15 +2,16 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Http\Traits\TwoColumnUniqueUndeletedTrait;
|
||||||
|
use App\Models\Traits\Loggable;
|
||||||
use App\Models\Traits\Searchable;
|
use App\Models\Traits\Searchable;
|
||||||
|
use App\Presenters\AssetModelPresenter;
|
||||||
use App\Presenters\Presentable;
|
use App\Presenters\Presentable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Watson\Validating\ValidatingTrait;
|
use Watson\Validating\ValidatingTrait;
|
||||||
use \App\Presenters\AssetModelPresenter;
|
|
||||||
use App\Http\Traits\TwoColumnUniqueUndeletedTrait;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model for Asset Models. Asset Models contain higher level
|
* Model for Asset Models. Asset Models contain higher level
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Models\Traits\Loggable;
|
||||||
use App\Models\Traits\Searchable;
|
use App\Models\Traits\Searchable;
|
||||||
use App\Presenters\Presentable;
|
use App\Presenters\Presentable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
|
@ -4,21 +4,15 @@ namespace App\Models;
|
||||||
|
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Models\Traits\Acceptable;
|
use App\Models\Traits\Acceptable;
|
||||||
|
use App\Models\Traits\Loggable;
|
||||||
use App\Models\Traits\Searchable;
|
use App\Models\Traits\Searchable;
|
||||||
|
use App\Presenters\ConsumablePresenter;
|
||||||
use App\Presenters\Presentable;
|
use App\Presenters\Presentable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Watson\Validating\ValidatingTrait;
|
use Watson\Validating\ValidatingTrait;
|
||||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
||||||
use App\Presenters\ConsumablePresenter;
|
|
||||||
use App\Models\Actionlog;
|
|
||||||
use App\Models\ConsumableAssignment;
|
|
||||||
use App\Models\User;
|
|
||||||
use App\Models\Location;
|
|
||||||
use App\Models\Manufacturer;
|
|
||||||
use App\Models\Supplier;
|
|
||||||
use App\Models\Category;
|
|
||||||
|
|
||||||
class Consumable extends SnipeModel
|
class Consumable extends SnipeModel
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,13 +3,13 @@
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
|
use App\Models\Traits\Loggable;
|
||||||
use App\Models\Traits\Searchable;
|
use App\Models\Traits\Searchable;
|
||||||
use App\Presenters\Presentable;
|
use App\Presenters\Presentable;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
use Watson\Validating\ValidatingTrait;
|
use Watson\Validating\ValidatingTrait;
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Models\Traits\Acceptable;
|
use App\Models\Traits\Acceptable;
|
||||||
|
use App\Models\Traits\Loggable;
|
||||||
use App\Notifications\CheckinLicenseNotification;
|
use App\Notifications\CheckinLicenseNotification;
|
||||||
use App\Notifications\CheckoutLicenseNotification;
|
use App\Notifications\CheckoutLicenseNotification;
|
||||||
use App\Presenters\Presentable;
|
use App\Presenters\Presentable;
|
||||||
|
|
|
@ -1,17 +1,115 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models\Traits;
|
||||||
|
|
||||||
|
use App\Models\Actionlog;
|
||||||
|
use App\Models\Asset;
|
||||||
|
use App\Models\License;
|
||||||
|
use App\Models\LicenseSeat;
|
||||||
|
use App\Models\Location;
|
||||||
use App\Models\Setting;
|
use App\Models\Setting;
|
||||||
use App\Notifications\AuditNotification;
|
use App\Notifications\AuditNotification;
|
||||||
use Illuminate\Support\Facades\Auth;
|
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
|
|
||||||
trait Loggable
|
trait Loggable
|
||||||
{
|
{
|
||||||
// an attribute for setting whether or not the item was imported
|
// an attribute for setting whether or not the item was imported
|
||||||
public ?bool $imported = false;
|
public ?bool $imported = false;
|
||||||
|
|
||||||
|
public static function bootLoggable()
|
||||||
|
{
|
||||||
|
\Log::error("LOGGABLE IS BOOTING!!!!!!!!!!!");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen to the Asset updating event. This fires automatically every time an existing asset is saved.
|
||||||
|
*
|
||||||
|
* @param Asset $asset
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static::saving(function ($model) {
|
||||||
|
$attributes = $model->getAttributes();
|
||||||
|
$attributesOriginal = $model->getRawOriginal();
|
||||||
|
$same_checkout_counter = false;
|
||||||
|
$same_checkin_counter = false;
|
||||||
|
$restoring_or_deleting = false;
|
||||||
|
|
||||||
|
|
||||||
|
// This is a gross hack to prevent the double logging when restoring an asset
|
||||||
|
if (array_key_exists('deleted_at', $attributes) && array_key_exists('deleted_at', $attributesOriginal)) {
|
||||||
|
$restoring_or_deleting = (($attributes['deleted_at'] != $attributesOriginal['deleted_at']));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('checkout_counter', $attributes) && array_key_exists('checkout_counter', $attributesOriginal)) {
|
||||||
|
$same_checkout_counter = (($attributes['checkout_counter'] == $attributesOriginal['checkout_counter']));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('checkin_counter', $attributes) && array_key_exists('checkin_counter', $attributesOriginal)) {
|
||||||
|
$same_checkin_counter = (($attributes['checkin_counter'] == $attributesOriginal['checkin_counter']));
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the asset isn't being checked out or audited, log the update.
|
||||||
|
// (Those other actions already create log entries.)
|
||||||
|
if (($attributes['assigned_to'] == $attributesOriginal['assigned_to'])
|
||||||
|
&& ($same_checkout_counter) && ($same_checkin_counter)
|
||||||
|
&& ((isset($attributes['next_audit_date']) ? $attributes['next_audit_date'] : null) == (isset($attributesOriginal['next_audit_date']) ? $attributesOriginal['next_audit_date'] : null))
|
||||||
|
&& ($attributes['last_checkout'] == $attributesOriginal['last_checkout']) && (!$restoring_or_deleting)) {
|
||||||
|
$changed = [];
|
||||||
|
|
||||||
|
foreach ($model->getRawOriginal() as $key => $value) {
|
||||||
|
if ($model->getRawOriginal()[$key] != $model->getAttributes()[$key]) {
|
||||||
|
$changed[$key]['old'] = $model->getRawOriginal()[$key];
|
||||||
|
$changed[$key]['new'] = $model->getAttributes()[$key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($changed)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$logAction = new Actionlog();
|
||||||
|
$logAction->item_type = self::class;
|
||||||
|
$logAction->item_id = $model->id;
|
||||||
|
$logAction->created_at = date('Y-m-d H:i:s');
|
||||||
|
$logAction->created_by = auth()->id();
|
||||||
|
$logAction->log_meta = json_encode($changed);
|
||||||
|
$logAction->logaction('update');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
static::updating(function ($model) {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen to the Asset deleting event.
|
||||||
|
*
|
||||||
|
* @param Asset $asset
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static::deleting(function ($model) {
|
||||||
|
$logAction = new Actionlog();
|
||||||
|
$logAction->item_type = self::class;
|
||||||
|
$logAction->item_id = $model->id;
|
||||||
|
$logAction->created_at = date('Y-m-d H:i:s');
|
||||||
|
$logAction->created_by = auth()->id();
|
||||||
|
$logAction->logaction('delete');
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen to the Asset deleting event.
|
||||||
|
*
|
||||||
|
* @param Asset $asset
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static::restoring(function ($model) {
|
||||||
|
$logAction = new Actionlog();
|
||||||
|
$logAction->item_type = self::class;
|
||||||
|
$logAction->item_id = $model->id;
|
||||||
|
$logAction->created_at = date('Y-m-d H:i:s');
|
||||||
|
$logAction->created_by = auth()->id();
|
||||||
|
$logAction->logaction('restore');
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
||||||
* @since [v3.4]
|
* @since [v3.4]
|
|
@ -10,63 +10,6 @@ use Carbon\Carbon;
|
||||||
|
|
||||||
class AssetObserver
|
class AssetObserver
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Listen to the Asset updating event. This fires automatically every time an existing asset is saved.
|
|
||||||
*
|
|
||||||
* @param Asset $asset
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function updating(Asset $asset)
|
|
||||||
{
|
|
||||||
$attributes = $asset->getAttributes();
|
|
||||||
$attributesOriginal = $asset->getRawOriginal();
|
|
||||||
$same_checkout_counter = false;
|
|
||||||
$same_checkin_counter = false;
|
|
||||||
$restoring_or_deleting = false;
|
|
||||||
|
|
||||||
|
|
||||||
// This is a gross hack to prevent the double logging when restoring an asset
|
|
||||||
if (array_key_exists('deleted_at', $attributes) && array_key_exists('deleted_at', $attributesOriginal)){
|
|
||||||
$restoring_or_deleting = (($attributes['deleted_at'] != $attributesOriginal['deleted_at']));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (array_key_exists('checkout_counter', $attributes) && array_key_exists('checkout_counter', $attributesOriginal)){
|
|
||||||
$same_checkout_counter = (($attributes['checkout_counter'] == $attributesOriginal['checkout_counter']));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (array_key_exists('checkin_counter', $attributes) && array_key_exists('checkin_counter', $attributesOriginal)){
|
|
||||||
$same_checkin_counter = (($attributes['checkin_counter'] == $attributesOriginal['checkin_counter']));
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the asset isn't being checked out or audited, log the update.
|
|
||||||
// (Those other actions already create log entries.)
|
|
||||||
if (($attributes['assigned_to'] == $attributesOriginal['assigned_to'])
|
|
||||||
&& ($same_checkout_counter) && ($same_checkin_counter)
|
|
||||||
&& ((isset( $attributes['next_audit_date']) ? $attributes['next_audit_date'] : null) == (isset($attributesOriginal['next_audit_date']) ? $attributesOriginal['next_audit_date']: null))
|
|
||||||
&& ($attributes['last_checkout'] == $attributesOriginal['last_checkout']) && (!$restoring_or_deleting))
|
|
||||||
{
|
|
||||||
$changed = [];
|
|
||||||
|
|
||||||
foreach ($asset->getRawOriginal() as $key => $value) {
|
|
||||||
if ((array_key_exists($key, $asset->getAttributes())) && ($asset->getRawOriginal()[$key] != $asset->getAttributes()[$key])) {
|
|
||||||
$changed[$key]['old'] = $asset->getRawOriginal()[$key];
|
|
||||||
$changed[$key]['new'] = $asset->getAttributes()[$key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($changed)){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$logAction = new Actionlog();
|
|
||||||
$logAction->item_type = Asset::class;
|
|
||||||
$logAction->item_id = $asset->id;
|
|
||||||
$logAction->created_at = date('Y-m-d H:i:s');
|
|
||||||
$logAction->created_by = auth()->id();
|
|
||||||
$logAction->log_meta = json_encode($changed);
|
|
||||||
$logAction->logaction('update');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listen to the Asset created event, and increment
|
* Listen to the Asset created event, and increment
|
||||||
|
@ -115,37 +58,7 @@ class AssetObserver
|
||||||
$logAction->logaction('create');
|
$logAction->logaction('create');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Listen to the Asset deleting event.
|
|
||||||
*
|
|
||||||
* @param Asset $asset
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function deleting(Asset $asset)
|
|
||||||
{
|
|
||||||
$logAction = new Actionlog();
|
|
||||||
$logAction->item_type = Asset::class;
|
|
||||||
$logAction->item_id = $asset->id;
|
|
||||||
$logAction->created_at = date('Y-m-d H:i:s');
|
|
||||||
$logAction->created_by = auth()->id();
|
|
||||||
$logAction->logaction('delete');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Listen to the Asset deleting event.
|
|
||||||
*
|
|
||||||
* @param Asset $asset
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function restoring(Asset $asset)
|
|
||||||
{
|
|
||||||
$logAction = new Actionlog();
|
|
||||||
$logAction->item_type = Asset::class;
|
|
||||||
$logAction->item_id = $asset->id;
|
|
||||||
$logAction->created_at = date('Y-m-d H:i:s');
|
|
||||||
$logAction->created_by = auth()->id();
|
|
||||||
$logAction->logaction('restore');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes every time an asset is saved.
|
* Executes every time an asset is saved.
|
||||||
|
|
Loading…
Reference in a new issue