From 03b992e48c60f944490b6a0c3a6883a301b7d7b4 Mon Sep 17 00:00:00 2001 From: vendin Date: Sat, 21 Dec 2019 02:09:51 +0300 Subject: [PATCH] For user relation use morphTo --- migrations/create_devices_table.php.stub | 5 ++--- migrations/create_logins_table.php.stub | 3 ++- src/Interfaces/HasLoginsAndDevicesInterface.php | 12 ++++++------ src/Models/Device.php | 9 +++++---- src/Models/HasLoginsAndDevices.php | 15 ++++++++------- src/Models/Login.php | 10 ++++++---- src/Services/AuthChecker.php | 2 ++ tests/AuthCheckerTest.php | 4 ++++ 8 files changed, 35 insertions(+), 25 deletions(-) diff --git a/migrations/create_devices_table.php.stub b/migrations/create_devices_table.php.stub index 2aa6d80..e85d916 100644 --- a/migrations/create_devices_table.php.stub +++ b/migrations/create_devices_table.php.stub @@ -10,7 +10,8 @@ class CreateDevicesTable extends Migration { Schema::create('devices', function (Blueprint $table) { $table->bigIncrements('id'); - $table->bigInteger('user_id')->unsigned()->index(); + $table->bigInteger('user_id')->unsigned(); + $table->string('user_type'); $table->string('platform')->nullable(); $table->string('platform_version')->nullable(); $table->string('browser')->nullable(); @@ -21,8 +22,6 @@ class CreateDevicesTable extends Migration $table->boolean('is_trusted')->default(0)->index(); $table->boolean('is_untrusted')->default(0)->index(); $table->timestamps(); - - // $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); }); } diff --git a/migrations/create_logins_table.php.stub b/migrations/create_logins_table.php.stub index 73a7c96..d702e5a 100644 --- a/migrations/create_logins_table.php.stub +++ b/migrations/create_logins_table.php.stub @@ -12,7 +12,8 @@ class CreateLoginsTable extends Migration $table->bigIncrements('id'); $table->ipAddress('ip_address'); $table->string('type')->default(\Lab404\AuthChecker\Models\Login::TYPE_LOGIN)->index(); - $table->bigInteger('user_id')->unsigned()->index(); + $table->bigInteger('user_id')->unsigned(); + $table->string('user_type'); $table->bigInteger('device_id')->unsigned()->index()->nullable(); $table->timestamps(); diff --git a/src/Interfaces/HasLoginsAndDevicesInterface.php b/src/Interfaces/HasLoginsAndDevicesInterface.php index 144c3d5..de8e4ef 100644 --- a/src/Interfaces/HasLoginsAndDevicesInterface.php +++ b/src/Interfaces/HasLoginsAndDevicesInterface.php @@ -2,7 +2,7 @@ namespace Lab404\AuthChecker\Interfaces; -use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphMany; /** * @package Lab404\AuthChecker\Interfaces @@ -14,15 +14,15 @@ */ interface HasLoginsAndDevicesInterface { - public function logins(): HasMany; + public function logins(): MorphMany; - public function auths(): HasMany; + public function auths(): MorphMany; - public function fails(): HasMany; + public function fails(): MorphMany; - public function lockouts(): HasMany; + public function lockouts(): MorphMany; - public function devices(): HasMany; + public function devices(): MorphMany; public function hasDevices(): bool; diff --git a/src/Models/Device.php b/src/Models/Device.php index d386489..3768ee3 100644 --- a/src/Models/Device.php +++ b/src/Models/Device.php @@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\MorphOne; +use Illuminate\Database\Eloquent\Relations\MorphTo; /** * @package Lab404\AuthChecker\Models @@ -14,6 +16,7 @@ * @property \Lab404\AuthChecker\Models\Login[] $logins * @property \Illuminate\Contracts\Auth\Authenticatable $user * @property int $user_id + * @property string $user_type * @property string $platform * @property string $platform_version * @property string $browser @@ -60,10 +63,8 @@ public function login(): HasOne return $relation; } - public function user(): BelongsTo + public function user(): MorphTo { - $model = config('auth.providers.users.model'); - - return $this->belongsTo($model); + return $this->morphTo(); } } diff --git a/src/Models/HasLoginsAndDevices.php b/src/Models/HasLoginsAndDevices.php index aa67eff..2669118 100644 --- a/src/Models/HasLoginsAndDevices.php +++ b/src/Models/HasLoginsAndDevices.php @@ -3,6 +3,7 @@ namespace Lab404\AuthChecker\Models; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphMany; /** * Class HasLoginsAndDevices @@ -11,14 +12,14 @@ */ trait HasLoginsAndDevices { - public function logins(): HasMany + public function logins(): MorphMany { $model = config('auth-checker.models.login') ?? Login::class; - return $this->hasMany($model); + return $this->morphMany($model, 'user'); } - public function auths(): HasMany + public function auths(): MorphMany { $relation = $this->logins(); $relation->where('type', Login::TYPE_LOGIN); @@ -26,7 +27,7 @@ public function auths(): HasMany return $relation; } - public function fails(): HasMany + public function fails(): MorphMany { $relation = $this->logins(); $relation->where('type', Login::TYPE_FAILED); @@ -34,7 +35,7 @@ public function fails(): HasMany return $relation; } - public function lockouts(): HasMany + public function lockouts(): MorphMany { $relation = $this->logins(); $relation->where('type', Login::TYPE_LOCKOUT); @@ -42,11 +43,11 @@ public function lockouts(): HasMany return $relation; } - public function devices(): HasMany + public function devices(): MorphMany { $model = config('auth-checker.models.device') ?? Device::class; - return $this->hasMany($model); + return $this->morphMany($model, 'user'); } public function hasDevices(): bool diff --git a/src/Models/Login.php b/src/Models/Login.php index 84a11d0..defc0be 100644 --- a/src/Models/Login.php +++ b/src/Models/Login.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\MorphTo; /** * @package Lab404\AuthChecker\Models @@ -12,6 +13,7 @@ * @property int $device_id * @property \Illuminate\Contracts\Auth\Authenticatable $user * @property int $user_id + * @property string $user_type * @property string $ip_address * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at @@ -27,22 +29,22 @@ class Login extends Model /** @var array $casts */ protected $casts = [ 'user_id' => 'integer', + 'user_type' => 'string', 'device_id' => 'integer', 'ip_address' => 'string', ]; /** @var array $fillable */ protected $fillable = [ 'user_id', + 'user_type', 'ip_address', 'created_at', 'type', ]; - public function user(): BelongsTo + public function user(): MorphTo { - $model = config('auth.providers.users.model'); - - return $this->belongsTo($model); + return $this->morphTo(); } public function device(): BelongsTo diff --git a/src/Services/AuthChecker.php b/src/Services/AuthChecker.php index 72a38e6..e0fdbc0 100644 --- a/src/Services/AuthChecker.php +++ b/src/Services/AuthChecker.php @@ -102,6 +102,7 @@ public function createUserDeviceByAgent(HasLoginsAndDevicesInterface $user, Agen $device->is_mobile = $agent->isMobile() ? true : false; $device->language = count($agent->languages()) ? $agent->languages()[0] : null; $device->user_id = $user->getKey(); + $device->user_type = get_class($user); $device->save(); @@ -136,6 +137,7 @@ public function createUserLoginForDevice( $login = new $model([ 'user_id' => $user->getKey(), + 'user_type' => get_class($user), 'ip_address' => $ip, 'device_id' => $device->id, 'type' => $type, diff --git a/tests/AuthCheckerTest.php b/tests/AuthCheckerTest.php index ebb741d..112a62b 100644 --- a/tests/AuthCheckerTest.php +++ b/tests/AuthCheckerTest.php @@ -78,6 +78,7 @@ public function it_should_log_device_login() { $device = new Device(); $device->user_id = 1; + $device->user_type = 'App\Models\User'; $device->save(); $this->config->set('auth-checker.throttle', 15); @@ -86,6 +87,7 @@ public function it_should_log_device_login() $login = new Login(['ip_address' => '1.2.3.4']); $login->user_id = 1; + $login->user_type = 'App\Models\User'; $device->logins()->save($login); $this->config->set('auth-checker.throttle', 0); @@ -103,10 +105,12 @@ public function it_should_not_log_device_login() { $device = new Device(); $device->user_id = 1; + $device->user_type = 'App\Models\User'; $device->save(); $login = new Login(['ip_address' => '1.2.3.4']); $login->user_id = 1; + $login->user_type = 'App\Models\User'; $device->logins()->save($login); $this->config->set('auth-checker.throttle', 5);