mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-13 17:14:10 -08:00
5d4920c741
* Improvemenets to unit tests. * Break up modelfactory into multiple files, populate many states. * Begin testing validation at the unit test level, test relationships. * Add tests for Asset::availableForCheckout. * Model factories now generate all needed relationships on demand, which allows us to unit test with a empty database. * To faciliate the empty database, we move to using sqlite in memory as the unit testing database. * Fix bug with logs of checkouts to non users. * Fix location finding for assets. Also Fix location show page to show users associated with location. Still need some work to show assets. * More test and generator improvements * More unit test fixes. PermissionsTest is borked still. * More Updates * Rewrite permissionstest. Check that we have access on the model level rather than via web requests. Also test delete permissions. * Fix seeders. * Make the default asset model factory generate assets that are rtd for testing. * Save progress. * Rebase tests, fix department unit test, update database for functional tests. * Update functional and api tests to use new modelfactory signatures.
172 lines
4.9 KiB
PHP
172 lines
4.9 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\CheckinNotification;
|
|
use App\Notifications\CheckoutNotification;
|
|
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 = null /*target is overridable for components*/)
|
|
{
|
|
$log = new Actionlog;
|
|
|
|
// 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;
|
|
}
|
|
|
|
$log->user_id = Auth::user()->id;
|
|
|
|
// @FIXME This needs to be generalized with new asset checkout.
|
|
if(isset($target)) {
|
|
$log->target_type = get_class($target);
|
|
$log->target_id = $target->id;
|
|
} else {
|
|
if (!is_null($this->asset_id)) {
|
|
$log->target_type = Asset::class;
|
|
$log->target_id = $this->asset_id;
|
|
} elseif (!is_null($this->assigned_to)) {
|
|
$log->target_type = User::class;
|
|
$log->target_id = $this->assigned_to;
|
|
}
|
|
}
|
|
|
|
$item = call_user_func(array($log->target_type, 'find'), $log->target_id);
|
|
if($this->assignedTo) {
|
|
$item = $this->assignedTo;
|
|
}
|
|
$class = get_class($item);
|
|
if($class == Location::class) {
|
|
// We can checkout to a location
|
|
$log->location_id = $item->id;
|
|
} else if ($class== Asset::class) {
|
|
$log->location_id = $item->rtd_location_id;
|
|
} else {
|
|
$log->location_id = $item->location_id;
|
|
}
|
|
$log->note = $note;
|
|
$log->logaction('checkout');
|
|
|
|
$params = [
|
|
'item' => $log->item,
|
|
'target' => $log->target,
|
|
'admin' => $log->user,
|
|
'note' => $note
|
|
];
|
|
Setting::getSettings()->notify(new CheckoutNotification($params));
|
|
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* @author Daniel Meltzer <parallelgrapefruit@gmail.com
|
|
* @since [v3.4]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
public function logCheckin($target, $note)
|
|
{
|
|
$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 = [
|
|
'item' => $log->item,
|
|
'admin' => $log->user,
|
|
'note' => $note
|
|
];
|
|
Setting::getSettings()->notify(new CheckinNotification($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('created');
|
|
$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;
|
|
}
|
|
}
|