Przeglądaj źródła

Add UserDevice model

Daniel Supernault 6 lat temu
rodzic
commit
ad7f44e362

+ 23 - 1
app/Listeners/AuthLogin.php

@@ -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(),
+            ]);
+        });
+    }
 }

+ 5 - 0
app/User.php

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

+ 23 - 0
app/UserDevice.php

@@ -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);
+    }
+}

+ 39 - 0
database/migrations/2019_03_06_065528_create_user_devices_table.php

@@ -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');
+    }
+}