diff --git a/system/rest/settings.go b/system/rest/settings.go index b033ca0e8..0240bf564 100644 --- a/system/rest/settings.go +++ b/system/rest/settings.go @@ -70,6 +70,10 @@ func (ctrl *Settings) Set(ctx context.Context, r *request.SettingsSet) (interfac map[string]string{"key": r.Key, "ownedBy": strconv.FormatUint(r.OwnerID, 10)}, ) + if err != nil { + return nil, err + } + s := &settings.Value{Name: r.Key, OwnedBy: r.OwnerID} if err = s.SetValue(fmt.Sprintf("attachment:%d", att.ID)); err != nil { return nil, err diff --git a/system/service/application.go b/system/service/application.go index 9fb46f826..ae9581e83 100644 --- a/system/service/application.go +++ b/system/service/application.go @@ -78,7 +78,7 @@ func (svc *application) FindByID(ID uint64) (app *types.Application, err error) aaProps = &applicationActionProps{application: &types.Application{ID: ID}} ) - err = svc.db.Transaction(func() error { + err = func() error { if ID == 0 { return ApplicationErrInvalidID() } @@ -94,7 +94,7 @@ func (svc *application) FindByID(ID uint64) (app *types.Application, err error) } return nil - }) + }() return app, svc.recordAction(svc.ctx, aaProps, ApplicationActionLookup, err) } @@ -104,7 +104,7 @@ func (svc *application) Find(filter types.ApplicationFilter) (aa types.Applicati aaProps = &applicationActionProps{filter: &filter} ) - err = svc.db.Transaction(func() error { + err = func() error { filter.IsReadable = svc.ac.FilterReadableApplications(svc.ctx) if filter.Deleted > rh.FilterStateExcluded { @@ -120,7 +120,7 @@ func (svc *application) Find(filter types.ApplicationFilter) (aa types.Applicati aa, f, err = svc.application.Find(filter) return err - }) + }() return aa, f, svc.recordAction(svc.ctx, aaProps, ApplicationActionSearch, err) } @@ -130,7 +130,7 @@ func (svc *application) Create(new *types.Application) (app *types.Application, aaProps = &applicationActionProps{new: new} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if !svc.ac.CanCreateApplication(svc.ctx) { return ApplicationErrNotAllowedToCreate() } @@ -147,7 +147,7 @@ func (svc *application) Create(new *types.Application) (app *types.Application, _ = svc.eventbus.WaitFor(svc.ctx, event.ApplicationAfterCreate(new, nil)) return nil - }) + }() return app, svc.recordAction(svc.ctx, aaProps, ApplicationActionCreate, err) } @@ -157,7 +157,7 @@ func (svc *application) Update(upd *types.Application) (app *types.Application, aaProps = &applicationActionProps{update: upd} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if upd.ID == 0 { return ApplicationErrInvalidID() } @@ -187,7 +187,7 @@ func (svc *application) Update(upd *types.Application) (app *types.Application, _ = svc.eventbus.WaitFor(svc.ctx, event.ApplicationAfterUpdate(upd, app)) return nil - }) + }() return app, svc.recordAction(svc.ctx, aaProps, ApplicationActionUpdate, err) } @@ -198,7 +198,7 @@ func (svc *application) Delete(ID uint64) (err error) { app *types.Application ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if ID == 0 { return ApplicationErrInvalidID() } @@ -221,7 +221,7 @@ func (svc *application) Delete(ID uint64) (err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.ApplicationAfterDelete(nil, app)) return nil - }) + }() return svc.recordAction(svc.ctx, aaProps, ApplicationActionDelete, err) } @@ -232,7 +232,7 @@ func (svc *application) Undelete(ID uint64) (err error) { app *types.Application ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if ID == 0 { return ApplicationErrInvalidID() } @@ -257,7 +257,7 @@ func (svc *application) Undelete(ID uint64) (err error) { // @todo add event // _ = svc.eventbus.WaitFor(svc.ctx, event.ApplicationAfterUndelete(nil, app)) return nil - }) + }() return svc.recordAction(svc.ctx, aaProps, ApplicationActionDelete, err) } diff --git a/system/service/auth.go b/system/service/auth.go index 16d4ddfd0..94f60cf70 100644 --- a/system/service/auth.go +++ b/system/service/auth.go @@ -167,7 +167,7 @@ func (svc auth) External(profile goth.User) (u *types.User, err error) { } ) - err = svc.db.Transaction(func() error { + err = func() error { if !svc.settings.Auth.External.Enabled { return AuthErrExternalDisabledByConfig(aam) } @@ -326,7 +326,7 @@ func (svc auth) External(profile goth.User) (u *types.User, err error) { // Owner loaded, carry on. return nil - }) + }() return u, svc.recordAction(svc.ctx, aam, AuthActionAuthenticate, err) } @@ -494,7 +494,7 @@ func (svc auth) InternalLogin(email string, password string) (u *types.User, err } ) - err = svc.db.Transaction(func() error { + err = func() error { if !svc.settings.Auth.Internal.Enabled { return AuthErrInteralLoginDisabledByConfig() } @@ -558,7 +558,7 @@ func (svc auth) InternalLogin(email string, password string) (u *types.User, err _ = svc.eventbus.WaitFor(svc.ctx, event.AuthAfterLogin(u, authProvider)) return nil - }) + }() return u, svc.recordAction(svc.ctx, aam, AuthActionAuthenticate, err) } @@ -579,7 +579,7 @@ func (svc auth) SetPassword(userID uint64, password string) (err error) { } ) - err = svc.db.Transaction(func() error { + err = func() error { if !svc.settings.Auth.Internal.Enabled { return AuthErrInteralLoginDisabledByConfig(aam) } @@ -598,7 +598,7 @@ func (svc auth) SetPassword(userID uint64, password string) (err error) { } return nil - }) + }() return svc.recordAction(svc.ctx, aam, AuthActionChangePassword, err) } @@ -638,7 +638,7 @@ func (svc auth) ChangePassword(userID uint64, oldPassword, AuthActionPassword st } ) - err = svc.db.Transaction(func() error { + err = func() error { if !svc.settings.Auth.Internal.Enabled { return AuthErrInteralLoginDisabledByConfig(aam) } @@ -670,7 +670,7 @@ func (svc auth) ChangePassword(userID uint64, oldPassword, AuthActionPassword st } return nil - }) + }() return svc.recordAction(svc.ctx, aam, AuthActionChangePassword, err) } @@ -722,14 +722,14 @@ func (svc auth) ValidateAuthRequestToken(token string) (u *types.User, err error } ) - err = svc.db.Transaction(func() error { + err = func() error { u, err = svc.loadUserFromToken(token, credentialsTypeAuthToken) if err != nil && u != nil { aam.setUser(u) svc.ctx = internalAuth.SetIdentityToContext(svc.ctx, u) } return err - }) + }() return u, svc.recordAction(svc.ctx, aam, AuthActionValidateToken, err) } @@ -753,7 +753,7 @@ func (svc auth) loadFromTokenAndConfirmEmail(token, tokenType string) (u *types. } ) - err = svc.db.Transaction(func() error { + err = func() error { if !svc.settings.Auth.Internal.Enabled { return AuthErrInternalSignupDisabledByConfig(aam) } @@ -776,7 +776,7 @@ func (svc auth) loadFromTokenAndConfirmEmail(token, tokenType string) (u *types. } return nil - }) + }() return u, svc.recordAction(svc.ctx, aam, AuthActionConfirmEmail, err) } @@ -790,7 +790,7 @@ func (svc auth) ExchangePasswordResetToken(token string) (u *types.User, t strin } ) - err = svc.db.Transaction(func() error { + err = func() error { if !svc.settings.Auth.Internal.Enabled || !svc.settings.Auth.Internal.PasswordReset.Enabled { return AuthErrPasswordResetDisabledByConfig(aam) } @@ -811,7 +811,7 @@ func (svc auth) ExchangePasswordResetToken(token string) (u *types.User, t strin } return nil - }) + }() return u, t, svc.recordAction(svc.ctx, aam, AuthActionExchangePasswordResetToken, err) } @@ -824,7 +824,7 @@ func (svc auth) SendEmailAddressConfirmationToken(email string) (err error) { } ) - err = svc.db.Transaction(func() error { + err = func() error { if !svc.settings.Auth.Internal.Enabled || !svc.settings.Auth.Internal.PasswordReset.Enabled { return AuthErrPasswordResetDisabledByConfig(aam) } @@ -835,7 +835,7 @@ func (svc auth) SendEmailAddressConfirmationToken(email string) (err error) { } return svc.sendEmailAddressConfirmationToken(u) - }) + }() return svc.recordAction(svc.ctx, aam, AuthActionSendEmailConfirmationToken, err) } diff --git a/system/service/role.go b/system/service/role.go index 95f87d90c..fa15e06b2 100644 --- a/system/service/role.go +++ b/system/service/role.go @@ -101,7 +101,7 @@ func (svc role) Find(filter types.RoleFilter) (rr types.RoleSet, f types.RoleFil raProps = &roleActionProps{filter: &filter} ) - err = svc.db.Transaction(func() error { + err = func() error { filter.IsReadable = svc.ac.FilterReadableRoles(svc.ctx) if filter.Deleted > 0 { @@ -117,7 +117,7 @@ func (svc role) Find(filter types.RoleFilter) (rr types.RoleSet, f types.RoleFil rr, f, err = svc.role.Find(filter) return err - }) + }() return rr, f, svc.recordAction(svc.ctx, raProps, RoleActionSearch, err) } @@ -127,11 +127,11 @@ func (svc role) FindByID(roleID uint64) (r *types.Role, err error) { raProps = &roleActionProps{role: &types.Role{ID: roleID}} ) - err = svc.db.Transaction(func() error { + err = func() error { r, err = svc.findByID(roleID) raProps.setRole(r) return err - }) + }() return r, svc.recordAction(svc.ctx, raProps, RoleActionLookup, err) } @@ -149,11 +149,11 @@ func (svc role) FindByName(name string) (r *types.Role, err error) { raProps = &roleActionProps{role: &types.Role{Name: name}} ) - err = svc.db.Transaction(func() error { + err = func() error { r, err = svc.role.FindByName(name) raProps.setRole(r) return err - }) + }() return r, svc.recordAction(svc.ctx, raProps, RoleActionLookup, err) } @@ -163,11 +163,11 @@ func (svc role) FindByHandle(h string) (r *types.Role, err error) { raProps = &roleActionProps{role: &types.Role{Handle: h}} ) - err = svc.db.Transaction(func() error { + err = func() error { r, err = svc.role.FindByName(h) raProps.setRole(r) return err - }) + }() return r, svc.recordAction(svc.ctx, raProps, RoleActionLookup, err) } @@ -197,7 +197,7 @@ func (svc role) Create(new *types.Role) (r *types.Role, err error) { raProps = &roleActionProps{new: new} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if !handle.IsValid(new.Handle) { return RoleErrInvalidHandle() } @@ -222,7 +222,7 @@ func (svc role) Create(new *types.Role) (r *types.Role, err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.RoleAfterCreate(new, r)) return - }) + }() return r, svc.recordAction(svc.ctx, raProps, RoleActionCreate, err) @@ -233,7 +233,7 @@ func (svc role) Update(upd *types.Role) (r *types.Role, err error) { raProps = &roleActionProps{update: upd} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if upd.ID == 0 { return RoleErrInvalidID() } @@ -271,7 +271,7 @@ func (svc role) Update(upd *types.Role) (r *types.Role, err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.RoleAfterUpdate(upd, r)) return nil - }) + }() return r, svc.recordAction(svc.ctx, raProps, RoleActionUpdate, err) } @@ -304,7 +304,7 @@ func (svc role) Delete(roleID uint64) (err error) { raProps = &roleActionProps{role: &types.Role{ID: roleID}} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if r, err = svc.findByID(roleID); err != nil { return err } @@ -326,7 +326,7 @@ func (svc role) Delete(roleID uint64) (err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.RoleAfterDelete(nil, r)) return - }) + }() return svc.recordAction(svc.ctx, raProps, RoleActionDelete, err) } @@ -337,7 +337,7 @@ func (svc role) Undelete(roleID uint64) (err error) { raProps = &roleActionProps{role: &types.Role{ID: roleID}} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if r, err = svc.findByID(roleID); err != nil { return err } @@ -353,7 +353,7 @@ func (svc role) Undelete(roleID uint64) (err error) { } return nil - }) + }() return svc.recordAction(svc.ctx, raProps, RoleActionUndelete, err) } @@ -364,7 +364,7 @@ func (svc role) Archive(roleID uint64) (err error) { raProps = &roleActionProps{role: &types.Role{ID: roleID}} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if r, err = svc.findByID(roleID); err != nil { return err } @@ -380,7 +380,7 @@ func (svc role) Archive(roleID uint64) (err error) { } return - }) + }() return svc.recordAction(svc.ctx, raProps, RoleActionArchive, err) } @@ -391,7 +391,7 @@ func (svc role) Unarchive(roleID uint64) (err error) { raProps = &roleActionProps{role: &types.Role{ID: roleID}} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if r, err = svc.findByID(roleID); err != nil { return err } @@ -407,7 +407,7 @@ func (svc role) Unarchive(roleID uint64) (err error) { } return nil - }) + }() return svc.recordAction(svc.ctx, raProps, RoleActionUnarchive, err) } @@ -423,7 +423,7 @@ func (svc role) Merge(roleID, targetRoleID uint64) (err error) { } ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if roleID == 0 || targetRoleID == 0 { return RoleErrInvalidID() } @@ -453,7 +453,7 @@ func (svc role) Merge(roleID, targetRoleID uint64) (err error) { } return nil - }) + }() return svc.recordAction(svc.ctx, raProps, RoleActionMerge, err) } @@ -478,7 +478,7 @@ func (svc role) MemberList(roleID uint64) (mm []*types.RoleMember, err error) { } ) - err = svc.db.Transaction(func() error { + err = func() error { if roleID == permissions.EveryoneRoleID || roleID == 0 { return RoleErrInvalidID() } @@ -496,7 +496,7 @@ func (svc role) MemberList(roleID uint64) (mm []*types.RoleMember, err error) { } return nil - }) + }() return mm, svc.recordAction(svc.ctx, raProps, RoleActionMembers, err) } @@ -513,7 +513,7 @@ func (svc role) MemberAdd(roleID, memberID uint64) (err error) { } ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if roleID == permissions.EveryoneRoleID || roleID == 0 || memberID == 0 { return RoleErrInvalidID() } @@ -544,7 +544,7 @@ func (svc role) MemberAdd(roleID, memberID uint64) (err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.RoleMemberAfterAdd(m, r)) return nil - }) + }() return svc.recordAction(svc.ctx, raProps, RoleActionMemberAdd, err) } @@ -560,7 +560,7 @@ func (svc role) MemberRemove(roleID, memberID uint64) (err error) { } ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if roleID == permissions.EveryoneRoleID || roleID == 0 || memberID == 0 { return RoleErrInvalidID() } @@ -591,7 +591,7 @@ func (svc role) MemberRemove(roleID, memberID uint64) (err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.RoleMemberAfterRemove(m, r)) return nil - }) + }() return svc.recordAction(svc.ctx, raProps, RoleActionMemberRemove, err) } diff --git a/system/service/user.go b/system/service/user.go index f3f9ec5ff..2d83421ae 100644 --- a/system/service/user.go +++ b/system/service/user.go @@ -140,7 +140,7 @@ func (svc user) FindByID(userID uint64) (u *types.User, err error) { uaProps = &userActionProps{user: &types.User{ID: userID}} ) - err = svc.db.Transaction(func() error { + err = func() error { if userID == 0 { return UserErrInvalidID() } @@ -157,7 +157,7 @@ func (svc user) FindByID(userID uint64) (u *types.User, err error) { u, err = svc.proc(svc.user.FindByID(userID)) return err - }) + }() return u, svc.recordAction(svc.ctx, uaProps, UserActionLookup, err) } @@ -167,10 +167,10 @@ func (svc user) FindByEmail(email string) (u *types.User, err error) { uaProps = &userActionProps{user: &types.User{Email: email}} ) - err = svc.db.Transaction(func() error { + err = func() error { u, err = svc.proc(svc.user.FindByEmail(email)) return err - }) + }() return u, svc.recordAction(svc.ctx, uaProps, UserActionLookup, err) } @@ -180,10 +180,10 @@ func (svc user) FindByUsername(username string) (u *types.User, err error) { uaProps = &userActionProps{user: &types.User{Username: username}} ) - err = svc.db.Transaction(func() error { + err = func() error { u, err = svc.proc(svc.user.FindByUsername(username)) return err - }) + }() return u, svc.recordAction(svc.ctx, uaProps, UserActionLookup, err) } @@ -193,10 +193,10 @@ func (svc user) FindByHandle(handle string) (u *types.User, err error) { uaProps = &userActionProps{user: &types.User{Handle: handle}} ) - err = svc.db.Transaction(func() error { + err = func() error { u, err = svc.proc(svc.user.FindByHandle(handle)) return err - }) + }() return u, svc.recordAction(svc.ctx, uaProps, UserActionLookup, err) } @@ -261,7 +261,7 @@ func (svc user) Find(filter types.UserFilter) (uu types.UserSet, f types.UserFil uaProps = &userActionProps{filter: &filter} ) - err = svc.db.Transaction(func() error { + err = func() error { if filter.Deleted > 0 { // If list with deleted users is requested // user must have access permissions to system (ie: is admin) @@ -290,7 +290,7 @@ func (svc user) Find(filter types.UserFilter) (uu types.UserSet, f types.UserFil svc.handlePrivateData(u) return nil }) - }) + }() return uu, f, svc.recordAction(svc.ctx, uaProps, UserActionSearch, err) } @@ -300,7 +300,7 @@ func (svc user) Create(new *types.User) (u *types.User, err error) { uaProps = &userActionProps{new: new} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if !svc.ac.CanCreateUser(svc.ctx) { return UserErrNotAllowedToCreate() } @@ -341,7 +341,7 @@ func (svc user) Create(new *types.User) (u *types.User, err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.UserAfterCreate(new, u)) return - }) + }() return u, svc.recordAction(svc.ctx, uaProps, UserActionCreate, err) } @@ -356,7 +356,7 @@ func (svc user) Update(upd *types.User) (u *types.User, err error) { uaProps = &userActionProps{update: upd} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if upd.ID == 0 { return UserErrInvalidID() } @@ -402,7 +402,7 @@ func (svc user) Update(upd *types.User) (u *types.User, err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.UserAfterUpdate(upd, u)) return - }) + }() return u, svc.recordAction(svc.ctx, uaProps, UserActionUpdate, err) } @@ -475,7 +475,7 @@ func (svc user) Delete(userID uint64) (err error) { uaProps = &userActionProps{user: &types.User{ID: userID}} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if userID == 0 { return UserErrInvalidID() } @@ -498,7 +498,7 @@ func (svc user) Delete(userID uint64) (err error) { _ = svc.eventbus.WaitFor(svc.ctx, event.UserAfterDelete(nil, u)) return nil - }) + }() return svc.recordAction(svc.ctx, uaProps, UserActionDelete, err) } @@ -509,7 +509,7 @@ func (svc user) Undelete(userID uint64) (err error) { uaProps = &userActionProps{user: &types.User{ID: userID}} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if userID == 0 { return UserErrInvalidID() } @@ -533,7 +533,7 @@ func (svc user) Undelete(userID uint64) (err error) { } return nil - }) + }() return svc.recordAction(svc.ctx, uaProps, UserActionUndelete, err) @@ -545,7 +545,7 @@ func (svc user) Suspend(userID uint64) (err error) { uaProps = &userActionProps{user: &types.User{ID: userID}} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if userID == 0 { return UserErrInvalidID() } @@ -565,7 +565,7 @@ func (svc user) Suspend(userID uint64) (err error) { } return nil - }) + }() return svc.recordAction(svc.ctx, uaProps, UserActionSuspend, err) @@ -577,7 +577,7 @@ func (svc user) Unsuspend(userID uint64) (err error) { uaProps = &userActionProps{user: &types.User{ID: userID}} ) - err = svc.db.Transaction(func() (err error) { + err = func() (err error) { if userID == 0 { return UserErrInvalidID() } @@ -597,7 +597,7 @@ func (svc user) Unsuspend(userID uint64) (err error) { } return nil - }) + }() return svc.recordAction(svc.ctx, uaProps, UserActionUnsuspend, err) @@ -612,7 +612,7 @@ func (svc user) SetPassword(userID uint64, newPassword string) (err error) { uaProps = &userActionProps{user: &types.User{ID: userID}} ) - err = svc.db.Transaction(func() error { + err = func() error { if u, err = svc.user.FindByID(userID); err != nil { return err } @@ -632,7 +632,7 @@ func (svc user) SetPassword(userID uint64, newPassword string) (err error) { } return nil - }) + }() return svc.recordAction(svc.ctx, uaProps, UserActionSetPassword, err)