Skip to content

Commit c575b6c

Browse files
committed
Merge branch 'hotfix/enabled'
2 parents c1a6918 + e0724e4 commit c575b6c

11 files changed

+131
-50
lines changed

.scrutinizer.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
filter:
2+
excluded_paths:
3+
- tests/*
14
build:
25
nodes:
36
coverage:

README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ You can use the facade for shorter code. Add `AuthLog` to your aliases:
2929
```
3030

3131
## Publish package
32-
Create `config/authlog.php` configuration file using the following artisan command:
32+
Publish configuration and migration files using the following artisan command:
3333
```sh
3434
$ php artisan vendor:publish --provider="Biscolab\LaravelAuthLog\AuthLogServiceProvider"
3535
```
@@ -54,8 +54,32 @@ Edit `config/authlog.php`
5454

5555
> Remember to run the `php artisan config:cache` command
5656
57+
## Add `AuthLoggable` trait to `User` model
58+
59+
Use `Biscolab\LaravelAuthLog\Traits\AuthLoggable` in `App\User`
60+
61+
```php
62+
<?php
63+
64+
namespace App;
65+
66+
// .....
67+
68+
use Biscolab\LaravelAuthLog\Traits\AuthLoggable;
69+
70+
class User extends Authenticatable
71+
{
72+
use AuthLoggable;
73+
74+
// other traits
75+
// ......
76+
77+
```
78+
5779
## Database
5880

81+
> Your users' table id MUST be either of type `unsignedInteger` or `unsignedBigInteger`. If you have a custom users' table edit `datatbase/migratons/2019_09_19_000000_create_authlog_table.php` after `vendor:publish` artisan command
82+
5983
Run migrations
6084

6185
```sh

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
],
2020
"require": {
2121
"php": "^7.1",
22-
"laravel/framework": "^5.5|^6.0",
23-
"illuminate/support": "^5.5|^6.0"
22+
"laravel/framework": "^5.5|^6.0"
2423
},
2524
"require-dev": {
2625
"orchestra/testbench": "~3.0",

config/authlog.php

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
*/
3131
'table_name' => 'authlog',
3232

33+
/**
34+
* Users table size in order to add foreign keys
35+
* Supported: 'int', 'big'
36+
*/
37+
'users_table_size' => 'big',
38+
3339
/**
3440
* AuthLog model class MUST implements Biscolab\LaravelAuthLog\Models\AuthLogInterface
3541
*/

database/migrations/2019_09_19_000000_create_authlog_table.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
use Illuminate\Support\Facades\Schema;
4-
use Illuminate\Database\Schema\Blueprint;
3+
use Biscolab\LaravelAuthLog\Facades\AuthLog;
54
use Illuminate\Database\Migrations\Migration;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
67

78
/**
89
* Class CreateAuthlogTable
@@ -20,25 +21,25 @@ public function up()
2021

2122
Schema::create(config('authlog.table_name'), function (Blueprint $table) {
2223

23-
$table->increments('id');
24-
$table->unsignedInteger('user_id');
25-
$table->unsignedInteger('blame_on_user_id')->nullable();
24+
$user_id_type = AuthLog::getMigrateUserIdType();
25+
$table->bigIncrements('id');
26+
$table->$user_id_type('user_id');
27+
$table->$user_id_type('blame_on_user_id')->nullable();
2628
$table->string('ip', 255)->nullable()->default('');
2729
$table->string('session_id', 255)->nullable()->default('');
2830
$table->text('user_agent')->nullable();
2931
$table->boolean('killed_from_console')->default(false);
3032
$table->dateTime('logged_out_at')->nullable();
31-
$table->dateTime('created_at');
32-
$table->dateTime('updated_at');
33+
$table->timestamps();
3334

3435
$table->index('session_id', 'session_id');
3536
$table->index('user_id', 'user_id');
3637
$table->index('blame_on_user_id', 'blame_on_user_id');
3738
$table->index(['logged_out_at', 'blame_on_user_id', 'killed_from_console'], 'logged_out_at');
3839

39-
$table->foreign('blame_on_user_id', 'fk_authlog_blame_on_user')->references('id')->on('users')->onDelete('RESTRICT
40+
$table->foreign('blame_on_user_id')->references('id')->on('users')->onDelete('RESTRICT
4041
')->onUpdate('RESTRICT');
41-
$table->foreign('user_id', 'fk_authlog_user')->references('id')->on('users')->onDelete('RESTRICT
42+
$table->foreign('user_id')->references('id')->on('users')->onDelete('RESTRICT
4243
')->onUpdate('RESTRICT');
4344

4445
});

src/AuthLogServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function boot()
3232
__DIR__ . '/../config/authlog.php' => config_path('authlog.php'),
3333
], 'config');
3434

35+
$this->publishes([
36+
__DIR__ . '/../database/migrations/' => database_path('migrations')
37+
], 'migrations');
38+
3539
$this->registerEventSubscribers();
3640

3741
$this->registerAuthSessionFacades();

src/Facades/AuthLog.php

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* @method static string getSessionAuthLogIdKey()
1919
* @method static string setAuthLogId()
2020
* @method static string getAuthLogId()
21+
* @method static string getTableUserIdType()
22+
* @method static string getMigrateUserIdType()
23+
* @method static bool userTableIdIsInt()
2124
* @package Biscolab\LaravelAuthLog\Facades
2225
*/
2326
class AuthLog extends Facade

src/LaravelAuthLog.php

+38-1
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ public function skipByIp(): bool
4040
*/
4141
public function canRegisterAuthLog(): bool
4242
{
43-
return !$this->skipByIp();
43+
44+
return !$this->skipByIp() && $this->isEnabled();
4445
}
4546

4647
/**
4748
* @return bool
4849
*/
4950
public function isEnabled(): bool
5051
{
52+
5153
return config('authlog.enabled', false);
5254
}
5355

@@ -95,4 +97,39 @@ public function getAuthLogId(): ?int
9597

9698
return Session::get($this->getSessionAuthLogIdKey());
9799
}
100+
101+
/**
102+
* @return string
103+
*/
104+
public function getTableUserIdType(): string
105+
{
106+
107+
$users_table_cols = \DB::select('describe users');
108+
109+
foreach ($users_table_cols as $col) {
110+
if ($col->Field === 'id') {
111+
return $col->Type;
112+
}
113+
}
114+
115+
return '';
116+
}
117+
118+
/**
119+
* @return bool
120+
*/
121+
public function userTableIdIsInt(): bool
122+
{
123+
124+
return ('int(11) unsigned' === $this->getTableUserIdType());
125+
}
126+
127+
/**
128+
* @return string
129+
*/
130+
public function getMigrateUserIdType(): string
131+
{
132+
133+
return $this->userTableIdIsInt() ? 'unsignedInteger' : 'unsignedBigInteger';
134+
}
98135
}

src/Traits/AuthLoggable.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function getSessionData(): array
4343
*/
4444
public function registerLogin(): ?AuthLogInterface
4545
{
46-
if(!authlog()->canRegisterAuthLog()){
46+
47+
if (!authlog()->canRegisterAuthLog()) {
4748
return null;
4849
}
4950

@@ -59,15 +60,16 @@ public function registerLogin(): ?AuthLogInterface
5960
}
6061

6162
/**
62-
* @param int|null $blame_on_user_id
63-
*
6463
* @return AuthLogInterface|null
6564
*/
66-
public function registerLogout(?int $blame_on_user_id = null): ?AuthLogInterface
65+
public function registerLogout(): ?AuthLogInterface
6766
{
6867

68+
if (!authlog()->canRegisterAuthLog()) {
69+
return null;
70+
}
6971
if (!empty($this->current_auth_log)) {
70-
return $this->current_auth_log->createLogout($blame_on_user_id);
72+
return $this->current_auth_log->createLogout();
7173
}
7274

7375
return null;
@@ -105,7 +107,8 @@ public function logins()
105107
public function logouts()
106108
{
107109

108-
return $this->logs()->whereNotNull('logged_out_at')->whereNull('blame_on_user_id')->where('killed_from_console', false);
110+
return $this->logs()->whereNotNull('logged_out_at')->whereNull('blame_on_user_id')->where('killed_from_console',
111+
false);
109112
}
110113

111114
/**

tests/Listeners/UserEventSubscriber.php tests/Listeners/UserEventSubscriberTest.php

+31-30
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,67 @@
1010

1111
namespace Biscolab\LaravelAuthLog\Tests;
1212

13+
use Biscolab\LaravelAuthLog\Listeners\UserEventSubscriber;
14+
use Carbon\Carbon;
1315
use Illuminate\Auth\Events\Login;
1416
use Illuminate\Auth\Events\Logout;
17+
use Illuminate\Http\Request;
1518
use Illuminate\Support\Facades\Auth;
1619
use Illuminate\Support\Facades\Event;
1720

18-
class UserEventSubscriber extends TestCase
21+
class UserEventSubscriberTest extends TestCase
1922
{
2023

24+
private $user;
25+
2126
/**
2227
* @test
2328
*/
2429
public function testLoginEvent()
2530
{
2631

2732
Event::fake();
33+
$user = $this->user;
34+
Auth::login($user);
2835

29-
$this->expectsEvents([
30-
Login::class
31-
]);
3236

33-
$user = User::create([
34-
'name' => 'Roberto Belotti',
35-
'email' => env('DEFAULT_USER_EMAIL'),
36-
'password' => bcrypt(env('DEFAULT_USER_PASSWORD'))
37-
]);
37+
Event::assertDispatched(Login::class, function ($e) use ($user) {
38+
$subscriber = new UserEventSubscriber();
3839

39-
// $this->events->dispatch(new Events\Login(
40-
// $this->name, $user, $remember
41-
// ));
4240

43-
// \event(new Login('web', $user, false));
41+
$subscriber->handleUserLogin($e);
42+
return $e->user->id === $user->id;
43+
});
4444

45-
Auth::login($user);
46-
Event::assertDispatched(Login::class, function ($e) use ($user) {
45+
$this->user->load(['logs']);
46+
47+
$this->assertEquals(1, $this->user->logs->count());
48+
49+
event(new Logout('web', $user));
50+
Event::assertDispatched(Logout::class, function ($e) use ($user) {
51+
$subscriber = new UserEventSubscriber();
4752

53+
54+
$subscriber->handleUserLogout($e);
4855
return $e->user->id === $user->id;
4956
});
5057

58+
$this->user->load(['logouts']);
59+
60+
$this->assertEquals(1, $this->user->logouts->count());
61+
5162
}
5263

53-
/**
54-
* @test
55-
*/
56-
public function testLogoutEvent()
64+
protected function setUp(): void
5765
{
5866

59-
Event::fake();
60-
61-
$user = User::create([
67+
parent::setUp();
68+
$this->carbon = \Mockery::mock(Carbon::class);
69+
$this->request = \Mockery::mock(Request::class);
70+
$this->user = User::create([
6271
'name' => 'Roberto Belotti',
6372
'email' => env('DEFAULT_USER_EMAIL'),
6473
'password' => bcrypt(env('DEFAULT_USER_PASSWORD'))
6574
]);
66-
67-
event(new Logout('web', $user));
68-
69-
Event::assertDispatched(Logout::class, function ($e) use ($user) {
70-
71-
return $e->user->id === $user->id;
72-
});
73-
7475
}
7576
}

tests/Middlewares/AuthLogMiddlewareTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testAddAuthLogIdHeaderToResponse()
4747
protected function tearDown(): void
4848
{
4949

50-
parent::tearDown(); // TODO: Change the autogenerated stub
50+
parent::tearDown();
5151
}
5252

5353
protected function setUp(): void

0 commit comments

Comments
 (0)