Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor logging via "Loggable" trait #15789

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions app/Enums/ActionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Enums;

enum ActionType: string {
case Restore = 'restore';
case TwoFactorReset = '2FA reset';
case CheckinFrom = 'checkin from';
case RequestCanceled = 'request canceled';
case Requested = 'requested';
case DeleteSeats = 'delete seats';
case AddSeats = 'add seats';
case Update = 'update';
case Create = 'create';
case Delete = 'delete';
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ public function update(UpdateAssetRequest $request, Asset $asset): JsonResponse
}
}
}
if ($asset->save()) {
if ($asset->save()) { //kerplow - this is blowing *something* up?
if (($request->filled('assigned_user')) && ($target = User::find($request->get('assigned_user')))) {
$location = $target->location_id;
} elseif (($request->filled('assigned_asset')) && ($target = Asset::find($request->get('assigned_asset')))) {
Expand Down
10 changes: 2 additions & 8 deletions app/Http/Controllers/Api/ManufacturersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,9 @@ public function restore($id) : JsonResponse
return response()->json(Helper::formatStandardApiResponse('error', trans('general.not_deleted', ['item_type' => trans('general.manufacturer')])), 200);
}

$manufacturer->setLogMessage('restore');
if ($manufacturer->restore()) {

$logaction = new Actionlog();
$logaction->item_type = Manufacturer::class;
$logaction->item_id = $manufacturer->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->created_by = auth()->id();
$logaction->logaction('restore');


return response()->json(Helper::formatStandardApiResponse('success', trans('admin/manufacturers/message.restore.success')), 200);
}

Expand Down
22 changes: 4 additions & 18 deletions app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use App\Enums\ActionType;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\SaveUserRequest;
Expand Down Expand Up @@ -689,17 +690,8 @@ public function postTwoFactorReset(Request $request) : JsonResponse
$this->authorize('update', $user);
$user->two_factor_secret = null;
$user->two_factor_enrolled = 0;
$user->saveQuietly();

// Log the reset
$logaction = new Actionlog();
$logaction->target_type = User::class;
$logaction->target_id = $user->id;
$logaction->item_type = User::class;
$logaction->item_id = $user->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->created_by = auth()->id();
$logaction->logaction('2FA reset');
$user->setLogMessage(ActionType::TwoFactorReset);
$user->save();

return response()->json(['message' => trans('admin/settings/general.two_factor_reset_success')], 200);
} catch (\Exception $e) {
Expand Down Expand Up @@ -742,15 +734,9 @@ public function restore($userId) : JsonResponse
return response()->json(Helper::formatStandardApiResponse('error', trans('general.not_deleted', ['item_type' => trans('general.user')])), 200);
}

$user->setLogMessage(ActionType::Restore);
if ($user->restore()) {

$logaction = new Actionlog();
$logaction->item_type = User::class;
$logaction->item_id = $user->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->created_by = auth()->id();
$logaction->logaction('restore');

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/users/message.success.restored')), 200);
}

Expand Down
9 changes: 2 additions & 7 deletions app/Http/Controllers/AssetModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Enums\ActionType;
use App\Helpers\Helper;
use App\Http\Requests\ImageUploadRequest;
use App\Http\Requests\StoreAssetModelRequest;
Expand Down Expand Up @@ -232,14 +233,8 @@ public function getRestore($id) : RedirectResponse
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.asset_model')]));
}

$model->setLogMessage(ActionType::Restore);
if ($model->restore()) {
$logaction = new Actionlog();
$logaction->item_type = AssetModel::class;
$logaction->item_id = $model->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->created_by = auth()->id();
$logaction->logaction('restore');


// Redirect them to the deleted page if there are more, otherwise the section index
$deleted_models = AssetModel::onlyTrashed()->count();
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Controllers/LocationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Enums\ActionType;
use App\Http\Requests\ImageUploadRequest;
use App\Models\Actionlog;
use App\Models\Asset;
Expand Down Expand Up @@ -273,13 +274,8 @@ public function postRestore($id) : RedirectResponse
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.location')]));
}

$location->setLogMessage(ActionType::Restore);
if ($location->restore()) {
$logaction = new Actionlog();
$logaction->item_type = Location::class;
$logaction->item_id = $location->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->created_by = auth()->id();
$logaction->logaction('restore');

return redirect()->route('locations.index')->with('success', trans('admin/locations/message.restore.success'));
}
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Controllers/ManufacturersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Enums\ActionType;
use App\Http\Requests\ImageUploadRequest;
use App\Models\Actionlog;
use App\Models\Manufacturer;
Expand Down Expand Up @@ -214,13 +215,8 @@ public function restore($id) : RedirectResponse
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.manufacturer')]));
}

$manufacturer->setLogMessage(ActionType::Restore);
if ($manufacturer->restore()) {
$logaction = new Actionlog();
$logaction->item_type = Manufacturer::class;
$logaction->item_id = $manufacturer->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->created_by = auth()->id();
$logaction->logaction('restore');

// Redirect them to the deleted page if there are more, otherwise the section index
$deleted_manufacturers = Manufacturer::onlyTrashed()->count();
Expand Down
69 changes: 38 additions & 31 deletions app/Http/Controllers/Users/BulkUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Users;

use App\Enums\ActionType;
use App\Events\UserMerged;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
Expand Down Expand Up @@ -311,49 +312,52 @@ public function destroy(Request $request)
protected function logItemCheckinAndDelete($items, $itemType)
{
foreach ($items as $item) {
$item_id = $item->id;
$logAction = new Actionlog();

if ($itemType == License::class){
$item_id = $item->license_id;
\Log::error("ITEM DUMP: ".print_r($item, true));
if (gettype($item) == 'object' && get_class($item) != 'stdClass') {
$real_item = $item;
} else {
$item_id = $item->id;

if ($itemType == License::class) {
$item_id = $item->license_id; //FIXME - funkery happening here
$real_item = License::find($item->license_id);
} else {
$real_item = (new $itemType())::find($item_id);
}
}
if (property_exists($item, 'assigned_type')) {
$assigned_to = (new ($item->assigned_type))::find($item->assigned_to);
} else {
$assigned_to = User::find($item->assigned_to);
}

$logAction->item_id = $item_id;
// We can't rely on get_class here because the licenses/accessories fetched above are not eloquent models, but simply arrays.
$logAction->item_type = $itemType;
$logAction->target_id = $item->assigned_to;
$logAction->target_type = User::class;
$logAction->created_at = auth()->id();
$logAction->note = 'Bulk checkin items';
$logAction->logaction('checkin from');
$real_item->setTarget($assigned_to); // will this work?!!?!?!?
//$logAction->target_id = $item->assigned_to;
//$logAction->target_type = User::class;
$real_item->setNote('Bulk checkin items');
$real_item->setLogMessage(ActionType::CheckinFrom);
$real_item->logWithoutSave(ActionType::CheckinFrom);
}
}

private function logAccessoriesCheckin(Collection $accessoryUserRows): void
{
foreach ($accessoryUserRows as $accessoryUserRow) {
$logAction = new Actionlog();
$logAction->item_id = $accessoryUserRow->accessory_id;
$logAction->item_type = Accessory::class;
$logAction->target_id = $accessoryUserRow->assigned_to;
$logAction->target_type = User::class;
$logAction->created_at = auth()->id();
$logAction->note = 'Bulk checkin items';
$logAction->logaction('checkin from');
$accessory = Accessory::find($accessoryUserRow->accessory_id);
$accessory->setTarget(User::find($accessoryUserRow->assigned_to)); //FIXME - what if accessory was checked out to location?
$accessory->setNote('Bulk checkin items');
$accessory->logWithoutSave(ActionType::CheckinFrom);
}
}

private function logConsumablesCheckin(Collection $consumableUserRows): void
{
\Log::error("Logging Consumables Checkin!");
foreach ($consumableUserRows as $consumableUserRow) {
$logAction = new Actionlog();
$logAction->item_id = $consumableUserRow->consumable_id;
$logAction->item_type = Consumable::class;
$logAction->target_id = $consumableUserRow->assigned_to;
$logAction->target_type = User::class;
$logAction->created_at = auth()->id();
$logAction->note = 'Bulk checkin items';
$logAction->logaction('checkin from');
$consumable = Consumable::find($consumableUserRow->consumable_id);
$consumable->setTarget(User::find($consumableUserRow->assigned_to));
$consumable->setNote('Bulk checkin items');
$consumable->logWithoutSave(ActionType::CheckinFrom);
}
}

Expand Down Expand Up @@ -431,9 +435,12 @@ public function merge(Request $request)
$managedLocation->save();
}

$user_to_merge->delete();
$user_to_merge->setNote('Deleting user cuz he was MERGED');
$user_to_merge->delete(); //BELETED!
\Log::error("User has been DELETED!!!!!!!!!!!");
\Log::error("User's action log is: ".print_r($user_to_merge->userlog()->get()->toArray(), true));

event(new UserMerged($user_to_merge, $merge_into_user, $admin));
event(new UserMerged($user_to_merge, $merge_into_user, $admin)); //HATE

}

Expand Down
9 changes: 2 additions & 7 deletions app/Http/Controllers/Users/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Users;

use App\Enums\ActionType;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\DeleteUserRequest;
Expand Down Expand Up @@ -367,14 +368,8 @@ public function getRestore($id = null)
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.user')]));
}

$user->setLogMessage(ActionType::Restore);
if ($user->restore()) {
$logaction = new Actionlog();
$logaction->item_type = User::class;
$logaction->item_id = $user->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->created_by = auth()->id();
$logaction->logaction('restore');

// Redirect them to the deleted page if there are more, otherwise the section index
$deleted_users = User::onlyTrashed()->count();
if ($deleted_users > 0) {
Expand Down
56 changes: 28 additions & 28 deletions app/Http/Controllers/ViewAssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Enums\ActionType;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\AssetModel;
Expand Down Expand Up @@ -93,17 +94,18 @@ public function getRequestItem(Request $request, $itemType, $itemId = null, $can

$user = auth()->user();

$logaction = new Actionlog();
$logaction->item_id = $data['asset_id'] = $item->id;
$logaction->item_type = $fullItemType;
$logaction->created_at = $data['requested_date'] = date('Y-m-d H:i:s');

//$logaction = new Actionlog();
//$logaction->item_id = $data['asset_id'] = $item->id;
//$logaction->item_type = $fullItemType;
//$logaction->created_at = $data['requested_date'] = date('Y-m-d H:i:s');
$data['requested_date'] = date('Y-m-d H:i:s');
if ($user->location_id) {
$logaction->location_id = $user->location_id;
$item->setLocation($user->location);
}

$logaction->target_id = $data['user_id'] = auth()->id();
$logaction->target_type = User::class;
$item->setTarget($user);
//$logaction->target_id = $data['user_id'] = auth()->id();
//$logaction->target_type = User::class;

$data['item_quantity'] = $request->has('request-quantity') ? e($request->input('request-quantity')) : 1;
$data['requested_by'] = $user->present()->fullName();
Expand All @@ -122,17 +124,17 @@ public function getRequestItem(Request $request, $itemType, $itemId = null, $can
if (($item_request = $item->isRequestedBy($user)) || $cancel_by_admin) {
$item->cancelRequest($requestingUser);
$data['item_quantity'] = ($item_request) ? $item_request->qty : 1;
$logaction->logaction('request_canceled');
$item->logWithoutSave(ActionType::RequestCanceled);

if (($settings->alert_email != '') && ($settings->alerts_enabled == '1') && (! config('app.lock_passwords'))) {
$settings->notify(new RequestAssetCancelation($data));
}

return redirect()->back()->with('success')->with('success', trans('admin/hardware/message.requests.canceled'));
} else {
$item->request();
$item->request(); //!!!!!!!!!!!!!
if (($settings->alert_email != '') && ($settings->alerts_enabled == '1') && (! config('app.lock_passwords'))) {
$logaction->logaction('requested');
$item->logWithoutSave(ActionType::Requested);
$settings->notify(new RequestAssetNotification($data));
}

Expand Down Expand Up @@ -163,37 +165,35 @@ public function getRequestAsset($assetId = null) : RedirectResponse
$data['item_quantity'] = 1;
$settings = Setting::getSettings();

$logaction = new Actionlog();
$logaction->item_id = $data['asset_id'] = $asset->id;
$logaction->item_type = $data['item_type'] = Asset::class;
$logaction->created_at = $data['requested_date'] = date('Y-m-d H:i:s');
//$logaction = new Actionlog();
$data['asset_id'] = $asset->id;
$data['item_type'] = Asset::class;
$data['requested_date'] = date('Y-m-d H:i:s');

if ($user->location_id) {
$logaction->location_id = $user->location_id;
}
$logaction->target_id = $data['user_id'] = auth()->id();
$logaction->target_type = User::class;
$asset->setLocation = $user->location;
$asset->setTarget($user);
$data['user_id'] = auth()->id();

// If it's already requested, cancel the request.
if ($asset->isRequestedBy(auth()->user())) {
$asset->cancelRequest();
$asset->decrement('requests_counter', 1);
$asset->cancelRequest(); //wait, what?
$asset->decrement('requests_counter', 1); //this too

$logaction->logaction('request canceled');
$asset->logWithoutSave(ActionType::RequestCanceled);
try {
$settings->notify(new RequestAssetCancelation($data));
$settings->notify(new RequestAssetCancelation($data)); //and probably this
} catch (\Exception $e) {
Log::warning($e);
}
return redirect()->route('requestable-assets')
->with('success')->with('success', trans('admin/hardware/message.requests.canceled'));
}

$logaction->logaction('requested');
$asset->request();
$asset->increment('requests_counter', 1);
$asset->logWithoutSave(ActionType::Requested); //ARGH
$asset->request(); //HERE <-
$asset->increment('requests_counter', 1); //ARGH
try {
$settings->notify(new RequestAssetNotification($data));
$settings->notify(new RequestAssetNotification($data)); // ANd this.
} catch (\Exception $e) {
Log::warning($e);
}
Expand Down
Loading
Loading