UserUpdate updates fields on a user
(w http.ResponseWriter, r *http.Request)
| 49 | |
| 50 | // UserUpdate updates fields on a user |
| 51 | func (a *API) UserUpdate(w http.ResponseWriter, r *http.Request) error { |
| 52 | ctx := r.Context() |
| 53 | config := a.getConfig(ctx) |
| 54 | instanceID := getInstanceID(ctx) |
| 55 | |
| 56 | params := &UserUpdateParams{} |
| 57 | jsonDecoder := json.NewDecoder(r.Body) |
| 58 | err := jsonDecoder.Decode(params) |
| 59 | if err != nil { |
| 60 | return badRequestError("Could not read User Update params: %v", err) |
| 61 | } |
| 62 | |
| 63 | claims := getClaims(ctx) |
| 64 | userID, err := uuid.FromString(claims.Subject) |
| 65 | if err != nil { |
| 66 | return badRequestError("Could not read User ID claim") |
| 67 | } |
| 68 | |
| 69 | user, err := models.FindUserByID(a.db, userID) |
| 70 | if err != nil { |
| 71 | if models.IsNotFoundError(err) { |
| 72 | return notFoundError(err.Error()) |
| 73 | } |
| 74 | return internalServerError("Database error finding user").WithInternalError(err) |
| 75 | } |
| 76 | |
| 77 | log := getLogEntry(r) |
| 78 | log.Debugf("Checking params for token %v", params) |
| 79 | |
| 80 | err = a.db.Transaction(func(tx *storage.Connection) error { |
| 81 | var terr error |
| 82 | if params.Password != "" { |
| 83 | if terr = user.UpdatePassword(tx, params.Password); terr != nil { |
| 84 | return internalServerError("Error during password storage").WithInternalError(terr) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if params.Data != nil { |
| 89 | if terr = user.UpdateUserMetaData(tx, params.Data); terr != nil { |
| 90 | return internalServerError("Error updating user").WithInternalError(terr) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if params.AppData != nil { |
| 95 | if !a.isAdmin(ctx, user, config.JWT.Aud) { |
| 96 | return unauthorizedError("Updating app_metadata requires admin privileges") |
| 97 | } |
| 98 | |
| 99 | if terr = user.UpdateAppMetaData(tx, params.AppData); terr != nil { |
| 100 | return internalServerError("Error updating user").WithInternalError(terr) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if params.EmailChangeToken != "" { |
| 105 | log.Debugf("Got change token %v", params.EmailChangeToken) |
| 106 | |
| 107 | if params.EmailChangeToken != user.EmailChangeToken { |
| 108 | return unauthorizedError("Email Change Token didn't match token on file") |
nothing calls this directly
no test coverage detected