Repository navigation
Expand file tree
/
Copy pathadmin_users.php
More file actions
727 lines (599 loc) · 22.5 KB
/
Copy pathadmin_users.php
File metadata and controls
727 lines (599 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/billing.php';
require_admin_user();
$pageTitle = 'Admin Users';
$currentPage = 'admin_users';
$message = isset($_GET['msg']) ? (string)$_GET['msg'] : '';
$error = '';
$users = [];
function admin_best_plan_key(?string $planKeys): string
{
$keys = array_filter(array_map('trim', explode(',', (string)$planKeys)));
$priority = [
'unlimited' => 4,
'coachplus' => 3,
'coach' => 2,
'free' => 1,
];
$bestPlan = 'free';
$bestScore = 0;
foreach ($keys as $key) {
$normalized = function_exists('normalize_plan_key')
? normalize_plan_key($key)
: strtolower($key);
$score = $priority[$normalized] ?? 1;
if ($score > $bestScore) {
$bestScore = $score;
$bestPlan = $normalized;
}
}
return $bestPlan;
}
function admin_display_plan_label(?string $planKeys): string
{
return match (admin_best_plan_key($planKeys)) {
'coach' => 'Coach',
'coachplus' => 'Coach Plus',
'unlimited' => 'Unlimited',
default => 'Free',
};
}
function admin_first_value(?string $csv, string $default = ''): string
{
$values = array_filter(array_map('trim', explode(',', (string)$csv)));
if (empty($values)) {
return $default;
}
return strtolower((string)reset($values));
}
function admin_display_subscription_status(?string $statuses, int $hasBillingOverride): string
{
if ($hasBillingOverride === 1 && str_contains(strtolower((string)$statuses), 'trialing')) {
return 'Admin Trial';
}
if ($hasBillingOverride === 1) {
return 'Admin Override';
}
$statuses = strtolower((string)$statuses);
if (str_contains($statuses, 'active')) {
return 'Active';
}
if (str_contains($statuses, 'trialing')) {
return 'Trialing';
}
if (str_contains($statuses, 'past_due')) {
return 'Past Due';
}
if (str_contains($statuses, 'canceled')) {
return 'Canceled';
}
return 'Free';
}
try {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf_token();
$action = (string)($_POST['action'] ?? '');
if ($action === 'update_billing_settings') {
update_app_setting('billing_enabled', !empty($_POST['billing_enabled']) ? '1' : '0');
update_app_setting('billing_enforcement_enabled', !empty($_POST['billing_enforcement_enabled']) ? '1' : '0');
header('Location: admin_users.php?msg=' . urlencode('Billing settings updated.'));
exit;
}
if ($action === 'admin_update_coach_plan') {
$targetUserId = (int)($_POST['user_id'] ?? 0);
$planKey = normalize_plan_key((string)($_POST['plan_key'] ?? 'free'));
$billingInterval = (string)($_POST['billing_interval'] ?? 'manual');
$subscriptionStatus = (string)($_POST['subscription_status'] ?? 'active');
if ($targetUserId <= 0) {
throw new RuntimeException('Invalid coach selected.');
}
if (!in_array($planKey, ['free', 'coach', 'coachplus', 'unlimited'], true)) {
throw new RuntimeException('Invalid plan selected.');
}
if (!in_array($billingInterval, ['free', 'monthly', 'yearly', 'manual'], true)) {
throw new RuntimeException('Invalid billing interval.');
}
if (!in_array($subscriptionStatus, ['free', 'active', 'trialing', 'inactive', 'canceled'], true)) {
throw new RuntimeException('Invalid subscription status.');
}
$teamStmt = db()->prepare("
SELECT DISTINCT t.id
FROM teams t
INNER JOIN team_memberships tm ON tm.team_id = t.id
WHERE tm.user_id = :user_id
AND COALESCE(t.is_archived, 0) = 0
");
$teamStmt->execute([
'user_id' => $targetUserId,
]);
$teamIds = array_map('intval', $teamStmt->fetchAll(PDO::FETCH_COLUMN));
if (empty($teamIds)) {
throw new RuntimeException('No active team found for this user.');
}
$isFree = $planKey === 'free';
$isTrial = $subscriptionStatus === 'trialing';
$billingOverride = 1;
$trialEndsAt = $isTrial ? date('Y-m-d H:i:s', strtotime('+30 days')) : null;
$subscriptionCurrentPeriodEnd = null;
if ($isFree && !$isTrial) {
$billingInterval = 'free';
$subscriptionStatus = 'free';
}
if ($isTrial && $billingInterval === 'free' && !$isFree) {
$billingInterval = 'manual';
}
if ($billingOverride === 1 && !$isTrial) {
$subscriptionCurrentPeriodEnd = null;
}
$placeholders = implode(',', array_fill(0, count($teamIds), '?'));
$stmt = db()->prepare("
UPDATE teams
SET
plan_key = ?,
subscription_status = ?,
billing_interval = ?,
billing_override = ?,
is_trial = ?,
trial_ends_at = ?,
subscription_current_period_end = ?
WHERE id IN ($placeholders)
");
$stmt->execute(array_merge([
$planKey,
$subscriptionStatus,
$billingInterval,
$billingOverride,
$isTrial ? 1 : 0,
$trialEndsAt,
$subscriptionCurrentPeriodEnd,
], $teamIds));
if ($isTrial && $trialEndsAt !== null) {
$userStmt = db()->prepare("
SELECT full_name, email
FROM users
WHERE id = :id
LIMIT 1
");
$userStmt->execute([
'id' => $targetUserId,
]);
$coach = $userStmt->fetch(PDO::FETCH_ASSOC);
if ($coach) {
try {
send_trial_started_email(
(string)$coach['email'],
(string)$coach['full_name'],
'BenchBuddy Team',
date('F j, Y', strtotime($trialEndsAt))
);
} catch (Throwable $e) {
error_log('Trial started email failed: ' . $e->getMessage());
}
}
}
header('Location: admin_users.php?msg=' . urlencode('Coach plan updated successfully.'));
exit;
}
if ($action === 'impersonate_user') {
$targetUserId = (int)($_POST['target_user_id'] ?? 0);
if ($targetUserId <= 0) {
throw new RuntimeException('Invalid user selected.');
}
$stmt = db()->prepare("
SELECT full_name, email
FROM users
WHERE id = :id
LIMIT 1
");
$stmt->execute([
'id' => $targetUserId,
]);
$targetUser = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$targetUser) {
throw new RuntimeException('User not found.');
}
$name = trim((string)($targetUser['full_name'] ?? ''));
if ($name === '') {
$name = (string)($targetUser['email'] ?? 'this user');
}
start_user_impersonation($targetUserId);
header('Location: index.php?msg=' . urlencode('You are now impersonating ' . $name . '.'));
exit;
}
if ($action === 'stop_impersonation') {
stop_user_impersonation();
header('Location: admin_users.php?msg=' . urlencode('Impersonation ended.'));
exit;
}
if ($action === 'delete_user_account') {
$targetUserId = (int)($_POST['target_user_id'] ?? 0);
if ($targetUserId <= 0) {
throw new RuntimeException('Invalid user selected.');
}
if ($targetUserId === current_user_id()) {
throw new RuntimeException('You cannot delete your own admin account here.');
}
delete_user_account_and_data($targetUserId);
header('Location: admin_users.php?msg=' . urlencode('User account deleted successfully.'));
exit;
}
}
$stmt = db()->query("
SELECT
u.id,
u.full_name,
u.email,
u.role,
u.is_active,
u.created_at,
u.last_active_at,
COUNT(DISTINCT tm.team_id) AS team_count,
COUNT(DISTINCT p.id) AS player_count,
COUNT(DISTINCT active_sessions.session_id) AS active_session_count,
GROUP_CONCAT(DISTINCT t.plan_key ORDER BY t.plan_key SEPARATOR ', ') AS plan_keys,
GROUP_CONCAT(
DISTINCT
CASE
WHEN COALESCE(t.is_trial, 0) = 1
AND t.subscription_status = 'trialing'
THEN 'trialing'
WHEN COALESCE(t.billing_override, 0) = 1
THEN t.subscription_status
ELSE t.subscription_status
END
ORDER BY t.subscription_status
SEPARATOR ', '
) AS subscription_statuses, GROUP_CONCAT(DISTINCT t.billing_interval ORDER BY t.billing_interval SEPARATOR ', ') AS billing_intervals,
MAX(COALESCE(t.billing_override, 0)) AS has_billing_override,
MAX(
CASE
WHEN COALESCE(t.billing_override, 0) = 1
AND t.subscription_status <> 'trialing'
THEN 0
ELSE COALESCE(t.is_trial, 0)
END
) AS is_trial,
MIN(
CASE
WHEN COALESCE(t.is_trial, 0) = 1
AND t.subscription_status = 'trialing'
THEN t.trial_ends_at
ELSE NULL
END
) AS trial_ends_at
FROM users u
LEFT JOIN team_memberships tm ON tm.user_id = u.id
LEFT JOIN teams t ON t.id = tm.team_id
LEFT JOIN players p ON p.team_id = tm.team_id
LEFT JOIN user_sessions active_sessions
ON active_sessions.user_id = u.id
AND active_sessions.logged_out_at IS NULL
AND active_sessions.last_seen_at >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)
GROUP BY
u.id,
u.full_name,
u.email,
u.role,
u.is_active,
u.created_at,
u.last_active_at
ORDER BY u.full_name ASC, u.email ASC
");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
$error = $e->getMessage();
}
require_once __DIR__ . '/includes/header.php';
?>
<style>
.admin-users-card-list {
display: grid;
gap: 16px;
}
.admin-user-card {
border: 1px solid var(--border, #e5e7eb);
border-radius: 16px;
padding: 18px;
background: #fff;
}
.admin-user-card-header {
display: flex;
justify-content: space-between;
gap: 16px;
align-items: flex-start;
margin-bottom: 14px;
}
.admin-user-card-header h3 {
margin: 0;
}
.admin-user-card-header p {
margin: 4px 0 0;
}
.admin-user-pill-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
justify-content: flex-end;
}
.admin-user-card-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 12px;
margin-top: 14px;
}
.admin-user-card-grid div {
background: #f8fafc;
border-radius: 12px;
padding: 10px;
}
.admin-user-card-grid span {
display: block;
font-size: 12px;
color: #64748b;
margin-bottom: 4px;
}
.admin-user-card-grid strong {
display: block;
font-size: 14px;
}
.admin-user-plan-controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: end;
margin-top: 16px;
}
.admin-user-plan-controls select {
min-width: 150px;
}
.admin-user-actions {
margin-top: 14px;
}
@media (max-width: 900px) {
.admin-user-card-header {
display: block;
}
.admin-user-pill-row {
justify-content: flex-start;
margin-top: 10px;
}
.admin-user-card-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.admin-user-plan-controls {
display: grid;
grid-template-columns: 1fr;
}
.admin-user-plan-controls select,
.admin-user-plan-controls button {
width: 100%;
}
}
.btn-danger {
background: #dc2626;
border-color: #dc2626;
color: #fff;
}
.btn-danger:hover {
background: #b91c1c;
border-color: #b91c1c;
}
</style>
<h1 class="page-title brand-title-font">Admin Users</h1>
<?php if ($message !== ''): ?>
<div class="msg ok"><?= h($message) ?></div>
<?php endif; ?>
<?php if ($error !== ''): ?>
<div class="msg err"><?= h($error) ?></div>
<?php endif; ?>
<?php if (is_impersonating()): ?>
<div class="card">
<h2>Impersonation Active</h2>
<p class="muted">You are currently impersonating <?= h(current_user_name()) ?>.</p>
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="action" value="stop_impersonation">
<button type="submit" class="btn btn-secondary">Stop Impersonation</button>
</form>
</div>
<?php endif; ?>
<?php if (current_user_role() === 'admin'): ?>
<div class="card">
<h2>Billing Controls</h2>
<p class="muted">
Control whether billing is visible and whether plan limits are enforced.
</p>
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="action" value="update_billing_settings">
<label class="toggle-row" for="billing_enabled_toggle">
<span class="toggle-label-text">Billing enabled</span>
<span class="toggle-switch">
<input
type="checkbox"
id="billing_enabled_toggle"
name="billing_enabled"
value="1"
<?= billing_enabled() ? 'checked' : '' ?>
>
<span class="toggle-slider"></span>
</span>
</label>
<label class="toggle-row" for="billing_enforcement_enabled_toggle">
<span class="toggle-label-text">Enforce plan limits</span>
<span class="toggle-switch">
<input
type="checkbox"
id="billing_enforcement_enabled_toggle"
name="billing_enforcement_enabled"
value="1"
<?= billing_enforcement_enabled() ? 'checked' : '' ?>
>
<span class="toggle-slider"></span>
</span>
</label>
<button type="submit" class="btn">Save Billing Settings</button>
</form>
</div>
<?php endif; ?>
<div class="card">
<h2>All Users</h2>
<?php if (empty($users)): ?>
<p class="muted">No users found.</p>
<?php else: ?>
<div class="admin-users-card-list">
<?php foreach ($users as $row): ?>
<?php
$currentPlanKey = admin_best_plan_key($row['plan_keys'] ?? '');
$currentBillingInterval = admin_first_value($row['billing_intervals'] ?? '', 'free');
$currentSubscriptionStatus = admin_first_value($row['subscription_statuses'] ?? '', 'free');
$isTrial = (
str_contains(strtolower((string)($row['subscription_statuses'] ?? '')), 'trialing')
&& (int)($row['is_trial'] ?? 0) === 1
&& !empty($row['trial_ends_at'])
);
?>
<article class="admin-user-card">
<div class="admin-user-card-header">
<div>
<h3><?= h((string)$row['full_name']) ?></h3>
<p class="muted"><?= h((string)$row['email']) ?></p>
</div>
<div class="admin-user-pill-row">
<span class="pill">
<?= h(((int)$row['is_active'] === 1) ? 'Active' : 'Inactive') ?>
</span>
<span class="pill">
<?= h(admin_display_plan_label($row['plan_keys'] ?? '')) ?>
</span>
<span class="pill">
<?= h(admin_display_subscription_status(
$row['subscription_statuses'] ?? '',
(int)($row['has_billing_override'] ?? 0)
)) ?>
</span>
<?php if ($isTrial): ?>
<span class="pill status-warning">
Trial ends <?= h(date('M j, Y', strtotime((string)$row['trial_ends_at']))) ?>
</span>
<?php endif; ?>
<?php if ((int)($row['active_session_count'] ?? 0) > 0): ?>
<span class="pill locked">
Online · <?= (int)$row['active_session_count'] ?> device<?= (int)$row['active_session_count'] === 1 ? '' : 's' ?>
</span>
<?php else: ?>
<span class="pill draft">Offline</span>
<?php endif; ?>
</div>
</div>
<div class="admin-user-card-grid">
<div>
<span>Role</span>
<strong><?= h((string)$row['role']) ?></strong>
</div>
<div>
<span>Teams</span>
<strong><?= (int)$row['team_count'] ?></strong>
</div>
<div>
<span>Players</span>
<strong><?= (int)$row['player_count'] ?></strong>
</div>
<div>
<span>Last Active</span>
<strong><?= h(format_local_datetime($row['last_active_at'] ?? null, 'Never')) ?></strong>
</div>
<div>
<span>Created</span>
<strong><?= h(date('M j, Y', strtotime((string)$row['created_at']))) ?></strong>
</div>
<div>
<span>Billing Interval</span>
<strong><?= h(ucwords(str_replace('_', ' ', $currentBillingInterval))) ?></strong>
</div>
<div>
<span>Subscription Status</span>
<strong><?= h(ucwords(str_replace('_', ' ', $currentSubscriptionStatus))) ?></strong>
</div>
<div>
<span>Trial</span>
<strong>
<?php if ($isTrial): ?>
Ends <?= h(date('M j, Y', strtotime((string)$row['trial_ends_at']))) ?>
<?php else: ?>
No active trial
<?php endif; ?>
</strong>
</div>
</div>
<?php if ((int)$row['is_active'] === 1): ?>
<form method="post" class="admin-plan-form admin-user-plan-controls">
<?= csrf_field() ?>
<input type="hidden" name="action" value="admin_update_coach_plan">
<input type="hidden" name="user_id" value="<?= (int)$row['id'] ?>">
<select name="plan_key">
<option value="free" <?= $currentPlanKey === 'free' ? 'selected' : '' ?>>Free</option>
<option value="coach" <?= $currentPlanKey === 'coach' ? 'selected' : '' ?>>Coach</option>
<option value="coachplus" <?= $currentPlanKey === 'coachplus' ? 'selected' : '' ?>>Coach Plus</option>
<option value="unlimited" <?= $currentPlanKey === 'unlimited' ? 'selected' : '' ?>>Unlimited</option>
</select>
<select name="billing_interval">
<option value="free" <?= $currentBillingInterval === 'free' ? 'selected' : '' ?>>Free</option>
<option value="monthly" <?= $currentBillingInterval === 'monthly' ? 'selected' : '' ?>>Monthly</option>
<option value="yearly" <?= $currentBillingInterval === 'yearly' ? 'selected' : '' ?>>Yearly</option>
<option value="manual" <?= $currentBillingInterval === 'manual' ? 'selected' : '' ?>>Manual</option>
</select>
<select name="subscription_status">
<option value="free" <?= $currentSubscriptionStatus === 'free' ? 'selected' : '' ?>>Free</option>
<option value="active" <?= $currentSubscriptionStatus === 'active' ? 'selected' : '' ?>>Active</option>
<option value="trialing" <?= $currentSubscriptionStatus === 'trialing' ? 'selected' : '' ?>>Trialing</option>
<option value="inactive" <?= $currentSubscriptionStatus === 'inactive' ? 'selected' : '' ?>>Inactive</option>
<option value="canceled" <?= $currentSubscriptionStatus === 'canceled' ? 'selected' : '' ?>>Canceled</option>
</select>
<button type="submit" class="btn-sm">Save Plan</button>
</form>
<?php endif; ?>
<div class="admin-user-actions">
<?php if ((int)$row['is_active'] === 1 && (int)$row['id'] !== current_user_id()): ?>
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="action" value="impersonate_user">
<input type="hidden" name="target_user_id" value="<?= (int)$row['id'] ?>">
<button type="submit" class="btn-sm">Impersonate</button>
</form>
<?php else: ?>
<span class="muted"><?= ((int)$row['is_active'] === 1) ? 'Current user' : 'Inactive' ?></span>
<?php endif; ?>
<?php if ((int)$row['id'] !== current_user_id()): ?>
<form
method="post"
onsubmit="
const confirmation = prompt(
'This will permanently delete the account and related data.\n\nType DELETE to continue.'
);
if (confirmation !== 'DELETE') {
alert('Account deletion cancelled.');
return false;
}
return true;
"
style="margin-top:10px;"
>
<?= csrf_field() ?>
<input type="hidden" name="action" value="delete_user_account">
<input type="hidden" name="target_user_id" value="<?= (int)$row['id'] ?>">
<button type="submit" class="btn btn-sm btn-danger">
Delete Account
</button>
</form>
<?php endif; ?>
</div>
</article>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>