Skip to content

Commit 616d27a

Browse files
committed
Added auth guards configuration support(#13)
1 parent 5e3a11b commit 616d27a

File tree

4 files changed

+105
-6
lines changed

4 files changed

+105
-6
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ A sample meta
177177
]
178178
```
179179

180-
### Custom History
180+
### Custom history
181181

182182
Besides the built in `created/updating/deleting/restoring` events, you may store custom history record with `ModelChanged` event.
183183

@@ -217,6 +217,28 @@ This will translate your model history into
217217

218218
You may set whitelist and blacklist in config file. Please follow the description guide in the published config file.
219219

220+
### Auth guards
221+
222+
If your users are using non-default auth guards, you might see all `$history->hasUser()` become `false` even though the history sources were generated by authenticated users.
223+
224+
To fix this, you'll need to enable custom auth guards scanning in config file:
225+
226+
```php
227+
/*
228+
|--------------------------------------------------------------
229+
| Enable auth guards scan
230+
|--------------------------------------------------------------
231+
|
232+
| You only need to enable this if your users are using non-default auth guards.
233+
| In that case, all tracked user operations will be anonymous.
234+
|
235+
| - Set to `true` to use a full scan mode: all auth guards will be checked. However this does not ensure guard priority.
236+
| - Set to an array to scan only specific auth guards(in the given order). e.g. `['web', 'api', 'admin']`
237+
|
238+
*/
239+
'auth_guards' => null
240+
```
241+
220242
### Known issues
221243

222244
1. When updating a model, if its model label(attributes returned from `getModelLabel`) has been modified, the history message will use its new attributes, which might not be what you expect.

src/HistoryObserver.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Panoscape\History;
44

55
use Illuminate\Support\Str;
6+
use Illuminate\Support\Facades\Auth;
67

78
class HistoryObserver
89
{
@@ -106,12 +107,12 @@ public static function getModelName($model)
106107

107108
public static function getUserID()
108109
{
109-
return auth()->check() ? auth()->user()->id : null;
110+
return static::getAuth()->check() ? static::getAuth()->user()->id : null;
110111
}
111112

112113
public static function getUserType()
113114
{
114-
return auth()->check() ? get_class(auth()->user()) : null;
115+
return static::getAuth()->check() ? get_class(static::getAuth()->user()) : null;
115116
}
116117

117118
public static function isIgnored($model, $key)
@@ -124,16 +125,38 @@ public static function isIgnored($model, $key)
124125

125126
public static function filter($action)
126127
{
127-
if(!auth()->check()) {
128+
if(!static::getAuth()->check()) {
128129
if(in_array('nobody', config('history.user_blacklist'))) {
129130
return false;
130131
}
131132
}
132-
elseif(in_array(get_class(auth()->user()), config('history.user_blacklist'))) {
133+
elseif(in_array(get_class(static::getAuth()->user()), config('history.user_blacklist'))) {
133134
return false;
134135
}
135136

136137
return is_null($action) || in_array($action, config('history.events_whitelist'));
137138
}
139+
140+
private static function getAuth()
141+
{
142+
$guards = config('history.auth_guards');
143+
if(is_bool($guards) && $guards == true)
144+
return auth(static::activeGuard());
145+
if(is_array($guards))
146+
{
147+
foreach($guards as $guard)
148+
if(auth($guard)->check()) return auth($guard);
149+
}
150+
return auth();
151+
}
152+
153+
private static function activeGuard()
154+
{
155+
foreach(array_keys(config('auth.guards')) as $guard)
156+
{
157+
if(auth($guard)->check()) return $guard;
158+
}
159+
return null;
160+
}
138161

139162
}

src/config/history.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,19 @@
9393
'env_blacklist' => [
9494

9595
],
96+
97+
/*
98+
|--------------------------------------------------------------
99+
| Enable auth guards scan
100+
|--------------------------------------------------------------
101+
|
102+
| You only need to enable this if your users are using non-default auth guards.
103+
| In that case, all tracked user operations will be anonymous.
104+
|
105+
| - Set to `true` to use a full scan mode: all auth guards will be checked. However this does not ensure guard priority.
106+
| - Set to an array to scan only specific auth guards(in the given order). e.g. `['web', 'api', 'admin']`
107+
|
108+
*/
109+
'auth_guards' => null,
96110

97111
];

tests/TestCaseTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Orchestra\Testbench\TestCase;
66
use Illuminate\Database\Schema\Blueprint;
77
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Auth;
89
use Panoscape\History\History;
910
use Panoscape\History\HistoryServiceProvider;
1011
use Panoscape\History\Events\ModelChanged;
@@ -36,6 +37,12 @@ protected function getEnvironmentSetUp($app)
3637
'password'
3738
]
3839
]);
40+
$app['config']->set('history.auth_guards', ['web','admin']);
41+
// custom auth guard mock
42+
$app['config']->set('auth.guards.admin.driver', 'admin-login');
43+
Auth::viaRequest('admin-login', function(Request $request) {
44+
return null;
45+
});
3946

4047
$app['router']->post('articles', function(Request $request) {
4148
return Article::create(['title' => $request->title]);
@@ -58,7 +65,7 @@ protected function getEnvironmentSetUp($app)
5865
event(new ModelChanged($model, 'Query Article ' . $model->title, $model->pluck('id')->toArray()));
5966
}
6067
return $model;
61-
});
68+
});
6269
}
6370

6471
public function setUp(): void
@@ -166,6 +173,32 @@ public function testAnonymous()
166173
$this->assertNull($history->user());
167174
}
168175

176+
public function testCustomGuard()
177+
{
178+
$user = User::first();
179+
$this->assertNotNull($user);
180+
181+
$content = ['title' => 'voluptas ut rem'];
182+
$this->actingAsAdmin($user)->json('POST', '/articles', $content)->assertJson($content);
183+
184+
$article = Article::first();
185+
$this->assertNotNull($article);
186+
$histories = $article->histories;
187+
$this->assertNotNull($histories);
188+
$this->assertEquals(1, count($histories));
189+
$history = $histories[0];
190+
$this->assertTrue($history->hasUser());
191+
$this->assertNotNull($history->user());
192+
$this->assertEquals($user->toJson(), $history->user()->toJson());
193+
$this->assertEquals($article->makeHidden('histories')->toJson(), $history->model()->toJson());
194+
195+
$operations = $user->operations;
196+
$this->assertNotNull($operations);
197+
$this->assertEquals(1, count($operations));
198+
$operation = $operations[0];
199+
$this->assertEquals($history->toJson(), $operation->toJson());
200+
}
201+
169202
public function testCustomEvent()
170203
{
171204
Article::create(['title' => 'maxime fugit saepe']);
@@ -178,4 +211,11 @@ public function testCustomEvent()
178211
$this->assertEquals('Query Article ' . $article->title, $history->message);
179212
$this->assertEquals([$article->id], $history->meta);
180213
}
214+
215+
private function actingAsAdmin($admin) {
216+
$defaultGuard = config('auth.defaults.guard');
217+
$this->actingAs($admin, 'admin');
218+
Auth::shouldUse($defaultGuard);
219+
return $this;
220+
}
181221
}

0 commit comments

Comments
 (0)