182 lines
6.4 KiB
PHP
182 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use App\Models\Tenant;
|
|
use App\Models\Package;
|
|
use App\Models\TenantPackage;
|
|
use App\Models\PackagePurchase;
|
|
use Illuminate\Auth\Events\Registered;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Validation\Rules;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\App;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class MarketingRegisterController extends Controller
|
|
{
|
|
/**
|
|
* Show the registration page.
|
|
*/
|
|
public function create(Request $request): Response
|
|
{
|
|
$package_id = $request->query('package_id');
|
|
$package = $package_id ? Package::find($package_id) : null;
|
|
|
|
$privacyHtml = view('legal.datenschutz-partial')->render();
|
|
|
|
return Inertia::render('auth/register', [
|
|
'package' => $package,
|
|
'privacyHtml' => $privacyHtml,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Handle an incoming registration request.
|
|
*
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'username' => ['required', 'string', 'max:255', 'unique:'.User::class],
|
|
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
|
|
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
|
'first_name' => ['required', 'string', 'max:255'],
|
|
'last_name' => ['required', 'string', 'max:255'],
|
|
'address' => ['required', 'string', 'max:500'],
|
|
'phone' => ['required', 'string', 'max:20'],
|
|
'privacy_consent' => ['accepted'],
|
|
'package_id' => ['nullable', 'integer'],
|
|
]);
|
|
|
|
$shouldAutoVerify = App::environment(['local', 'testing']);
|
|
|
|
$user = User::create([
|
|
'username' => $validated['username'],
|
|
'email' => $validated['email'],
|
|
'first_name' => $validated['first_name'],
|
|
'last_name' => $validated['last_name'],
|
|
'address' => $validated['address'],
|
|
'phone' => $validated['phone'],
|
|
'password' => Hash::make($validated['password']),
|
|
'role' => 'user',
|
|
'pending_purchase' => !empty($validated['package_id']),
|
|
]);
|
|
|
|
if ($user->pending_purchase) {
|
|
session()->put('pending_user_id', $user->id);
|
|
}
|
|
|
|
if ($shouldAutoVerify) {
|
|
$user->forceFill(['email_verified_at' => now()])->save();
|
|
}
|
|
|
|
$tenant = Tenant::create([
|
|
'user_id' => $user->id,
|
|
'name' => $validated['first_name'] . ' ' . $validated['last_name'],
|
|
'slug' => Str::slug($validated['first_name'] . ' ' . $validated['last_name'] . '-' . now()->timestamp),
|
|
'email' => $validated['email'],
|
|
'is_active' => true,
|
|
'is_suspended' => false,
|
|
'event_credits_balance' => 0,
|
|
'subscription_tier' => 'free',
|
|
'subscription_expires_at' => null,
|
|
'settings' => json_encode([
|
|
'branding' => [
|
|
'logo_url' => null,
|
|
'primary_color' => '#3B82F6',
|
|
'secondary_color' => '#1F2937',
|
|
'font_family' => 'Inter, sans-serif',
|
|
],
|
|
'features' => [
|
|
'photo_likes_enabled' => false,
|
|
'event_checklist' => false,
|
|
'custom_domain' => false,
|
|
'advanced_analytics' => false,
|
|
],
|
|
'custom_domain' => null,
|
|
'contact_email' => $validated['email'],
|
|
'event_default_type' => 'general',
|
|
]),
|
|
]);
|
|
|
|
event(new Registered($user));
|
|
|
|
Auth::login($user);
|
|
|
|
// Send Welcome Email
|
|
Mail::to($user)->queue(new \App\Mail\Welcome($user));
|
|
|
|
$dashboardUrl = route('dashboard');
|
|
|
|
if (!empty($validated['package_id'])) {
|
|
$package = Package::find($validated['package_id']);
|
|
if (!$package) {
|
|
$request->session()->flash('error', __('auth.registration_failed'));
|
|
// No action if package not found
|
|
return redirect()->route('dashboard')->with('success', __('auth.registration_success'));
|
|
} else if ((float) $package->price <= 0.0) {
|
|
// Assign free package
|
|
TenantPackage::create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'active' => true,
|
|
'price' => 0,
|
|
]);
|
|
|
|
PackagePurchase::create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'type' => $package->type === 'endcustomer' ? 'endcustomer_event' : 'reseller_subscription',
|
|
'price' => 0,
|
|
'purchased_at' => now(),
|
|
'provider_id' => 'free',
|
|
]);
|
|
|
|
$tenant->update(['subscription_status' => 'active']);
|
|
|
|
$user->update(['role' => 'tenant_admin', 'pending_purchase' => false]);
|
|
Auth::login($user); // Re-login to refresh session
|
|
|
|
$request->session()->flash('success', __('auth.registration_success'));
|
|
|
|
if ($shouldAutoVerify) {
|
|
return redirect()->route('dashboard')->with('success', __('auth.registration_success'));
|
|
} else {
|
|
return redirect()->route('verification.notice')->with('status', 'registration-success');
|
|
}
|
|
} else {
|
|
// For paid package, keep pending_purchase true, redirect to buy
|
|
$request->session()->flash('success', __('auth.registration_success'));
|
|
return redirect()->route('buy.packages', $package->id)->with('success', __('auth.registration_success'));
|
|
}
|
|
}
|
|
|
|
// No package
|
|
$request->session()->flash('success', __('auth.registration_success'));
|
|
|
|
if ($shouldAutoVerify) {
|
|
return redirect()->route('dashboard')->with('success', __('auth.registration_success'));
|
|
}
|
|
|
|
session()->flash('status', 'registration-success');
|
|
|
|
return redirect()->route('verification.notice');
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|