Skip to content

Commit 9dd10d2

Browse files
committed
phlix-server: auto-activate first profile + migration to fix existing users
- UserProfileManager::create(): first profile for a user now auto-activates (fixes the docblock contract that was never implemented: 'first profile created automatically becomes active') - Migration 079_activate_first_profiles: one-time data fix activating the first profile for every user who has profiles but none active (fixes existing users stranded with is_active=FALSE after the stream gating fix)
1 parent 33e0009 commit 9dd10d2

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- Migration: 079_activate_first_profiles
2+
--
3+
-- Problem: UserProfileManager::create() was inserting profiles with is_active=FALSE
4+
-- and nothing was ever auto-activating the first profile (despite the docblock
5+
-- contract). getActiveProfile() queries WHERE is_active=TRUE, so ALL existing
6+
-- users with profiles have getActiveProfile() returning null — breaking the
7+
-- WebPortalRouter stream_url gating fix (which requires an active profile).
8+
--
9+
-- Fix: For every user who has at least one profile but NONE active, activate
10+
-- their first profile (by created_at order). This is a one-time data correction
11+
-- that makes the docblock contract ("first profile becomes active") true for
12+
-- existing users, matching the behavior now implemented in create().
13+
--
14+
-- No schema changes — purely data.
15+
16+
-- Activate the first profile (by created_at) for each user who has profiles but no active one
17+
UPDATE user_profiles AS p
18+
INNER JOIN (
19+
SELECT user_id, MIN(created_at) as earliest
20+
FROM user_profiles
21+
GROUP BY user_id
22+
HAVING user_id NOT IN (
23+
SELECT user_id FROM user_profiles WHERE is_active = TRUE
24+
)
25+
) AS first_per_user ON p.user_id = first_per_user.user_id AND p.created_at = first_per_user.earliest
26+
SET p.is_active = TRUE;

src/Auth/UserProfileManager.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ public function create(string $userId, array $data): string
339339

340340
$id = $this->generateUuid();
341341
$isAdmin = $data['is_admin'] ?? false;
342+
// The first profile for a user auto-becomes active (per docblock contract).
343+
// is_active = true only if: explicitly set, OR it's the user's first profile.
344+
$isFirstProfile = UserRow::int($countRow, 'count', 0) === 0;
345+
$isActive = $data['is_active'] ?? $isFirstProfile;
342346

343347
$this->db->query(
344348
"INSERT INTO user_profiles (id, user_id, name, avatar_url, is_active, is_admin)
@@ -348,7 +352,7 @@ public function create(string $userId, array $data): string
348352
$userId,
349353
$name,
350354
$data['avatar_url'] ?? null,
351-
$data['is_active'] ?? false,
355+
$isActive,
352356
$isAdmin,
353357
]
354358
);

0 commit comments

Comments
 (0)