-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UserUpdate endpoint to Quarterdeck (#154)
Co-authored-by: Patrick Deziel <42919891+pdeziel@users.noreply.github.com>
- Loading branch information
1 parent
6792de1
commit 62c459b
Showing
9 changed files
with
380 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package quarterdeck | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/oklog/ulid/v2" | ||
"github.com/rotationalio/ensign/pkg/quarterdeck/api/v1" | ||
"github.com/rotationalio/ensign/pkg/quarterdeck/db/models" | ||
"github.com/rotationalio/ensign/pkg/quarterdeck/middleware" | ||
"github.com/rotationalio/ensign/pkg/quarterdeck/tokens" | ||
ulids "github.com/rotationalio/ensign/pkg/utils/ulid" | ||
) | ||
|
||
func (s *Server) UserUpdate(c *gin.Context) { | ||
//TODO: add functionality to update email | ||
var ( | ||
err error | ||
userID ulid.ULID | ||
user *api.User | ||
model *models.User | ||
claims *tokens.Claims | ||
) | ||
|
||
if userID, err = ulid.Parse(c.Param("id")); err != nil { | ||
c.Error(err) | ||
c.JSON(http.StatusNotFound, api.ErrorResponse("user id not found")) | ||
return | ||
} | ||
|
||
if err = c.BindJSON((&user)); err != nil { | ||
c.Error(err) | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse("could not parse request")) | ||
return | ||
} | ||
|
||
// Sanity check: the URL endpoint and the user ID on the model match. | ||
if !ulids.IsZero(user.UserID) && user.UserID.Compare(userID) != 0 { | ||
c.Error(api.ErrModelIDMismatch) | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse(api.ErrModelIDMismatch)) | ||
return | ||
} | ||
|
||
// Validate the request from the API side. | ||
if err = user.ValidateUpdate(); err != nil { | ||
c.Error(err) | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse(err)) | ||
return | ||
} | ||
|
||
// Fetch the user claims from the request | ||
if claims, err = middleware.GetClaims(c); err != nil { | ||
c.Error(err) | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse("user claims unavailable")) | ||
return | ||
} | ||
|
||
//retrieve the orgID and userID from the claims and check if they are valid | ||
orgID := claims.ParseOrgID() | ||
requesterID := claims.ParseUserID() | ||
if ulids.IsZero(orgID) || ulids.IsZero(requesterID) { | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse("invalid user claims")) | ||
return | ||
} | ||
|
||
// Create a thin model to update in the database | ||
model = &models.User{ | ||
ID: user.UserID, | ||
Name: user.Name, | ||
} | ||
|
||
// Attempt to update the name in the database | ||
if err = model.Update(c.Request.Context(), orgID); err != nil { | ||
// Check if the error is a not found error or a validation error. | ||
var verr *models.ValidationError | ||
|
||
switch { | ||
case errors.Is(err, models.ErrNotFound): | ||
c.JSON(http.StatusNotFound, api.ErrorResponse("user id not found")) | ||
case errors.As(err, &verr): | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse(verr)) | ||
default: | ||
c.JSON(http.StatusInternalServerError, api.ErrorResponse("an internal error occurred")) | ||
} | ||
|
||
c.Error(err) | ||
return | ||
} | ||
|
||
// Populate the response from the model | ||
c.JSON(http.StatusOK, model.ToAPI(c.Request.Context())) | ||
} |
Oops, something went wrong.