ajout app
This commit is contained in:
0
SNIPE-IT/app/Listeners/.gitkeep
Normal file
0
SNIPE-IT/app/Listeners/.gitkeep
Normal file
264
SNIPE-IT/app/Listeners/CheckoutableListener.php
Normal file
264
SNIPE-IT/app/Listeners/CheckoutableListener.php
Normal file
@ -0,0 +1,264 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\CheckoutableCheckedOut;
|
||||
use App\Models\Accessory;
|
||||
use App\Models\Asset;
|
||||
use App\Models\CheckoutAcceptance;
|
||||
use App\Models\Component;
|
||||
use App\Models\Consumable;
|
||||
use App\Models\LicenseSeat;
|
||||
use App\Models\Recipients\AdminRecipient;
|
||||
use App\Models\Setting;
|
||||
use App\Notifications\CheckinAccessoryNotification;
|
||||
use App\Notifications\CheckinAssetNotification;
|
||||
use App\Notifications\CheckinLicenseSeatNotification;
|
||||
use App\Notifications\CheckoutAccessoryNotification;
|
||||
use App\Notifications\CheckoutAssetNotification;
|
||||
use App\Notifications\CheckoutConsumableNotification;
|
||||
use App\Notifications\CheckoutLicenseSeatNotification;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Exception;
|
||||
use Log;
|
||||
|
||||
class CheckoutableListener
|
||||
{
|
||||
private array $skipNotificationsFor = [
|
||||
Component::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* Notify the user and post to webhook about the checked out checkoutable
|
||||
* and add a record to the checkout_requests table.
|
||||
*/
|
||||
public function onCheckedOut($event)
|
||||
{
|
||||
if ($this->shouldNotSendAnyNotifications($event->checkoutable)){
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a checkout acceptance and attach it in the notification
|
||||
*/
|
||||
$acceptance = $this->getCheckoutAcceptance($event);
|
||||
|
||||
try {
|
||||
if (! $event->checkedOutTo->locale) {
|
||||
Notification::locale(Setting::getSettings()->locale)->send(
|
||||
$this->getNotifiables($event),
|
||||
$this->getCheckoutNotification($event, $acceptance)
|
||||
);
|
||||
} else {
|
||||
Notification::send(
|
||||
$this->getNotifiables($event),
|
||||
$this->getCheckoutNotification($event, $acceptance)
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->shouldSendWebhookNotification()) {
|
||||
|
||||
//slack doesn't include the url in its messaging format so this is needed to hit the endpoint
|
||||
|
||||
if(Setting::getSettings()->webhook_selected =='slack' || Setting::getSettings()->webhook_selected =='general') {
|
||||
|
||||
|
||||
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
|
||||
->notify($this->getCheckoutNotification($event));
|
||||
}
|
||||
}
|
||||
} catch (ClientException $e) {
|
||||
Log::warning("Exception caught during checkout notification: " . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Log::warning("Exception caught during checkout notification: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the user and post to webhook about the checked in checkoutable
|
||||
*/
|
||||
public function onCheckedIn($event)
|
||||
{
|
||||
\Log::debug('onCheckedIn in the Checkoutable listener fired');
|
||||
|
||||
if ($this->shouldNotSendAnyNotifications($event->checkoutable)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the appropriate notification
|
||||
*/
|
||||
if ($event->checkedOutTo && $event->checkoutable){
|
||||
$acceptances = CheckoutAcceptance::where('checkoutable_id', $event->checkoutable->id)
|
||||
->where('assigned_to_id', $event->checkedOutTo->id)
|
||||
->get();
|
||||
|
||||
foreach($acceptances as $acceptance){
|
||||
if($acceptance->isPending()){
|
||||
$acceptance->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Use default locale
|
||||
if (! $event->checkedOutTo->locale) {
|
||||
Notification::locale(Setting::getSettings()->locale)->send(
|
||||
$this->getNotifiables($event),
|
||||
$this->getCheckinNotification($event)
|
||||
);
|
||||
} else {
|
||||
Notification::send(
|
||||
$this->getNotifiables($event),
|
||||
$this->getCheckinNotification($event)
|
||||
);
|
||||
}
|
||||
//slack doesn't include the url in its messaging format so this is needed to hit the endpoint
|
||||
if(Setting::getSettings()->webhook_selected =='slack' || Setting::getSettings()->webhook_selected =='general') {
|
||||
|
||||
if ($this->shouldSendWebhookNotification()) {
|
||||
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
|
||||
->notify($this->getCheckinNotification($event));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ClientException $e) {
|
||||
Log::warning("Exception caught during checkout notification: " . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Log::warning("Exception caught during checkin notification: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a checkout acceptance
|
||||
* @param Event $event
|
||||
* @return mixed
|
||||
*/
|
||||
private function getCheckoutAcceptance($event)
|
||||
{
|
||||
if (! $event->checkoutable->requireAcceptance()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$acceptance = new CheckoutAcceptance;
|
||||
$acceptance->checkoutable()->associate($event->checkoutable);
|
||||
$acceptance->assignedTo()->associate($event->checkedOutTo);
|
||||
$acceptance->save();
|
||||
|
||||
return $acceptance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entities to be notified of the passed event
|
||||
*
|
||||
* @param Event $event
|
||||
* @return Collection
|
||||
*/
|
||||
private function getNotifiables($event)
|
||||
{
|
||||
$notifiables = collect();
|
||||
|
||||
/**
|
||||
* Notify who checked out the item as long as the model can route notifications
|
||||
*/
|
||||
if (method_exists($event->checkedOutTo, 'routeNotificationFor')) {
|
||||
$notifiables->push($event->checkedOutTo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify Admin users if the settings is activated
|
||||
*/
|
||||
if ((Setting::getSettings()) && (Setting::getSettings()->admin_cc_email != '')) {
|
||||
$notifiables->push(new AdminRecipient());
|
||||
}
|
||||
|
||||
return $notifiables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the appropriate notification for the event
|
||||
*
|
||||
* @param CheckoutableCheckedIn $event
|
||||
* @return Notification
|
||||
*/
|
||||
private function getCheckinNotification($event)
|
||||
{
|
||||
|
||||
$notificationClass = null;
|
||||
|
||||
switch (get_class($event->checkoutable)) {
|
||||
case Accessory::class:
|
||||
$notificationClass = CheckinAccessoryNotification::class;
|
||||
break;
|
||||
case Asset::class:
|
||||
$notificationClass = CheckinAssetNotification::class;
|
||||
break;
|
||||
case LicenseSeat::class:
|
||||
$notificationClass = CheckinLicenseSeatNotification::class;
|
||||
break;
|
||||
}
|
||||
|
||||
\Log::debug('Notification class: '.$notificationClass);
|
||||
|
||||
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the appropriate notification for the event
|
||||
*
|
||||
* @param CheckoutableCheckedOut $event
|
||||
* @param CheckoutAcceptance|null $acceptance
|
||||
* @return Notification
|
||||
*/
|
||||
private function getCheckoutNotification($event, $acceptance = null)
|
||||
{
|
||||
$notificationClass = null;
|
||||
|
||||
switch (get_class($event->checkoutable)) {
|
||||
case Accessory::class:
|
||||
$notificationClass = CheckoutAccessoryNotification::class;
|
||||
break;
|
||||
case Asset::class:
|
||||
$notificationClass = CheckoutAssetNotification::class;
|
||||
break;
|
||||
case Consumable::class:
|
||||
$notificationClass = CheckoutConsumableNotification::class;
|
||||
break;
|
||||
case LicenseSeat::class:
|
||||
$notificationClass = CheckoutLicenseSeatNotification::class;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the listeners for the subscriber.
|
||||
*
|
||||
* @param Illuminate\Events\Dispatcher $events
|
||||
*/
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen(
|
||||
\App\Events\CheckoutableCheckedIn::class,
|
||||
'App\Listeners\CheckoutableListener@onCheckedIn'
|
||||
);
|
||||
|
||||
$events->listen(
|
||||
\App\Events\CheckoutableCheckedOut::class,
|
||||
'App\Listeners\CheckoutableListener@onCheckedOut'
|
||||
);
|
||||
}
|
||||
|
||||
private function shouldNotSendAnyNotifications($checkoutable): bool
|
||||
{
|
||||
return in_array(get_class($checkoutable), $this->skipNotificationsFor);
|
||||
}
|
||||
|
||||
private function shouldSendWebhookNotification(): bool
|
||||
{
|
||||
return Setting::getSettings() && Setting::getSettings()->webhook_endpoint;
|
||||
}
|
||||
}
|
44
SNIPE-IT/app/Listeners/LogFailedLogin.php
Normal file
44
SNIPE-IT/app/Listeners/LogFailedLogin.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Illuminate\Auth\Events\Failed;
|
||||
|
||||
class LogFailedLogin
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param \Illuminate\Auth\Events\Failed $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Failed $event)
|
||||
{
|
||||
$now = new Carbon();
|
||||
try {
|
||||
DB::table('login_attempts')->insert(
|
||||
[
|
||||
'username' => $event->credentials['username'],
|
||||
'user_agent' => request()->header('User-Agent'),
|
||||
'remote_ip' => request()->ip(),
|
||||
'successful' => 0,
|
||||
'created_at' => $now,
|
||||
]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
\Log::debug($e);
|
||||
}
|
||||
}
|
||||
}
|
152
SNIPE-IT/app/Listeners/LogListener.php
Normal file
152
SNIPE-IT/app/Listeners/LogListener.php
Normal file
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\AccessoryCheckedIn;
|
||||
use App\Events\AccessoryCheckedOut;
|
||||
use App\Events\AssetCheckedIn;
|
||||
use App\Events\AssetCheckedOut;
|
||||
use App\Events\CheckoutableCheckedIn;
|
||||
use App\Events\CheckoutableCheckedOut;
|
||||
use App\Events\CheckoutAccepted;
|
||||
use App\Events\CheckoutDeclined;
|
||||
use App\Events\ComponentCheckedIn;
|
||||
use App\Events\ComponentCheckedOut;
|
||||
use App\Events\ConsumableCheckedOut;
|
||||
use App\Events\ItemAccepted;
|
||||
use App\Events\ItemDeclined;
|
||||
use App\Events\LicenseCheckedIn;
|
||||
use App\Events\LicenseCheckedOut;
|
||||
use App\Models\Actionlog;
|
||||
use App\Models\User;
|
||||
use App\Models\LicenseSeat;
|
||||
use App\Events\UserMerged;
|
||||
|
||||
class LogListener
|
||||
{
|
||||
/**
|
||||
* These onBlah methods are used by the subscribe() method further down in this file.
|
||||
* This one creates an action_logs entry for the checkin
|
||||
* @param CheckoutableCheckedIn $event
|
||||
* @return void
|
||||
*
|
||||
*/
|
||||
public function onCheckoutableCheckedIn(CheckoutableCheckedIn $event)
|
||||
{
|
||||
$event->checkoutable->logCheckin($event->checkedOutTo, $event->note, $event->action_date, $event->originalValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* These onBlah methods are used by the subscribe() method further down in this file.
|
||||
* This one creates an action_logs entry for the checkout
|
||||
*
|
||||
* @param CheckoutableCheckedOut $event
|
||||
* @return void
|
||||
*
|
||||
*/
|
||||
public function onCheckoutableCheckedOut(CheckoutableCheckedOut $event)
|
||||
{
|
||||
$event->checkoutable->logCheckout($event->note, $event->checkedOutTo, $event->checkoutable->last_checkout, $event->originalValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* These onBlah methods are used by the subscribe() method further down in this file.
|
||||
* This creates the entry in the action_logs table for the accept/decline action
|
||||
*/
|
||||
public function onCheckoutAccepted(CheckoutAccepted $event)
|
||||
{
|
||||
|
||||
\Log::debug('event passed to the onCheckoutAccepted listener:');
|
||||
$logaction = new Actionlog();
|
||||
$logaction->item()->associate($event->acceptance->checkoutable);
|
||||
$logaction->target()->associate($event->acceptance->assignedTo);
|
||||
$logaction->accept_signature = $event->acceptance->signature_filename;
|
||||
$logaction->filename = $event->acceptance->stored_eula_file;
|
||||
$logaction->action_type = 'accepted';
|
||||
|
||||
// TODO: log the actual license seat that was checked out
|
||||
if ($event->acceptance->checkoutable instanceof LicenseSeat) {
|
||||
$logaction->item()->associate($event->acceptance->checkoutable->license);
|
||||
}
|
||||
|
||||
$logaction->save();
|
||||
}
|
||||
|
||||
public function onCheckoutDeclined(CheckoutDeclined $event)
|
||||
{
|
||||
$logaction = new Actionlog();
|
||||
$logaction->item()->associate($event->acceptance->checkoutable);
|
||||
$logaction->target()->associate($event->acceptance->assignedTo);
|
||||
$logaction->accept_signature = $event->acceptance->signature_filename;
|
||||
$logaction->action_type = 'declined';
|
||||
|
||||
// TODO: log the actual license seat that was checked out
|
||||
if ($event->acceptance->checkoutable instanceof LicenseSeat) {
|
||||
$logaction->item()->associate($event->acceptance->checkoutable->license);
|
||||
}
|
||||
|
||||
$logaction->save();
|
||||
}
|
||||
|
||||
|
||||
public function onUserMerged(UserMerged $event)
|
||||
{
|
||||
|
||||
$to_from_array = [
|
||||
'to_id' => $event->merged_to->id,
|
||||
'to_username' => $event->merged_to->username,
|
||||
'from_id' => $event->merged_from->id,
|
||||
'from_username' => $event->merged_from->username,
|
||||
];
|
||||
|
||||
// Add a record to the users being merged FROM
|
||||
\Log::debug('Users merged: '.$event->merged_from->id .' ('.$event->merged_from->username.') merged into '. $event->merged_to->id. ' ('.$event->merged_to->username.')');
|
||||
$logaction = new Actionlog();
|
||||
$logaction->item_id = $event->merged_from->id;
|
||||
$logaction->item_type = User::class;
|
||||
$logaction->target_id = $event->merged_to->id;
|
||||
$logaction->target_type = User::class;
|
||||
$logaction->action_type = 'merged';
|
||||
$logaction->note = trans('general.merged_log_this_user_from', $to_from_array);
|
||||
$logaction->user_id = $event->admin->id;
|
||||
$logaction->save();
|
||||
|
||||
// Add a record to the users being merged TO
|
||||
$logaction = new Actionlog();
|
||||
$logaction->target_id = $event->merged_from->id;
|
||||
$logaction->target_type = User::class;
|
||||
$logaction->item_id = $event->merged_to->id;
|
||||
$logaction->item_type = User::class;
|
||||
$logaction->action_type = 'merged';
|
||||
$logaction->note = trans('general.merged_log_this_user_into', $to_from_array);
|
||||
$logaction->user_id = $event->admin->id;
|
||||
$logaction->save();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the listeners for the subscriber.
|
||||
*
|
||||
* @param Illuminate\Events\Dispatcher $events
|
||||
*/
|
||||
public function subscribe($events)
|
||||
{
|
||||
$list = [
|
||||
'CheckoutableCheckedIn',
|
||||
'CheckoutableCheckedOut',
|
||||
'CheckoutAccepted',
|
||||
'CheckoutDeclined',
|
||||
'UserMerged',
|
||||
];
|
||||
|
||||
foreach ($list as $event) {
|
||||
$events->listen(
|
||||
'App\Events\\'.$event,
|
||||
'App\Listeners\LogListener@on'.$event
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
45
SNIPE-IT/app/Listeners/LogSuccessfulLogin.php
Normal file
45
SNIPE-IT/app/Listeners/LogSuccessfulLogin.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Illuminate\Auth\Events\Login;
|
||||
|
||||
class LogSuccessfulLogin
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param Login $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Login $event)
|
||||
{
|
||||
$now = new Carbon();
|
||||
|
||||
try {
|
||||
DB::table('login_attempts')->insert(
|
||||
[
|
||||
'username' => $event->user->username,
|
||||
'user_agent' => request()->header('User-Agent'),
|
||||
'remote_ip' => request()->ip(),
|
||||
'successful' => 1,
|
||||
'created_at' => $now,
|
||||
]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
\Log::debug($e);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user