diff --git a/server/backend/database/memory/database.go b/server/backend/database/memory/database.go index 77794d008..6b5eea6da 100644 --- a/server/backend/database/memory/database.go +++ b/server/backend/database/memory/database.go @@ -402,6 +402,52 @@ func (d *DB) CreateUserInfo( return info, nil } +// DeleteUserInfoByName deletes a user by name. +func (d *DB) DeleteUserInfoByName(_ context.Context, username string) error { + txn := d.db.Txn(true) + defer txn.Abort() + + raw, err := txn.First(tblUsers, "username", username) + if err != nil { + return fmt.Errorf("find user by username: %w", err) + } + if raw == nil { + return fmt.Errorf("%s: %w", username, database.ErrUserNotFound) + } + + info := raw.(*database.UserInfo).DeepCopy() + if err = txn.Delete(tblUsers, info); err != nil { + return fmt.Errorf("delete account %s: %w", info.ID, err) + } + + txn.Commit() + return nil +} + +// ChangePassword changes to new password. +func (d *DB) ChangePassword(_ context.Context, username, hashedNewPassword string) error { + txn := d.db.Txn(true) + defer txn.Abort() + + raw, err := txn.First(tblUsers, "username", username) + if err != nil { + return fmt.Errorf("find user by username: %w", err) + } + if raw == nil { + return fmt.Errorf("%s: %w", username, database.ErrUserNotFound) + } + + info := raw.(*database.UserInfo).DeepCopy() + info.HashedPassword = hashedNewPassword + if err := txn.Insert(tblUsers, info); err != nil { + return fmt.Errorf("change password user: %w", err) + } + + txn.Commit() + + return nil +} + // FindUserInfoByID finds a user by the given ID. func (d *DB) FindUserInfoByID(_ context.Context, clientID types.ID) (*database.UserInfo, error) { txn := d.db.Txn(false) @@ -627,52 +673,6 @@ func (d *DB) UpdateClientInfoAfterPushPull( return nil } -// DeleteUserInfoByName deletes a user by name. -func (d *DB) DeleteUserInfoByName(_ context.Context, username string) error { - txn := d.db.Txn(true) - defer txn.Abort() - - raw, err := txn.First(tblUsers, "username", username) - if err != nil { - return fmt.Errorf("find user by username: %w", err) - } - if raw == nil { - return fmt.Errorf("%s: %w", username, database.ErrUserNotFound) - } - - info := raw.(*database.UserInfo).DeepCopy() - if err = txn.Delete(tblUsers, info); err != nil { - return fmt.Errorf("delete account %s: %w", info.ID, err) - } - - txn.Commit() - return nil -} - -// ChangePassword changes to new password. -func (d *DB) ChangePassword(_ context.Context, username, hashedNewPassword string) error { - txn := d.db.Txn(true) - defer txn.Abort() - - raw, err := txn.First(tblUsers, "username", username) - if err != nil { - return fmt.Errorf("find user by username: %w", err) - } - if raw == nil { - return fmt.Errorf("%s: %w", username, database.ErrUserNotFound) - } - - info := raw.(*database.UserInfo).DeepCopy() - info.HashedPassword = hashedNewPassword - if err := txn.Insert(tblUsers, info); err != nil { - return fmt.Errorf("change password user: %w", err) - } - - txn.Commit() - - return nil -} - // FindDeactivateCandidatesPerProject finds the clients that need housekeeping per project. func (d *DB) FindDeactivateCandidatesPerProject( _ context.Context,