Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m30s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m28s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m36s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 1m45s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 1m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 1m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 1m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 54s
- Rollen mit globalen Rechten und Rechten für einzelne Objekte (z. B. „Beiträge schreiben in Kategorie MAV-Info“); Rollen sind additiv, Zuweisungen merken sich ihre Herkunft (manuell oder später Identity-Provider-Gruppe) - Systemrolle „Administration“ ersetzt users.is_admin (Migration übernimmt bestehende Administratoren); nicht löschbar, man kann sie sich nicht selbst entziehen - Rechte je Person vorberechnet und gecacht, Versionszähler invalidiert bei Änderungen - Gate-Anbindung: registrierte Rechte entscheidet das Rechtesystem, Objektregeln die Policies; Admin-Seiten und Plugin-Ressourcen hinter passenden Rechten - Redaktion nach Kategorie: Schreiben und Veröffentlichen getrennt, Liste im Admin auf eigene Kategorien beschränkt, ohne Veröffentlichungsrecht bleibt ein Beitrag Entwurf - Zielgruppen (Rolle/Abteilung/Standort) für Kategorien und Termine als indizierte SQL-Bedingung in Listen, Detailseiten, Kacheln und .ics-Export - Admin: Rollen-Verwaltung, „Rechte prüfen“, Redaktion und Sichtbarkeit direkt an Kategorie bzw. Termin - Kern-Ressourcen explizit registriert, damit nach Updates liegengebliebene alte Dateien nicht doppelt geladen werden - docs/PLUGINS.md für Plugin-Entwicklung; Demo-Rolle „MAV“ Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
141 lines
4.2 KiB
PHP
141 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\News\Models;
|
||
|
||
use App\Models\User;
|
||
use App\Support\Images;
|
||
use App\Support\Permissions\Audiences;
|
||
use App\Support\RichText;
|
||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||
use Illuminate\Database\Eloquent\Builder;
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
use Illuminate\Support\Str;
|
||
use Inhaus\Plugins\News\Database\Factories\NewsPostFactory;
|
||
|
||
#[Fillable([
|
||
'category_id', 'author_id', 'title', 'slug', 'teaser', 'body',
|
||
'cover_path', 'is_featured', 'published_at',
|
||
])]
|
||
class NewsPost extends Model
|
||
{
|
||
use HasFactory;
|
||
|
||
protected static function newFactory(): NewsPostFactory
|
||
{
|
||
return NewsPostFactory::new();
|
||
}
|
||
|
||
protected static function booted(): void
|
||
{
|
||
static::saving(function (NewsPost $post): void {
|
||
if (blank($post->slug)) {
|
||
$post->slug = static::uniqueSlug($post->title, $post->getKey());
|
||
}
|
||
|
||
if ($post->isDirty('body') || $post->body_html === null) {
|
||
$richText = app(RichText::class);
|
||
$post->body_html = $richText->toSafeHtml($post->body);
|
||
$post->reading_minutes = $richText->readingMinutes($post->body_html);
|
||
}
|
||
});
|
||
|
||
// Bildvarianten erzeugen bzw. beim Austausch die alten Dateien entfernen.
|
||
static::saved(function (NewsPost $post): void {
|
||
if (! $post->wasRecentlyCreated && ! $post->wasChanged('cover_path')) {
|
||
return;
|
||
}
|
||
|
||
$previous = $post->wasRecentlyCreated ? null : $post->getOriginal('cover_path');
|
||
|
||
if ($previous && $previous !== $post->cover_path) {
|
||
app(Images::class)->deleteWithVariants($previous);
|
||
}
|
||
|
||
if ($post->cover_path) {
|
||
app(Images::class)->createVariants($post->cover_path);
|
||
}
|
||
});
|
||
|
||
static::deleted(fn (NewsPost $post) => app(Images::class)->deleteWithVariants($post->cover_path));
|
||
}
|
||
|
||
protected function casts(): array
|
||
{
|
||
return [
|
||
'is_featured' => 'boolean',
|
||
'published_at' => 'datetime',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return BelongsTo<NewsCategory, $this>
|
||
*/
|
||
public function category(): BelongsTo
|
||
{
|
||
return $this->belongsTo(NewsCategory::class);
|
||
}
|
||
|
||
/**
|
||
* @return BelongsTo<User, $this>
|
||
*/
|
||
public function author(): BelongsTo
|
||
{
|
||
return $this->belongsTo(User::class);
|
||
}
|
||
|
||
/**
|
||
* Veröffentlicht = Veröffentlichungsdatum gesetzt und erreicht.
|
||
*
|
||
* @param Builder<NewsPost> $query
|
||
*/
|
||
public function scopePublished(Builder $query): void
|
||
{
|
||
$query->whereNotNull('published_at')->where('published_at', '<=', now());
|
||
}
|
||
|
||
public function isPublished(): bool
|
||
{
|
||
return $this->published_at !== null && $this->published_at->isPast();
|
||
}
|
||
|
||
public function coverUrl(string $variant = 'large'): ?string
|
||
{
|
||
return app(Images::class)->url($this->cover_path, $variant);
|
||
}
|
||
|
||
public static function uniqueSlug(string $title, ?int $ignoreId = null): string
|
||
{
|
||
$base = Str::slug($title, language: 'de') ?: 'beitrag';
|
||
$slug = $base;
|
||
|
||
for ($i = 2; static::query()->where('slug', $slug)->when($ignoreId, fn ($q) => $q->whereKeyNot($ignoreId))->exists(); $i++) {
|
||
$slug = "{$base}-{$i}";
|
||
}
|
||
|
||
return $slug;
|
||
}
|
||
|
||
/**
|
||
* Nur Beiträge, die diese Person sehen darf – die Zielgruppe hängt an der Kategorie.
|
||
*
|
||
* @param Builder<NewsPost> $query
|
||
*/
|
||
public function scopeVisibleTo(Builder $query, ?User $user): void
|
||
{
|
||
if ($user?->can('news.manage')) {
|
||
return;
|
||
}
|
||
|
||
$query->where(function (Builder $q) use ($user) {
|
||
$q->whereNull('news_posts.category_id');
|
||
$q->orWhere(fn (Builder $inner) => Audiences::constrain($inner, 'news_category', 'news_posts.category_id', $user));
|
||
});
|
||
}
|
||
|
||
public function isVisibleTo(?User $user): bool
|
||
{
|
||
return $this->category_id === null || $user?->can('news.manage') || Audiences::allows('news_category', $this->category_id, $user);
|
||
}
|
||
}
|