Skip to content
This repository was archived by the owner on Jun 1, 2019. It is now read-only.

Commit 4163be6

Browse files
committed
Merge pull request #19 from lowerends/add-ignored-user
Add ignored user for whom logging is disabled.
2 parents edc6843 + 7312f29 commit 4163be6

File tree

6 files changed

+95
-12
lines changed

6 files changed

+95
-12
lines changed

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ This service provider must be registered.
3636
You'll also need to publish and run the migration in order to create the db-table.
3737
```
3838
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
39-
php artisan migrate
39+
php artisan migrate
4040
```
4141

4242

@@ -65,12 +65,12 @@ The configuration will be written to ```config/activitylog.php```. The options
6565
Logging some activity is very simple.
6666
```php
6767

68-
/*
68+
/*
6969
The log-function takes two parameters:
7070
- $text: the activity you wish to log.
71-
- $user: optional can be an user id or a user object.
71+
- $user: optional can be an user id or a user object.
7272
if not proved the id of Auth::user() will be used
73-
73+
7474
*/
7575
Activity::log('Some activity that you wish to log');
7676
```
@@ -122,6 +122,33 @@ public function getActivityDescriptionForEvent($eventName)
122122
```
123123
The result of this function will be logged, unless the result is an empty string.
124124

125+
### Disable logging under certain conditions
126+
If you want to disable logging under certain conditions, such as for a specific user, create a class in your application namespace that implements the `Spatie\Activitylog\Handlers\BeforeHandlerInterface`. This interface defines an `ignore()` method in which you can code any custom logic to determine whether logging should be ignored or not. Returning `true` from this function will ignore logging, returning `false` will continue with the logging. Add the namespaced class name to the `beforeCallback` field in the configuration file:
127+
```php
128+
'beforeCallback' => '\App\Handlers\BeforeHandler',
129+
```
130+
For example, this callback class could look like this to disable logging for the first user:
131+
```php
132+
<?php
133+
134+
namespace App\Handlers;
135+
136+
use Spatie\Activitylog\Handlers\BeforeHandlerInterface;
137+
138+
class BeforeHandler implements BeforeHandlerInterface
139+
{
140+
public function ignore()
141+
{
142+
if (\Auth::user()->id == 1)
143+
{
144+
return true;
145+
}
146+
147+
return false;
148+
}
149+
}
150+
```
151+
125152
### Retrieving logged entries
126153
All events will be logged in the `activity_log`-table. This package provides an Eloquent model to work with the table. You can use all the normal Eloquent methods that you know and love. Here's how you can get the last 100 activities together with the associated users.
127154

@@ -131,6 +158,10 @@ use Spatie\Activitylog\Models\Activity;
131158
$latestActivities = Activity::with('user')->latest()->limit(100)->get();
132159
```
133160

161+
## Contributing
162+
163+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
164+
134165
### Cleaning up the log
135166

136167
Over time your log will grow. To clean up the database table you can run this command:

src/Spatie/Activitylog/ActivitylogSupervisor.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Config\Repository;
66
use Illuminate\Auth\Guard;
7+
use Spatie\Activitylog\Handlers\BeforeHandler;
78
use Spatie\Activitylog\Handlers\DefaultLaravelHandler;
89
use Request;
910
use Config;
@@ -23,21 +24,28 @@ class ActivitylogSupervisor
2324
* Create the logsupervisor using a default Handler
2425
* Also register Laravels Log Handler if needed.
2526
*
26-
* @param Handlers\ActivitylogHandlerInterface $handler
27+
* @param Handlers\ActivitylogHandlerInterface $logHandler
28+
* @param Handlers\BeforeHandlerInterface $beforeHandler
2729
* @param Repository $config
2830
* @param Guard $auth
2931
*/
30-
public function __construct(Handlers\ActivitylogHandlerInterface $handler, Repository $config, Guard $auth)
32+
public function __construct(Handlers\ActivitylogHandlerInterface $logHandler, Handlers\BeforeHandler $beforeHandler, Repository $config, Guard $auth)
3133
{
3234
$this->config = $config;
3335

34-
$this->logHandlers[] = $handler;
36+
$this->logHandlers[] = $logHandler;
37+
3538
if ($this->config->get('activitylog.alsoLogInDefaultLog')) {
3639
$this->logHandlers[] = new DefaultLaravelHandler();
3740
}
38-
$this->auth = $auth;
3941

42+
if ($beforeCallback = $this->config->get('activitylog.beforeCallback')) {
43+
$this->beforeHandler = new $beforeCallback;
44+
} else {
45+
$this->beforeHandler = $beforeHandler;
46+
}
4047

48+
$this->auth = $auth;
4149
}
4250

4351
/**
@@ -50,6 +58,10 @@ public function __construct(Handlers\ActivitylogHandlerInterface $handler, Repos
5058
*/
5159
public function log($text, $userId = '')
5260
{
61+
if ($this->beforeHandler->ignore()) {
62+
return false;
63+
}
64+
5365
$userId = $this->normalizeUserId($userId);
5466

5567
$ipAddress = Request::getClientIp();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Spatie\Activitylog\Handlers;
4+
5+
class BeforeHandler implements BeforeHandlerInterface
6+
{
7+
/**
8+
* Don't log if this function returns true.
9+
*
10+
* @return bool
11+
*/
12+
public function ignore()
13+
{
14+
return false;
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\Activitylog\Handlers;
4+
5+
interface BeforeHandlerInterface
6+
{
7+
/**
8+
* Don't log if this function returns true.
9+
*
10+
* @return bool
11+
*/
12+
public function ignore();
13+
}

src/config/activitylog.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@
3434
|
3535
*/
3636
'defaultUserId' => '',
37+
38+
/*
39+
|--------------------------------------------------------------------------
40+
| Handler that is called before logging is done
41+
|--------------------------------------------------------------------------
42+
|
43+
| If you want to disable logging based on some custom conditions, create
44+
| a handler class that implements the BeforeHandlerInterface and
45+
| reference it here.
46+
|
47+
*/
48+
'beforeCallback' => null,
3749
];

tests/ActivityLogSupervisorTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
class ActivityLogSupervistorTest extends PHPUnit_Framework_TestCase
88
{
9-
109
protected $logHandler;
1110
protected $activityLogSupervisor;
1211
protected $config;
1312

1413
public function setUp()
1514
{
1615
$this->logHandler = Mockery::mock('\Spatie\Activitylog\Handlers\EloquentHandler');
16+
$this->beforeHandler = Mockery::mock('\Spatie\Activitylog\Handlers\BeforeHandler');
1717
$this->config = Mockery::mock('\Illuminate\Config\Repository');
1818
$this->auth = Mockery::mock('Illuminate\Auth\Guard');
1919

2020
$this->config->shouldReceive('get')->andReturn(false);
21-
$this->activityLogSupervisor = new ActivitylogSupervisor($this->logHandler, $this->config, $this->auth);
21+
$this->activityLogSupervisor = new ActivitylogSupervisor($this->logHandler, $this->beforeHandler, $this->config, $this->auth);
2222
}
2323

2424
/**
@@ -57,5 +57,4 @@ public function it_normalizes_a_numeric_user_id()
5757

5858
$this->assertSame(123, $normalizedUserId);
5959
}
60-
61-
}
60+
}

0 commit comments

Comments
 (0)