Skip to content

Manage accounts from a web admin page - #150

Open
Lorena-J wants to merge 1 commit into
mainfrom
task/h5-proposal-manage-accounts-from-a-web-admi
Open

Lorena-J wants to merge 1 commit into
mainfrom
task/h5-proposal-manage-accounts-from-a-web-admi

Conversation

@Lorena-J

Copy link
Copy Markdown
Collaborator

Closes #82.

Summary

  • Add an account administration page for creating, renaming, unlocking and deleting accounts, issuing passwords, and ending sessions. The route table checks the administrator role and the server logs changes.
  • Reuse the CLI account operations and show token usage and disk space per project. The same report is available through refract users report <email> [since].
  • Require an explicit refract users admin <email> grant for existing installations. Renaming an account revokes its sessions and outstanding email links. Account removal checks project reservations up front and reports any partial deletion.

For deployments behind a reverse proxy, use Refract's built-in sign-in or Cloudflare Access for account administration.

Verification

  • make test, TypeScript, formatting, lint, and Spanish strings
  • Administration flow in Chrome and WebKit
  • Regression tests for route permissions, migrations, email changes, report parity, and partial deletion

Comment thread cmd/refract/admin_api.go
if cleared {
out.Count = 1
}
case "remove":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: An administrator can delete their own account (and every project in it) from the admin page with no password or recent sign-in. /api/account/delete requires both, through Login or recentSignIn, but manage → apply "remove" (and "reset") accepts any administrator session plus confirmed: true. Anyone holding a stolen or left-open admin session can wipe any account, their own included, with no reauthentication. Fix: require reauthentication (the current password, or recentSignIn) for destructive admin actions, or at least refuse remove on the actor's own ID and send them to the self-service flow.

Comment thread internal/users/admin.go
if err := putPassword(ctx, tx, u.ID, hash); err != nil {
return auth.User{}, "", err
}
if err := s.markVerified(ctx, tx, u.ID); err != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: Resetting the password of an unconfirmed account marks it confirmed, but the squatter's data stays. issuePassword calls markVerified and never calls clearUnverified, which ConfirmByProvider runs for exactly this case. Example: someone signs up for victim@x, adds an AI provider or a git remote token, and never confirms. The admin resets the password for the real owner, and the owner now gets a confirmed account still carrying the attacker's connections and settings. Fix: in the reset and add paths, run clearUnverified(ctx, tx, u.ID) before markVerified when the account is not yet verified.

func (s *Service) StorageBytes(ctx context.Context, id string) (int64, error) {
defer lockRepo(id)()
var bytes int64
err := filepath.WalkDir(s.Path(id), func(_ string, entry fs.DirEntry, err error) error {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: The account report fails as a whole whenever one owned project has no directory. filepath.WalkDir on a missing root passes the ENOENT to the callback, which returns it, so accountUsageAndStorage returns an error and the admin sees 'Could not load account usage and storage.' This happens while a purge has renamed the directory to purgePrefix+id but the row is still listed, or for a project whose directory was lost. Fix: treat os.ErrNotExist on the root as 0 bytes (if errors.Is(err, fs.ErrNotExist) && path == root { return filepath.SkipDir }, or check it before walking).

// StorageBytes measures regular files in a project, including Git history
// and excluding generated build data. It never follows symbolic links.
func (s *Service) StorageBytes(ctx context.Context, id string) (int64, error) {
defer lockRepo(id)()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): StorageBytes holds lockRepo(id) for the whole recursive walk, .git included. The report measures every project of the account one after another, so opening an account in the admin page blocks that person's saves, checkpoints and pushes on large repositories until the walk ends. A disk-usage estimate doesn't need the repository lock. Fix: walk without lockRepo, and tolerate files vanishing mid-walk (skip fs.ErrNotExist).

Comment thread internal/users/users.go
}
u := auth.User{ID: newID(), Email: email}
at := s.now().Unix()
if _, err := tx.ExecContext(ctx, `INSERT INTO users (id, email, created_at, email_verified_at, is_admin)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing (not from this change, not blocking): A request already past session lookup can recreate an account an admin just deleted or renamed. The sessions middleware sets identity to the session's email, then Middleware → resolve → createTrusted inserts a fresh, confirmed account for that address. If it was the only account, n == 0 also makes the new account an administrator and adopts the ownerless projects. The new self-delete-from-admin and rename paths make this easier to hit. Fix: in password mode, resolve identities by the session's user ID rather than re-creating by email, or refuse creation when the identity came from a session.

Comment thread internal/users/admin.go
}
defer tx.Rollback()
var one int
err = tx.QueryRowContext(ctx, `SELECT 1 FROM users WHERE email = ?`, to).Scan(&one)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): Changing an account's email to the address it already has fails with 'An account already has this email address.' The existence check SELECT 1 FROM users WHERE email = ? matches the account being renamed. Fix: add AND id != ? (and treat a no-op rename as success), or return a clearer message.

Comment thread cmd/refract/admin_api.go
return out, "", err
}
switch in.Action {
case "reset":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): Nothing stops an administrator from removing the last administrator, themselves included. remove (and resetting or renaming one's own account) goes through with no check. After that, nobody can open Account administration until an operator runs refract users admin on the server. Fix: refuse remove when the target is the only remaining admin, or at least warn in the dialog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: manage accounts from a web admin page

2 participants