Stelle das U0-Fundament mit CMS, Mitgliedern und Filament bereit.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
89b35ac480
commit
2035c46edd
130 files changed
+18519
-50
No files matched your search
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms\Blocks;
|
||||
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class BlockRegistry
|
||||
{
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function labels(): array
|
||||
{
|
||||
return [
|
||||
'heading' => 'Überschrift und Einleitung',
|
||||
'prose' => 'Fließtext',
|
||||
'image_text' => 'Bild und Text',
|
||||
'call_to_action' => 'Handlungsaufruf',
|
||||
'cards' => 'Karten',
|
||||
'faq' => 'FAQ',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string, mixed>> $blocks
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public function normalize(array $blocks): array
|
||||
{
|
||||
$normalized = [];
|
||||
|
||||
foreach (array_values($blocks) as $index => $block) {
|
||||
if (! is_array($block)) {
|
||||
throw ValidationException::withMessages([
|
||||
"blocks.$index" => 'Ein Baustein ist unvollständig.',
|
||||
]);
|
||||
}
|
||||
|
||||
$type = $block['type'] ?? null;
|
||||
|
||||
if (! is_string($type) || ! array_key_exists($type, $this->labels())) {
|
||||
throw ValidationException::withMessages([
|
||||
"blocks.$index.type" => 'Dieser Bausteintyp ist nicht freigegeben.',
|
||||
]);
|
||||
}
|
||||
|
||||
$normalized[] = $this->normalizeOne($type, $block, $index);
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $block
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function normalizeOne(string $type, array $block, int $index): array
|
||||
{
|
||||
$rules = match ($type) {
|
||||
'heading' => [
|
||||
'heading' => ['required', 'string', 'max:200'],
|
||||
'intro' => ['nullable', 'string', 'max:1000'],
|
||||
],
|
||||
'prose' => [
|
||||
'body' => ['required', 'string', 'max:20000'],
|
||||
],
|
||||
'image_text' => [
|
||||
'image' => ['nullable', 'string', 'max:255'],
|
||||
'alt' => ['required', 'string', 'max:300'],
|
||||
'body' => ['required', 'string', 'max:5000'],
|
||||
],
|
||||
'call_to_action' => [
|
||||
'heading' => ['required', 'string', 'max:200'],
|
||||
'text' => ['nullable', 'string', 'max:1000'],
|
||||
'label' => ['required', 'string', 'max:80'],
|
||||
'url' => ['required', 'string', 'max:500'],
|
||||
],
|
||||
'cards' => [
|
||||
'items' => ['required', 'array', 'min:1', 'max:6'],
|
||||
'items.*.title' => ['required', 'string', 'max:120'],
|
||||
'items.*.text' => ['nullable', 'string', 'max:500'],
|
||||
'items.*.url' => ['nullable', 'string', 'max:500'],
|
||||
],
|
||||
'faq' => [
|
||||
'items' => ['required', 'array', 'min:1', 'max:20'],
|
||||
'items.*.question' => ['required', 'string', 'max:300'],
|
||||
'items.*.answer' => ['required', 'string', 'max:2000'],
|
||||
],
|
||||
default => [],
|
||||
};
|
||||
|
||||
$validator = Validator::make($block, $rules);
|
||||
|
||||
if ($validator->fails()) {
|
||||
$messages = [];
|
||||
|
||||
foreach ($validator->errors()->messages() as $key => $errors) {
|
||||
$messages["blocks.$index.$key"] = $errors;
|
||||
}
|
||||
|
||||
throw ValidationException::withMessages($messages);
|
||||
}
|
||||
|
||||
$clean = ['type' => $type];
|
||||
|
||||
foreach (array_keys($rules) as $key) {
|
||||
if (str_contains($key, '.*')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$clean[$key] = $block[$key] ?? null;
|
||||
}
|
||||
|
||||
if ($type === 'image_text') {
|
||||
$clean['image'] = $this->imagePath($clean['image'] ?? null, $index);
|
||||
}
|
||||
|
||||
if (in_array($type, ['call_to_action', 'cards'], true)) {
|
||||
$clean = $this->assertSafeUrls($clean, $index);
|
||||
}
|
||||
|
||||
return $clean;
|
||||
}
|
||||
|
||||
private function imagePath(mixed $image, int $index): ?string
|
||||
{
|
||||
if (is_array($image)) {
|
||||
$image = $image[0] ?? null;
|
||||
}
|
||||
|
||||
if ($image === null || $image === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_string($image) || str_contains($image, '..') || ! str_starts_with($image, 'cms/')) {
|
||||
throw ValidationException::withMessages([
|
||||
"blocks.$index.image" => 'Das Bild liegt nicht im öffentlichen Medienbereich.',
|
||||
]);
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $block
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function assertSafeUrls(array $block, int $index): array
|
||||
{
|
||||
if (array_key_exists('url', $block)) {
|
||||
$block['url'] = $this->safeUrl($block['url'], "blocks.$index.url");
|
||||
}
|
||||
|
||||
if (isset($block['items']) && is_array($block['items'])) {
|
||||
foreach ($block['items'] as $itemIndex => $item) {
|
||||
if (is_array($item) && filled($item['url'] ?? null)) {
|
||||
$block['items'][$itemIndex]['url'] = $this->safeUrl($item['url'], "blocks.$index.items.$itemIndex.url");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $block;
|
||||
}
|
||||
|
||||
private function safeUrl(mixed $url, string $key): string
|
||||
{
|
||||
if (! is_string($url)) {
|
||||
throw ValidationException::withMessages([$key => 'Die Adresse ist ungültig.']);
|
||||
}
|
||||
|
||||
$isRelative = str_starts_with($url, '/') && ! str_starts_with($url, '//');
|
||||
$isAbsolute = preg_match('#^https?://#i', $url) === 1;
|
||||
|
||||
if (! $isRelative && ! $isAbsolute) {
|
||||
throw ValidationException::withMessages([$key => 'Erlaubt sind Seitenpfade oder http(s)-Adressen.']);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class NavigationItem extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'label',
|
||||
'page_id',
|
||||
'url',
|
||||
'placement',
|
||||
'sort_order',
|
||||
];
|
||||
|
||||
public function page(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Page::class);
|
||||
}
|
||||
|
||||
public function href(): ?string
|
||||
{
|
||||
if ($this->page !== null) {
|
||||
return $this->page->path === '/' ? url('/') : url($this->page->path);
|
||||
}
|
||||
|
||||
if ($this->url === null || $this->url === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (str_starts_with($this->url, '/') && ! str_starts_with($this->url, '//')) {
|
||||
return url($this->url);
|
||||
}
|
||||
|
||||
return $this->url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms;
|
||||
|
||||
use App\Domain\Identity\StaffRole;
|
||||
use App\Models\User;
|
||||
|
||||
class NavigationItemPolicy
|
||||
{
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return $user->hasStaffRole(StaffRole::Redakteur);
|
||||
}
|
||||
|
||||
public function view(User $user, NavigationItem $item): bool
|
||||
{
|
||||
return $this->viewAny($user);
|
||||
}
|
||||
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return $this->viewAny($user);
|
||||
}
|
||||
|
||||
public function update(User $user, NavigationItem $item): bool
|
||||
{
|
||||
return $this->viewAny($user);
|
||||
}
|
||||
|
||||
public function delete(User $user, NavigationItem $item): bool
|
||||
{
|
||||
return $this->viewAny($user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
class Page extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'path',
|
||||
];
|
||||
|
||||
public function revisions(): HasMany
|
||||
{
|
||||
return $this->hasMany(PageRevision::class)->latest('id');
|
||||
}
|
||||
|
||||
public function publishedRevision(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(PageRevision::class, 'published_revision_id');
|
||||
}
|
||||
|
||||
public function latestRevision(): HasOne
|
||||
{
|
||||
return $this->hasOne(PageRevision::class)->latestOfMany();
|
||||
}
|
||||
|
||||
public function navigationItems(): HasMany
|
||||
{
|
||||
return $this->hasMany(NavigationItem::class);
|
||||
}
|
||||
|
||||
public function isPublished(): bool
|
||||
{
|
||||
return $this->published_revision_id !== null;
|
||||
}
|
||||
|
||||
public function publicUrl(): string
|
||||
{
|
||||
return $this->path === '/' ? url('/') : url($this->path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms;
|
||||
|
||||
use App\Domain\Identity\StaffRole;
|
||||
use App\Models\User;
|
||||
|
||||
class PagePolicy
|
||||
{
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return $user->hasStaffRole(StaffRole::Redakteur);
|
||||
}
|
||||
|
||||
public function view(User $user, Page $page): bool
|
||||
{
|
||||
return $this->viewAny($user);
|
||||
}
|
||||
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return $this->viewAny($user);
|
||||
}
|
||||
|
||||
public function update(User $user, Page $page): bool
|
||||
{
|
||||
return $this->viewAny($user);
|
||||
}
|
||||
|
||||
public function publish(User $user, Page $page): bool
|
||||
{
|
||||
return $this->viewAny($user);
|
||||
}
|
||||
|
||||
public function delete(User $user, Page $page): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class PageRevision extends Model
|
||||
{
|
||||
public const UPDATED_AT = null;
|
||||
|
||||
protected $fillable = [
|
||||
'page_id',
|
||||
'blocks',
|
||||
'seo_title',
|
||||
'seo_description',
|
||||
'created_by',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'blocks' => 'array',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function page(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Page::class);
|
||||
}
|
||||
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms;
|
||||
|
||||
use App\Domain\Cms\Blocks\BlockRegistry;
|
||||
use App\Jobs\PublishPublicMedia;
|
||||
use App\Models\User;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class PageService
|
||||
{
|
||||
public function __construct(private readonly BlockRegistry $blocks) {}
|
||||
|
||||
/**
|
||||
* @param list<array<string, mixed>> $blocks
|
||||
*/
|
||||
public function saveRevision(Page $page, array $blocks, ?string $seoTitle, ?string $seoDescription, ?User $author): PageRevision
|
||||
{
|
||||
$normalized = $this->blocks->normalize($blocks);
|
||||
|
||||
$revision = $page->revisions()->create([
|
||||
'blocks' => $normalized,
|
||||
'seo_title' => $seoTitle !== null && $seoTitle !== '' ? $seoTitle : null,
|
||||
'seo_description' => $seoDescription !== null && $seoDescription !== '' ? $seoDescription : null,
|
||||
'created_by' => $author?->id,
|
||||
]);
|
||||
|
||||
foreach ($normalized as $block) {
|
||||
if (($block['type'] ?? null) === 'image_text' && filled($block['image'] ?? null)) {
|
||||
PublishPublicMedia::dispatch($block['image']);
|
||||
}
|
||||
}
|
||||
|
||||
return $revision;
|
||||
}
|
||||
|
||||
public function publish(Page $page, PageRevision $revision): void
|
||||
{
|
||||
if ($revision->page_id !== $page->id) {
|
||||
throw ValidationException::withMessages([
|
||||
'revision' => 'Die Revision gehört zu einer anderen Seite.',
|
||||
]);
|
||||
}
|
||||
|
||||
$page->published_revision_id = $revision->id;
|
||||
$page->save();
|
||||
}
|
||||
|
||||
public function restore(Page $page, PageRevision $source, ?User $author): PageRevision
|
||||
{
|
||||
if ($source->page_id !== $page->id) {
|
||||
throw ValidationException::withMessages([
|
||||
'revision' => 'Die Revision gehört zu einer anderen Seite.',
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->saveRevision(
|
||||
$page,
|
||||
$source->blocks,
|
||||
$source->seo_title,
|
||||
$source->seo_description,
|
||||
$author,
|
||||
);
|
||||
}
|
||||
|
||||
public function normalizePath(string $path): string
|
||||
{
|
||||
$path = '/'.trim($path, '/');
|
||||
|
||||
if ($path === '/') {
|
||||
return '/';
|
||||
}
|
||||
|
||||
$path = mb_strtolower($path);
|
||||
|
||||
if (preg_match('#^/[a-z0-9]+(?:-[a-z0-9]+)*$#', $path) !== 1) {
|
||||
throw ValidationException::withMessages([
|
||||
'path' => 'Der Pfad darf nur Kleinbuchstaben, Ziffern und Bindestriche enthalten.',
|
||||
]);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Identity;
|
||||
|
||||
enum AccountStatus: string
|
||||
{
|
||||
case Aktiv = 'aktiv';
|
||||
case Gesperrt = 'gesperrt';
|
||||
case Geloescht = 'geloescht';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Aktiv => 'Aktiv',
|
||||
self::Gesperrt => 'Gesperrt',
|
||||
self::Geloescht => 'Gelöscht',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Identity;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class AssignStaffRoles
|
||||
{
|
||||
/**
|
||||
* @param list<string> $roles
|
||||
*/
|
||||
public function sync(User $user, array $roles, User $actor): void
|
||||
{
|
||||
$roles = array_values(array_unique($roles));
|
||||
|
||||
foreach ($roles as $role) {
|
||||
if (StaffRole::tryFrom($role) === null) {
|
||||
throw ValidationException::withMessages([
|
||||
'roles' => 'Unbekannte Personalrolle.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
$actor->hasStaffRole(StaffRole::Systemverwalter)
|
||||
&& $user->is($actor)
|
||||
&& ! in_array(StaffRole::Systemverwalter->value, $roles, true)
|
||||
) {
|
||||
throw ValidationException::withMessages([
|
||||
'roles' => 'Das eigene Systemverwalter-Recht bleibt bestehen.',
|
||||
]);
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($user, $roles): void {
|
||||
$user->staffRoles()->delete();
|
||||
|
||||
foreach ($roles as $role) {
|
||||
$user->staffRoles()->create(['role' => $role]);
|
||||
}
|
||||
});
|
||||
|
||||
$user->unsetRelation('staffRoles');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Identity;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
class Party extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'type',
|
||||
'display_name',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'type' => PartyType::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function user(): HasOne
|
||||
{
|
||||
return $this->hasOne(User::class);
|
||||
}
|
||||
|
||||
public function profile(): HasOne
|
||||
{
|
||||
return $this->hasOne(Profile::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Identity;
|
||||
|
||||
enum PartyType: string
|
||||
{
|
||||
case Person = 'person';
|
||||
case Organisation = 'organisation';
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Identity;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Profile extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'party_id',
|
||||
'display_name',
|
||||
'visibility',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'visibility' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function party(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Party::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Identity;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class RegisterAccount
|
||||
{
|
||||
public function register(string $name, string $email, string $password, bool $adult): User
|
||||
{
|
||||
if (! $adult) {
|
||||
throw ValidationException::withMessages([
|
||||
'adult' => 'Die Plattform richtet sich an Erwachsene. Ein Konto setzt die Bestätigung voraus.',
|
||||
]);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($name, $email, $password): User {
|
||||
$party = Party::query()->create([
|
||||
'type' => PartyType::Person,
|
||||
'display_name' => $name,
|
||||
]);
|
||||
|
||||
Profile::query()->create([
|
||||
'party_id' => $party->id,
|
||||
'display_name' => $name,
|
||||
'visibility' => ['display_name' => 'private'],
|
||||
]);
|
||||
|
||||
return User::query()->create([
|
||||
'party_id' => $party->id,
|
||||
'name' => $name,
|
||||
'email' => $email,
|
||||
'password' => $password,
|
||||
'status' => AccountStatus::Aktiv,
|
||||
'confirmed_adult_at' => now(),
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Identity;
|
||||
|
||||
enum StaffRole: string
|
||||
{
|
||||
case Redakteur = 'redakteur';
|
||||
case Fachpruefer = 'fachpruefer';
|
||||
case Katalogverwalter = 'katalogverwalter';
|
||||
case Kundenbetreuung = 'kundenbetreuung';
|
||||
case Verifikationspruefer = 'verifikationspruefer';
|
||||
case Moderator = 'moderator';
|
||||
case Systemverwalter = 'systemverwalter';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Redakteur => 'Redakteur',
|
||||
self::Fachpruefer => 'Fachprüfer',
|
||||
self::Katalogverwalter => 'Katalogverwalter',
|
||||
self::Kundenbetreuung => 'Kundenbetreuung',
|
||||
self::Verifikationspruefer => 'Verifikationsprüfer',
|
||||
self::Moderator => 'Moderator',
|
||||
self::Systemverwalter => 'Systemverwalter',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function options(): array
|
||||
{
|
||||
$options = [];
|
||||
|
||||
foreach (self::cases() as $role) {
|
||||
$options[$role->value] = $role->label();
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Identity;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class StaffRoleAssignment extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'role',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'role' => StaffRole::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user