Add UserDevice model

This commit is contained in:
Daniel Supernault 2019-03-06 00:55:03 -07:00
parent 94130fe14c
commit ad7f44e362
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
4 changed files with 90 additions and 1 deletions

View File

@ -7,6 +7,7 @@ use App\{
Follower,
Profile,
User,
UserDevice,
UserFilter,
UserSetting
};
@ -30,6 +31,13 @@ class AuthLogin
return;
}
$this->userSettings($user);
$this->userState($user);
$this->userDevice($user);
}
protected function userSettings($user)
{
if (empty($user->settings)) {
DB::transaction(function() use($user) {
UserSetting::firstOrCreate([
@ -37,7 +45,10 @@ class AuthLogin
]);
});
}
}
protected function userState($user)
{
if($user->status != null) {
$profile = $user->profile;
if(!$profile) {
@ -66,4 +77,15 @@ class AuthLogin
}
}
}
protected function userDevice($user)
{
$device = DB::transaction(function() use($user) {
return UserDevice::firstOrCreate([
'user_id' => $user->id,
'ip' => request()->ip(),
'user_agent' => request()->userAgent(),
]);
});
}
}

View File

@ -71,4 +71,9 @@ class User extends Authenticatable
{
return 'App.User.'.$this->id;
}
public function devices()
{
return $this->hasMany(UserDevice::class);
}
}

23
app/UserDevice.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserDevice extends Model
{
protected $fillable = [
'user_id',
'ip',
'user_agent'
];
public $timestamps = [
'last_active_at'
];
public function user()
{
return $this->belongsTo(User::class);
}
}

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserDevicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_devices', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->string('ip')->index();
$table->string('user_agent')->index();
$table->string('fingerprint')->nullable();
$table->string('name')->nullable();
$table->boolean('trusted')->nullable();
$table->timestamp('last_active_at')->nullable();
$table->unique(['user_id', 'ip', 'user_agent', 'fingerprint'], 'user_ip_agent_index');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_devices');
}
}