database: add NewUser
This commit is contained in:
parent
38e32ddd6c
commit
4f7b577734
@ -74,13 +74,10 @@ func main() {
|
||||
log.Fatalf("failed to hash password: %v", err)
|
||||
}
|
||||
|
||||
user := database.User{
|
||||
Username: username,
|
||||
Password: string(hashed),
|
||||
Admin: *admin,
|
||||
Enabled: true,
|
||||
}
|
||||
if err := db.StoreUser(ctx, &user); err != nil {
|
||||
user := database.NewUser(username)
|
||||
user.Password = string(hashed)
|
||||
user.Admin = *admin
|
||||
if err := db.StoreUser(ctx, user); err != nil {
|
||||
log.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
case "change-password":
|
||||
|
@ -106,8 +106,9 @@ func main() {
|
||||
if ok {
|
||||
log.Printf("user %q: updating existing user", username)
|
||||
} else {
|
||||
u = database.NewUser(username)
|
||||
// "!!" is an invalid crypt format, thus disables password auth
|
||||
u = &database.User{Username: username, Password: "!!", Enabled: true}
|
||||
u.Password = "!!"
|
||||
usersCreated++
|
||||
log.Printf("user %q: creating new user", username)
|
||||
}
|
||||
|
@ -98,6 +98,13 @@ type User struct {
|
||||
DownstreamInteractedAt time.Time
|
||||
}
|
||||
|
||||
func NewUser(username string) *User {
|
||||
return &User{
|
||||
Username: username,
|
||||
Enabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *User) CheckPassword(password string) (upgraded bool, err error) {
|
||||
// Password auth disabled
|
||||
if u.Password == "" {
|
||||
|
@ -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
|
||||
record := database.User{
|
||||
Username: username,
|
||||
Enabled: true,
|
||||
}
|
||||
user, err := s.createUser(ctx, &record)
|
||||
record := database.NewUser(username)
|
||||
user, err := s.createUser(ctx, record)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to automatically create user %q after successful authentication: %v", username, err)
|
||||
}
|
||||
|
@ -48,10 +48,7 @@ func createTempPostgresDB(t *testing.T) database.Database {
|
||||
}
|
||||
|
||||
func createTestUser(t *testing.T, db database.Database) *database.User {
|
||||
record := &database.User{
|
||||
Username: testUsername,
|
||||
Enabled: true,
|
||||
}
|
||||
record := database.NewUser(testUsername)
|
||||
if err := record.SetPassword(testPassword); err != nil {
|
||||
t.Fatalf("failed to generate bcrypt hash: %v", err)
|
||||
}
|
||||
|
12
service.go
12
service.go
@ -971,13 +971,11 @@ func handleUserCreate(ctx *serviceContext, params []string) error {
|
||||
return fmt.Errorf("flag -password is required")
|
||||
}
|
||||
|
||||
user := &database.User{
|
||||
Username: *username,
|
||||
Nick: *nick,
|
||||
Realname: *realname,
|
||||
Admin: *admin,
|
||||
Enabled: *enabled,
|
||||
}
|
||||
user := database.NewUser(*username)
|
||||
user.Nick = *nick
|
||||
user.Realname = *realname
|
||||
user.Admin = *admin
|
||||
user.Enabled = *enabled
|
||||
if !*disablePassword {
|
||||
if err := user.SetPassword(*password); err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user