Save user id in Session model

This commit is contained in:
Igor Scheller 2023-09-17 15:03:56 +02:00 committed by Michael Weimann
parent 67d5950926
commit 102c8428c8
8 changed files with 104 additions and 2 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Database\Factories\Engelsystem\Models;
use Engelsystem\Models\Session;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class SessionFactory extends Factory
@ -17,6 +18,7 @@ class SessionFactory extends Factory
return [
'id' => $this->faker->lexify('??????????'),
'payload' => $this->faker->text(100),
'user_id' => $this->faker->optional()->passthrough(User::factory()),
];
}
}

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Schema\Blueprint;
class AddUserToSessionsTable extends Migration
{
use Reference;
/**
* Run the migration
*/
public function up(): void
{
$this->schema->table('sessions', function (Blueprint $table): void {
$this->referencesUser($table)->nullable()->index()->after('payload');
});
}
/**
* Reverse the migration
*/
public function down(): void
{
$this->schema->table('sessions', function (Blueprint $table): void {
$table->dropForeign('sessions_user_id_foreign');
$table->dropColumn('user_id');
});
}
}

View File

@ -33,6 +33,7 @@ class DatabaseHandler extends AbstractHandler
$session->id = $id;
$session->payload = $data;
$session->last_activity = Carbon::now();
$session->user_id = auth()->user()?->id;
$session->save();
// The save return can't be used directly as it won't change if the second call is in the same second

View File

@ -5,14 +5,19 @@ declare(strict_types=1);
namespace Engelsystem\Models;
use Carbon\Carbon;
use Engelsystem\Models\User\User;
use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @property string $id
* @property string $payload
* @property int|null $user_id
* @property Carbon $last_activity
*
* @property-read User|null $user
*
* @method static Builder|Session whereId($value)
* @method static Builder|Session wherePayload($value)
* @method static Builder|Session whereLastActivity($value)
@ -20,6 +25,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
class Session extends BaseModel
{
use HasFactory;
use UsesUserModel;
public $incrementing = false; // phpcs:ignore
@ -28,6 +34,7 @@ class Session extends BaseModel
/** @var array<string, string|null> default attributes */
protected $attributes = [ // phpcs:ignore
'payload' => '',
'user_id' => null,
];
/** @var array<string> */
@ -39,5 +46,11 @@ class Session extends BaseModel
protected $fillable = [ // phpcs:ignore
'id',
'payload',
'user_id',
];
/** @var array<string, string> */
protected $casts = [ // phpcs:ignore
'user_id' => 'integer',
];
}

View File

@ -14,6 +14,7 @@ use Engelsystem\Models\NewsComment;
use Engelsystem\Models\OAuth;
use Engelsystem\Models\Privilege;
use Engelsystem\Models\Question;
use Engelsystem\Models\Session;
use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftEntry;
use Engelsystem\Models\UserAngelType;
@ -52,6 +53,7 @@ use Illuminate\Support\Collection as SupportCollection;
* @property-read Collection|AngelType[] $userAngelTypes
* @property-read UserAngelType $pivot
* @property-read Collection|ShiftEntry[] $shiftEntries
* @property-read Collection|Session[] $sessions
* @property-read Collection|Worklog[] $worklogs
* @property-read Collection|Worklog[] $worklogsCreated
* @property-read Collection|Question[] $questionsAsked
@ -222,6 +224,11 @@ class User extends BaseModel
return $this->hasMany(Worklog::class, 'creator_id');
}
public function sessions(): HasMany
{
return $this->hasMany(Session::class);
}
public function questionsAsked(): HasMany
{
return $this->hasMany(Question::class, 'user_id')

View File

@ -5,8 +5,11 @@ declare(strict_types=1);
namespace Engelsystem\Test\Unit\Http\SessionHandlers;
use Engelsystem\Config\Config;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Carbon;
use Engelsystem\Http\SessionHandlers\DatabaseHandler;
use Engelsystem\Models\Session;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
@ -36,16 +39,34 @@ class DatabaseHandlerTest extends TestCase
*/
public function testWrite(): void
{
$user = User::factory()->create();
$auth = $this->createMock(Authenticator::class);
$auth->expects($this->exactly(2))
->method('user')
->willReturnOnConsecutiveCalls(null, $user);
$this->app->instance('authenticator', $auth);
$handler = new DatabaseHandler($this->database);
$userExists = false;
foreach (['Lorem Ipsum', 'Dolor Sit!'] as $data) {
$this->assertTrue($handler->write('id-foo', $data));
$return = Session::whereId('id-foo')->get();
$this->assertCount(1, $return);
$return = $return->first();
$this->assertEquals($data, $return->payload);
/** @var Session $session */
$session = $return->first();
$this->assertEquals($data, $session->payload);
if ($userExists) {
$this->assertNotNull($session->user);
$this->assertEquals($user->id, $session->user->id);
} else {
$this->assertNull($session->user);
}
$userExists = true;
}
}

View File

@ -6,6 +6,7 @@ namespace Engelsystem\Test\Unit\Models;
use Engelsystem\Helpers\Carbon;
use Engelsystem\Models\Session;
use Engelsystem\Models\User\User;
/**
* This class provides tests for the Session model
@ -19,9 +20,11 @@ class SessionTest extends ModelTest
*/
public function testCreate(): void
{
$user = User::factory()->create();
Session::create([
'id' => 'foo',
'payload' => 'lorem ipsum',
'user_id' => $user->id,
'last_activity' => Carbon::now(),
]);
Session::create([
@ -32,5 +35,9 @@ class SessionTest extends ModelTest
$session = Session::find('foo');
$this->assertNotNull($session);
$this->assertEquals('lorem ipsum', $session->payload);
$this->assertInstanceOf(User::class, $session->user);
$session = Session::find('bar');
$this->assertNull($session->user);
}
}

View File

@ -15,6 +15,7 @@ use Engelsystem\Models\NewsComment;
use Engelsystem\Models\OAuth;
use Engelsystem\Models\Privilege;
use Engelsystem\Models\Question;
use Engelsystem\Models\Session;
use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftEntry;
use Engelsystem\Models\User\Contact;
@ -375,6 +376,22 @@ class UserTest extends ModelTest
$this->assertCount(2, $user->shiftEntries);
}
/**
* @covers \Engelsystem\Models\User\User::sessions
*/
public function testSessions(): void
{
$user = new User($this->data);
$user->save();
Session::factory(2)->create();
Session::factory(3)->create(['user_id' => $user->id]);
Session::factory(2)->create();
Session::factory(4)->create(['user_id' => $user->id]);
$this->assertCount(7, $user->sessions);
}
/**
* @covers \Engelsystem\Models\User\User::worklogs
*/