Skip to content

Commit

Permalink
chore(refactor): admin delete should use retrieveRequestParams
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Oct 28, 2024
1 parent 26d2e36 commit 373735c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
20 changes: 8 additions & 12 deletions internal/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"context"
"encoding/json"
"net/http"
"time"

Expand Down Expand Up @@ -512,21 +511,18 @@ func (a *API) adminUserDelete(w http.ResponseWriter, r *http.Request) error {
user := getUser(ctx)
adminUser := getAdminUser(ctx)

var err error
// ShouldSoftDelete defaults to false
params := &adminUserDeleteParams{}
body, err := getBodyBytes(r)
if err != nil {
return internalServerError("Could not read body").WithInternalError(err)
}
if len(body) > 0 {
if err := json.Unmarshal(body, params); err != nil {
return badRequestError(ErrorCodeBadJSON, "Could not read params: %v", err)
if err := retrieveRequestParams(r, params); err != nil {
if err.(*HTTPError).ErrorCode == ErrorCodeBadJSON {
// request body is empty so the default behavior should be to hard delete the user
params.ShouldSoftDelete = false
} else {
return err
}
} else {
params.ShouldSoftDelete = false
}

err = a.db.Transaction(func(tx *storage.Connection) error {
err := a.db.Transaction(func(tx *storage.Connection) error {
if terr := models.NewAuditLogEntry(r, tx, adminUser, models.UserDeletedAction, "", map[string]interface{}{
"user_id": user.ID,
"user_email": user.Email,
Expand Down
8 changes: 2 additions & 6 deletions internal/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ func isStringInSlice(checkValue string, list []string) bool {
return false
}

// getBodyBytes returns a byte array of the request's Body.
func getBodyBytes(req *http.Request) ([]byte, error) {
return utilities.GetBodyBytes(req)
}

type RequestParams interface {
AdminUserParams |
CreateSSOProviderParams |
Expand All @@ -82,6 +77,7 @@ type RequestParams interface {
VerifyFactorParams |
VerifyParams |
adminUserUpdateFactorParams |
adminUserDeleteParams |
ChallengeFactorParams |
struct {
Email string `json:"email"`
Expand All @@ -94,7 +90,7 @@ type RequestParams interface {

// retrieveRequestParams is a generic method that unmarshals the request body into the params struct provided
func retrieveRequestParams[A RequestParams](r *http.Request, params *A) error {
body, err := getBodyBytes(r)
body, err := utilities.GetBodyBytes(r)
if err != nil {
return internalServerError("Could not read body into byte slice").WithInternalError(err)
}
Expand Down

0 comments on commit 373735c

Please sign in to comment.