Merge pull request #973 from pixelfed/frontend-ui-refactor

Add UserDevice model
This commit is contained in:
daniel 2019-03-06 01:01:30 -07:00 committed by GitHub
commit 5a94ed7ba7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 1 deletions

View file

@ -7,6 +7,7 @@ use App\{
Follower, Follower,
Profile, Profile,
User, User,
UserDevice,
UserFilter, UserFilter,
UserSetting UserSetting
}; };
@ -30,6 +31,13 @@ class AuthLogin
return; return;
} }
$this->userSettings($user);
$this->userState($user);
$this->userDevice($user);
}
protected function userSettings($user)
{
if (empty($user->settings)) { if (empty($user->settings)) {
DB::transaction(function() use($user) { DB::transaction(function() use($user) {
UserSetting::firstOrCreate([ UserSetting::firstOrCreate([
@ -37,7 +45,10 @@ class AuthLogin
]); ]);
}); });
} }
}
protected function userState($user)
{
if($user->status != null) { if($user->status != null) {
$profile = $user->profile; $profile = $user->profile;
if(!$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; 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');
}
}