satellite/console: change user's email endpoint/feature
WHAT: change user's email endpoint and appropriate service method was implemented WHY: make it possible to change user's email for temporary filezilla account Change-Id: Ieea41bf49819a42b5f433e8dfaeec24c6d5ddc9f
This commit is contained in:
parent
60bb34a096
commit
f8c3848c78
@ -253,6 +253,29 @@ func (a *Auth) DeleteAccount(w http.ResponseWriter, r *http.Request) {
|
||||
a.serveJSONError(w, errNotImplemented)
|
||||
}
|
||||
|
||||
// ChangeEmail auth user, changes users email for a new one.
|
||||
func (a *Auth) ChangeEmail(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
var emailChange struct {
|
||||
NewEmail string `json:"newEmail"`
|
||||
}
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(&emailChange)
|
||||
if err != nil {
|
||||
a.serveJSONError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = a.service.ChangeEmail(ctx, emailChange.NewEmail)
|
||||
if err != nil {
|
||||
a.serveJSONError(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// ChangePassword auth user, changes users password for a new one.
|
||||
func (a *Auth) ChangePassword(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
@ -173,6 +173,7 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, mail
|
||||
authRouter := router.PathPrefix("/api/v0/auth").Subrouter()
|
||||
authRouter.Handle("/account", server.withAuth(http.HandlerFunc(authController.GetAccount))).Methods(http.MethodGet)
|
||||
authRouter.Handle("/account", server.withAuth(http.HandlerFunc(authController.UpdateAccount))).Methods(http.MethodPatch)
|
||||
authRouter.Handle("/account/change-email", server.withAuth(http.HandlerFunc(authController.ChangeEmail))).Methods(http.MethodPost)
|
||||
authRouter.Handle("/account/change-password", server.withAuth(http.HandlerFunc(authController.ChangePassword))).Methods(http.MethodPost)
|
||||
authRouter.Handle("/account/delete", server.withAuth(http.HandlerFunc(authController.DeleteAccount))).Methods(http.MethodPost)
|
||||
authRouter.HandleFunc("/logout", authController.Logout).Methods(http.MethodPost)
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
@ -867,6 +868,32 @@ func (s *Service) UpdateAccount(ctx context.Context, fullName string, shortName
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChangeEmail updates email for a given user.
|
||||
func (s *Service) ChangeEmail(ctx context.Context, newEmail string) (err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
auth, err := s.getAuthAndAuditLog(ctx, "change email")
|
||||
if err != nil {
|
||||
return Error.Wrap(err)
|
||||
}
|
||||
|
||||
if _, err := mail.ParseAddress(newEmail); err != nil {
|
||||
return ErrValidation.Wrap(err)
|
||||
}
|
||||
|
||||
_, err = s.store.Users().GetByEmail(ctx, newEmail)
|
||||
if err == nil {
|
||||
return ErrEmailUsed.New(emailUsedErrMsg)
|
||||
}
|
||||
|
||||
auth.User.Email = newEmail
|
||||
err = s.store.Users().Update(ctx, &auth.User)
|
||||
if err != nil {
|
||||
return Error.Wrap(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChangePassword updates password for a given user.
|
||||
func (s *Service) ChangePassword(ctx context.Context, pass, newPass string) (err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
@ -130,5 +130,19 @@ func TestService(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
require.Equal(t, "service error: project usage error: some buckets still exist", err.Error())
|
||||
})
|
||||
|
||||
t.Run("TestChangeEmail", func(t *testing.T) {
|
||||
const newEmail = "newEmail@example.com"
|
||||
|
||||
err = service.ChangeEmail(authCtx2, newEmail)
|
||||
require.NoError(t, err)
|
||||
|
||||
userWithUpdatedEmail, err := service.GetUserByEmail(authCtx2, newEmail)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, newEmail, userWithUpdatedEmail.Email)
|
||||
|
||||
err = service.ChangeEmail(authCtx2, newEmail)
|
||||
require.Error(t, err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user