Skip to content
Merged
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
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
},
"require": {
"php": "^8.0",
"laravel/framework": "^9.0|^10.0",
"jenssegers/agent": "^2.6.4"
"laravel/framework": "^9.0|^10.0"
},
"require-dev": {
"pestphp/pest-plugin-laravel": "^1.4.0|^2.0.0",
Expand Down
10 changes: 5 additions & 5 deletions src/Middlewares/VisitMonitoringMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Binafy\LaravelUserMonitoring\Middlewares;

use Binafy\LaravelUserMonitoring\Utills\Detector;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Jenssegers\Agent\Agent;

class VisitMonitoringMiddleware
{
Expand All @@ -21,17 +21,17 @@ public function handle(Request $request, Closure $next): mixed
return $next($request);
}

$agent = new Agent();
$detector = new Detector();
$guard = config('user-monitoring.user.guard', 'web');
$exceptPages = config('user-monitoring.visit_monitoring.except_pages', []);

if (empty($exceptPages) || !$this->checkIsExceptPages($request->path(), $exceptPages)) {
// Store visit
DB::table(config('user-monitoring.visit_monitoring.table'))->insert([
'user_id' => auth($guard)->id(),
'browser_name' => $agent->browser(),
'platform' => $agent->platform(),
'device' => $agent->device(),
'browser_name' => $detector->getBrowser(),
'platform' => $detector->getBrowser(),
'device' => $detector->getDevice(),
'ip' => $request->ip(),
'page' => $request->url(),
'created_at' => now(),
Expand Down
29 changes: 12 additions & 17 deletions src/Providers/LaravelUserMonitoringEventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,53 @@

namespace Binafy\LaravelUserMonitoring\Providers;

use Binafy\LaravelUserMonitoring\Utills\Detector;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Jenssegers\Agent\Agent;

class LaravelUserMonitoringEventServiceProvider extends EventServiceProvider
{
public function boot()
public function boot(): void
{
$agent = new Agent();
$detector = new Detector();
$guard = config('user-monitoring.user.guard');
$table = config('user-monitoring.authentication_monitoring.table');

// Login Event
if (config('user-monitoring.authentication_monitoring.on_login', false)) {
Event::listen(function (Login $event) use ($agent, $guard, $table) {
Event::listen(function (Login $event) use ($detector, $guard, $table) {
DB::table($table)
->insert(
$this->insertData($guard, $agent, 'login'),
$this->insertData($guard, $detector, 'login'),
);
});
}

// Logout Event
if (config('user-monitoring.authentication_monitoring.on_logout', false)) {
Event::listen(function (Logout $event) use ($agent, $guard, $table) {
Event::listen(function (Logout $event) use ($detector, $guard, $table) {
DB::table($table)
->insert(
$this->insertData($guard, $agent, 'logout'),
$this->insertData($guard, $detector, 'logout'),
);
});
}
}

/**
* Insert data.
*
* @param string $guard
* @param Agent $agent
* @param string $actionType
* @return array
* Get insert data.
*/
private function insertData(string $guard, Agent $agent, string $actionType): array
private function insertData(string $guard, Detector $detector, string $actionType): array
{
return [
'user_id' => auth($guard)->id(),
'action_type' => $actionType,
'browser_name' => $agent->browser(),
'platform' => $agent->platform(),
'device' => $agent->device(),
'browser_name' => $detector->getBrowser(),
'platform' => $detector->getBrowser(),
'device' => $detector->getDevice(),
'ip' => request()->ip(),
'page' => request()->url(),
'created_at' => now(),
Expand Down
16 changes: 6 additions & 10 deletions src/Traits/Actionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Binafy\LaravelUserMonitoring\Traits;

use Binafy\LaravelUserMonitoring\Utills\ActionType;
use Binafy\LaravelUserMonitoring\Utills\Detector;
use Illuminate\Support\Facades\DB;
use Jenssegers\Agent\Agent;

trait Actionable
{
Expand Down Expand Up @@ -60,27 +60,23 @@ protected static function boot(): void

/**
* Insert action monitoring into DB.
*
* @param mixed $model
* @param string $actionType
* @return void
*/
private static function insertActionMonitoring(mixed $model, string $actionType): void
{
$agent = new Agent();
$detector = new Detector();
$guard = config('user-monitoring.user.guard');

DB::table(config('user-monitoring.action_monitoring.table'))->insert([
'user_id' => auth($guard)->id(),
'action_type' => $actionType,
'table_name' => $model->getTable(),
'browser_name' => $agent->browser(),
'platform' => $agent->platform(),
'device' => $agent->device(),
'browser_name' => $detector->getBrowser(),
'platform' => $detector->getBrowser(),
'device' => $detector->getDevice(),
'ip' => request()->ip(),
'page' => request()->url(),
'created_at' => now(),
'updated_at' => now(),
]);
}
}
}
74 changes: 74 additions & 0 deletions src/Utills/Detector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Binafy\LaravelUserMonitoring\Utills;

class Detector
{
/**
* An array of browser names.
*
* @var array
*/
public array $browserName = [
'Edge' => 'Edge',
'MSIE' => 'Internet Explorer',
'Trident' => 'Internet Explorer',
'Firefox' => 'Firefox',
'OPR' => 'Opera',
'Chrome' => 'Chrome',
'Safari' => 'Safari',
'Opera' => 'Opera',
];

/**
* An array of device names.
*
* @var array
*/
public array $deviceName = [
'/iPhone/i' => 'iPhone',
'/iPad/i' => 'iPad',
'/Android/i' => 'Android Device',
'/Windows/i' => 'Windows',
];

/**
* Get browser name.
*/
public function getBrowser(): string
{
if (PHP_SAPI === 'cli') {
return 'CLI';
}

$userAgent = $_SERVER['HTTP_USER_AGENT'];

foreach ($this->browserName as $key => $browser) {
if (str_contains($userAgent, $key)) {
return $browser;
}
}

return 'Unknown Browser';
}

/**
* Get device name.
*/
public function getDevice(): string
{
if (PHP_SAPI === 'cli') {
return 'CLI';
}

$userAgent = $_SERVER['HTTP_USER_AGENT'];

foreach ($this->deviceName as $pattern => $name) {
if (preg_match($pattern, $userAgent)) {
return $name;
}
}

return 'Unknown Device Name';
}
}
3 changes: 2 additions & 1 deletion tests/Feature/VisitMonitoringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@
// Ajax

test('visit monitoring store ajax requests', function () {
\Pest\Laravel\withoutExceptionHandling();
get('/', ['X-Requested-With' => 'XMLHttpRequest']);

// DB Assertions
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 1);
assertDatabaseHas(config('user-monitoring.visit_monitoring.table'), ['created_at' => now()]);
assertDatabaseHas(config('user-monitoring.visit_monitoring.table'), ['id' => 1]);
});

test('visit monitoring skip store when ajax mode is off for ajax requests', function () {
Expand Down