Add User.ID
For now it's just a new field that'll be useful to generate user ident strings. It uses the SQLite implicit rowid column. In the future the DB interface will need to be updated to use user IDs instead of usernames.
This commit is contained in:
parent
7c31c26d86
commit
0812c795f4
23
db.go
23
db.go
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Created bool
|
ID int64
|
||||||
Username string
|
Username string
|
||||||
Password string // hashed
|
Password string // hashed
|
||||||
Admin bool
|
Admin bool
|
||||||
@ -190,7 +190,7 @@ func (db *DB) ListUsers() ([]User, error) {
|
|||||||
db.lock.RLock()
|
db.lock.RLock()
|
||||||
defer db.lock.RUnlock()
|
defer db.lock.RUnlock()
|
||||||
|
|
||||||
rows, err := db.db.Query("SELECT username, password, admin FROM User")
|
rows, err := db.db.Query("SELECT rowid, username, password, admin FROM User")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -200,10 +200,9 @@ func (db *DB) ListUsers() ([]User, error) {
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var user User
|
var user User
|
||||||
var password *string
|
var password *string
|
||||||
if err := rows.Scan(&user.Username, &password, &user.Admin); err != nil {
|
if err := rows.Scan(&user.ID, &user.Username, &password, &user.Admin); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
user.Created = true
|
|
||||||
user.Password = fromStringPtr(password)
|
user.Password = fromStringPtr(password)
|
||||||
users = append(users, user)
|
users = append(users, user)
|
||||||
}
|
}
|
||||||
@ -218,11 +217,11 @@ func (db *DB) GetUser(username string) (*User, error) {
|
|||||||
db.lock.RLock()
|
db.lock.RLock()
|
||||||
defer db.lock.RUnlock()
|
defer db.lock.RUnlock()
|
||||||
|
|
||||||
user := &User{Created: true, Username: username}
|
user := &User{Username: username}
|
||||||
|
|
||||||
var password *string
|
var password *string
|
||||||
row := db.db.QueryRow("SELECT password, admin FROM User WHERE username = ?", username)
|
row := db.db.QueryRow("SELECT rowid, password, admin FROM User WHERE username = ?", username)
|
||||||
if err := row.Scan(&password, &user.Admin); err != nil {
|
if err := row.Scan(&user.ID, &password, &user.Admin); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
user.Password = fromStringPtr(password)
|
user.Password = fromStringPtr(password)
|
||||||
@ -236,15 +235,17 @@ func (db *DB) StoreUser(user *User) error {
|
|||||||
password := toStringPtr(user.Password)
|
password := toStringPtr(user.Password)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if user.Created {
|
if user.ID != 0 {
|
||||||
_, err = db.db.Exec("UPDATE User SET password = ?, admin = ? WHERE username = ?",
|
_, err = db.db.Exec("UPDATE User SET password = ?, admin = ? WHERE username = ?",
|
||||||
password, user.Admin, user.Username)
|
password, user.Admin, user.Username)
|
||||||
} else {
|
} else {
|
||||||
_, err = db.db.Exec("INSERT INTO User(username, password, admin) VALUES (?, ?, ?)",
|
var res sql.Result
|
||||||
|
res, err = db.db.Exec("INSERT INTO User(username, password, admin) VALUES (?, ?, ?, ?)",
|
||||||
user.Username, password, user.Admin)
|
user.Username, password, user.Admin)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
user.Created = true
|
return err
|
||||||
}
|
}
|
||||||
|
user.ID, err = res.LastInsertId()
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user