diff --git a/app/Listeners/AuthLogin.php b/app/Listeners/AuthLogin.php index 22cc0c2fe..7d3059a8f 100644 --- a/app/Listeners/AuthLogin.php +++ b/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(), + ]); + }); + } } diff --git a/app/User.php b/app/User.php index 8ed942d11..509b2b914 100644 --- a/app/User.php +++ b/app/User.php @@ -71,4 +71,9 @@ class User extends Authenticatable { return 'App.User.'.$this->id; } + + public function devices() + { + return $this->hasMany(UserDevice::class); + } } diff --git a/app/UserDevice.php b/app/UserDevice.php new file mode 100644 index 000000000..0ef037a35 --- /dev/null +++ b/app/UserDevice.php @@ -0,0 +1,23 @@ +belongsTo(User::class); + } +} diff --git a/database/migrations/2019_03_06_065528_create_user_devices_table.php b/database/migrations/2019_03_06_065528_create_user_devices_table.php new file mode 100644 index 000000000..f9c728c4a --- /dev/null +++ b/database/migrations/2019_03_06_065528_create_user_devices_table.php @@ -0,0 +1,39 @@ +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'); + } +}