Skip to content
Closed
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
24 changes: 13 additions & 11 deletions app/Http/Controllers/Api/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Http\Controllers\Controller;
use App\Http\Transformers\ActionlogsTransformer;
use App\Models\Actionlog;
use App\Models\Location;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

Expand All @@ -26,22 +28,22 @@ public function index(Request $request) : JsonResponse | array
$actionlogs = $actionlogs->TextSearch(e($request->input('search')));
}


// If we want allll the logs
if (($request->filled('id')) && ($request->filled('type'))) {
$actionlogs = $actionlogs->ByTargetOrItem($request->input('id'), $request->input('type'));
}

// We've passed a target
if (($request->filled('target_type')) && ($request->filled('target_id'))) {
$actionlogs = $actionlogs->where('target_id', '=', $request->input('target_id'))
->where('target_type', '=', 'App\\Models\\'.ucwords($request->input('target_type')));
->where('target_type', '=', 'App\\Models\\' . ucwords($request->input('target_type')));
}

// We've passed an item
if (($request->filled('item_type')) && ($request->filled('item_id'))) {
$actionlogs = $actionlogs->where(function($query) use ($request)
{
$query->where('item_id', '=', $request->input('item_id'))
->where('item_type', '=', 'App\\Models\\'.ucwords($request->input('item_type')))
->orWhere(function($query) use ($request)
{
$query->where('target_id', '=', $request->input('item_id'))
->where('target_type', '=', 'App\\Models\\'.ucwords($request->input('item_type')));
});
});
$actionlogs = $actionlogs->where('item_id', '=', $request->input('item_id'))
->where('item_type', '=', 'App\\Models\\'.ucwords($request->input('item_type')));
}

if ($request->filled('action_type')) {
Expand Down
23 changes: 16 additions & 7 deletions app/Http/Controllers/Users/UserFilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,40 @@ public function store(UploadFileRequest $request, User $user)
{
$this->authorize('update', $user);
$files = $request->file('file');
$errors = 0;

if (is_null($files)) {
return redirect()->back()->with('error', trans('admin/users/message.upload.nofiles'));
}

foreach ($files as $file) {
$file_name = $request->handleFile('private_uploads/users/', 'user-'.$user->id, $file);
$file_name = $request->handleFile('private_uploads/users/', 'user-' . $user->id, $file);

//Log the uploaded file to the log
$logAction = new Actionlog();
$logAction->item_id = $user->id;
$logAction->item_type = User::class;
$logAction->created_by = auth()->id();
$logAction->note = $request->input('notes');
$logAction->target_id = null;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->target_type = User::class;
$logAction->target_id = $user->id;
$logAction->action_date = date("Y-m-d H:i:s");
$logAction->filename = $file_name;
$logAction->action_source = 'gui';
$logAction->user_agent = request()->header('User-Agent');
$logAction->remote_ip = request()->ip();
$logAction->action_type = 'uploaded';

if (! $logAction->save()) {
return JsonResponse::create(['error' => 'Failed validation: '.print_r($logAction->getErrors(), true)], 500);
if (!$logAction->save()) {
$errors++;
}

return redirect()->back()->withFragment('files')->with('success', trans('admin/users/message.upload.success'));
}

if ($errors > 0) {
return redirect()->back()->withFragment('files')->with('warning', 'Some files could not be uploaded');
}

return redirect()->back()->withFragment('files')->with('success', trans('admin/users/message.upload.success'));

}

Expand Down
2 changes: 2 additions & 0 deletions app/Listeners/LogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function onCheckoutAccepted(CheckoutAccepted $event)
$logaction->note = $event->acceptance->note;
$logaction->action_type = 'accepted';
$logaction->action_date = $event->acceptance->accepted_at;
$logaction->created_by = auth()->user()->id;

// TODO: log the actual license seat that was checked out
if ($event->acceptance->checkoutable instanceof LicenseSeat) {
Expand All @@ -84,6 +85,7 @@ public function onCheckoutDeclined(CheckoutDeclined $event)
$logaction->note = $event->acceptance->note;
$logaction->action_type = 'declined';
$logaction->action_date = $event->acceptance->declined_at;
$logaction->created_by = auth()->user()->id;

// TODO: log the actual license seat that was checked out
if ($event->acceptance->checkoutable instanceof LicenseSeat) {
Expand Down
16 changes: 16 additions & 0 deletions app/Models/Actionlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,20 @@ public function scopeOrderByCreatedBy($query, $order)
{
return $query->leftJoin('users as admin_sort', 'action_logs.created_by', '=', 'admin_sort.id')->select('action_logs.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
}

public function scopeByTargetOrItem($query, $id, $type) {

return $query->where(function($query) use ($id, $type)
{
$query->where('item_id', '=', $id)
->where('item_type', '=', 'App\\Models\\'.ucwords($type));
})
->orWhere(function($query) use ($id, $type)
{
$query->where('target_id', '=', $id)
->where('target_type', '=', 'App\\Models\\'.ucwords($type));
});

}

}
7 changes: 7 additions & 0 deletions app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,13 @@ public function uploads()
->orderBy('created_at', 'desc');
}

public function targetLogs()
{
return $this->hasMany('\App\Models\Actionlog', 'target_id')
->where('target_type', '=', Asset::class)
->orderBy('created_at', 'desc');
}

/**
* Determines whether the asset is checked out to a user
*
Expand Down
35 changes: 34 additions & 1 deletion app/Observers/AccessoryObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Accessory;
use App\Models\Actionlog;
use Illuminate\Support\Facades\Auth;

class AccessoryObserver
{
Expand All @@ -16,12 +15,43 @@ class AccessoryObserver
*/
public function updated(Accessory $accessory)
{
$attributes = $accessory->getAttributes();
$attributesOriginal = $accessory->getRawOriginal();
$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 (!$restoring_or_deleting) {
$changed = [];
foreach ($accessory->getRawOriginal() as $key => $value) {
if ((array_key_exists($key, $accessory->getAttributes())) && ($accessory->getRawOriginal()[$key] != $accessory->getAttributes()[$key])) {
$changed[$key]['old'] = $accessory->getRawOriginal()[$key];
$changed[$key]['new'] = $accessory->getAttributes()[$key];
}
}
}


if (empty($changed)){
return;
}

$logAction = new Actionlog();
$logAction->item_type = Accessory::class;
$logAction->item_id = $accessory->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->action_date = date('Y-m-d H:i:s');
$logAction->log_meta = json_encode($changed);
$logAction->logaction('update');



}

/**
Expand All @@ -38,6 +68,8 @@ public function created(Accessory $accessory)
$logAction->item_id = $accessory->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->action_date = date('Y-m-d H:i:s');

if($accessory->imported) {
$logAction->setActionSource('importer');
}
Expand All @@ -56,6 +88,7 @@ public function deleting(Accessory $accessory)
$logAction->item_type = Accessory::class;
$logAction->item_id = $accessory->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->action_date = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->logaction('delete');
}
Expand Down
3 changes: 1 addition & 2 deletions app/Observers/AssetObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Setting;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;

class AssetObserver
Expand Down Expand Up @@ -52,7 +51,7 @@ public function updating(Asset $asset)
$changed[$key]['old'] = $asset->getRawOriginal()[$key];
$changed[$key]['new'] = $asset->getAttributes()[$key];
}
}
}

if (empty($changed)){
return;
Expand Down
29 changes: 28 additions & 1 deletion app/Observers/ComponentObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Actionlog;
use App\Models\Component;
use Illuminate\Support\Facades\Auth;

class ComponentObserver
{
Expand All @@ -16,11 +15,37 @@ class ComponentObserver
*/
public function updated(Component $component)
{
$attributes = $component->getAttributes();
$attributesOriginal = $component->getRawOriginal();
$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 (!$restoring_or_deleting) {
$changed = [];
foreach ($component->getRawOriginal() as $key => $value) {
if ((array_key_exists($key, $component->getAttributes())) && ($component->getRawOriginal()[$key] != $component->getAttributes()[$key])) {
$changed[$key]['old'] = $component->getRawOriginal()[$key];
$changed[$key]['new'] = $component->getAttributes()[$key];
}
}
}


if (empty($changed)){
return;
}

$logAction = new Actionlog();
$logAction->item_type = Component::class;
$logAction->item_id = $component->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->action_date = date('Y-m-d H:i:s');
$logAction->log_meta = json_encode($changed);
$logAction->logaction('update');
}

Expand All @@ -38,6 +63,7 @@ public function created(Component $component)
$logAction->item_id = $component->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->action_date = date('Y-m-d H:i:s');
if($component->imported) {
$logAction->setActionSource('importer');
}
Expand All @@ -57,6 +83,7 @@ public function deleting(Component $component)
$logAction->item_id = $component->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->action_date = date('Y-m-d H:i:s');
$logAction->logaction('delete');
}
}
44 changes: 28 additions & 16 deletions app/Observers/ConsumableObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Actionlog;
use App\Models\Consumable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

Expand All @@ -19,25 +18,38 @@ class ConsumableObserver
public function updated(Consumable $consumable)
{

$changed = [];

foreach ($consumable->getRawOriginal() as $key => $value) {
// Check and see if the value changed
if ($consumable->getRawOriginal()[$key] != $consumable->getAttributes()[$key]) {
$changed[$key]['old'] = $consumable->getRawOriginal()[$key];
$changed[$key]['new'] = $consumable->getAttributes()[$key];
$attributes = $consumable->getAttributes();
$attributesOriginal = $consumable->getRawOriginal();
$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 (!$restoring_or_deleting) {
$changed = [];
foreach ($consumable->getRawOriginal() as $key => $value) {
if ((array_key_exists($key, $consumable->getAttributes())) && ($consumable->getRawOriginal()[$key] != $consumable->getAttributes()[$key])) {
$changed[$key]['old'] = $consumable->getRawOriginal()[$key];
$changed[$key]['new'] = $consumable->getAttributes()[$key];
}
}
}

if (count($changed) > 0) {
$logAction = new Actionlog();
$logAction->item_type = Consumable::class;
$logAction->item_id = $consumable->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');

if (empty($changed)){
return;
}

$logAction = new Actionlog();
$logAction->item_type = Consumable::class;
$logAction->item_id = $consumable->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->action_date = date('Y-m-d H:i:s');
$logAction->log_meta = json_encode($changed);
$logAction->logaction('update');
}

/**
Expand Down
Loading
Loading