LMIV-SNACKAUTOMAT/app/Http/Controllers/TenantController.php

262 lines
8.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Tenant;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class TenantController extends Controller
{
/**
* Zeige Mandanten-Auswahl für Super-Admin
*/
public function select()
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
// Normale Admins werden zu ihrem Dashboard weitergeleitet
return redirect()->route('admin.dashboard');
}
$tenants = Tenant::where('is_active', true)
->withCount(['users', 'vendingMachines', 'products'])
->get();
return view('admin.tenants.select', compact('tenants'));
}
/**
* Wechsle zu einem bestimmten Mandanten (nur für Super-Admin)
*/
public function switch(Request $request, Tenant $tenant)
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
abort(403, 'Nicht autorisiert');
}
if (!$tenant->is_active) {
return redirect()->back()->with('error', 'Mandant ist nicht aktiv.');
}
// Setze Mandanten-Kontext in Session
session(['current_tenant_id' => $tenant->id, 'current_tenant' => $tenant]);
return redirect()->route('admin.dashboard')
->with('success', "Gewechselt zu Mandant: {$tenant->name}");
}
/**
* Verlasse Mandanten-Kontext (zurück zur Auswahl)
*/
public function leave()
{
session()->forget(['current_tenant_id', 'current_tenant']);
return redirect()->route('tenants.select')
->with('success', 'Mandanten-Kontext verlassen.');
}
/**
* Zeige alle Mandanten (Super-Admin Management)
*/
public function index()
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
abort(403, 'Nicht autorisiert');
}
$tenants = Tenant::withCount(['users', 'vendingMachines', 'products'])
->paginate(10);
return view('admin.tenants.index', compact('tenants'));
}
/**
* Zeige Formular zum Erstellen eines neuen Mandanten
*/
public function create()
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
abort(403, 'Nicht autorisiert');
}
return view('admin.tenants.create');
}
/**
* Speichere einen neuen Mandanten
*/
public function store(Request $request)
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
abort(403, 'Nicht autorisiert');
}
$validated = $request->validate([
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'domain' => 'nullable|string|max:255|unique:tenants,domain',
'public_slug' => 'nullable|string|max:100|regex:/^[a-z0-9-]+$/|unique:tenants,public_slug',
'logo' => 'nullable|image|max:2048',
'is_active' => 'boolean',
'show_prices' => 'boolean',
'show_stock' => 'boolean',
'street' => 'nullable|string|max:255',
'house_number' => 'nullable|string|max:20',
'postal_code' => 'nullable|string|max:10',
'city' => 'nullable|string|max:255',
'country' => 'nullable|string|max:255'
], [
'public_slug.regex' => 'Der öffentliche Slug darf nur Kleinbuchstaben, Zahlen und Bindestriche enthalten.',
'public_slug.unique' => 'Dieser öffentliche Slug wird bereits verwendet.'
]);
if ($request->hasFile('logo')) {
$logoPath = $request->file('logo')->store('tenant-logos', 'public');
$validated['logo'] = $logoPath;
}
$tenant = Tenant::create($validated);
return redirect()->route('admin.tenants.index')
->with('success', 'Mandant erfolgreich erstellt.');
}
/**
* Zeige Details eines Mandanten
*/
public function show(Tenant $tenant)
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
abort(403, 'Nicht autorisiert');
}
$tenant->loadCount(['users', 'vendingMachines', 'products']);
return view('admin.tenants.show', compact('tenant'));
}
/**
* Zeige Formular zum Bearbeiten eines Mandanten
*/
public function edit(Tenant $tenant)
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
abort(403, 'Nicht autorisiert');
}
return view('admin.tenants.edit', compact('tenant'));
}
/**
* Aktualisiere einen Mandanten
*/
public function update(Request $request, Tenant $tenant)
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
abort(403, 'Nicht autorisiert');
}
// Debug Logging für Checkbox-Werte
\Log::info('TenantController Update:', [
'tenant_id' => $tenant->id,
'all_data' => $request->all(),
'has_show_prices' => $request->has('show_prices'),
'has_show_stock' => $request->has('show_stock'),
'show_prices_value' => $request->input('show_prices'),
'show_stock_value' => $request->input('show_stock')
]);
$validated = $request->validate([
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'domain' => 'nullable|string|max:255|unique:tenants,domain,' . $tenant->id,
'public_slug' => 'nullable|string|max:100|regex:/^[a-z0-9-]+$/|unique:tenants,public_slug,' . $tenant->id,
'logo' => 'nullable|image|max:2048',
'is_active' => 'boolean',
// ENTFERNT: 'show_prices' => 'boolean',
// ENTFERNT: 'show_stock' => 'boolean',
'street' => 'nullable|string|max:255',
'house_number' => 'nullable|string|max:20',
'postal_code' => 'nullable|string|max:10',
'city' => 'nullable|string|max:255',
'country' => 'nullable|string|max:255'
], [
'public_slug.regex' => 'Der öffentliche Slug darf nur Kleinbuchstaben, Zahlen und Bindestriche enthalten.',
'public_slug.unique' => 'Dieser öffentliche Slug wird bereits verwendet.'
]);
// Checkboxes manuell verarbeiten
$validated['show_prices'] = $request->has('show_prices');
$validated['show_stock'] = $request->has('show_stock');
\Log::info('Validated data for update:', $validated);
if ($request->hasFile('logo')) {
// Lösche altes Logo falls vorhanden
if ($tenant->logo && file_exists(storage_path('app/public/' . $tenant->logo))) {
unlink(storage_path('app/public/' . $tenant->logo));
}
$logoPath = $request->file('logo')->store('tenant-logos', 'public');
$validated['logo'] = $logoPath;
}
$tenant->update($validated);
\Log::info('Tenant nach Update:', [
'show_prices' => $tenant->fresh()->show_prices,
'show_stock' => $tenant->fresh()->show_stock
]);
return redirect()->route('admin.tenants.index')
->with('success', 'Mandant erfolgreich aktualisiert.');
}
/**
* Lösche einen Mandanten
*/
public function destroy(Tenant $tenant)
{
$user = Auth::user();
if (!$user->isSuperAdmin()) {
abort(403, 'Nicht autorisiert');
}
// Prüfe ob Mandant Daten hat
if ($tenant->users()->count() > 0 ||
$tenant->vendingMachines()->count() > 0 ||
$tenant->products()->count() > 0) {
return redirect()->route('admin.tenants.index')
->with('error', 'Mandant kann nicht gelöscht werden, da noch Daten vorhanden sind.');
}
// Lösche Logo falls vorhanden
if ($tenant->logo && file_exists(storage_path('app/public/' . $tenant->logo))) {
unlink(storage_path('app/public/' . $tenant->logo));
}
$tenant->delete();
return redirect()->route('admin.tenants.index')
->with('success', 'Mandant erfolgreich gelöscht.');
}
}