Skip to content

Commit

Permalink
Merge branch 'hotfix/enabled'
Browse files Browse the repository at this point in the history
  • Loading branch information
biscolab committed Oct 2, 2019
2 parents c1a6918 + e0724e4 commit c575b6c
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
filter:
excluded_paths:
- tests/*
build:
nodes:
coverage:
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ You can use the facade for shorter code. Add `AuthLog` to your aliases:
```

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

> Remember to run the `php artisan config:cache` command
## Add `AuthLoggable` trait to `User` model

Use `Biscolab\LaravelAuthLog\Traits\AuthLoggable` in `App\User`

```php
<?php

namespace App;

// .....

use Biscolab\LaravelAuthLog\Traits\AuthLoggable;

class User extends Authenticatable
{
use AuthLoggable;

// other traits
// ......

```

## Database

> 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
Run migrations

```sh
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
],
"require": {
"php": "^7.1",
"laravel/framework": "^5.5|^6.0",
"illuminate/support": "^5.5|^6.0"
"laravel/framework": "^5.5|^6.0"
},
"require-dev": {
"orchestra/testbench": "~3.0",
Expand Down
6 changes: 6 additions & 0 deletions config/authlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
*/
'table_name' => 'authlog',

/**
* Users table size in order to add foreign keys
* Supported: 'int', 'big'
*/
'users_table_size' => 'big',

/**
* AuthLog model class MUST implements Biscolab\LaravelAuthLog\Models\AuthLogInterface
*/
Expand Down
19 changes: 10 additions & 9 deletions database/migrations/2019_09_19_000000_create_authlog_table.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Biscolab\LaravelAuthLog\Facades\AuthLog;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
* Class CreateAuthlogTable
Expand All @@ -20,25 +21,25 @@ public function up()

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

$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('blame_on_user_id')->nullable();
$user_id_type = AuthLog::getMigrateUserIdType();
$table->bigIncrements('id');
$table->$user_id_type('user_id');
$table->$user_id_type('blame_on_user_id')->nullable();
$table->string('ip', 255)->nullable()->default('');
$table->string('session_id', 255)->nullable()->default('');
$table->text('user_agent')->nullable();
$table->boolean('killed_from_console')->default(false);
$table->dateTime('logged_out_at')->nullable();
$table->dateTime('created_at');
$table->dateTime('updated_at');
$table->timestamps();

$table->index('session_id', 'session_id');
$table->index('user_id', 'user_id');
$table->index('blame_on_user_id', 'blame_on_user_id');
$table->index(['logged_out_at', 'blame_on_user_id', 'killed_from_console'], 'logged_out_at');

$table->foreign('blame_on_user_id', 'fk_authlog_blame_on_user')->references('id')->on('users')->onDelete('RESTRICT
$table->foreign('blame_on_user_id')->references('id')->on('users')->onDelete('RESTRICT
')->onUpdate('RESTRICT');
$table->foreign('user_id', 'fk_authlog_user')->references('id')->on('users')->onDelete('RESTRICT
$table->foreign('user_id')->references('id')->on('users')->onDelete('RESTRICT
')->onUpdate('RESTRICT');

});
Expand Down
4 changes: 4 additions & 0 deletions src/AuthLogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function boot()
__DIR__ . '/../config/authlog.php' => config_path('authlog.php'),
], 'config');

$this->publishes([
__DIR__ . '/../database/migrations/' => database_path('migrations')
], 'migrations');

$this->registerEventSubscribers();

$this->registerAuthSessionFacades();
Expand Down
3 changes: 3 additions & 0 deletions src/Facades/AuthLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* @method static string getSessionAuthLogIdKey()
* @method static string setAuthLogId()
* @method static string getAuthLogId()
* @method static string getTableUserIdType()
* @method static string getMigrateUserIdType()
* @method static bool userTableIdIsInt()
* @package Biscolab\LaravelAuthLog\Facades
*/
class AuthLog extends Facade
Expand Down
39 changes: 38 additions & 1 deletion src/LaravelAuthLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ public function skipByIp(): bool
*/
public function canRegisterAuthLog(): bool
{
return !$this->skipByIp();

return !$this->skipByIp() && $this->isEnabled();
}

/**
* @return bool
*/
public function isEnabled(): bool
{

return config('authlog.enabled', false);
}

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

return Session::get($this->getSessionAuthLogIdKey());
}

/**
* @return string
*/
public function getTableUserIdType(): string
{

$users_table_cols = \DB::select('describe users');

foreach ($users_table_cols as $col) {
if ($col->Field === 'id') {
return $col->Type;
}
}

return '';
}

/**
* @return bool
*/
public function userTableIdIsInt(): bool
{

return ('int(11) unsigned' === $this->getTableUserIdType());
}

/**
* @return string
*/
public function getMigrateUserIdType(): string
{

return $this->userTableIdIsInt() ? 'unsignedInteger' : 'unsignedBigInteger';
}
}
15 changes: 9 additions & 6 deletions src/Traits/AuthLoggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function getSessionData(): array
*/
public function registerLogin(): ?AuthLogInterface
{
if(!authlog()->canRegisterAuthLog()){

if (!authlog()->canRegisterAuthLog()) {
return null;
}

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

/**
* @param int|null $blame_on_user_id
*
* @return AuthLogInterface|null
*/
public function registerLogout(?int $blame_on_user_id = null): ?AuthLogInterface
public function registerLogout(): ?AuthLogInterface
{

if (!authlog()->canRegisterAuthLog()) {
return null;
}
if (!empty($this->current_auth_log)) {
return $this->current_auth_log->createLogout($blame_on_user_id);
return $this->current_auth_log->createLogout();
}

return null;
Expand Down Expand Up @@ -105,7 +107,8 @@ public function logins()
public function logouts()
{

return $this->logs()->whereNotNull('logged_out_at')->whereNull('blame_on_user_id')->where('killed_from_console', false);
return $this->logs()->whereNotNull('logged_out_at')->whereNull('blame_on_user_id')->where('killed_from_console',
false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,67 @@

namespace Biscolab\LaravelAuthLog\Tests;

use Biscolab\LaravelAuthLog\Listeners\UserEventSubscriber;
use Carbon\Carbon;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;

class UserEventSubscriber extends TestCase
class UserEventSubscriberTest extends TestCase
{

private $user;

/**
* @test
*/
public function testLoginEvent()
{

Event::fake();
$user = $this->user;
Auth::login($user);

$this->expectsEvents([
Login::class
]);

$user = User::create([
'name' => 'Roberto Belotti',
'email' => env('DEFAULT_USER_EMAIL'),
'password' => bcrypt(env('DEFAULT_USER_PASSWORD'))
]);
Event::assertDispatched(Login::class, function ($e) use ($user) {
$subscriber = new UserEventSubscriber();

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

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

Auth::login($user);
Event::assertDispatched(Login::class, function ($e) use ($user) {
$this->user->load(['logs']);

$this->assertEquals(1, $this->user->logs->count());

event(new Logout('web', $user));
Event::assertDispatched(Logout::class, function ($e) use ($user) {
$subscriber = new UserEventSubscriber();


$subscriber->handleUserLogout($e);
return $e->user->id === $user->id;
});

$this->user->load(['logouts']);

$this->assertEquals(1, $this->user->logouts->count());

}

/**
* @test
*/
public function testLogoutEvent()
protected function setUp(): void
{

Event::fake();

$user = User::create([
parent::setUp();
$this->carbon = \Mockery::mock(Carbon::class);
$this->request = \Mockery::mock(Request::class);
$this->user = User::create([
'name' => 'Roberto Belotti',
'email' => env('DEFAULT_USER_EMAIL'),
'password' => bcrypt(env('DEFAULT_USER_PASSWORD'))
]);

event(new Logout('web', $user));

Event::assertDispatched(Logout::class, function ($e) use ($user) {

return $e->user->id === $user->id;
});

}
}
2 changes: 1 addition & 1 deletion tests/Middlewares/AuthLogMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testAddAuthLogIdHeaderToResponse()
protected function tearDown(): void
{

parent::tearDown(); // TODO: Change the autogenerated stub
parent::tearDown();
}

protected function setUp(): void
Expand Down

0 comments on commit c575b6c

Please sign in to comment.