Demo Free/Pro/Enterprise accounts wipe and reload sample data on authorization-code SSO callbacks so every sales walkthrough starts clean. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -123,6 +123,8 @@ class SsoLoginController extends Controller
|
||||
Auth::login($user, remember: true);
|
||||
$request->session()->regenerate();
|
||||
|
||||
app(\App\Services\Events\DemoLoginReseeder::class)->maybeResetAfterLogin($user);
|
||||
|
||||
return $this->finishCallback($request, $intended, null, $popup);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Events;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Reseed Events demo tenants after a fresh SSO login.
|
||||
*/
|
||||
class DemoLoginReseeder
|
||||
{
|
||||
public function maybeResetAfterLogin(User $user): void
|
||||
{
|
||||
if (! (bool) config('demo_accounts.enabled', false)
|
||||
|| ! (bool) config('demo_accounts.reset_on_login', true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$plan = $this->planForEmail($user->email);
|
||||
if ($plan === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$seedUser = $this->resolveSeedUser($user);
|
||||
if (! $seedUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
$lockKey = 'demo-reseed:events:'.$seedUser->public_id;
|
||||
if (! Cache::add($lockKey, 1, 120)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$identity = (string) ($seedUser->email ?: $seedUser->public_id);
|
||||
|
||||
dispatch(function () use ($identity, $plan, $lockKey) {
|
||||
try {
|
||||
Artisan::call('demo:seed', [
|
||||
'identity' => $identity,
|
||||
'--plan' => $plan,
|
||||
'--reset' => true,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('demo_events_login_reseed_failed', [
|
||||
'identity' => $identity,
|
||||
'plan' => $plan,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
} finally {
|
||||
Cache::forget($lockKey);
|
||||
}
|
||||
})->afterResponse();
|
||||
}
|
||||
|
||||
private function planForEmail(?string $email): ?string
|
||||
{
|
||||
$email = strtolower(trim((string) $email));
|
||||
if ($email === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$accounts = (array) config('demo_accounts.accounts', []);
|
||||
if (isset($accounts[$email])) {
|
||||
return $accounts[$email];
|
||||
}
|
||||
|
||||
$members = (array) config('demo_accounts.member_emails', []);
|
||||
$ownerEmail = $members[$email] ?? null;
|
||||
|
||||
return $ownerEmail ? ($accounts[$ownerEmail] ?? null) : null;
|
||||
}
|
||||
|
||||
private function resolveSeedUser(User $user): ?User
|
||||
{
|
||||
$email = strtolower((string) $user->email);
|
||||
$ownerEmail = ((array) config('demo_accounts.member_emails', []))[$email] ?? null;
|
||||
if (! $ownerEmail) {
|
||||
return $user;
|
||||
}
|
||||
|
||||
return User::query()->where('email', $ownerEmail)->first() ?? $user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user