[MM-55737] Add Request Context to UserStore.Save (#26109)

Этот коммит содержится в:
Trang
2024-02-16 23:12:49 +07:00
коммит произвёл GitHub
родитель 9643a16408
Коммит f130615011
44 изменённых файлов: 553 добавлений и 552 удалений

Просмотреть файл

@@ -475,7 +475,7 @@ type AppIface interface {
CheckIntegrity() <-chan model.IntegrityCheckResult
CheckMandatoryS3Fields(settings *model.FileSettings) *model.AppError
CheckPasswordAndAllCriteria(rctx request.CTX, user *model.User, password string, mfaToken string) *model.AppError
CheckPostReminders()
CheckPostReminders(rctx request.CTX)
CheckRolesExist(roleNames []string) *model.AppError
CheckUserAllAuthenticationCriteria(rctx request.CTX, user *model.User, mfaToken string) *model.AppError
CheckUserMfa(rctx request.CTX, user *model.User, token string) *model.AppError
@@ -811,7 +811,7 @@ type AppIface interface {
GetSiteURL() string
GetStatus(userID string) (*model.Status, *model.AppError)
GetStatusFromCache(userID string) *model.Status
GetSystemBot() (*model.Bot, *model.AppError)
GetSystemBot(rctx request.CTX) (*model.Bot, *model.AppError)
GetTeam(teamID string) (*model.Team, *model.AppError)
GetTeamByInviteId(inviteId string) (*model.Team, *model.AppError)
GetTeamByName(name string) (*model.Team, *model.AppError)
@@ -883,7 +883,7 @@ type AppIface interface {
GetUsersWithoutTeamPage(options *model.UserGetOptions, asAdmin bool) ([]*model.User, *model.AppError)
GetVerifyEmailToken(token string) (*model.Token, *model.AppError)
GetViewUsersRestrictions(c request.CTX, userID string) (*model.ViewUsersRestrictions, *model.AppError)
GetWarnMetricsBot() (*model.Bot, *model.AppError)
GetWarnMetricsBot(rctx request.CTX) (*model.Bot, *model.AppError)
GetWarnMetricsStatus(rctx request.CTX) (map[string]*model.WarnMetricStatus, *model.AppError)
HTTPService() httpservice.HTTPService
HandleCommandResponse(c request.CTX, command *model.Command, args *model.CommandArgs, response *model.CommandResponse, builtIn bool) (*model.CommandResponse, *model.AppError)

Просмотреть файл

@@ -95,7 +95,7 @@ func (a *App) CreateBot(c request.CTX, bot *model.Bot) (*model.Bot, *model.AppEr
return nil, vErr
}
user, nErr := a.Srv().Store().User().Save(model.UserFromBot(bot))
user, nErr := a.Srv().Store().User().Save(c, model.UserFromBot(bot))
if nErr != nil {
var appErr *model.AppError
var invErr *store.ErrInvalidInput
@@ -160,7 +160,7 @@ func (a *App) CreateBot(c request.CTX, bot *model.Bot) (*model.Bot, *model.AppEr
return savedBot, nil
}
func (a *App) GetWarnMetricsBot() (*model.Bot, *model.AppError) {
func (a *App) GetWarnMetricsBot(rctx request.CTX) (*model.Bot, *model.AppError) {
perPage := 1
userOptions := &model.UserGetOptions{
Page: 0,
@@ -186,10 +186,10 @@ func (a *App) GetWarnMetricsBot() (*model.Bot, *model.AppError) {
OwnerId: sysAdminList[0].Id,
}
return a.getOrCreateBot(warnMetricsBot)
return a.getOrCreateBot(rctx, warnMetricsBot)
}
func (a *App) GetSystemBot() (*model.Bot, *model.AppError) {
func (a *App) GetSystemBot(rctx request.CTX) (*model.Bot, *model.AppError) {
perPage := 1
userOptions := &model.UserGetOptions{
Page: 0,
@@ -215,10 +215,10 @@ func (a *App) GetSystemBot() (*model.Bot, *model.AppError) {
OwnerId: sysAdminList[0].Id,
}
return a.getOrCreateBot(systemBot)
return a.getOrCreateBot(rctx, systemBot)
}
func (a *App) getOrCreateBot(botDef *model.Bot) (*model.Bot, *model.AppError) {
func (a *App) getOrCreateBot(rctx request.CTX, botDef *model.Bot) (*model.Bot, *model.AppError) {
botUser, appErr := a.GetUserByUsername(botDef.Username)
if appErr != nil {
if appErr.StatusCode != http.StatusNotFound {
@@ -226,7 +226,7 @@ func (a *App) getOrCreateBot(botDef *model.Bot) (*model.Bot, *model.AppError) {
}
// cannot find this bot user, save the user
user, nErr := a.Srv().Store().User().Save(model.UserFromBot(botDef))
user, nErr := a.Srv().Store().User().Save(rctx, model.UserFromBot(botDef))
if nErr != nil {
var appError *model.AppError
var invErr *store.ErrInvalidInput

Просмотреть файл

@@ -768,7 +768,7 @@ func TestGetSystemBot(t *testing.T) {
require.Nil(t, th.App.PermanentDeleteAllUsers(th.Context))
_, err := th.App.GetSystemBot()
_, err := th.App.GetSystemBot(th.Context)
require.NotNil(t, err)
require.Equal(t, "app.bot.get_system_bot.empty_admin_list.app_error", err.Id)
})
@@ -781,7 +781,7 @@ func TestGetSystemBot(t *testing.T) {
_, err := th.App.GetUserByUsername(model.BotSystemBotUsername)
require.NotNil(t, err)
bot, err := th.App.GetSystemBot()
bot, err := th.App.GetSystemBot(th.Context)
require.Nil(t, err)
require.Equal(t, bot.Username, model.BotSystemBotUsername)
})
@@ -792,7 +792,7 @@ func TestGetSystemBot(t *testing.T) {
require.Nil(t, err)
require.True(t, botUser.IsBot)
bot, err := th.App.GetSystemBot()
bot, err := th.App.GetSystemBot(th.Context)
require.Nil(t, err)
require.Equal(t, bot.Username, model.BotSystemBotUsername)
require.Equal(t, bot.UserId, botUser.Id)

Просмотреть файл

@@ -723,7 +723,7 @@ func (a *App) postChannelPrivacyMessage(c request.CTX, user *model.User, channel
authorId = user.Id
authorUsername = user.Username
} else {
systemBot, err := a.GetSystemBot()
systemBot, err := a.GetSystemBot(c)
if err != nil {
return model.NewAppError("postChannelPrivacyMessage", "api.channel.post_channel_privacy_message.error", nil, "", http.StatusInternalServerError).Wrap(err)
}
@@ -801,7 +801,7 @@ func (a *App) RestoreChannel(c request.CTX, channel *model.Channel, userID strin
}
} else {
a.Srv().Go(func() {
systemBot, err := a.GetSystemBot()
systemBot, err := a.GetSystemBot(c)
if err != nil {
c.Logger().Error("Failed to post unarchive message", mlog.Err(err))
return
@@ -1447,7 +1447,7 @@ func (a *App) DeleteChannel(c request.CTX, channel *model.Channel, userID string
c.Logger().Warn("Failed to post archive message", mlog.Err(err))
}
} else {
systemBot, err := a.GetSystemBot()
systemBot, err := a.GetSystemBot(c)
if err != nil {
c.Logger().Warn("Failed to post archive message", mlog.Err(err))
} else {
@@ -2446,7 +2446,7 @@ func (a *App) postAddToTeamMessage(c request.CTX, user *model.User, addedUser *m
func (a *App) postRemoveFromChannelMessage(c request.CTX, removerUserId string, removedUser *model.User, channel *model.Channel) *model.AppError {
messageUserId := removerUserId
if messageUserId == "" {
systemBot, err := a.GetSystemBot()
systemBot, err := a.GetSystemBot(c)
if err != nil {
return model.NewAppError("postRemoveFromChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, "", http.StatusInternalServerError).Wrap(err)
}

Просмотреть файл

@@ -252,11 +252,11 @@ func (th *TestHelper) CreateUserOrGuest(guest bool) *model.User {
var err error
if guest {
if user, err = th.service.userService.CreateUser(user, users.UserCreateOptions{Guest: true}); err != nil {
if user, err = th.service.userService.CreateUser(th.Context, user, users.UserCreateOptions{Guest: true}); err != nil {
panic(err)
}
} else {
if user, err = th.service.userService.CreateUser(user, users.UserCreateOptions{}); err != nil {
if user, err = th.service.userService.CreateUser(th.Context, user, users.UserCreateOptions{}); err != nil {
panic(err)
}
}

Просмотреть файл

@@ -540,7 +540,7 @@ func (a *App) importUser(rctx request.CTX, data *imports.UserImportData, dryRun
var savedUser *model.User
var err error
if user.Id == "" {
if savedUser, err = a.ch.srv.userService.CreateUser(user, users.UserCreateOptions{FromImport: true}); err != nil {
if savedUser, err = a.ch.srv.userService.CreateUser(rctx, user, users.UserCreateOptions{FromImport: true}); err != nil {
var appErr *model.AppError
var invErr *store.ErrInvalidInput
switch {

Просмотреть файл

@@ -98,7 +98,7 @@ func (a *App) SendNotifyAdminPosts(c request.CTX, workspaceName string, currentS
return appErr
}
systemBot, appErr := a.GetSystemBot()
systemBot, appErr := a.GetSystemBot(c)
if appErr != nil {
return appErr
}

Просмотреть файл

@@ -61,7 +61,7 @@ func Test_SendNotifyAdminPosts(t *testing.T) {
appErr = th.App.SendNotifyAdminPosts(th.Context, "test", "", false)
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot()
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
// message sending is async, wait time for it
@@ -108,7 +108,7 @@ func Test_SendNotifyAdminPosts(t *testing.T) {
appErr = th.App.SendNotifyAdminPosts(th.Context, "test", "", true)
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot()
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
// message sending is async, wait time for it
@@ -153,7 +153,7 @@ func Test_SendNotifyAdminPosts(t *testing.T) {
appErr = th.App.SendNotifyAdminPosts(th.Context, "", "", false)
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot()
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
var channel *model.Channel
@@ -197,7 +197,7 @@ func Test_SendNotifyAdminPosts(t *testing.T) {
appErr = th.App.SendNotifyAdminPosts(th.Context, "", "", false)
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot()
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
var channel *model.Channel
@@ -342,7 +342,7 @@ func Test_SendNotifyAdminPosts(t *testing.T) {
appErr = th.App.SendNotifyAdminPosts(th.Context, "test", model.LicenseShortSkuProfessional, false) // try and send notification but workspace currentSKU has since changed to cloud-professional
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot()
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
// message sending is async, wait time for it
@@ -402,7 +402,7 @@ func Test_SendNotifyAdminPosts(t *testing.T) {
appErr = th.App.SendNotifyAdminPosts(th.Context, "test", "", false)
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot()
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
var channel *model.Channel

Просмотреть файл

@@ -1279,7 +1279,7 @@ func (a *OpenTracingAppLayer) CheckPasswordAndAllCriteria(rctx request.CTX, user
return resultVar0
}
func (a *OpenTracingAppLayer) CheckPostReminders() {
func (a *OpenTracingAppLayer) CheckPostReminders(rctx request.CTX) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.CheckPostReminders")
@@ -1291,7 +1291,7 @@ func (a *OpenTracingAppLayer) CheckPostReminders() {
}()
defer span.Finish()
a.app.CheckPostReminders()
a.app.CheckPostReminders(rctx)
}
func (a *OpenTracingAppLayer) CheckProviderAttributes(c request.CTX, user *model.User, patch *model.UserPatch) string {
@@ -9734,7 +9734,7 @@ func (a *OpenTracingAppLayer) GetSuggestions(c request.CTX, commandArgs *model.C
return resultVar0
}
func (a *OpenTracingAppLayer) GetSystemBot() (*model.Bot, *model.AppError) {
func (a *OpenTracingAppLayer) GetSystemBot(rctx request.CTX) (*model.Bot, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetSystemBot")
@@ -9746,7 +9746,7 @@ func (a *OpenTracingAppLayer) GetSystemBot() (*model.Bot, *model.AppError) {
}()
defer span.Finish()
resultVar0, resultVar1 := a.app.GetSystemBot()
resultVar0, resultVar1 := a.app.GetSystemBot(rctx)
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))
@@ -11391,7 +11391,7 @@ func (a *OpenTracingAppLayer) GetViewUsersRestrictions(c request.CTX, userID str
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetWarnMetricsBot() (*model.Bot, *model.AppError) {
func (a *OpenTracingAppLayer) GetWarnMetricsBot(rctx request.CTX) (*model.Bot, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetWarnMetricsBot")
@@ -11403,7 +11403,7 @@ func (a *OpenTracingAppLayer) GetWarnMetricsBot() (*model.Bot, *model.AppError)
}()
defer span.Finish()
resultVar0, resultVar1 := a.app.GetWarnMetricsBot()
resultVar0, resultVar1 := a.app.GetWarnMetricsBot(rctx)
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))

Просмотреть файл

@@ -231,7 +231,7 @@ func (th *TestHelper) CreateUserOrGuest(guest bool) *model.User {
}
var err error
user, err = th.Service.Store.User().Save(user)
user, err = th.Service.Store.User().Save(th.Context, user)
if err != nil {
panic(err)
}
@@ -252,7 +252,7 @@ func (th *TestHelper) CreateAdmin() *model.User {
}
var err error
user, err = th.Service.Store.User().Save(user)
user, err = th.Service.Store.User().Save(th.Context, user)
if err != nil {
panic(err)
}

Просмотреть файл

@@ -2157,8 +2157,8 @@ func (a *App) SetPostReminder(postID, userID string, targetTime int64) *model.Ap
return nil
}
func (a *App) CheckPostReminders() {
systemBot, appErr := a.GetSystemBot()
func (a *App) CheckPostReminders(rctx request.CTX) {
systemBot, appErr := a.GetSystemBot(rctx)
if appErr != nil {
mlog.Error("Failed to get system bot", mlog.Err(appErr))
return

Просмотреть файл

@@ -95,7 +95,7 @@ func (a *App) SendReportToUser(rctx request.CTX, job *model.Job, format string)
return model.NewAppError("SendReportToUser", "app.report.send_report_to_user.missing_date_range", nil, "", http.StatusInternalServerError)
}
systemBot, err := a.GetSystemBot()
systemBot, err := a.GetSystemBot(rctx)
if err != nil {
return err
}
@@ -238,7 +238,7 @@ func (a *App) StartUsersBatchExport(rctx request.CTX, dateRange string, startAt
}
a.Srv().Go(func() {
systemBot, err := a.GetSystemBot()
systemBot, err := a.GetSystemBot(rctx)
if err != nil {
rctx.Logger().Error("Failed to get the system bot", mlog.Err(err))
return

Просмотреть файл

@@ -1786,15 +1786,19 @@ func runDNDStatusExpireJob(a *App) {
func runPostReminderJob(a *App) {
if a.IsLeader() {
rctx := request.EmptyContext(a.Log())
withMut(&a.ch.postReminderMut, func() {
a.ch.postReminderTask = model.CreateRecurringTaskFromNextIntervalTime("Check Post reminders", a.CheckPostReminders, 5*time.Minute)
fn := func() { a.CheckPostReminders(rctx) }
a.ch.postReminderTask = model.CreateRecurringTaskFromNextIntervalTime("Check Post reminders", fn, 5*time.Minute)
})
}
a.ch.srv.AddClusterLeaderChangedListener(func() {
mlog.Info("Cluster leader changed. Determining if post reminder task should be running", mlog.Bool("isLeader", a.IsLeader()))
if a.IsLeader() {
rctx := request.EmptyContext(a.Log())
withMut(&a.ch.postReminderMut, func() {
a.ch.postReminderTask = model.CreateRecurringTaskFromNextIntervalTime("Check Post reminders", a.CheckPostReminders, 5*time.Minute)
fn := func() { a.CheckPostReminders(rctx) }
a.ch.postReminderTask = model.CreateRecurringTaskFromNextIntervalTime("Check Post reminders", fn, 5*time.Minute)
})
} else {
cancelTask(&a.ch.postReminderMut, &a.ch.postReminderTask)

Просмотреть файл

@@ -113,7 +113,7 @@ func (th *TestHelper) UpdateConfig(f func(*model.Config)) {
func (th *TestHelper) CreateUser(u *model.User) *model.User {
u.EmailVerified = true
user, err := th.dbStore.User().Save(u)
user, err := th.dbStore.User().Save(th.Context, u)
if err != nil {
panic(err)
}

Просмотреть файл

@@ -235,7 +235,7 @@ func (a *App) createUserOrGuest(c request.CTX, user *model.User, guest bool) (*m
return nil, err
}
ruser, nErr := a.ch.srv.userService.CreateUser(user, users.UserCreateOptions{Guest: guest})
ruser, nErr := a.ch.srv.userService.CreateUser(c, user, users.UserCreateOptions{Guest: guest})
if nErr != nil {
var appErr *model.AppError
var invErr *store.ErrInvalidInput

Просмотреть файл

@@ -499,13 +499,13 @@ func TestCreateUserConflict(t *testing.T) {
Email: "test@localhost",
Username: model.NewId(),
}
user, err := th.App.Srv().Store().User().Save(user)
user, err := th.App.Srv().Store().User().Save(th.Context, user)
require.NoError(t, err)
username := user.Username
var invErr *store.ErrInvalidInput
// Same id
_, err = th.App.Srv().Store().User().Save(user)
_, err = th.App.Srv().Store().User().Save(th.Context, user)
require.Error(t, err)
require.True(t, errors.As(err, &invErr))
assert.Equal(t, "id", invErr.Field)
@@ -515,7 +515,7 @@ func TestCreateUserConflict(t *testing.T) {
Email: "test@localhost",
Username: model.NewId(),
}
_, err = th.App.Srv().Store().User().Save(user)
_, err = th.App.Srv().Store().User().Save(th.Context, user)
require.Error(t, err)
require.True(t, errors.As(err, &invErr))
assert.Equal(t, "email", invErr.Field)
@@ -525,7 +525,7 @@ func TestCreateUserConflict(t *testing.T) {
Email: "test2@localhost",
Username: username,
}
_, err = th.App.Srv().Store().User().Save(user)
_, err = th.App.Srv().Store().User().Save(th.Context, user)
require.Error(t, err)
require.True(t, errors.As(err, &invErr))
assert.Equal(t, "username", invErr.Field)
@@ -568,7 +568,7 @@ func TestUpdateUserEmail(t *testing.T) {
Username: model.NewId(),
IsBot: true,
}
_, nErr := th.App.Srv().Store().User().Save(&botuser)
_, nErr := th.App.Srv().Store().User().Save(th.Context, &botuser)
assert.NoError(t, nErr)
newBotEmail := th.MakeEmail()
@@ -611,7 +611,7 @@ func TestUpdateUserEmail(t *testing.T) {
Username: model.NewId(),
IsBot: true,
}
_, nErr := th.App.Srv().Store().User().Save(&botuser)
_, nErr := th.App.Srv().Store().User().Save(th.Context, &botuser)
assert.NoError(t, nErr)
newBotEmail := th.MakeEmail()

Просмотреть файл

@@ -124,11 +124,11 @@ func (th *TestHelper) CreateUserOrGuest(guest bool) *model.User {
var err error
if guest {
if user, err = th.service.CreateUser(user, UserCreateOptions{Guest: true}); err != nil {
if user, err = th.service.CreateUser(th.Context, user, UserCreateOptions{Guest: true}); err != nil {
panic(err)
}
} else {
if user, err = th.service.CreateUser(user, UserCreateOptions{}); err != nil {
if user, err = th.service.CreateUser(th.Context, user, UserCreateOptions{}); err != nil {
panic(err)
}
}

Просмотреть файл

@@ -24,9 +24,9 @@ type UserCreateOptions struct {
}
// CreateUser creates a user
func (us *UserService) CreateUser(user *model.User, opts UserCreateOptions) (*model.User, error) {
func (us *UserService) CreateUser(rctx request.CTX, user *model.User, opts UserCreateOptions) (*model.User, error) {
if opts.FromImport {
return us.createUser(user)
return us.createUser(rctx, user)
}
user.Roles = model.SystemUserRoleId
@@ -54,17 +54,17 @@ func (us *UserService) CreateUser(user *model.User, opts UserCreateOptions) (*mo
user.Locale = *us.config().LocalizationSettings.DefaultClientLocale
}
return us.createUser(user)
return us.createUser(rctx, user)
}
func (us *UserService) createUser(user *model.User) (*model.User, error) {
func (us *UserService) createUser(rctx request.CTX, user *model.User) (*model.User, error) {
user.MakeNonNil()
if err := us.isPasswordValid(user.Password); user.AuthService == "" && err != nil {
return nil, err
}
ruser, err := us.store.Save(user)
ruser, err := us.store.Save(rctx, user)
if err != nil {
return nil, err
}

Просмотреть файл

@@ -36,7 +36,7 @@ func TestFirstUserPromoted(t *testing.T) {
th := Setup(t)
defer th.TearDown()
user, err := th.service.CreateUser(&model.User{
user, err := th.service.CreateUser(th.Context, &model.User{
Username: model.NewId(),
Password: model.NewId(),
Email: "user@example.com",
@@ -46,7 +46,7 @@ func TestFirstUserPromoted(t *testing.T) {
require.Equal(t, model.SystemAdminRoleId+" "+model.SystemUserRoleId, user.Roles)
user2, err := th.service.CreateUser(&model.User{
user2, err := th.service.CreateUser(th.Context, &model.User{
Username: model.NewId(),
Password: model.NewId(),
Email: "user2@example.com",
@@ -67,7 +67,7 @@ func TestFirstUserPromoted(t *testing.T) {
_, err = th.dbStore.Bot().Save(b)
require.NoError(t, err)
user3, err := th.service.CreateUser(&model.User{
user3, err := th.service.CreateUser(th.Context, &model.User{
Username: model.NewId(),
Password: model.NewId(),
Email: "user3@example.com",
@@ -77,7 +77,7 @@ func TestFirstUserPromoted(t *testing.T) {
require.Equal(t, model.SystemAdminRoleId+" "+model.SystemUserRoleId, user3.Roles)
user4, err := th.service.CreateUser(&model.User{
user4, err := th.service.CreateUser(th.Context, &model.User{
Username: model.NewId(),
Password: model.NewId(),
Email: "user4@example.com",