Changeset 324 in code for trunk


Ignore:
Timestamp:
Jun 8, 2020, 9:59:03 AM (5 years ago)
Author:
contact
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/cmd/sojuctl/main.go

    r308 r324  
    7070                        Password: string(hashed),
    7171                }
    72                 if err := db.CreateUser(&user); err != nil {
     72                if err := db.StoreUser(&user); err != nil {
    7373                        log.Fatalf("failed to create user: %v", err)
    7474                }
     
    9191
    9292                user := soju.User{
     93                        Created:  true,
    9394                        Username: username,
    9495                        Password: string(hashed),
    9596                }
    96                 if err := db.UpdatePassword(&user); err != nil {
     97                if err := db.StoreUser(&user); err != nil {
    9798                        log.Fatalf("failed to update password: %v", err)
    9899                }
    99 
    100100        default:
    101101                flag.Usage()
  • trunk/db.go

    r307 r324  
    1111
    1212type User struct {
     13        Created  bool
    1314        Username string
    1415        Password string // hashed
     
    200201                        return nil, err
    201202                }
     203                user.Created = true
    202204                user.Password = fromStringPtr(password)
    203205                users = append(users, user)
     
    214216        defer db.lock.RUnlock()
    215217
    216         user := &User{Username: username}
     218        user := &User{Created: true, Username: username}
    217219
    218220        var password *string
     
    225227}
    226228
    227 func (db *DB) CreateUser(user *User) error {
     229func (db *DB) StoreUser(user *User) error {
    228230        db.lock.Lock()
    229231        defer db.lock.Unlock()
    230232
    231233        password := toStringPtr(user.Password)
    232         _, err := db.db.Exec("INSERT INTO User(username, password) VALUES (?, ?)", user.Username, password)
    233         return err
    234 }
    235 
    236 func (db *DB) UpdatePassword(user *User) error {
    237         db.lock.Lock()
    238         defer db.lock.Unlock()
    239 
    240         password := toStringPtr(user.Password)
    241         _, err := db.db.Exec(`UPDATE User
    242         SET password = ?
    243         WHERE username = ?`,
    244                 password, user.Username)
     234
     235        var err error
     236        if user.Created {
     237                _, err = db.db.Exec("UPDATE User SET password = ? WHERE username = ?",
     238                        password, user.Username)
     239        } else {
     240                _, err = db.db.Exec("INSERT INTO User(username, password) VALUES (?, ?)",
     241                        user.Username, password)
     242                if err == nil {
     243                        user.Created = true
     244                }
     245        }
     246
    245247        return err
    246248}
  • trunk/user.go

    r313 r324  
    548548func (u *user) updatePassword(hashed string) error {
    549549        u.User.Password = hashed
    550         return u.srv.db.UpdatePassword(&u.User)
    551 }
     550        return u.srv.db.StoreUser(&u.User)
     551}
Note: See TracChangeset for help on using the changeset viewer.