Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[server] Add admin API to change email address

vishnukvmd77276d8d6cac
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index 9403efaa5a..14ceb3e3bd 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -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)
diff --git a/server/ente/admin.go b/server/ente/admin.go
index 66ad277411..c56e48e5bd 100644
--- a/server/ente/admin.go
+++ b/server/ente/admin.go
@@ -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 (
diff --git a/server/pkg/api/admin.go b/server/pkg/api/admin.go
index 90c9fd3e63..b153e19bb1 100644
--- a/server/pkg/api/admin.go
+++ b/server/pkg/api/admin.go
@@ -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 {
diff --git a/server/pkg/controller/user/userauth.go b/server/pkg/controller/user/userauth.go
index bf092cb147..bbc9942de2 100644
--- a/server/pkg/controller/user/userauth.go
+++ b/server/pkg/controller/user/userauth.go
@@ -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, "")