mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-26 06:04:08 -08:00
46f5f21368
* Added “show fields in email” to custom fields * Added “show images in email” to settings * Added nicer HTML emails * Break notifications out into their own, instead of trying to mash them all together * Remove old notification for accessory checkout * Janky fix for #5076 - “The asset you have attempted to accept was not checked out to you” * Add method for image url for accessories * Added accessory checkout email blade * Make accessory email notification on checkout screen consistent with assets * Added native consumables notifications * Fixes for asset notification * Updated notification blades with correct-er fields * Updated notifications * License checkin notification - does not work yet Need to figure out whether the license seat is assigned to a person or an asset before we can pass the target * Added alternate “cc” email for admins * Only try to trigger notifications if the target is a user * Fix tests * Fixed consumable URL * Removed unused notification * Pass target type in params * Show slack status * Pass additional parameters There is a logic bug in this :( Will send to slack twice, since the admin CC and the user are both using the same notification. Fuckity fuck fuck fuck. * Pass a variable to the notification to supress the duplicate slack message * Slack is broken :( Trying to fix Will try a git bisect * Put preview back into checkout * Pulled old archaic mail * Removed debugging * Fixed wrong email title * Fixed slack endpoint not firing * Poobot, we hardly knew ye. * Removed old, manual mail from API * Typo :-/ * Code cleanup * Use defined formatted date in JSON * Use static properties for checkin/checkout notifiers for cleaner code * Removed debugging * Use date formatter * Fixed target_type * Fixed language in consumable email
246 lines
6.8 KiB
PHP
246 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Actionlog;
|
|
use App\Models\Asset;
|
|
use App\Models\CheckoutRequest;
|
|
use App\Models\User;
|
|
use App\Notifications\CheckinAssetNotification;
|
|
use App\Notifications\AuditNotification;
|
|
use App\Notifications\CheckoutAssetNotification;
|
|
use App\Notifications\CheckoutAccessoryNotification;
|
|
use App\Notifications\CheckinAccessoryNotification;
|
|
use App\Notifications\CheckoutConsumableNotification;
|
|
use App\Notifications\CheckoutLicenseNotification;
|
|
use App\Notifications\CheckinLicenseNotification;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
trait Loggable
|
|
{
|
|
|
|
/**
|
|
* @author Daniel Meltzer <parallelgrapefruit@gmail.com
|
|
* @since [v3.4]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
|
|
public function log()
|
|
{
|
|
return $this->morphMany(Actionlog::class, 'item');
|
|
}
|
|
|
|
/**
|
|
* @author Daniel Meltzer <parallelgrapefruit@gmail.com
|
|
* @since [v3.4]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
public function logCheckout($note, $target /* What are we checking out to? */)
|
|
{
|
|
$settings = Setting::getSettings();
|
|
$log = new Actionlog;
|
|
$log = $this->determineLogItemType($log);
|
|
$log->user_id = Auth::user()->id;
|
|
|
|
if (!isset($target)) {
|
|
throw new Exception('All checkout logs require a target');
|
|
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->rtd_location_id;
|
|
} else {
|
|
$log->location_id = $target->location_id;
|
|
}
|
|
|
|
$log->note = $note;
|
|
$log->logaction('checkout');
|
|
|
|
$params = [
|
|
'item' => $log->item,
|
|
'target_type' => $log->target_type,
|
|
'target' => $target,
|
|
'admin' => $log->user,
|
|
'note' => $note,
|
|
'log_id' => $log->id,
|
|
'settings' => $settings,
|
|
];
|
|
|
|
|
|
// Send to the admin, if settings dictate
|
|
$recipient = new \App\Models\Recipients\AdminRecipient();
|
|
|
|
|
|
if (method_exists($target, 'notify')) {
|
|
$target->notify(new static::$checkoutClass($params));
|
|
}
|
|
|
|
if ($settings->admin_cc_email!='') {
|
|
$recipient->notify(new static::$checkoutClass($params));
|
|
}
|
|
|
|
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 <parallelgrapefruit@gmail.com
|
|
* @since [v3.4]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
public function logCheckin($target, $note)
|
|
{
|
|
$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;
|
|
}
|
|
|
|
|
|
$log->location_id = null;
|
|
$log->note = $note;
|
|
$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,
|
|
];
|
|
|
|
// Send to the admin, if settings dictate
|
|
$recipient = new \App\Models\Recipients\AdminRecipient();
|
|
|
|
$checkoutClass = null;
|
|
|
|
if (method_exists($target, 'notify')) {
|
|
$target->notify(new static::$checkinClass($params));
|
|
}
|
|
|
|
if ($settings->admin_cc_email!='') {
|
|
$recipient->notify(new static::$checkinClass($params));
|
|
}
|
|
|
|
return $log;
|
|
}
|
|
|
|
|
|
/**
|
|
* @author A. Gianotto <snipe@snipe.net>
|
|
* @since [v4.0]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
public function logAudit($note, $location_id)
|
|
{
|
|
$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->logaction('audit');
|
|
|
|
$params = [
|
|
'item' => $log->item,
|
|
'admin' => $log->user,
|
|
'location' => ($location) ? $location->name : '',
|
|
'note' => $note
|
|
];
|
|
Setting::getSettings()->notify(new AuditNotification($params));
|
|
|
|
return $log;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @author Daniel Meltzer <parallelgrapefruit@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 <parallelgrapefruit@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;
|
|
}
|
|
}
|