snipe-it/app/Models/Loggable.php
snipe 9aac1cbba4 Merge branch 'master' into rcs/merge_master_into_develop_for_rc_8
Signed-off-by: snipe <snipe@snipe.net>

# Conflicts:
#	README.md
#	app/Console/Commands/MoveUploadsToNewDisk.php
#	app/Http/Controllers/ActionlogController.php
#	app/Http/Controllers/Api/LicensesController.php
#	app/Http/Controllers/Api/StatuslabelsController.php
#	app/Http/Controllers/Assets/AssetCheckinController.php
#	app/Http/Controllers/Licenses/LicensesController.php
#	app/Http/Controllers/Users/BulkUsersController.php
#	app/Http/Requests/AssetCheckoutRequest.php
#	app/Importer/LicenseImporter.php
#	app/Models/Actionlog.php
#	app/Models/License.php
#	app/Models/User.php
#	app/Observers/AssetObserver.php
#	composer.lock
#	config/version.php
#	database/factories/LicenseFactory.php
#	database/migrations/2015_09_21_235926_create_custom_field_custom_fieldset.php
#	database/migrations/2018_10_18_191228_add_kits_licenses_table.php
#	database/migrations/2018_10_19_153910_add_kits_table.php
#	database/migrations/2018_10_19_154013_add_kits_models_table.php
#	database/migrations/2019_02_07_185953_add_kits_consumables_table.php
#	database/migrations/2019_02_07_190030_add_kits_accessories_table.php
#	package-lock.json
#	package.json
#	public/css/dist/all.css
#	public/css/dist/bootstrap-table.css
#	public/js/dist/bootstrap-table.js
#	public/mix-manifest.json
#	resources/lang/ar/general.php
#	resources/lang/ar/passwords.php
#	resources/lang/cs/general.php
#	resources/lang/cs/passwords.php
#	resources/lang/de/admin/custom_fields/general.php
#	resources/lang/de/admin/settings/general.php
#	resources/lang/de/admin/settings/message.php
#	resources/lang/fr/admin/custom_fields/general.php
#	resources/lang/fr/admin/hardware/general.php
#	resources/lang/fr/admin/locations/table.php
#	resources/lang/fr/admin/settings/message.php
#	resources/lang/hu/admin/custom_fields/general.php
#	resources/lang/hu/admin/settings/general.php
#	resources/lang/hu/general.php
#	resources/lang/it/admin/settings/general.php
#	resources/lang/nl/admin/custom_fields/general.php
#	resources/lang/nl/admin/settings/general.php
#	resources/lang/nl/general.php
#	resources/lang/pl/admin/custom_fields/general.php
#	resources/lang/sv-SE/passwords.php
#	resources/lang/tr/general.php
#	resources/views/hardware/view.blade.php
#	resources/views/partials/bootstrap-table.blade.php
#	resources/views/reports/activity.blade.php
#	resources/views/users/print.blade.php
2022-04-28 17:49:06 +01:00

254 lines
6.8 KiB
PHP

<?php
namespace App\Models;
use App\Models\Setting;
use App\Notifications\AuditNotification;
use Illuminate\Support\Facades\Auth;
trait Loggable
{
/**
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
* @since [v3.4]
* @return \App\Models\Actionlog
*/
public function log()
{
return $this->morphMany(Actionlog::class, 'item');
}
/**
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
* @since [v3.4]
* @return \App\Models\Actionlog
*/
public function logCheckout($note, $target, $action_date = null)
{
$log = new Actionlog;
$log = $this->determineLogItemType($log);
if (Auth::user()) {
$log->user_id = Auth::user()->id;
}
if (! isset($target)) {
throw new \Exception('All checkout logs require a target.');
return;
}
if (! isset($target->id)) {
throw new \Exception('That target seems invalid (no target ID available).');
return;
}
$log->target_type = get_class($target);
$log->target_id = $target->id;
// Figure out what the target is
if ($log->target_type == Location::class) {
$log->location_id = $target->id;
} elseif ($log->target_type == Asset::class) {
$log->location_id = $target->location_id;
} else {
$log->location_id = $target->location_id;
}
$log->note = $note;
$log->action_date = $action_date;
if (! $log->action_date) {
$log->action_date = date('Y-m-d H:i:s');
}
$log->logaction('checkout');
return $log;
}
/**
* Helper method to determine the log item type
*/
private function determineLogItemType($log)
{
// We need to special case licenses because of license_seat vs license. So much for clean polymorphism :
if (static::class == LicenseSeat::class) {
$log->item_type = License::class;
$log->item_id = $this->license_id;
} else {
$log->item_type = static::class;
$log->item_id = $this->id;
}
return $log;
}
/**
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
* @since [v3.4]
* @return \App\Models\Actionlog
*/
public function logCheckin($target, $note, $action_date = null)
{
$settings = Setting::getSettings();
$log = new Actionlog;
$log->target_type = get_class($target);
$log->target_id = $target->id;
if (static::class == LicenseSeat::class) {
$log->item_type = License::class;
$log->item_id = $this->license_id;
} else {
$log->item_type = static::class;
$log->item_id = $this->id;
if (static::class == Asset::class) {
if ($asset = Asset::find($log->item_id)) {
$asset->increment('checkin_counter', 1);
}
}
}
$log->location_id = null;
$log->note = $note;
$log->action_date = $action_date;
if (! $log->action_date) {
$log->action_date = date('Y-m-d H:i:s');
}
if (! $log->action_date) {
$log->action_date = date('Y-m-d H:i:s');
}
if (Auth::user()) {
$log->user_id = Auth::user()->id;
}
$log->logaction('checkin from');
// $params = [
// 'target' => $target,
// 'item' => $log->item,
// 'admin' => $log->user,
// 'note' => $note,
// 'target_type' => $log->target_type,
// 'settings' => $settings,
// ];
//
//
// $checkinClass = null;
//
// if (method_exists($target, 'notify')) {
// try {
// $target->notify(new static::$checkinClass($params));
// } catch (\Exception $e) {
// \Log::debug($e);
// }
//
// }
//
// // Send to the admin, if settings dictate
// $recipient = new \App\Models\Recipients\AdminRecipient();
//
// if (($settings->admin_cc_email!='') && (static::$checkinClass!='')) {
// try {
// $recipient->notify(new static::$checkinClass($params));
// } catch (\Exception $e) {
// \Log::debug($e);
// }
//
// }
return $log;
}
/**
* @author A. Gianotto <snipe@snipe.net>
* @since [v4.0]
* @return \App\Models\Actionlog
*/
public function logAudit($note, $location_id, $filename = null)
{
$log = new Actionlog;
$location = Location::find($location_id);
if (static::class == LicenseSeat::class) {
$log->item_type = License::class;
$log->item_id = $this->license_id;
} else {
$log->item_type = static::class;
$log->item_id = $this->id;
}
$log->location_id = ($location_id) ? $location_id : null;
$log->note = $note;
$log->user_id = Auth::user()->id;
$log->filename = $filename;
$log->logaction('audit');
$params = [
'item' => $log->item,
'filename' => $log->filename,
'admin' => $log->user,
'location' => ($location) ? $location->name : '',
'note' => $note,
];
Setting::getSettings()->notify(new AuditNotification($params));
return $log;
}
/**
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
* @since [v3.5]
* @return \App\Models\Actionlog
*/
public function logCreate($note = null)
{
$user_id = -1;
if (Auth::user()) {
$user_id = Auth::user()->id;
}
$log = new Actionlog;
if (static::class == LicenseSeat::class) {
$log->item_type = License::class;
$log->item_id = $this->license_id;
} else {
$log->item_type = static::class;
$log->item_id = $this->id;
}
$log->location_id = null;
$log->note = $note;
$log->user_id = $user_id;
$log->logaction('create');
$log->save();
return $log;
}
/**
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
* @since [v3.4]
* @return \App\Models\Actionlog
*/
public function logUpload($filename, $note)
{
$log = new Actionlog;
if (static::class == LicenseSeat::class) {
$log->item_type = License::class;
$log->item_id = $this->license_id;
} else {
$log->item_type = static::class;
$log->item_id = $this->id;
}
$log->user_id = Auth::user()->id;
$log->note = $note;
$log->target_id = null;
$log->created_at = date('Y-m-d H:i:s');
$log->filename = $filename;
$log->logaction('uploaded');
return $log;
}
}