diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | composer.json | 25 | ||||
-rw-r--r-- | config/auth_log.php | 9 | ||||
-rw-r--r-- | src/AuthLogServiceProvider.php | 24 | ||||
-rw-r--r-- | src/Models/AuthLog.php | 47 |
5 files changed, 106 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d0d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..94bf185 --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "lightscale/laralight-auth-log", + "description": "Log authentication laravel application to database", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "Lightscale\\LaralightAuthLog\\": "src/" + } + }, + "authors": [ + { + "name": "Sam Light", + "email": "sam@lightscale.co.uk" + } + ], + "require": {}, + "extra": { + "laravel": { + "providers": [ + "Lightscale\\LaralightAuthLog\\AuthLogServiceProvider" + ] + } + } +} diff --git a/config/auth_log.php b/config/auth_log.php new file mode 100644 index 0000000..d56e51b --- /dev/null +++ b/config/auth_log.php @@ -0,0 +1,9 @@ +<?php + +return [ + 'enabled' => env('AUTH_LOG_ENABLED', true), + 'table_name' => 'auth_log', + + 'model' => \Lightscale\LaralightAuthLog\Models\AuthLog::class, + 'user_model' => \Illuminate\Foundation\Auth\User::class, +]; diff --git a/src/AuthLogServiceProvider.php b/src/AuthLogServiceProvider.php new file mode 100644 index 0000000..55cc2c2 --- /dev/null +++ b/src/AuthLogServiceProvider.php @@ -0,0 +1,24 @@ +<?php + +namespace Lightscale\LaralightAuthLog; + +use Illuminate\Support\ServiceProvider; + +class AuthLogServiceProvider extends ServiceProvider +{ + public function boot(): void + { + $ns = "laralight-auth-log"; + $dir = __DIR__; + $root = "{$dir}/.."; + + $this->publishesMigrations([ + "{$root}/database/migrations" => database_path('migrations', "{$ns}:migrations"), + ]); + + $this->mergeConfigFrom("$root/config/auth_log.php", 'auth_log'); + $this->publishes([ + "{$root}/config/access_log.php" => config_path('auth_log.php'), + ], "{$ns}:config"); + } +} diff --git a/src/Models/AuthLog.php b/src/Models/AuthLog.php new file mode 100644 index 0000000..a5b1280 --- /dev/null +++ b/src/Models/AuthLog.php @@ -0,0 +1,47 @@ +<?php + +namespace Lightscale\LaralightAuthLog\Models; + +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Prunable; +use Illuminate\Database\Eloquent\Builder; + +class AuthLog extends Model +{ + use Prunable; + + const UPDATED_AT = null; + + protected $guarded = [ + 'user_id' + ]; + + public function __construct(array $attributes = []) + { + $this->setTable(config('auth_log.table_name')); + + parent::__construct($attributes); + } + + protected function casts(): array + { + return [ + 'properties' => 'collection', + ]; + } + + public function user(): BelongsTo + { + return $this->belongsTo(config('auth_log.user_model')); + } + + public function prunable(): Builder + { + $days = config('auth_log.max_age_days'); + if ($days === null) { + return static::whereRaw('1 = 0'); + } + else return static::where(static::CREATED_AT, '<=', now()->subDays($days)); + } +} |