database: add NewUser

This commit is contained in:
Simon Ser 2023-10-31 23:57:35 +01:00
parent 38e32ddd6c
commit 4f7b577734
6 changed files with 21 additions and 24 deletions

View File

@ -74,13 +74,10 @@ func main() {
log.Fatalf("failed to hash password: %v", err) log.Fatalf("failed to hash password: %v", err)
} }
user := database.User{ user := database.NewUser(username)
Username: username, user.Password = string(hashed)
Password: string(hashed), user.Admin = *admin
Admin: *admin, if err := db.StoreUser(ctx, user); err != nil {
Enabled: true,
}
if err := db.StoreUser(ctx, &user); err != nil {
log.Fatalf("failed to create user: %v", err) log.Fatalf("failed to create user: %v", err)
} }
case "change-password": case "change-password":

View File

@ -106,8 +106,9 @@ func main() {
if ok { if ok {
log.Printf("user %q: updating existing user", username) log.Printf("user %q: updating existing user", username)
} else { } else {
u = database.NewUser(username)
// "!!" is an invalid crypt format, thus disables password auth // "!!" is an invalid crypt format, thus disables password auth
u = &database.User{Username: username, Password: "!!", Enabled: true} u.Password = "!!"
usersCreated++ usersCreated++
log.Printf("user %q: creating new user", username) log.Printf("user %q: creating new user", username)
} }

View File

@ -98,6 +98,13 @@ type User struct {
DownstreamInteractedAt time.Time DownstreamInteractedAt time.Time
} }
func NewUser(username string) *User {
return &User{
Username: username,
Enabled: true,
}
}
func (u *User) CheckPassword(password string) (upgraded bool, err error) { func (u *User) CheckPassword(password string) (upgraded bool, err error) {
// Password auth disabled // Password auth disabled
if u.Password == "" { if u.Password == "" {

View File

@ -515,11 +515,8 @@ func (s *Server) getOrCreateUser(ctx context.Context, username string) (*user, e
} }
// Can't find the user in the DB -- try to create it // Can't find the user in the DB -- try to create it
record := database.User{ record := database.NewUser(username)
Username: username, user, err := s.createUser(ctx, record)
Enabled: true,
}
user, err := s.createUser(ctx, &record)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to automatically create user %q after successful authentication: %v", username, err) return nil, fmt.Errorf("failed to automatically create user %q after successful authentication: %v", username, err)
} }

View File

@ -48,10 +48,7 @@ func createTempPostgresDB(t *testing.T) database.Database {
} }
func createTestUser(t *testing.T, db database.Database) *database.User { func createTestUser(t *testing.T, db database.Database) *database.User {
record := &database.User{ record := database.NewUser(testUsername)
Username: testUsername,
Enabled: true,
}
if err := record.SetPassword(testPassword); err != nil { if err := record.SetPassword(testPassword); err != nil {
t.Fatalf("failed to generate bcrypt hash: %v", err) t.Fatalf("failed to generate bcrypt hash: %v", err)
} }

View File

@ -971,13 +971,11 @@ func handleUserCreate(ctx *serviceContext, params []string) error {
return fmt.Errorf("flag -password is required") return fmt.Errorf("flag -password is required")
} }
user := &database.User{ user := database.NewUser(*username)
Username: *username, user.Nick = *nick
Nick: *nick, user.Realname = *realname
Realname: *realname, user.Admin = *admin
Admin: *admin, user.Enabled = *enabled
Enabled: *enabled,
}
if !*disablePassword { if !*disablePassword {
if err := user.SetPassword(*password); err != nil { if err := user.SetPassword(*password); err != nil {
return err return err