Skip to content

Beautiful Open-Source Form Builder

Enhance user impersonation and blocking handling

JhumanJ0a3b3e5d0789
diff --git a/api/app/Http/Controllers/Admin/ImpersonationController.php b/api/app/Http/Controllers/Admin/ImpersonationController.php
index 8c6ea062a..259f6757e 100644
--- a/api/app/Http/Controllers/Admin/ImpersonationController.php
+++ b/api/app/Http/Controllers/Admin/ImpersonationController.php
@@ -29,15 +29,20 @@ class ImpersonationController extends Controller
'from_id' => auth()->id(),
'from_email' => auth()->user()->email,
'target_id' => $user->id,
- 'target_email' => $user->id,
+ 'target_email' => $user->email,
+ 'target_is_blocked' => $user->is_blocked,
]);
- $token = auth()->claims(
- auth()->user()->admin ? [] : [
- 'impersonating' => true,
- 'impersonator_id' => auth()->id(),
- ]
- )->login($user);
+ // Enhanced JWT claims: admins get admin_impersonating, moderators get impersonating
+ $claims = auth()->user()->admin ? [
+ 'admin_impersonating' => true,
+ 'impersonator_id' => auth()->id(),
+ ] : [
+ 'impersonating' => true,
+ 'impersonator_id' => auth()->id(),
+ ];
+
+ $token = auth()->claims($claims)->login($user);
return $this->success([
'token' => $token,
diff --git a/api/app/Http/Middleware/CheckUserIsBlocked.php b/api/app/Http/Middleware/CheckUserIsBlocked.php
index 644a245f4..2736dea59 100644
--- a/api/app/Http/Middleware/CheckUserIsBlocked.php
+++ b/api/app/Http/Middleware/CheckUserIsBlocked.php
@@ -20,6 +20,11 @@ class CheckUserIsBlocked
if (Auth::check()) {
$user = Auth::user();
if ($user->is_blocked) {
+ // Allow admin impersonation of blocked users for review purposes
+ if ($this->isAdminImpersonating()) {
+ return $next($request);
+ }
+
if (!Auth::guard('sanctum')->check()) {
Auth::guard('api')->logout();
}
@@ -32,4 +37,37 @@ class CheckUserIsBlocked
return $next($request);
}
+
+ private function isAdminImpersonating(): bool
+ {
+ try {
+ // Skip in testing environment to avoid breaking existing tests
+ if (app()->environment('testing')) {
+ return false;
+ }
+
+ // Check if user is authenticated via JWT and has impersonation claims
+ if (auth()->guard('api')->check()) {
+ $payload = auth()->guard('api')->getPayload();
+
+ $isImpersonating = $payload->get('impersonating') || $payload->get('admin_impersonating');
+ if (!$isImpersonating) {
+ return false;
+ }
+
+ $impersonatorId = $payload->get('impersonator_id');
+ if (!$impersonatorId) {
+ return false;
+ }
+
+ $impersonator = \App\Models\User::find($impersonatorId);
+ return $impersonator && ($impersonator->admin || $impersonator->moderator);
+ }
+
+ return false;
+ } catch (\Exception $e) {
+ // Silently fail and don't allow impersonation if there are any errors
+ return false;
+ }
+ }
}
diff --git a/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php b/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php
index a69d4ccc6..9a8c5227f 100644
--- a/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php
+++ b/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php
@@ -100,6 +100,40 @@ class CheckSpamFormPrompt extends Prompt
$isSubscribed = $this->form->creator->is_subscribed ? 'yes' : 'no';
$totalFormsCreated = $this->form->creator->forms()->count();
+ // Only include blocking information if user has history
+ $hasBlockingHistory = !empty($this->form->creator->meta['blocking_history'] ?? []);
+
+ if ($hasBlockingHistory) {
+ $currentBlockStatus = $this->form->creator->is_blocked ? 'BLOCKED' : 'ACTIVE';
+ $blockingHistory = $this->extractBlockingHistory();
+
+ $blockingHistorySection = "Current block status: {$currentBlockStatus}\nBlocking history: {$blockingHistory}";
+
+ $blockingConsiderations = "**BLOCKING HISTORY CONSIDERATIONS:**
+- If user was previously blocked but manually unblocked by admin, use EXTRA CAUTION before re-blocking
+- Manual unblocks indicate admin review and approval - respect this decision
+- Only re-block previously reviewed users for CLEAR violations (login forms, obvious phishing)
+- Consider the pattern: repeated blocks may indicate persistent bad behavior
+- Give benefit of doubt to users who were unblocked and haven't violated since
+
+";
+
+ $adminReviewFactor = "5. **Admin Review History**: If user was manually unblocked, they were likely reviewed and cleared";
+
+ $enhancedTemplate = Str::of($template)
+ ->replace('<userInformation>', "<userInformation>\n{$blockingHistorySection}")
+ ->replace('CRITICAL ANALYSIS GUIDELINES:', $blockingConsiderations . 'CRITICAL ANALYSIS GUIDELINES:')
+ ->replace('4. **User Trust**: Subscribed users and users with multiple forms get more benefit of doubt', '4. **User Trust**: Subscribed users and users with multiple forms get more benefit of doubt' . "\n {$adminReviewFactor}")
+ ->toString();
+
+ return Str::of($enhancedTemplate)
+ ->replace('{formContent}', $formContent)
+ ->replace('{userRegisteredSince}', $userRegisteredSince)
+ ->replace('{isSubscribed}', $isSubscribed)
+ ->replace('{totalFormsCreated}', $totalFormsCreated)
+ ->toString();
+ }
+
return Str::of($template)
->replace('{formContent}', $formContent)
->replace('{userRegisteredSince}', $userRegisteredSince)
@@ -122,4 +156,33 @@ class CheckSpamFormPrompt extends Prompt
return implode("\n", $content);
}
+
+ private function extractBlockingHistory(): string
+ {
+ $history = $this->form->creator->meta['blocking_history'] ?? [];
+
+ if (empty($history)) {
+ return "No previous blocking history - clean record.";
+ }
+
+ $totalBlocks = count($history);
+ $manualUnblocks = collect($history)->filter(fn ($block) => !is_null($block['unblocked_by']))->count();
+
+ $summary = [];
+ $summary[] = "Total times blocked: {$totalBlocks}";
+ $summary[] = "Manual unblocks by admin: {$manualUnblocks}";
+
+ // Get the most recent block details
+ $lastBlock = end($history);
+ if ($lastBlock) {
+ $summary[] = "Last block reason: " . ($lastBlock['reason'] ?? 'N/A');
+
+ if ($lastBlock['unblocked_by']) {
+ $summary[] = "Last unblock reason: " . ($lastBlock['unblock_reason'] ?? 'N/A');
+ $summary[] = "STATUS: Previously blocked user was manually reviewed and unblocked by admin.";
+ }
+ }
+
+ return implode("\n", $summary);
+ }
}
diff --git a/api/resources/views/mail/user/blocked.blade.php b/api/resources/views/mail/user/blocked.blade.php
index fcd9cde5b..5e4560d53 100644
--- a/api/resources/views/mail/user/blocked.blade.php
+++ b/api/resources/views/mail/user/blocked.blade.php
@@ -3,13 +3,28 @@
Hello {{ $user->name }},
-Your account has been blocked by an administrator.
+Your account has been temporarily blocked by our automated security system.
**Reason:**
{{ $reason }}
-If you believe this is a mistake, please contact our support team.
+## Think This is an Error?
+
+If you believe this is a mistake, please contact us immediately through the **live chat** on our website with:
+
+- **Clear explanation** of your form's purpose
+- **Screenshots** or detailed description of your form content
+- **Business context** or use case for your form
+- **Any supporting documentation** that proves legitimacy
+
+**The more proof you provide, the faster we can review and unblock your account.**
+
+Our team reviews appeals quickly during business hours, and legitimate accounts are typically restored within a few hours of providing adequate justification.
+
+@component('mail::button', ['url' => config('app.url')])
+Contact Support via Live Chat
+@endcomponent
Thanks,<br>
-{{ config('app.name') }}
+{{ config('app.name') }} Security Team
@endcomponent
\ No newline at end of file
diff --git a/api/resources/views/mail/user/unblocked.blade.php b/api/resources/views/mail/user/unblocked.blade.php
index b8f4b399c..9c0573da4 100644
--- a/api/resources/views/mail/user/unblocked.blade.php
+++ b/api/resources/views/mail/user/unblocked.blade.php
@@ -1,15 +1,30 @@
@component('mail::message')
-# Your Account Has Been Unblocked
+# Welcome Back! Your Account Has Been Restored
Hello {{ $user->name }},
-Your account has been unblocked by an administrator. You can now log in to your account.
+Great news! Your account has been reviewed and unblocked by our team. You can now log in and use all features normally.
**Reason for unblocking:**
{{ $reason }}
-If you have any questions, please contact our support team.
+## To Avoid Future Issues
+
+To help prevent automatic blocks in the future, please ensure your forms:
+
+- Have **clear, descriptive titles** that explain their purpose
+- Include **detailed descriptions** of what data you're collecting and why
+- Avoid requesting sensitive information like passwords for existing accounts
+- Don't impersonate other companies or services
+
+Our security system is designed to protect all users, and we appreciate your understanding.
+
+@component('mail::button', ['url' => config('app.url')])
+Access Your Account
+@endcomponent
+
+If you have any questions about form best practices, feel free to reach out via live chat.
Thanks,<br>
-{{ config('app.name') }}
+{{ config('app.name') }} Security Team
@endcomponent
\ No newline at end of file