Skip to content

Commit

Permalink
For user relation use morphTo
Browse files Browse the repository at this point in the history
  • Loading branch information
Vendin committed Dec 20, 2019
1 parent c40ce30 commit 03b992e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 25 deletions.
5 changes: 2 additions & 3 deletions migrations/create_devices_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
});
}

Expand Down
3 changes: 2 additions & 1 deletion migrations/create_logins_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
12 changes: 6 additions & 6 deletions src/Interfaces/HasLoginsAndDevicesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Lab404\AuthChecker\Interfaces;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;

/**
* @package Lab404\AuthChecker\Interfaces
Expand All @@ -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;

Expand Down
9 changes: 5 additions & 4 deletions src/Models/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}
15 changes: 8 additions & 7 deletions src/Models/HasLoginsAndDevices.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lab404\AuthChecker\Models;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;

/**
* Class HasLoginsAndDevices
Expand All @@ -11,42 +12,42 @@
*/
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);

return $relation;
}

public function fails(): HasMany
public function fails(): MorphMany
{
$relation = $this->logins();
$relation->where('type', Login::TYPE_FAILED);

return $relation;
}

public function lockouts(): HasMany
public function lockouts(): MorphMany
{
$relation = $this->logins();
$relation->where('type', Login::TYPE_LOCKOUT);

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
Expand Down
10 changes: 6 additions & 4 deletions src/Models/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Services/AuthChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions tests/AuthCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 03b992e

Please sign in to comment.