Introduce User.Created
For Network and Channel, the database only needed to define one Store operation to create/update a record. However since User is missing an ID we couldn't have a single StoreUser function like other types. We had CreateUser and UpdatePassword. As new User fields get added (e.g. the upcoming Admin flag) this isn't sustainable. We could have CreateUser and UpdateUser, but this wouldn't be consistent with other types. Instead, introduce User.Created which indicates whether the record is already stored in the DB. This can be used in a new StoreUser function to decide whether we need to UPDATE or INSERT without relying on SQL constraints and INSERT OR UPDATE. The ListUsers and GetUser functions set User.Created to true.
This commit is contained in:
parent
d0cf1d2882
commit
998546cdc3
@ -69,7 +69,7 @@ func main() {
|
||||
Username: username,
|
||||
Password: string(hashed),
|
||||
}
|
||||
if err := db.CreateUser(&user); err != nil {
|
||||
if err := db.StoreUser(&user); err != nil {
|
||||
log.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
case "change-password":
|
||||
@ -90,13 +90,13 @@ func main() {
|
||||
}
|
||||
|
||||
user := soju.User{
|
||||
Created: true,
|
||||
Username: username,
|
||||
Password: string(hashed),
|
||||
}
|
||||
if err := db.UpdatePassword(&user); err != nil {
|
||||
if err := db.StoreUser(&user); err != nil {
|
||||
log.Fatalf("failed to update password: %v", err)
|
||||
}
|
||||
|
||||
default:
|
||||
flag.Usage()
|
||||
if cmd != "help" {
|
||||
|
28
db.go
28
db.go
@ -10,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Created bool
|
||||
Username string
|
||||
Password string // hashed
|
||||
}
|
||||
@ -199,6 +200,7 @@ func (db *DB) ListUsers() ([]User, error) {
|
||||
if err := rows.Scan(&user.Username, &password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Created = true
|
||||
user.Password = fromStringPtr(password)
|
||||
users = append(users, user)
|
||||
}
|
||||
@ -213,7 +215,7 @@ func (db *DB) GetUser(username string) (*User, error) {
|
||||
db.lock.RLock()
|
||||
defer db.lock.RUnlock()
|
||||
|
||||
user := &User{Username: username}
|
||||
user := &User{Created: true, Username: username}
|
||||
|
||||
var password *string
|
||||
row := db.db.QueryRow("SELECT password FROM User WHERE username = ?", username)
|
||||
@ -224,24 +226,24 @@ func (db *DB) GetUser(username string) (*User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (db *DB) CreateUser(user *User) error {
|
||||
func (db *DB) StoreUser(user *User) error {
|
||||
db.lock.Lock()
|
||||
defer db.lock.Unlock()
|
||||
|
||||
password := toStringPtr(user.Password)
|
||||
_, err := db.db.Exec("INSERT INTO User(username, password) VALUES (?, ?)", user.Username, password)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) UpdatePassword(user *User) error {
|
||||
db.lock.Lock()
|
||||
defer db.lock.Unlock()
|
||||
var err error
|
||||
if user.Created {
|
||||
_, err = db.db.Exec("UPDATE User SET password = ? WHERE username = ?",
|
||||
password, user.Username)
|
||||
} else {
|
||||
_, err = db.db.Exec("INSERT INTO User(username, password) VALUES (?, ?)",
|
||||
user.Username, password)
|
||||
if err == nil {
|
||||
user.Created = true
|
||||
}
|
||||
}
|
||||
|
||||
password := toStringPtr(user.Password)
|
||||
_, err := db.db.Exec(`UPDATE User
|
||||
SET password = ?
|
||||
WHERE username = ?`,
|
||||
password, user.Username)
|
||||
return err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user