Added login log

This commit is contained in:
snipe 2018-07-16 23:49:08 -07:00
parent bf761946da
commit 0f85d6810b
6 changed files with 143 additions and 6 deletions

View file

@ -0,0 +1,46 @@
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Failed;
use Illuminate\Http\Request;
use DB;
use Carbon\Carbon;
class LogFailedLogin
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param \Illuminate\Auth\Events\Failed $event
* @return void
*/
public function handle(Failed $event)
{
$now = new Carbon();
try {
DB::table('login_attempts')->insert(
[
'username' => $event->credentials['username'],
'user_agent' => request()->header('User-Agent'),
'remote_ip' => request()->ip(),
'successful' => 0,
'created_at' => $now;
]
);
} catch (\Exception $e) {
\Log::debug($e);
}
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Http\Request;
use DB;
use Carbon\Carbon;
class LogSuccessfulLogin
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param Login $event
* @return void
*/
public function handle(Login $event)
{
$now = new Carbon();
try {
DB::table('login_attempts')->insert(
[
'username' => $event->user->username,
'user_agent' => request()->header('User-Agent'),
'remote_ip' => request()->ip(),
'successful' => 1,
'created_at' => $now
]
);
} catch (\Exception $e) {
\Log::debug($e);
}
}
}

View file

@ -13,10 +13,16 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
];
'Illuminate\Auth\Events\Login' => [
'App\Listeners\LogSuccessfulLogin',
],
'Illuminate\Auth\Events\Failed' => [
'App\Listeners\LogFailedLogin',
],
];
/**
* Register any events for your application.

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLoginAttemptsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('login_attempts', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->nullable()->default(null);
$table->ipAddress('remote_ip')->nullable()->default(null);
$table->string('user_agent')->nullable()->default(null);
$table->boolean('successful')->nullable()->default(null);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('login_attempts');
}
}

View file

@ -2,7 +2,8 @@
return array(
'activated_help_text' => 'This user can login',
'activated_disabled_help_text' => 'You cannot edit activation status for your own account.',
'assets_user' => 'Assets assigned to :name',
'bulk_update_warn' => 'You are about to edit the properties of :user_count users. Please note that you cannot change your own user attributes using this form, and must make edits to your own user individually.',
'bulk_update_help' => 'This form allows you to update multiple users at once. Only fill in the fields you need to change. Any fields left blank will remain unchanged.',

View file

@ -51,7 +51,7 @@
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">{{ trans('auth/general.remember_me') }}
<input name="remember" type="checkbox" value="1">{{ trans('auth/general.remember_me') }}
</label>
</div>
</fieldset>