Add self-info endpoint for ProjectConfigurator discovery, sync shared updates
GET /api/info reports name/tabs/mount types (empty - state lives in Postgres, no shared file mounts) so ProjectConfigurator's Projects workspace can register this instance from just a URL. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
557a61b4ee
commit
bbf5d8bb05
126 files changed
+2897
-8542
No files matched your search
@@ -24,6 +24,9 @@ type User struct {
|
||||
Role Role
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
// LastLoginAt is nil until the account's first successful POST
|
||||
// /api/login (registration alone does not set it - see UpdateLastLogin).
|
||||
LastLoginAt *time.Time
|
||||
}
|
||||
|
||||
// Store performs common auth persistence against a caller-owned pool.
|
||||
@@ -67,7 +70,8 @@ func EnsureSchema(ctx context.Context, pool *pgxpool.Pool, options SchemaOptions
|
||||
totp_secret TEXT NOT NULL DEFAULT '',
|
||||
totp_enabled BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
last_login_at TIMESTAMPTZ
|
||||
);
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS email TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS role TEXT NOT NULL DEFAULT 'user';
|
||||
@@ -75,6 +79,10 @@ func EnsureSchema(ctx context.Context, pool *pgxpool.Pool, options SchemaOptions
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS totp_enabled BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ NOT NULL DEFAULT now();
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now();
|
||||
-- last_login_at is nullable (NULL means "never logged in" - registration
|
||||
-- alone does not count, only a successful POST /api/login) and has no
|
||||
-- DEFAULT, unlike every other column here.
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS last_login_at TIMESTAMPTZ;
|
||||
UPDATE auth_users SET role = 'superadmin'
|
||||
WHERE id = (SELECT id FROM auth_users ORDER BY created_at, id LIMIT 1)
|
||||
AND NOT EXISTS (SELECT 1 FROM auth_users WHERE role = 'superadmin');
|
||||
@@ -204,9 +212,9 @@ func (s *Store) CreateUser(ctx context.Context, username, email, passwordHash st
|
||||
VALUES ($1, $2, $3,
|
||||
CASE WHEN EXISTS (SELECT 1 FROM auth_users) THEN 'user' ELSE 'superadmin' END,
|
||||
now())
|
||||
RETURNING id, username, email, password_hash, role, created_at, updated_at
|
||||
RETURNING id, username, email, password_hash, role, created_at, updated_at, last_login_at
|
||||
`, username, email, passwordHash).Scan(
|
||||
&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt,
|
||||
&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt, &user.LastLoginAt,
|
||||
)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
@@ -224,7 +232,7 @@ func (s *Store) FindByIdentifier(ctx context.Context, identifier string) (*User,
|
||||
return nil, err
|
||||
}
|
||||
return s.find(ctx, `
|
||||
SELECT id, username, email, password_hash, role, created_at, updated_at
|
||||
SELECT id, username, email, password_hash, role, created_at, updated_at, last_login_at
|
||||
FROM auth_users
|
||||
WHERE username = $1 OR lower(email) = lower($1)
|
||||
`, identifier)
|
||||
@@ -236,7 +244,7 @@ func (s *Store) FindByID(ctx context.Context, id int64) (*User, error) {
|
||||
return nil, err
|
||||
}
|
||||
return s.find(ctx, `
|
||||
SELECT id, username, email, password_hash, role, created_at, updated_at
|
||||
SELECT id, username, email, password_hash, role, created_at, updated_at, last_login_at
|
||||
FROM auth_users WHERE id = $1
|
||||
`, id)
|
||||
}
|
||||
@@ -264,7 +272,7 @@ func (s *Store) ListUsers(ctx context.Context) ([]User, error) {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT id, username, email, role, created_at, updated_at
|
||||
SELECT id, username, email, role, created_at, updated_at, last_login_at
|
||||
FROM auth_users ORDER BY created_at, id
|
||||
`)
|
||||
if err != nil {
|
||||
@@ -274,7 +282,7 @@ func (s *Store) ListUsers(ctx context.Context) ([]User, error) {
|
||||
users := make([]User, 0)
|
||||
for rows.Next() {
|
||||
var user User
|
||||
if err := rows.Scan(&user.ID, &user.Username, &user.Email, &user.Role, &user.CreatedAt, &user.UpdatedAt); err != nil {
|
||||
if err := rows.Scan(&user.ID, &user.Username, &user.Email, &user.Role, &user.CreatedAt, &user.UpdatedAt, &user.LastLoginAt); err != nil {
|
||||
return nil, fmt.Errorf("scan auth user: %w", err)
|
||||
}
|
||||
users = append(users, user)
|
||||
@@ -292,9 +300,9 @@ func (s *Store) CreateManagedUser(ctx context.Context, username, email, password
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
INSERT INTO auth_users (username, email, password_hash, role, updated_at)
|
||||
VALUES ($1, $2, $3, $4, now())
|
||||
RETURNING id, username, email, password_hash, role, created_at, updated_at
|
||||
RETURNING id, username, email, password_hash, role, created_at, updated_at, last_login_at
|
||||
`, username, email, passwordHash, role).Scan(
|
||||
&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt,
|
||||
&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt, &user.LastLoginAt,
|
||||
)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
@@ -326,6 +334,20 @@ func (s *Store) UpdateUser(ctx context.Context, id int64, username, email, passw
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateLastLogin stamps last_login_at to now(). Callers should treat this as
|
||||
// best-effort: an already-successful login (the session is live) shouldn't
|
||||
// fail because this update didn't - see AuthGuard's handleLogin for the
|
||||
// intended "log and continue" pattern.
|
||||
func (s *Store) UpdateLastLogin(ctx context.Context, id int64) error {
|
||||
if err := s.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := s.pool.Exec(ctx, `UPDATE auth_users SET last_login_at = now() WHERE id = $1`, id); err != nil {
|
||||
return fmt.Errorf("update last login: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteUser removes one account. Callers enforce the superadmin/self policy.
|
||||
func (s *Store) DeleteUser(ctx context.Context, id int64) error {
|
||||
if err := s.validate(); err != nil {
|
||||
@@ -358,7 +380,7 @@ func (s *Store) FindSessionUser(ctx context.Context, tokenHash string) (*User, e
|
||||
return nil, err
|
||||
}
|
||||
return s.find(ctx, `
|
||||
SELECT u.id, u.username, u.email, u.password_hash, u.role, u.created_at, u.updated_at
|
||||
SELECT u.id, u.username, u.email, u.password_hash, u.role, u.created_at, u.updated_at, u.last_login_at
|
||||
FROM auth_sessions s
|
||||
JOIN auth_users u ON u.id = s.user_id
|
||||
WHERE s.token_hash = $1 AND s.expires_at > now()
|
||||
@@ -423,7 +445,7 @@ func (s *Store) validate() error {
|
||||
func (s *Store) find(ctx context.Context, query string, arg any) (*User, error) {
|
||||
var user User
|
||||
err := s.pool.QueryRow(ctx, query, arg).Scan(
|
||||
&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt,
|
||||
&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt, &user.LastLoginAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
|
||||
@@ -61,6 +61,9 @@ func TestStoreIntegration(t *testing.T) {
|
||||
if created.Role != RoleSuperAdmin {
|
||||
t.Fatalf("first CreateUser() role = %q, want %q", created.Role, RoleSuperAdmin)
|
||||
}
|
||||
if created.LastLoginAt != nil {
|
||||
t.Fatalf("CreateUser() LastLoginAt = %v, want nil before any login", created.LastLoginAt)
|
||||
}
|
||||
|
||||
byIdentifier, err := store.FindByIdentifier(ctx, "root")
|
||||
if err != nil {
|
||||
@@ -101,4 +104,18 @@ func TestStoreIntegration(t *testing.T) {
|
||||
if len(users) != 1 || users[0].ID != created.ID {
|
||||
t.Fatalf("ListUsers() = %+v, want exactly the created user", users)
|
||||
}
|
||||
if users[0].LastLoginAt != nil {
|
||||
t.Fatalf("ListUsers()[0].LastLoginAt = %v, want nil before any login", users[0].LastLoginAt)
|
||||
}
|
||||
|
||||
if err := store.UpdateLastLogin(ctx, created.ID); err != nil {
|
||||
t.Fatalf("UpdateLastLogin() error = %v", err)
|
||||
}
|
||||
afterLogin, err := store.FindByID(ctx, created.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("FindByID() after UpdateLastLogin error = %v", err)
|
||||
}
|
||||
if afterLogin == nil || afterLogin.LastLoginAt == nil {
|
||||
t.Fatalf("FindByID() after UpdateLastLogin = %+v, want LastLoginAt set", afterLogin)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user