FEATURE #1367: add log of login and logout
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Enumerations;
|
||||||
|
|
||||||
|
use App\Models\ActionLogType;
|
||||||
|
|
||||||
|
class ActionLogTypesForLogger extends BaseEnumeration
|
||||||
|
{
|
||||||
|
protected static function init()
|
||||||
|
{
|
||||||
|
$types = ActionLogType::all(['string_id', 'id']);
|
||||||
|
|
||||||
|
foreach ($types as $type) {
|
||||||
|
self::push($type->string_id, $type->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use Illuminate\Auth\Events\Login;
|
||||||
|
|
||||||
|
class LogLogin extends Logger
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @param Login $event
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle(Login $event)
|
||||||
|
{
|
||||||
|
$parameters = [
|
||||||
|
'id' => $event->user->id,
|
||||||
|
'name' => $event->user->name,
|
||||||
|
'email' => $event->user->email
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->saveLog($parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for action name. Used only internally
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function getName()
|
||||||
|
{
|
||||||
|
return 'login';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use Illuminate\Auth\Events\Logout;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
|
||||||
|
class LogLogout extends Logger
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @param Logout $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle(Logout $event)
|
||||||
|
{
|
||||||
|
$parameters = [
|
||||||
|
'id' => $event->user->id,
|
||||||
|
'name' => $event->user->name,
|
||||||
|
'email' => $event->user->email
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->saveLog($parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for action name. Used only internally
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function getName()
|
||||||
|
{
|
||||||
|
return 'logout';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use App\Enumerations\ActionLogTypesForLogger;
|
||||||
|
use App\Events\Event;
|
||||||
|
use App\Models\ActionLog;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
abstract class Logger
|
||||||
|
{
|
||||||
|
private $actionTypes = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the event listener.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->actionTypes = ActionLogTypesForLogger::getWithTranslations();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut for loging events
|
||||||
|
*
|
||||||
|
* @param array $parameters
|
||||||
|
*/
|
||||||
|
protected function saveLog($parameters = [])
|
||||||
|
{
|
||||||
|
// try to translate string id into integer id
|
||||||
|
$typeId = $this->getTypeId();
|
||||||
|
|
||||||
|
// is user logged in and exists given type?
|
||||||
|
if (!Auth::check() || is_null($typeId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get user id
|
||||||
|
$userId = Auth::user()->id;
|
||||||
|
|
||||||
|
// setup object for insertion
|
||||||
|
$data = [
|
||||||
|
'user_id' => $userId,
|
||||||
|
'type_id' => $typeId,
|
||||||
|
'parameters' => json_encode((array)$parameters)
|
||||||
|
];
|
||||||
|
|
||||||
|
// create new logged event
|
||||||
|
ActionLog::create($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate string id to integer id
|
||||||
|
*
|
||||||
|
* @return int|null ID of action type when exists, null otherwise
|
||||||
|
*/
|
||||||
|
private function getTypeId()
|
||||||
|
{
|
||||||
|
$name = $this->getName();
|
||||||
|
if (!isset($this->actionTypes[$name])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->actionTypes[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for action name. Used only internally
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
abstract protected function getName();
|
||||||
|
}
|
||||||
@@ -9,8 +9,9 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
*/
|
*/
|
||||||
class ActionLog extends Model
|
class ActionLog extends Model
|
||||||
{
|
{
|
||||||
|
protected $table = 'action_log';
|
||||||
|
|
||||||
public $timestamps = true;
|
public $timestamps = false;
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'type_id',
|
'type_id',
|
||||||
|
|||||||
@@ -13,8 +13,12 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $listen = [
|
protected $listen = [
|
||||||
'App\Events\SomeEvent' => [
|
// internal Laravel events - should work as auth.login and auth.logout, however it doesn't...
|
||||||
'App\Listeners\EventListener',
|
'Illuminate\Auth\Events\Login' => [
|
||||||
|
'App\Listeners\LogLogin'
|
||||||
|
],
|
||||||
|
'Illuminate\Auth\Events\Logout' => [
|
||||||
|
'App\Listeners\LogLogout'
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class SeedActionLogTypes extends Seeder
|
|||||||
'login',
|
'login',
|
||||||
'logout',
|
'logout',
|
||||||
'userEdit',
|
'userEdit',
|
||||||
|
'createUser',
|
||||||
'newCustomField',
|
'newCustomField',
|
||||||
'editCustomField',
|
'editCustomField',
|
||||||
'deleteCustomField',
|
'deleteCustomField',
|
||||||
|
|||||||
Reference in New Issue
Block a user