Skip to content

Commit

Permalink
Move admin actions near User related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gusah009 committed Jul 21, 2024
1 parent 411fda4 commit 9ab1142
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions server/backend/database/memory/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9ab1142

Please sign in to comment.