feat(license): EnsureActiveLicense full implementation + license/dashboard routes

- EnsureActiveLicense: full child-role check via licenseAssignments().isActive()
- License model: add isActive() method, fix fillable order, expires_at cast to date
- LicenseFactory: active/expired/inactive states
- routes/web.php: /dashboard with auth+license middleware, /lizenz-abgelaufen
- tests: EnsureActiveLicenseTest (3 tests — block/allow/teacher bypass)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
main
Boban Blaskovic 2026-05-22 05:16:06 +02:00
parent ac945ad4f0
commit 6de4131389
5 changed files with 100 additions and 5 deletions

View File

@ -9,7 +9,21 @@ class EnsureActiveLicense
{
public function handle(Request $request, Closure $next): mixed
{
// Full implementation in Phase 4 (requires License model)
$user = $request->user();
if (!$user || !$user->hasRole('child')) {
return $next($request);
}
$hasActive = $user->licenseAssignments()
->with('license')
->get()
->some(fn($a) => $a->license && $a->license->isActive());
if (!$hasActive) {
return redirect('/lizenz-abgelaufen');
}
return $next($request);
}
}

View File

@ -12,20 +12,27 @@ class License extends Model
{
use HasFactory, HasUuid, BelongsToTenant, SoftDeletes;
protected $fillable = ['type', 'seats', 'expires_at', 'active', 'tenant_id'];
protected $fillable = ['tenant_id', 'type', 'seats', 'expires_at', 'active'];
protected $casts = [
'active' => 'boolean',
'expires_at' => 'date',
'active' => 'boolean',
];
public function tenant()
public function isActive(): bool
{
return $this->belongsTo(Tenant::class);
if (!$this->active) return false;
if ($this->expires_at && $this->expires_at->isPast()) return false;
return true;
}
public function assignments()
{
return $this->hasMany(LicenseAssignment::class);
}
public function tenant()
{
return $this->belongsTo(Tenant::class);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Database\Factories;
use App\Models\Tenant;
use Illuminate\Database\Eloquent\Factories\Factory;
class LicenseFactory extends Factory
{
public function definition(): array
{
return [
'tenant_id' => Tenant::factory(),
'type' => 'per_student',
'seats' => 30,
'expires_at' => now()->addYear(),
'active' => true,
];
}
public function active(): static
{
return $this->state([
'active' => true,
'expires_at' => now()->addYear(),
]);
}
public function expired(): static
{
return $this->state([
'active' => true,
'expires_at' => now()->subDay(),
]);
}
public function inactive(): static
{
return $this->state(['active' => false]);
}
}

View File

@ -10,3 +10,9 @@ Route::get('/', function () {
Route::middleware(['auth', 'role:school-admin,teacher'])->group(function () {
Route::get('/school/dashboard', fn() => response('ok'))->name('school.dashboard');
});
Route::middleware(['auth', 'license'])->group(function () {
Route::get('/dashboard', fn() => response('ok'))->name('dashboard');
});
Route::get('/lizenz-abgelaufen', fn() => response('Lizenz abgelaufen', 200))->name('lizenz.abgelaufen');

View File

@ -0,0 +1,27 @@
<?php
use App\Models\License;
use App\Models\Tenant;
use App\Models\User;
use function Pest\Laravel\actingAs;
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
it('blockt child-Zugriff bei abgelaufener Lizenz', function () {
$tenant = Tenant::factory()->create();
$license = License::factory()->for($tenant)->expired()->create();
$child = User::factory()->for($tenant)->withRole('child')->withLicense($license)->create();
actingAs($child)->get('/dashboard')->assertRedirect('/lizenz-abgelaufen');
});
it('erlaubt child-Zugriff bei aktiver Lizenz', function () {
$tenant = Tenant::factory()->create();
$license = License::factory()->for($tenant)->active()->create();
$child = User::factory()->for($tenant)->withRole('child')->withLicense($license)->create();
actingAs($child)->get('/dashboard')->assertOk();
});
it('erlaubt Lehrer-Zugriff ohne Lizenz-Check', function () {
$teacher = User::factory()->withRole('teacher')->create();
actingAs($teacher)->get('/dashboard')->assertOk();
});