[server] Add admin API to change email address

This commit is contained in:
vishnukvmd 2024-03-13 17:31:38 +05:30
parent ccb6a4a283
commit 77276d8d6c
4 changed files with 32 additions and 2 deletions

View file

@ -620,6 +620,7 @@ func main() {
adminAPI.POST("/user/disable-2fa", adminHandler.DisableTwoFactor)
adminAPI.POST("/user/disable-passkeys", adminHandler.RemovePasskeys)
adminAPI.POST("/user/close-family", adminHandler.CloseFamily)
adminAPI.PUT("/user/change-email", adminHandler.ChangeEmail)
adminAPI.DELETE("/user/delete", adminHandler.DeleteUser)
adminAPI.POST("/user/recover", adminHandler.RecoverAccount)
adminAPI.GET("/email-hash", adminHandler.GetEmailHash)

View file

@ -46,6 +46,11 @@ type UpdateSubscriptionRequest struct {
Attributes SubscriptionAttributes `json:"attributes"`
}
type ChangeEmailRequest struct {
UserID int64 `json:"userID" binding:"required"`
Email string `json:"email" binding:"required"`
}
type AddOnAction string
const (

View file

@ -306,6 +306,25 @@ func (h *AdminHandler) UpdateSubscription(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{})
}
func (h *AdminHandler) ChangeEmail(c *gin.Context) {
var r ente.ChangeEmailRequest
if err := c.ShouldBindJSON(&r); err != nil {
handler.Error(c, stacktrace.Propagate(ente.ErrBadRequest, "Bad request"))
return
}
adminID := auth.GetUserID(c.Request.Header)
go h.DiscordController.NotifyAdminAction(
fmt.Sprintf("Admin (%d) updating email for user: %d", adminID, r.UserID))
err := h.UserController.UpdateEmail(c, r.UserID, r.Email)
if err != nil {
logrus.WithError(err).Error("Failed to update email")
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
logrus.Info("Updated email")
c.JSON(http.StatusOK, gin.H{})
}
func (h *AdminHandler) ReQueueItem(c *gin.Context) {
var r ente.ReQueueItemRequest
if err := c.ShouldBindJSON(&r); err != nil {

View file

@ -197,7 +197,13 @@ func (c *UserController) ChangeEmail(ctx *gin.Context, request ente.EmailVerific
if err != nil {
return stacktrace.Propagate(err, "")
}
_, err = c.UserRepo.GetUserIDWithEmail(email)
return c.UpdateEmail(ctx, auth.GetUserID(ctx.Request.Header), email)
}
// UpdateEmail updates the email address of the user with the provided userID
func (c *UserController) UpdateEmail(ctx *gin.Context, userID int64, email string) error {
_, err := c.UserRepo.GetUserIDWithEmail(email)
if err == nil {
// email already owned by a user
return stacktrace.Propagate(ente.ErrPermissionDenied, "")
@ -206,7 +212,6 @@ func (c *UserController) ChangeEmail(ctx *gin.Context, request ente.EmailVerific
// unknown error, rethrow
return stacktrace.Propagate(err, "")
}
userID := auth.GetUserID(ctx.Request.Header)
user, err := c.UserRepo.Get(userID)
if err != nil {
return stacktrace.Propagate(err, "")