Phase 10, part 4: inviting users, and what admins can change
Settings → Users lists everyone with their access and status. Admins invite people by email (a link for 7 days), as admins or as members with modules each to view or edit; the invitation's page sets the password, profile and emails wanted. Admins can resend or withdraw invitations, change access, turn accounts off and on, unlock, reset two-factor, sign someone out, and remove them; never the owner, nor themselves. Your account gets the emails wanted and a photo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+48
-4
@@ -236,10 +236,17 @@ func SaveProfile(d *db.DB, u *User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// SetStatus activates, disables or re-invites a user.
|
||||
// SetStatus activates, disables or re-invites a user. A disabled user's
|
||||
// sessions end.
|
||||
func SetStatus(d *db.DB, id int64, s Status) error {
|
||||
_, err := d.Exec(`UPDATE users SET status = ? WHERE id = ?`, string(s), id)
|
||||
return err
|
||||
if _, err := d.Exec(`UPDATE users SET status = ? WHERE id = ?`, string(s), id); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == Disabled {
|
||||
_, err := d.Exec(`DELETE FROM sessions WHERE user_id = ?`, id)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SignedIn records a sign-in.
|
||||
@@ -257,7 +264,7 @@ func Delete(d *db.DB, id int64) error {
|
||||
defer tx.Rollback()
|
||||
// Not left to ON DELETE CASCADE: SQLite enforces it only with
|
||||
// foreign_keys on, and an imported database may not have had them.
|
||||
for _, q := range []string{`DELETE FROM sessions WHERE user_id = ?`, `DELETE FROM memberships WHERE user_id = ?`, `DELETE FROM users WHERE id = ?`} {
|
||||
for _, q := range []string{`DELETE FROM sessions WHERE user_id = ?`, `DELETE FROM links WHERE user_id = ?`, `DELETE FROM memberships WHERE user_id = ?`, `DELETE FROM users WHERE id = ?`} {
|
||||
if _, err := tx.Exec(q, id); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -409,3 +416,40 @@ func HasOwner(d *db.DB, team int64) (bool, error) {
|
||||
err := d.QueryRow(`SELECT count(*) FROM memberships WHERE team_id = ? AND role = 'owner'`, team).Scan(&n)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
// TeamUser is a user with their place in the team.
|
||||
type TeamUser struct {
|
||||
*User
|
||||
Membership
|
||||
}
|
||||
|
||||
// Members are the team's users, people first, then by name.
|
||||
func Members(d *db.DB, team int64) ([]TeamUser, error) {
|
||||
cols := strings.ReplaceAll("u."+userCols, ", ", ", u.")
|
||||
rows, err := d.Query(`SELECT `+cols+`, m.role, m.modules FROM users u JOIN memberships m ON m.user_id = u.id
|
||||
WHERE m.team_id = ? ORDER BY u.kind DESC, u.name COLLATE NOCASE, u.email`, team)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []TeamUser
|
||||
for rows.Next() {
|
||||
var role, modules string
|
||||
u, err := scanUser(rowWith{rows, []any{&role, &modules}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := Membership{Team: team, User: u.ID, Role: Role(role), Modules: map[string]Access{}}
|
||||
_ = json.Unmarshal([]byte(modules), &m.Modules)
|
||||
out = append(out, TeamUser{User: u, Membership: m})
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// rowWith scans a user's columns followed by some more.
|
||||
type rowWith struct {
|
||||
row interface{ Scan(...any) error }
|
||||
more []any
|
||||
}
|
||||
|
||||
func (r rowWith) Scan(dest ...any) error { return r.row.Scan(append(dest, r.more...)...) }
|
||||
|
||||
Reference in New Issue
Block a user