Skip to content

Commit acb866d

Browse files
authored
Merge pull request #57 from binafy/add-turn-off-guest-mode
[1.x] Add `guest` mode to config
2 parents 0e547b2 + 6aac745 commit acb866d

File tree

7 files changed

+188
-27
lines changed

7 files changed

+188
-27
lines changed

README.md

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
- [Turn ON-OFF](#turn-on-off)
2222
- [Views](#visit-monitoring-views)
2323
- [Ajax Requests](#ajax-requests)
24+
- [Visit Monitoring Guest Mode](#visit-monitoring-guest-mode)
2425
- [Action Monitoring](#action-monitoring)
2526
- [Views](#action-monitoring-views)
2627
- [Reverse Proxy Config](#action-monitoring-reverse-proxy-config)
28+
- [Action Monitoring Guest Mode](#action-monitoring-guest-mode)
2729
- [Authentication Monitoring](#authentication-monitoring)
2830
- [Views](#authentication-monitoring-views)
2931
- [How to use in big projects](#how-to-use-in-big-projects)
@@ -338,6 +340,27 @@ Maybe you may disable record visits for `Ajax` requests, you can use config to d
338340

339341
When set to false, Ajax requests will not be recorded.
340342

343+
<a name="visit-monitoring-guest-mode"></a>
344+
### Visit Monitoring Guest Mode
345+
346+
Determines whether to track and store `visits` for users who are not authenticated (guests).
347+
When set to `true`, the package will also monitor guest user activity.
348+
When set to `false`, only authenticated user visits will be recorded.
349+
350+
```php
351+
/*
352+
* Configuration settings for visit monitoring.
353+
*/
354+
'visit_monitoring' => [
355+
...
356+
357+
/*
358+
* Determines whether to store `visits` even when the user is not logged in.
359+
*/
360+
'guest_mode' => true,
361+
],
362+
```
363+
341364
<a name="action-monitoring"></a>
342365
## Action Monitoring
343366

@@ -385,48 +408,70 @@ If you want to disable some actions like created, you can use the config file:
385408

386409
![Action Monitoring Preview](/art/actions-monitoring/preview.png "Action Monitoring")
387410

388-
<a name="authentication-monitoring"></a>
389-
## Authentication Monitoring
411+
<a name="action-monitoring-reverse-proxy-config"></a>
412+
### Action Monitoring Reverse Proxy Config
390413

391-
Have you ever thought about monitoring the entry and exit of users of your application? Now you can :) <br>
392-
If you want to monitor users when logging in or logout of your application, you need to migrate the migrations to the config file and change true for monitoring authentication.
414+
If you are using Reverse Proxy (Nginx or Cloudflare), you can use config to get real IP from a specific header like `X-Real-IP` or `X-Forwarded-For`:
393415

394416
```php
395-
'authentication_monitoring' => [
417+
'action_monitoring' => [
396418
...
397419

398420
/*
399-
* Enable or disable monitoring of user login and logout events.
400-
* Set to true to track these actions, or false to disable.
421+
* If your application is behind a reverse proxy (e.g., Nginx or Cloudflare),
422+
* enable this setting to fetch the real client IP from the proxy headers.
401423
*/
402-
'on_login' => true,
403-
'on_logout' => true,
424+
'use_reverse_proxy_ip' => false,
425+
426+
/*
427+
* The header used by reverse proxies to forward the real client IP.
428+
* Common values are 'X-Forwarded-For' or 'X-Real-IP'.
429+
*/
430+
'real_ip_header' => 'X-Forwarded-For',
404431
],
405432
```
406433

407-
<a name="action-monitoring-reverse-proxy-config"></a>
408-
### Action Monitoring Reverse Proxy Config
434+
<a name="action-monitoring-guest-mode"></a>
435+
### Action Monitoring Guest Mode
409436

410-
If you are using Reverse Proxy (Nginx or Cloudflare), you can use config to get real IP from a specific header like `X-Real-IP` or `X-Forwarded-For`:
437+
Determines whether to track and store `actions` for users who are not authenticated (guests).
438+
When set to `true`, the package will also monitor guest user activity.
439+
When set to `false`, only authenticated user visits will be recorded.
411440

412441
```php
442+
/*
443+
* Configuration settings for action monitoring.
444+
*/
413445
'action_monitoring' => [
414446
...
415447

416448
/*
417-
* If your application is behind a reverse proxy (e.g., Nginx or Cloudflare),
418-
* enable this setting to fetch the real client IP from the proxy headers.
449+
* Determines whether to store `actions` even when the user is not logged in.
419450
*/
420-
'use_reverse_proxy_ip' => false,
451+
'guest_mode' => true,
452+
],
453+
```
454+
455+
<a name="authentication-monitoring"></a>
456+
## Authentication Monitoring
457+
458+
Have you ever thought about monitoring the entry and exit of users of your application? Now you can :) <br>
459+
If you want to monitor users when logging in or logout of your application, you need to migrate the migrations to the config file and change true for monitoring authentication.
460+
461+
```php
462+
'authentication_monitoring' => [
463+
...
421464

422465
/*
423-
* The header used by reverse proxies to forward the real client IP.
424-
* Common values are 'X-Forwarded-For' or 'X-Real-IP'.
466+
* Enable or disable monitoring of user login and logout events.
467+
* Set to true to track these actions, or false to disable.
425468
*/
426-
'real_ip_header' => 'X-Forwarded-For',
469+
'on_login' => true,
470+
'on_logout' => true,
427471
],
428472
```
429473

474+
430475
<a name="authentication-monitoring-views"></a>
431476
### Authentication Monitoring Views
432477

config/user-monitoring.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
* https://laravel.com/docs/scheduling
9494
*/
9595
'delete_days' => 0,
96+
97+
/*
98+
* Determines whether to store `visits` even when the user is not logged in.
99+
*/
100+
'guest_mode' => true,
96101
],
97102

98103
/*
@@ -126,6 +131,11 @@
126131
* Common values are 'X-Forwarded-For' or 'X-Real-IP'.
127132
*/
128133
'real_ip_header' => 'X-Forwarded-For',
134+
135+
/*
136+
* Determines whether to store `actions` even when the user is not logged in.
137+
*/
138+
'guest_mode' => true,
129139
],
130140

131141
/*

src/Middlewares/VisitMonitoringMiddleware.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public function handle(Request $request, Closure $next): mixed
2121
if (config('user-monitoring.visit_monitoring.ajax_requests', false) === false && $request->ajax()) {
2222
return $next($request);
2323
}
24+
if (!config('user-monitoring.visit_monitoring.guest_mode', true) && is_null(UserUtils::getUserId())) {
25+
return $next($request);
26+
}
2427

2528
$detector = new Detector();
2629
$exceptPages = config('user-monitoring.visit_monitoring.except_pages', []);

src/Providers/LaravelUserMonitoringEventServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class LaravelUserMonitoringEventServiceProvider extends EventServiceProvider
1414
{
1515
public function boot(): void
1616
{
17-
$detector = new Detector();
17+
$detector = new Detector;
1818
$table = config('user-monitoring.authentication_monitoring.table');
1919

2020
// Login Event

src/Traits/Actionable.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ protected static function boot(): void
1616
{
1717
parent::boot();
1818

19+
if (!config('user-monitoring.action_monitoring.guest_mode', true) && is_null(UserUtils::getUserId())) {
20+
return;
21+
}
22+
1923
if (config('user-monitoring.action_monitoring.on_store', false)) {
2024
static::created(function (mixed $model) {
2125
static::insertActionMonitoring($model, ActionType::ACTION_STORE);
@@ -87,7 +91,7 @@ private static function insertActionMonitoring(mixed $model, string $actionType)
8791
private static function getRealIP(): string
8892
{
8993
return config('user-monitoring.use_reverse_proxy_ip')
90-
? request()->header(config('user-monitoring.real_ip_header')) ?: request()->ip()
94+
? request()->header(config('user-monitoring.real_ip_header'))
9195
: request()->ip();
9296
}
9397
}

tests/Feature/ActionMonitoringTest.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Testing\RefreshDatabase;
77
use Illuminate\Support\Facades\DB;
88
use Tests\SetUp\Models\Product;
9+
use Tests\SetUp\Models\ProductSoftDelete;
910
use function Pest\Laravel\{assertDatabaseCount, assertDatabaseHas};
1011

1112
/*
@@ -226,7 +227,7 @@
226227
$user = createUser();
227228
auth()->login($user);
228229

229-
$product = \Tests\SetUp\Models\ProductSoftDelete::query()->create([
230+
$product = ProductSoftDelete::query()->create([
230231
'title' => 'milwad',
231232
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
232233
]);
@@ -252,7 +253,7 @@
252253
$user = createUser();
253254
auth()->login($user);
254255

255-
$product = \Tests\SetUp\Models\ProductSoftDelete::query()->create([
256+
$product = ProductSoftDelete::query()->create([
256257
'title' => 'milwad',
257258
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
258259
]);
@@ -302,3 +303,61 @@
302303
expect($actionMonitoring->getTypeColor())->toBe($colors[$type]);
303304
}
304305
});
306+
307+
test('action not stored when the guest mode is off and user not logged in', function () {
308+
config()->set('user-monitoring.action_monitoring.guest_mode', false);
309+
310+
$product = Product::query()->create([
311+
'title' => 'milwad',
312+
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
313+
]);
314+
$product->delete();
315+
316+
// DB Assertions
317+
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 0);
318+
});
319+
320+
test('action stored when the guest mode is on and user not logged in', function () {
321+
config()->set('user-monitoring.action_monitoring.guest_mode', true);
322+
323+
$product = Product::query()->create([
324+
'title' => 'milwad',
325+
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
326+
]);
327+
$product->delete();
328+
329+
// DB Assertions
330+
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 2);
331+
});
332+
333+
test('action stored when the guest mode is off and user logged in', function () {
334+
config()->set('user-monitoring.action_monitoring.guest_mode', false);
335+
336+
$user = createUser();
337+
auth()->login($user);
338+
339+
$product = Product::query()->create([
340+
'title' => 'milwad',
341+
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
342+
]);
343+
$product->delete();
344+
345+
// DB Assertions
346+
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 2);
347+
});
348+
349+
test('action stored when the guest mode is on and user logged in', function () {
350+
config()->set('user-monitoring.action_monitoring.guest_mode', true);
351+
352+
$user = createUser();
353+
auth()->login($user);
354+
355+
$product = Product::query()->create([
356+
'title' => 'milwad',
357+
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
358+
]);
359+
$product->delete();
360+
361+
// DB Assertions
362+
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 2);
363+
});

tests/Feature/VisitMonitoringTest.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
// Ajax
6363

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

6867
// DB Assertions
@@ -80,12 +79,53 @@
8079
assertDatabaseMissing(config('user-monitoring.visit_monitoring.table'), ['page' => 'http:\/\/localhost']);
8180
});
8281

82+
test('visit monitoring skip store when guest mode is off and user not logged in', function () {
83+
config()->set('user-monitoring.visit_monitoring.guest_mode', false);
84+
85+
$response = get('/');
86+
$response->assertContent('milwad');
87+
88+
// DB Assertions
89+
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 0);
90+
assertDatabaseMissing(config('user-monitoring.visit_monitoring.table'), ['page' => 'http:\/\/localhost']);
91+
});
92+
93+
test('visit monitoring store when guest mode is off and user logged in', function () {
94+
config()->set('user-monitoring.visit_monitoring.guest_mode', false);
95+
96+
$user = createUser();
97+
$response = actingAs($user)->get('/');
98+
$response->assertContent('milwad');
99+
100+
// DB Assertions
101+
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 1);
102+
});
103+
104+
test('visit monitoring store when guest mode is on and user logged in', function () {
105+
config()->set('user-monitoring.visit_monitoring.guest_mode', true);
106+
107+
$user = createUser();
108+
$response = actingAs($user)->get('/');
109+
$response->assertContent('milwad');
110+
111+
// DB Assertions
112+
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 1);
113+
});
114+
115+
test('visit monitoring store when guest mode is on and user not logged in', function () {
116+
config()->set('user-monitoring.visit_monitoring.guest_mode', true);
117+
118+
$response = get('/');
119+
$response->assertContent('milwad');
120+
121+
// DB Assertions
122+
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 1);
123+
});
124+
83125
/**
84-
* Create user.
85-
*
86-
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
126+
* Create user and return it.
87127
*/
88-
function createUser()
128+
function createUser(): \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
89129
{
90130
return User::query()->create([
91131
'name' => 'milwad',

0 commit comments

Comments
 (0)