1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-24 15:49:44 +02:00

Fix order of DB transaction commits

Some DB commit commands were out of sequence and so have been fixed to be consist across the board. Specially, audit log entries have their own DB TX and so should be  executed outside of any other commit cycle.
This commit is contained in:
sauls8t 2018-02-04 15:43:57 +00:00
parent 880f39f1cb
commit 0997655e0a
16 changed files with 100 additions and 75 deletions

View file

@ -170,6 +170,8 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
}
}
ctx.Transaction.Commit()
if addUser {
event.Handler().Publish(string(event.TypeAddUser))
h.Store.Audit.Record(ctx, audit.EventTypeUserAdd)
@ -180,8 +182,6 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
h.Store.Audit.Record(ctx, audit.EventTypeAccountAdd)
}
ctx.Transaction.Commit()
// If we did not add user or give them access (account) then we error back
if !addUser && !addAccount {
response.WriteDuplicateError(w, method, "user")
@ -382,12 +382,12 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
return
}
ctx.Transaction.Commit()
h.Store.Audit.Record(ctx, audit.EventTypeUserDelete)
event.Handler().Publish(string(event.TypeRemoveUser))
ctx.Transaction.Commit()
response.WriteEmpty(w)
}
@ -474,10 +474,10 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
return
}
h.Store.Audit.Record(ctx, audit.EventTypeUserUpdate)
ctx.Transaction.Commit()
h.Store.Audit.Record(ctx, audit.EventTypeUserUpdate)
response.WriteEmpty(w)
}
@ -515,6 +515,7 @@ func (h *Handler) ChangePassword(w http.ResponseWriter, r *http.Request) {
u, err := h.Store.User.Get(ctx, userID)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
@ -577,8 +578,9 @@ func (h *Handler) ForgotPassword(w http.ResponseWriter, r *http.Request) {
}
if err == sql.ErrNoRows {
response.WriteEmpty(w)
ctx.Transaction.Rollback()
h.Runtime.Log.Info(fmt.Sprintf("User %s not found for password reset process", u.Email))
response.WriteEmpty(w)
return
}
@ -621,6 +623,7 @@ func (h *Handler) ResetPassword(w http.ResponseWriter, r *http.Request) {
u, err := h.Store.User.GetByToken(ctx, token)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
@ -636,9 +639,9 @@ func (h *Handler) ResetPassword(w http.ResponseWriter, r *http.Request) {
return
}
h.Store.Audit.Record(ctx, audit.EventTypeUserPasswordReset)
ctx.Transaction.Commit()
h.Store.Audit.Record(ctx, audit.EventTypeUserPasswordReset)
response.WriteEmpty(w)
}