- zentrale Regel Booking::canAccessTourGallery() (Buchung oder Admin) - Galerie-Ansicht und Upload nutzen diese Regel - Bild-Auslieferung prüft die Kategorie: Tour-Bilder nur für Teilnehmer, Papierkorb nur für Admins, unbekannte Kategorien gesperrt - unberechtigte Bild-Requests liefern HTTP 403 statt Inhalt - Bilder mit Cache-Control private/must-revalidate und ETag/304 - MIME-Typ wird am Bildinhalt ermittelt (Platzhalter war JPEG als .png) - öffentlichen Link auf ein geschütztes Tour-Bild von der Startseite entfernt
74 lines
2 KiB
PHP
74 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Core\Model;
|
|
|
|
class Booking extends Model
|
|
{
|
|
protected static string $table = 'bookings';
|
|
|
|
public static function countForTour(int $tourId): int
|
|
{
|
|
return (int) db()->fetchValue('SELECT COUNT(*) FROM `bookings` WHERE tour_id = ?', [$tourId]);
|
|
}
|
|
|
|
/**
|
|
* true, wenn der Benutzer die Bildergalerie einer Tour sehen darf:
|
|
* Admins immer, sonst nur Teilnehmer (vorhandene Buchung).
|
|
*/
|
|
public static function canAccessTourGallery(int $tourId, ?User $user): bool
|
|
{
|
|
if ($user === null || $tourId <= 0) {
|
|
return false;
|
|
}
|
|
|
|
if ($user->isAdmin()) {
|
|
return true;
|
|
}
|
|
|
|
return static::forUser($tourId, (int) $user->id) !== null;
|
|
}
|
|
|
|
public static function forUser(int $tourId, int $userId): ?static
|
|
{
|
|
$row = db()->fetchOne('SELECT * FROM `bookings` WHERE tour_id = ? AND user_id = ?', [$tourId, $userId]);
|
|
|
|
return $row === null ? null : static::fromRow($row);
|
|
}
|
|
|
|
public static function forTour(int $tourId): array
|
|
{
|
|
$rows = db()->fetchAll('SELECT * FROM `bookings` WHERE tour_id = ?', [$tourId]);
|
|
|
|
return array_map(fn(array $row) => static::fromRow($row), $rows);
|
|
}
|
|
|
|
public static function deleteForUser(int $tourId, int $userId): void
|
|
{
|
|
db()->execute('DELETE FROM `bookings` WHERE tour_id = ? AND user_id = ?', [$tourId, $userId]);
|
|
}
|
|
|
|
/** Bucht einen Raum oder aktualisiert die Raumbuchung eines Benutzers. */
|
|
public static function joinRoom(int $tourId, int $userId, int $roomId): void
|
|
{
|
|
$booking = static::forUser($tourId, $userId);
|
|
|
|
if ($booking !== null) {
|
|
$booking->room_id = $roomId;
|
|
$booking->save();
|
|
|
|
return;
|
|
}
|
|
|
|
static::create([
|
|
'tour_id' => $tourId,
|
|
'user_id' => $userId,
|
|
'room_id' => $roomId,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
}
|