Implement compliance exports and retention overrides
This commit is contained in:
190
app/Http/Controllers/Api/Tenant/DataExportController.php
Normal file
190
app/Http/Controllers/Api/Tenant/DataExportController.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Tenant;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Tenant\DataExportStoreRequest;
|
||||
use App\Jobs\GenerateDataExport;
|
||||
use App\Models\DataExport;
|
||||
use App\Models\Event;
|
||||
use App\Models\Tenant;
|
||||
use App\Support\ApiError;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class DataExportController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$tenant = $this->resolveTenant($request);
|
||||
|
||||
$exports = DataExport::query()
|
||||
->with('event')
|
||||
->where('tenant_id', $tenant->id)
|
||||
->whereIn('scope', ['tenant', 'event'])
|
||||
->latest()
|
||||
->limit(10)
|
||||
->get()
|
||||
->map(fn (DataExport $export) => [
|
||||
'id' => $export->id,
|
||||
'scope' => $export->scope?->value ?? $export->scope,
|
||||
'status' => $export->status,
|
||||
'include_media' => (bool) $export->include_media,
|
||||
'size_bytes' => $export->size_bytes,
|
||||
'created_at' => optional($export->created_at)->toIso8601String(),
|
||||
'expires_at' => optional($export->expires_at)->toIso8601String(),
|
||||
'download_url' => $export->isReady() && ! $export->hasExpired()
|
||||
? route('api.v1.tenant.exports.download', $export)
|
||||
: null,
|
||||
'error_message' => $export->error_message,
|
||||
'event' => $export->event ? [
|
||||
'id' => $export->event->id,
|
||||
'slug' => $export->event->slug,
|
||||
'name' => $export->event->name,
|
||||
] : null,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'data' => $exports,
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(DataExportStoreRequest $request): JsonResponse
|
||||
{
|
||||
$tenant = $this->resolveTenant($request);
|
||||
$user = $request->user();
|
||||
|
||||
if (! $user) {
|
||||
return ApiError::response(
|
||||
'export_user_missing',
|
||||
'Export user missing',
|
||||
'Unable to determine the requesting user.',
|
||||
Response::HTTP_UNAUTHORIZED
|
||||
);
|
||||
}
|
||||
|
||||
$payload = $request->validated();
|
||||
$scope = $payload['scope'];
|
||||
$event = null;
|
||||
|
||||
if ($scope === 'event') {
|
||||
$event = Event::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->find($payload['event_id']);
|
||||
|
||||
if (! $event) {
|
||||
return ApiError::response(
|
||||
'export_event_missing',
|
||||
'Event not found',
|
||||
'The selected event does not exist for this tenant.',
|
||||
Response::HTTP_NOT_FOUND
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$hasInProgress = DataExport::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->whereIn('status', [DataExport::STATUS_PENDING, DataExport::STATUS_PROCESSING])
|
||||
->exists();
|
||||
|
||||
if ($hasInProgress) {
|
||||
return ApiError::response(
|
||||
'export_in_progress',
|
||||
'Export already in progress',
|
||||
'Please wait for the current export to finish before requesting another.',
|
||||
Response::HTTP_CONFLICT
|
||||
);
|
||||
}
|
||||
|
||||
$export = DataExport::query()->create([
|
||||
'user_id' => $user->id,
|
||||
'tenant_id' => $tenant->id,
|
||||
'event_id' => $event?->id,
|
||||
'scope' => $scope,
|
||||
'include_media' => (bool) ($payload['include_media'] ?? false),
|
||||
'status' => DataExport::STATUS_PENDING,
|
||||
]);
|
||||
|
||||
GenerateDataExport::dispatch($export->id);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Export started.',
|
||||
'data' => [
|
||||
'id' => $export->id,
|
||||
'scope' => $export->scope?->value ?? $export->scope,
|
||||
'status' => $export->status,
|
||||
'include_media' => (bool) $export->include_media,
|
||||
'created_at' => optional($export->created_at)->toIso8601String(),
|
||||
],
|
||||
], Response::HTTP_ACCEPTED);
|
||||
}
|
||||
|
||||
public function download(Request $request, DataExport $export): StreamedResponse|JsonResponse
|
||||
{
|
||||
$tenant = $this->resolveTenant($request);
|
||||
|
||||
if ((int) $export->tenant_id !== (int) $tenant->id) {
|
||||
return ApiError::response(
|
||||
'export_not_found',
|
||||
'Export not found',
|
||||
'The requested export is not available for this tenant.',
|
||||
Response::HTTP_NOT_FOUND
|
||||
);
|
||||
}
|
||||
|
||||
if (! $export->isReady() || $export->hasExpired() || ! $export->path) {
|
||||
return ApiError::response(
|
||||
'export_not_ready',
|
||||
'Export not ready',
|
||||
'The export is not ready or has expired.',
|
||||
Response::HTTP_BAD_REQUEST
|
||||
);
|
||||
}
|
||||
|
||||
$disk = 'local';
|
||||
|
||||
if (! Storage::disk($disk)->exists($export->path)) {
|
||||
return ApiError::response(
|
||||
'export_missing',
|
||||
'Export not found',
|
||||
'The export archive could not be located.',
|
||||
Response::HTTP_NOT_FOUND
|
||||
);
|
||||
}
|
||||
|
||||
return Storage::disk($disk)->download(
|
||||
$export->path,
|
||||
sprintf('fotospiel-data-export-%s.zip', $export->created_at?->format('Ymd') ?? now()->format('Ymd')),
|
||||
[
|
||||
'Cache-Control' => 'private, no-store',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function resolveTenant(Request $request): Tenant
|
||||
{
|
||||
$tenant = $request->attributes->get('tenant');
|
||||
|
||||
if ($tenant instanceof Tenant) {
|
||||
return $tenant;
|
||||
}
|
||||
|
||||
$tenantId = $request->attributes->get('tenant_id')
|
||||
?? $request->attributes->get('current_tenant_id')
|
||||
?? $request->user()?->tenant_id;
|
||||
|
||||
if ($tenantId) {
|
||||
$tenant = Tenant::query()->find($tenantId);
|
||||
if ($tenant) {
|
||||
$request->attributes->set('tenant', $tenant);
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
}
|
||||
|
||||
abort(401, 'Tenant context missing.');
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\DataExportScope;
|
||||
use App\Models\DataExport;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -45,6 +46,7 @@ class ProfileController extends Controller
|
||||
->all();
|
||||
|
||||
$recentExports = $user->dataExports()
|
||||
->where('scope', DataExportScope::USER->value)
|
||||
->latest()
|
||||
->limit(5)
|
||||
->get()
|
||||
@@ -61,6 +63,7 @@ class ProfileController extends Controller
|
||||
]);
|
||||
|
||||
$pendingExport = $user->dataExports()
|
||||
->where('scope', DataExportScope::USER->value)
|
||||
->whereIn('status', [
|
||||
DataExport::STATUS_PENDING,
|
||||
DataExport::STATUS_PROCESSING,
|
||||
@@ -68,6 +71,7 @@ class ProfileController extends Controller
|
||||
->exists();
|
||||
|
||||
$lastReadyExport = $user->dataExports()
|
||||
->where('scope', DataExportScope::USER->value)
|
||||
->where('status', DataExport::STATUS_READY)
|
||||
->latest('created_at')
|
||||
->first();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\DataExportScope;
|
||||
use App\Jobs\GenerateDataExport;
|
||||
use App\Models\DataExport;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
@@ -17,6 +18,7 @@ class ProfileDataExportController extends Controller
|
||||
abort_unless($user, 403);
|
||||
|
||||
$hasRecentExport = $user->dataExports()
|
||||
->where('scope', DataExportScope::USER->value)
|
||||
->whereIn('status', [DataExport::STATUS_PENDING, DataExport::STATUS_PROCESSING])
|
||||
->exists();
|
||||
|
||||
@@ -25,6 +27,7 @@ class ProfileDataExportController extends Controller
|
||||
}
|
||||
|
||||
$recentReadyExport = $user->dataExports()
|
||||
->where('scope', DataExportScope::USER->value)
|
||||
->where('status', DataExport::STATUS_READY)
|
||||
->where('created_at', '>=', now()->subDay())
|
||||
->exists();
|
||||
@@ -36,6 +39,8 @@ class ProfileDataExportController extends Controller
|
||||
$export = $user->dataExports()->create([
|
||||
'tenant_id' => $user->tenant_id,
|
||||
'status' => DataExport::STATUS_PENDING,
|
||||
'scope' => DataExportScope::USER->value,
|
||||
'include_media' => false,
|
||||
]);
|
||||
|
||||
GenerateDataExport::dispatch($export->id);
|
||||
|
||||
32
app/Http/Controllers/SuperAdmin/DataExportController.php
Normal file
32
app/Http/Controllers/SuperAdmin/DataExportController.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\SuperAdmin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\DataExport;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class DataExportController extends Controller
|
||||
{
|
||||
public function download(DataExport $export): StreamedResponse
|
||||
{
|
||||
if (! $export->isReady() || $export->hasExpired() || ! $export->path) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$disk = 'local';
|
||||
|
||||
if (! Storage::disk($disk)->exists($export->path)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return Storage::disk($disk)->download(
|
||||
$export->path,
|
||||
sprintf('fotospiel-data-export-%s.zip', $export->created_at?->format('Ymd') ?? now()->format('Ymd')),
|
||||
[
|
||||
'Cache-Control' => 'private, no-store',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
41
app/Http/Requests/Tenant/DataExportStoreRequest.php
Normal file
41
app/Http/Requests/Tenant/DataExportStoreRequest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Tenant;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class DataExportStoreRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'scope' => ['required', Rule::in(['tenant', 'event'])],
|
||||
'event_id' => ['required_if:scope,event', 'integer', 'exists:events,id'],
|
||||
'include_media' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'scope.required' => 'Export scope is required.',
|
||||
'scope.in' => 'Export scope must be tenant or event.',
|
||||
'event_id.required_if' => 'Event export requires an event.',
|
||||
'event_id.exists' => 'Selected event could not be found.',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user