Added save/update/delete observers

This should make it easier to handle action logging between the GUI and the API
This commit is contained in:
snipe 2017-06-15 20:54:14 -07:00
parent 99f65cbf69
commit 770092f23f
12 changed files with 374 additions and 69 deletions

View file

@ -90,7 +90,6 @@ class AccessoriesController extends Controller
// Was the accessory created?
if ($accessory->save()) {
$accessory->logCreate();
// Redirect to the new accessory page
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.create.success'));
}
@ -108,7 +107,6 @@ class AccessoriesController extends Controller
{
// Check if the accessory exists
if (is_null($item = Accessory::find($accessoryId))) {
// Redirect to the blogs management page
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
}
@ -131,9 +129,7 @@ class AccessoriesController extends Controller
*/
public function update(Request $request, $accessoryId = null)
{
// Check if the accessory exists
if (is_null($accessory = Accessory::find($accessoryId))) {
// Redirect to the accessory index page
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
}
@ -154,7 +150,6 @@ class AccessoriesController extends Controller
// Was the accessory updated?
if ($accessory->save()) {
// Redirect to the updated accessory page
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.update.success'));
}
return redirect()->back()->withInput()->withErrors($accessory->getErrors());
@ -169,9 +164,7 @@ class AccessoriesController extends Controller
*/
public function destroy(Request $request, $accessoryId)
{
// Check if the blog post exists
if (is_null($accessory = Accessory::find($accessoryId))) {
// Redirect to the blogs management page
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found'));
}
@ -182,7 +175,6 @@ class AccessoriesController extends Controller
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.assoc_users', array('count'=> $accessory->hasUsers())));
}
$accessory->delete();
// Redirect to the locations management page
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.delete.success'));
}

View file

@ -342,7 +342,7 @@ class AssetsController extends Controller
}
if ($asset->save()) {
$asset->logCreate();
if ($request->get('assigned_user')) {
$target = User::find(request('assigned_user'));
} elseif ($request->get('assigned_asset')) {

View file

@ -1198,20 +1198,10 @@ class AssetsController extends Controller
$update_array['requestable'] = e(Input::get('requestable'));
}
if (DB::table('assets')
DB::table('assets')
->where('id', $key)
->update($update_array)) {
$logAction = new Actionlog();
$logAction->item_type = Asset::class;
$logAction->item_id = $key;
$logAction->created_at = date("Y-m-d H:i:s");
->update($update_array);
if (Input::has('rtd_location_id')) {
$logAction->location_id = e(Input::get('rtd_location_id'));
}
$logAction->user_id = Auth::user()->id;
$logAction->logaction('update');
}
} // endforeach
return redirect()->to("hardware")->with('success', trans('admin/hardware/message.update.success'));
// no values given, nothing to update
@ -1238,17 +1228,9 @@ class AssetsController extends Controller
$update_array['deleted_at'] = date('Y-m-d H:i:s');
$update_array['assigned_to'] = null;
if (DB::table('assets')
DB::table('assets')
->where('id', $asset->id)
->update($update_array)) {
$logAction = new Actionlog();
$logAction->item_type = Asset::class;
$logAction->item_id = $asset->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('deleted');
}
->update($update_array);
} // endforeach
return redirect()->to("hardware")->with('success', trans('admin/hardware/message.delete.success'));

View file

@ -2,7 +2,6 @@
namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\Actionlog;
use App\Models\Company;
use App\Models\Component;
use App\Models\CustomField;
@ -12,7 +11,6 @@ use App\Models\Asset;
use Auth;
use Config;
use DB;
use DeepCopyTest\H;
use Input;
use Lang;
use Mail;
@ -79,10 +77,7 @@ class ComponentsController extends Controller
public function store()
{
$this->authorize('create', Component::class);
// create a new model instance
$component = new Component();
// Update the component data
$component->name = Input::get('name');
$component->category_id = Input::get('category_id');
$component->location_id = Input::get('location_id');
@ -95,10 +90,7 @@ class ComponentsController extends Controller
$component->qty = Input::get('qty');
$component->user_id = Auth::id();
// Was the component created?
if ($component->save()) {
$component->logCreate();
// Redirect to the new component page
return redirect()->route('components.index')->with('success', trans('admin/components/message.create.success'));
}
return redirect()->back()->withInput()->withErrors($component->getErrors());
@ -115,9 +107,7 @@ class ComponentsController extends Controller
*/
public function edit($componentId = null)
{
// Check if the component exists
if (is_null($item = Component::find($componentId))) {
// Redirect to the blogs management page
return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist'));
}
@ -141,9 +131,7 @@ class ComponentsController extends Controller
*/
public function update($componentId = null)
{
// Check if the blog post exists
if (is_null($component = Component::find($componentId))) {
// Redirect to the blogs management page
return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist'));
}

View file

@ -3,7 +3,6 @@
namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\Actionlog;
use App\Models\Company;
use App\Models\Consumable;
use App\Models\Setting;
@ -91,10 +90,7 @@ class ConsumablesController extends Controller
$consumable->qty = Input::get('qty');
$consumable->user_id = Auth::id();
// Was the consumable created?
if ($consumable->save()) {
$consumable->logCreate();
// Redirect to the new consumable page
return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.create.success'));
}
@ -113,9 +109,7 @@ class ConsumablesController extends Controller
*/
public function edit($consumableId = null)
{
// Check if the consumable exists
if (is_null($item = Consumable::find($consumableId))) {
// Redirect to the blogs management page
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
}
@ -175,9 +169,7 @@ class ConsumablesController extends Controller
*/
public function destroy($consumableId)
{
// Check if the blog post exists
if (is_null($consumable = Consumable::find($consumableId))) {
// Redirect to the blogs management page
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
}
$this->authorize($consumable);
@ -202,11 +194,7 @@ class ConsumablesController extends Controller
if (isset($consumable->id)) {
return view('consumables/view', compact('consumable'));
}
// Prepare the error message
$error = trans('admin/consumables/message.does_not_exist', compact('id'));
// Redirect to the user management page
return redirect()->route('consumables')->with('error', $error);
return redirect()->route('consumables')->with('error', trans('admin/consumables/message.does_not_exist', compact('id')));
}
/**
@ -220,13 +208,10 @@ class ConsumablesController extends Controller
*/
public function getCheckout($consumableId)
{
// Check if the consumable exists
if (is_null($consumable = Consumable::find($consumableId))) {
// Redirect to the consumable management page with error
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
}
$this->authorize('checkout', $consumable);
// Get the dropdown of users and then pass it to the checkout view
return view('consumables/checkout', compact('consumable'))->with('users_list', Helper::usersList());
}
@ -241,9 +226,7 @@ class ConsumablesController extends Controller
*/
public function postCheckout($consumableId)
{
// Check if the consumable exists
if (is_null($consumable = Consumable::find($consumableId))) {
// Redirect to the consumable management page with error
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
}
@ -252,13 +235,13 @@ class ConsumablesController extends Controller
$admin_user = Auth::user();
$assigned_to = e(Input::get('assigned_to'));
// Check if the user exists
// Check if the user exists
if (is_null($user = User::find($assigned_to))) {
// Redirect to the consumable management page with error
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.user_does_not_exist'));
}
// Update the consumable data
// Update the consumable data
$consumable->assigned_to = e(Input::get('assigned_to'));
$consumable->users()->attach($consumable->id, [

View file

@ -113,10 +113,7 @@ class LicensesController extends Controller
$license->termination_date = $request->input('termination_date');
$license->user_id = Auth::id();
// Was the license created?
if ($license->save()) {
$license->logCreate();
// Save the license seat data
return redirect()->route("licenses.index")->with('success', trans('admin/licenses/message.create.success'));
}
return redirect()->back()->withInput()->withErrors($license->getErrors());
@ -167,15 +164,12 @@ class LicensesController extends Controller
*/
public function update(Request $request, $licenseId = null)
{
// Check if the license exists
if (is_null($license = License::find($licenseId))) {
// Redirect to the blogs management page
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.does_not_exist'));
}
$this->authorize('update', $license);
// Update the license data
$license->company_id = Company::getIdForCurrentUser($request->input('company_id'));
$license->depreciation_id = $request->input('depreciation_id');
$license->expiration_date = $request->input('expiration_date');

View file

@ -0,0 +1,70 @@
<?php
namespace App\Observers;
use App\Models\Accessory;
use App\Models\Setting;
use App\Models\Actionlog;
use Auth;
class AccessoryObserver
{
/**
* Listen to the User created event.
*
* @param Accessory $accessory
* @return void
*/
public function updated(Accessory $accessory)
{
$logAction = new Actionlog();
$logAction->item_type = Accessory::class;
$logAction->item_id = $accessory->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('update');
}
/**
* Listen to the Accessory created event, and increment
* the next_auto_tag_base value in the settings table when i
* a new accessory is created.
*
* @param Accessory $accessory
* @return void
*/
public function created(Accessory $accessory)
{
$settings = Setting::first();
$settings->increment('next_auto_tag_base');
\Log::debug('Setting new next_auto_tag_base value');
$logAction = new Actionlog();
$logAction->item_type = Accessory::class;
$logAction->item_id = $accessory->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('create');
}
/**
* Listen to the Accessory deleting event.
*
* @param Accessory $accessory
* @return void
*/
public function deleting(Accessory $accessory)
{
$logAction = new Actionlog();
$logAction->item_type = Accessory::class;
$logAction->item_id = $accessory->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('delete');
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace App\Observers;
use App\Models\Asset;
use App\Models\Setting;
use App\Models\Actionlog;
use Auth;
class AssetObserver
{
/**
* Listen to the User created event.
*
* @param Asset $asset
* @return void
*/
public function updated(Asset $asset)
{
$logAction = new Actionlog();
$logAction->item_type = Asset::class;
$logAction->item_id = $asset->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('update');
}
/**
* Listen to the Asset created event, and increment
* the next_auto_tag_base value in the settings table when i
* a new asset is created.
*
* @param Asset $asset
* @return void
*/
public function created(Asset $asset)
{
$settings = Setting::first();
$settings->increment('next_auto_tag_base');
\Log::debug('Setting new next_auto_tag_base value');
$logAction = new Actionlog();
$logAction->item_type = Asset::class;
$logAction->item_id = $asset->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('create');
}
/**
* Listen to the Asset deleting event.
*
* @param Asset $asset
* @return void
*/
public function deleting(Asset $asset)
{
$logAction = new Actionlog();
$logAction->item_type = Asset::class;
$logAction->item_id = $asset->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('delete');
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace App\Observers;
use App\Models\Component;
use App\Models\Setting;
use App\Models\Actionlog;
use Auth;
class ComponentObserver
{
/**
* Listen to the User created event.
*
* @param Component $component
* @return void
*/
public function updated(Component $component)
{
$logAction = new Actionlog();
$logAction->item_type = Component::class;
$logAction->item_id = $component->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('update');
}
/**
* Listen to the Component created event, and increment
* the next_auto_tag_base value in the settings table when i
* a new component is created.
*
* @param Component $component
* @return void
*/
public function created(Component $component)
{
$settings = Setting::first();
$settings->increment('next_auto_tag_base');
\Log::debug('Setting new next_auto_tag_base value');
$logAction = new Actionlog();
$logAction->item_type = Component::class;
$logAction->item_id = $component->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('create');
}
/**
* Listen to the Component deleting event.
*
* @param Component $component
* @return void
*/
public function deleting(Component $component)
{
$logAction = new Actionlog();
$logAction->item_type = Component::class;
$logAction->item_id = $component->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('delete');
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace App\Observers;
use App\Models\Consumable;
use App\Models\Setting;
use App\Models\Actionlog;
use Auth;
class ConsumableObserver
{
/**
* Listen to the User created event.
*
* @param Consumable $consumable
* @return void
*/
public function updated(Consumable $consumable)
{
$logAction = new Actionlog();
$logAction->item_type = Consumable::class;
$logAction->item_id = $consumable->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('update');
}
/**
* Listen to the Consumable created event, and increment
* the next_auto_tag_base value in the settings table when i
* a new consumable is created.
*
* @param Consumable $consumable
* @return void
*/
public function created(Consumable $consumable)
{
$settings = Setting::first();
$settings->increment('next_auto_tag_base');
\Log::debug('Setting new next_auto_tag_base value');
$logAction = new Actionlog();
$logAction->item_type = Consumable::class;
$logAction->item_id = $consumable->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('create');
}
/**
* Listen to the Consumable deleting event.
*
* @param Consumable $consumable
* @return void
*/
public function deleting(Consumable $consumable)
{
$logAction = new Actionlog();
$logAction->item_type = Consumable::class;
$logAction->item_id = $consumable->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('delete');
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace App\Observers;
use App\Models\License;
use App\Models\Setting;
use App\Models\Actionlog;
use Auth;
class LicenseObserver
{
/**
* Listen to the User created event.
*
* @param License $license
* @return void
*/
public function updated(License $license)
{
$logAction = new Actionlog();
$logAction->item_type = License::class;
$logAction->item_id = $license->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('update');
}
/**
* Listen to the License created event, and increment
* the next_auto_tag_base value in the settings table when i
* a new license is created.
*
* @param License $license
* @return void
*/
public function created(License $license)
{
$settings = Setting::first();
$settings->increment('next_auto_tag_base');
\Log::debug('Setting new next_auto_tag_base value');
$logAction = new Actionlog();
$logAction->item_type = License::class;
$logAction->item_id = $license->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('create');
}
/**
* Listen to the License deleting event.
*
* @param License $license
* @return void
*/
public function deleting(License $license)
{
$logAction = new Actionlog();
$logAction->item_type = License::class;
$logAction->item_id = $license->id;
$logAction->created_at = date("Y-m-d H:i:s");
$logAction->user_id = Auth::user()->id;
$logAction->logaction('delete');
}
}

View file

@ -6,6 +6,17 @@ use Illuminate\Support\ServiceProvider;
use DB;
use Log;
use Illuminate\Support\Facades\Schema;
use App\Observers\AssetObserver;
use App\Observers\LicenseObserver;
use App\Observers\AccessoryObserver;
use App\Observers\ConsumableObserver;
use App\Observers\ComponentObserver;
use App\Models\Asset;
use App\Models\License;
use App\Models\Accessory;
use App\Models\Consumable;
use App\Models\Component;
/**
* This service provider handles a few custom validation rules.
@ -26,6 +37,11 @@ class AppServiceProvider extends ServiceProvider
public function boot()
{
Schema::defaultStringLength(191);
Asset::observe(AssetObserver::class);
Accessory::observe(AccessoryObserver::class);
Component::observe(ComponentObserver::class);
Consumable::observe(ConsumableObserver::class);
License::observe(LicenseObserver::class);
// Email array validator
Validator::extend('email_array', function ($attribute, $value, $parameters, $validator) {