From 1c846d83413118873f47aea4a0ea78263f748ddf Mon Sep 17 00:00:00 2001 From: Ezekiel Date: Mon, 11 Mar 2024 20:24:35 +0800 Subject: [PATCH] [GH-25494] add request context to public methods in bot go (#26408) --- server/channels/api4/apitestlib.go | 4 +- server/channels/api4/bot.go | 14 +-- server/channels/api4/bot_test.go | 70 +++++++-------- server/channels/api4/user.go | 24 +++--- server/channels/api4/user_test.go | 22 ++--- server/channels/app/app_iface.go | 18 ++-- server/channels/app/authorization.go | 8 +- server/channels/app/authorization_test.go | 52 +++++------ server/channels/app/bot.go | 42 ++++----- server/channels/app/bot_test.go | 86 +++++++++---------- server/channels/app/channel_test.go | 4 +- server/channels/app/helper_test.go | 4 +- server/channels/app/limits_test.go | 2 +- server/channels/app/notification_test.go | 2 +- .../app/opentracing/opentracing_layer.go | 36 ++++---- server/channels/app/plugin_api.go | 6 +- server/channels/app/user_test.go | 6 +- server/cmd/mmctl/commands/bot_e2e_test.go | 30 +++---- 18 files changed, 215 insertions(+), 215 deletions(-) diff --git a/server/channels/api4/apitestlib.go b/server/channels/api4/apitestlib.go index 8089e7ca10..f27245da32 100644 --- a/server/channels/api4/apitestlib.go +++ b/server/channels/api4/apitestlib.go @@ -485,9 +485,9 @@ func (th *TestHelper) InitBasic() *TestHelper { } func (th *TestHelper) DeleteBots() *TestHelper { - preexistingBots, _ := th.App.GetBots(&model.BotGetOptions{Page: 0, PerPage: 100}) + preexistingBots, _ := th.App.GetBots(th.Context, &model.BotGetOptions{Page: 0, PerPage: 100}) for _, bot := range preexistingBots { - th.App.PermanentDeleteBot(bot.UserId) + th.App.PermanentDeleteBot(th.Context, bot.UserId) } return th } diff --git a/server/channels/api4/bot.go b/server/channels/api4/bot.go index 5a4b45d493..b70b00f7b5 100644 --- a/server/channels/api4/bot.go +++ b/server/channels/api4/bot.go @@ -93,7 +93,7 @@ func patchBot(c *Context, w http.ResponseWriter, r *http.Request) { audit.AddEventParameter(auditRec, "id", botUserId) audit.AddEventParameterAuditable(auditRec, "bot", botPatch) - if err := c.App.SessionHasPermissionToManageBot(*c.AppContext.Session(), botUserId); err != nil { + if err := c.App.SessionHasPermissionToManageBot(c.AppContext, *c.AppContext.Session(), botUserId); err != nil { c.Err = err return } @@ -122,7 +122,7 @@ func getBot(c *Context, w http.ResponseWriter, r *http.Request) { includeDeleted, _ := strconv.ParseBool(r.URL.Query().Get("include_deleted")) - bot, appErr := c.App.GetBot(botUserId, includeDeleted) + bot, appErr := c.App.GetBot(c.AppContext, botUserId, includeDeleted) if appErr != nil { c.Err = appErr return @@ -170,7 +170,7 @@ func getBots(c *Context, w http.ResponseWriter, r *http.Request) { return } - bots, appErr := c.App.GetBots(&model.BotGetOptions{ + bots, appErr := c.App.GetBots(c.AppContext, &model.BotGetOptions{ Page: c.Params.Page, PerPage: c.Params.PerPage, OwnerId: OwnerId, @@ -211,7 +211,7 @@ func updateBotActive(c *Context, w http.ResponseWriter, active bool) { audit.AddEventParameter(auditRec, "id", botUserId) audit.AddEventParameter(auditRec, "enable", active) - if err := c.App.SessionHasPermissionToManageBot(*c.AppContext.Session(), botUserId); err != nil { + if err := c.App.SessionHasPermissionToManageBot(c.AppContext, *c.AppContext.Session(), botUserId); err != nil { c.Err = err return } @@ -245,7 +245,7 @@ func assignBot(c *Context, w http.ResponseWriter, _ *http.Request) { audit.AddEventParameter(auditRec, "id", botUserId) audit.AddEventParameter(auditRec, "user_id", userId) - if err := c.App.SessionHasPermissionToManageBot(*c.AppContext.Session(), botUserId); err != nil { + if err := c.App.SessionHasPermissionToManageBot(c.AppContext, *c.AppContext.Session(), botUserId); err != nil { c.Err = err return } @@ -257,7 +257,7 @@ func assignBot(c *Context, w http.ResponseWriter, _ *http.Request) { } } - bot, err := c.App.UpdateBotOwner(botUserId, userId) + bot, err := c.App.UpdateBotOwner(c.AppContext, botUserId, userId) if err != nil { c.Err = err return @@ -278,7 +278,7 @@ func convertBotToUser(c *Context, w http.ResponseWriter, r *http.Request) { return } - bot, err := c.App.GetBot(c.Params.BotUserId, false) + bot, err := c.App.GetBot(c.AppContext, c.Params.BotUserId, false) if err != nil { c.Err = err return diff --git a/server/channels/api4/bot_test.go b/server/channels/api4/bot_test.go index e1ad248c56..8a43e2ad3b 100644 --- a/server/channels/api4/bot_test.go +++ b/server/channels/api4/bot_test.go @@ -70,7 +70,7 @@ func TestCreateBot(t *testing.T) { createdBot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) require.Equal(t, bot.Username, createdBot.Username) require.Equal(t, bot.DisplayName, createdBot.DisplayName) require.Equal(t, bot.Description, createdBot.Description) @@ -117,7 +117,7 @@ func TestCreateBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) th.App.UpdateUserRoles(th.Context, bot.UserId, model.TeamUserRoleId+" "+model.SystemUserAccessTokenRoleId, false) rtoken, _, err := th.Client.CreateUserAccessToken(context.Background(), bot.UserId, "test token") @@ -183,7 +183,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { botPatch := &model.BotPatch{ @@ -207,7 +207,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBotSystemAdmin.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBotSystemAdmin.UserId) th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { botPatch := &model.BotPatch{ @@ -241,7 +241,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) _, _, err = th.Client.PatchBot(context.Background(), createdBot.UserId, &model.BotPatch{}) CheckErrorID(t, err, "store.sql_bot.get.missing.app_error") @@ -265,7 +265,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) _, _, err = th.Client.PatchBot(context.Background(), createdBot.UserId, &model.BotPatch{}) CheckErrorID(t, err, "api.context.permissions.app_error") @@ -289,7 +289,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) botPatch := &model.BotPatch{ Username: sToP(GenerateTestUsername()), @@ -340,7 +340,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) botPatch := &model.BotPatch{ Username: sToP(GenerateTestUsername()), @@ -371,7 +371,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) botPatch := &model.BotPatch{ Username: sToP(GenerateTestUsername()), @@ -402,7 +402,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) botPatch := &model.BotPatch{ Username: sToP(GenerateTestUsername()), @@ -440,7 +440,7 @@ func TestPatchBot(t *testing.T) { createdBot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) botPatch := &model.BotPatch{ Username: sToP(GenerateTestUsername()), @@ -474,7 +474,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) r, err := th.Client.DoAPIPut(context.Background(), "/bots/"+createdBot.UserId, `{"creator_id":"`+th.BasicUser2.Id+`"}`) require.NoError(t, err) @@ -511,7 +511,7 @@ func TestPatchBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) var botPatch *model.BotPatch @@ -536,7 +536,7 @@ func TestGetBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot1.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot1.UserId) bot2, resp, err := th.SystemAdminClient.CreateBot(context.Background(), &model.Bot{ Username: GenerateTestUsername(), @@ -545,7 +545,7 @@ func TestGetBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot2.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot2.UserId) deletedBot, resp, err := th.SystemAdminClient.CreateBot(context.Background(), &model.Bot{ Username: GenerateTestUsername(), @@ -553,7 +553,7 @@ func TestGetBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(deletedBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, deletedBot.UserId) deletedBot, resp, err = th.SystemAdminClient.DisableBot(context.Background(), deletedBot.UserId) require.NoError(t, err) CheckOKStatus(t, resp) @@ -571,7 +571,7 @@ func TestGetBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(myBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, myBot.UserId) th.RemovePermissionFromRole(model.PermissionCreateBot.Id, model.TeamUserRoleId) t.Run("get unknown bot", func(t *testing.T) { @@ -690,7 +690,7 @@ func TestGetBots(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot1.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot1.UserId) deletedBot1, resp, err := th.SystemAdminClient.CreateBot(context.Background(), &model.Bot{ Username: GenerateTestUsername(), @@ -698,7 +698,7 @@ func TestGetBots(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(deletedBot1.UserId) + defer th.App.PermanentDeleteBot(th.Context, deletedBot1.UserId) deletedBot1, resp, err = th.SystemAdminClient.DisableBot(context.Background(), deletedBot1.UserId) require.NoError(t, err) CheckOKStatus(t, resp) @@ -710,7 +710,7 @@ func TestGetBots(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot2.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot2.UserId) bot3, resp, err := th.SystemAdminClient.CreateBot(context.Background(), &model.Bot{ Username: GenerateTestUsername(), @@ -719,7 +719,7 @@ func TestGetBots(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot3.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot3.UserId) deletedBot2, resp, err := th.SystemAdminClient.CreateBot(context.Background(), &model.Bot{ Username: GenerateTestUsername(), @@ -727,7 +727,7 @@ func TestGetBots(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(deletedBot2.UserId) + defer th.App.PermanentDeleteBot(th.Context, deletedBot2.UserId) deletedBot2, resp, err = th.SystemAdminClient.DisableBot(context.Background(), deletedBot2.UserId) require.NoError(t, err) CheckOKStatus(t, resp) @@ -742,7 +742,7 @@ func TestGetBots(t *testing.T) { require.NoError(t, err) CheckCreatedStatus(t, resp) th.LoginBasic() - defer th.App.PermanentDeleteBot(orphanedBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, orphanedBot.UserId) // Automatic deactivation disabled th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.DisableBotsWhenOwnerIsDeactivated = false @@ -975,7 +975,7 @@ func TestDisableBot(t *testing.T) { createdBot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) _, _, err = th.Client.DisableBot(context.Background(), createdBot.UserId) CheckErrorID(t, err, "store.sql_bot.get.missing.app_error") @@ -1001,7 +1001,7 @@ func TestDisableBot(t *testing.T) { createdBot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) _, _, err = th.Client.DisableBot(context.Background(), createdBot.UserId) CheckErrorID(t, err, "api.context.permissions.app_error") @@ -1026,7 +1026,7 @@ func TestDisableBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) disabledBot, resp, err := client.DisableBot(context.Background(), bot.UserId) require.NoError(t, err) @@ -1080,7 +1080,7 @@ func TestEnableBot(t *testing.T) { createdBot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) _, resp, err = th.SystemAdminClient.DisableBot(context.Background(), createdBot.UserId) require.NoError(t, err) @@ -1110,7 +1110,7 @@ func TestEnableBot(t *testing.T) { createdBot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) _, resp, err = th.SystemAdminClient.DisableBot(context.Background(), createdBot.UserId) require.NoError(t, err) @@ -1139,7 +1139,7 @@ func TestEnableBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) _, resp, err = th.SystemAdminClient.DisableBot(context.Background(), bot.UserId) require.NoError(t, err) @@ -1195,7 +1195,7 @@ func TestAssignBot(t *testing.T) { bot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) before, resp, err := th.Client.GetBot(context.Background(), bot.UserId, "") require.NoError(t, err) @@ -1244,7 +1244,7 @@ func TestAssignBot(t *testing.T) { createdBot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) th.LoginBasic2() @@ -1276,7 +1276,7 @@ func TestAssignBot(t *testing.T) { bot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) // Simulate custom role by just changing the system user role th.AddPermissionToRole(model.PermissionCreateBot.Id, model.SystemUserRoleId) @@ -1312,7 +1312,7 @@ func TestAssignBot(t *testing.T) { bot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) bot2, resp, err := th.Client.CreateBot(context.Background(), &model.Bot{ Username: GenerateTestUsername(), @@ -1321,7 +1321,7 @@ func TestAssignBot(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot2.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot2.UserId) _, _, err = th.Client.AssignBot(context.Background(), bot.UserId, bot2.UserId) CheckErrorID(t, err, "api.context.permissions.app_error") @@ -1345,7 +1345,7 @@ func TestConvertBotToUser(t *testing.T) { bot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) _, resp, err = th.Client.ConvertBotToUser(context.Background(), bot.UserId, &model.UserPatch{}, false) require.Error(t, err) diff --git a/server/channels/api4/user.go b/server/channels/api4/user.go index f4ad7d1fb4..b0b012da33 100644 --- a/server/channels/api4/user.go +++ b/server/channels/api4/user.go @@ -430,7 +430,7 @@ func setProfileImage(c *Context, w http.ResponseWriter, r *http.Request) { return } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), c.Params.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), c.Params.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -501,7 +501,7 @@ func setDefaultProfileImage(c *Context, w http.ResponseWriter, r *http.Request) return } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), c.Params.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), c.Params.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -1260,7 +1260,7 @@ func updateUser(c *Context, w http.ResponseWriter, r *http.Request) { return } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), user.Id) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), user.Id) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -1337,7 +1337,7 @@ func patchUser(c *Context, w http.ResponseWriter, r *http.Request) { audit.AddEventParameterAuditable(auditRec, "user_patch", &patch) defer c.LogAuditRec(auditRec) - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), c.Params.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), c.Params.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -1417,7 +1417,7 @@ func deleteUser(c *Context, w http.ResponseWriter, r *http.Request) { audit.AddEventParameter(auditRec, "permanent", permanent) defer c.LogAuditRec(auditRec) - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), userId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), userId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -2458,7 +2458,7 @@ func createUserAccessToken(c *Context, w http.ResponseWriter, r *http.Request) { return } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), c.Params.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), c.Params.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -2550,7 +2550,7 @@ func getUserAccessTokensForUser(c *Context, w http.ResponseWriter, r *http.Reque return } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), c.Params.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), c.Params.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -2587,7 +2587,7 @@ func getUserAccessToken(c *Context, w http.ResponseWriter, r *http.Request) { return } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), accessToken.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), accessToken.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -2625,7 +2625,7 @@ func revokeUserAccessToken(c *Context, w http.ResponseWriter, r *http.Request) { audit.AddEventParameterAuditable(auditRec, "user", user) } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), accessToken.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), accessToken.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -2670,7 +2670,7 @@ func disableUserAccessToken(c *Context, w http.ResponseWriter, r *http.Request) audit.AddEventParameterAuditable(auditRec, "user", user) } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), accessToken.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), accessToken.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -2715,7 +2715,7 @@ func enableUserAccessToken(c *Context, w http.ResponseWriter, r *http.Request) { audit.AddEventParameterAuditable(auditRec, "user", user) } - if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), accessToken.UserId) { + if !c.App.SessionHasPermissionToUserOrBot(c.AppContext, *c.AppContext.Session(), accessToken.UserId) { c.SetPermissionError(model.PermissionEditOtherUsers) return } @@ -2965,7 +2965,7 @@ func convertUserToBot(c *Context, w http.ResponseWriter, r *http.Request) { return } - bot, appErr := c.App.ConvertUserToBot(user) + bot, appErr := c.App.ConvertUserToBot(c.AppContext, user) if appErr != nil { c.Err = appErr return diff --git a/server/channels/api4/user_test.go b/server/channels/api4/user_test.go index a9b9d2ea9e..c055c51631 100644 --- a/server/channels/api4/user_test.go +++ b/server/channels/api4/user_test.go @@ -916,7 +916,7 @@ func TestGetBotUser(t *testing.T) { createdBot, resp, err := th.Client.CreateBot(context.Background(), bot) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) botUser, _, err := th.Client.GetUser(context.Background(), createdBot.UserId, "") require.NoError(t, err) @@ -4556,7 +4556,7 @@ func TestCreateUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) t.Run("without MANAGE_BOT permission", func(t *testing.T) { th.RemovePermissionFromRole(model.PermissionManageBots.Id, model.TeamUserRoleId) @@ -4598,7 +4598,7 @@ func TestCreateUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) t.Run("only having MANAGE_BOTS permission", func(t *testing.T) { _, resp, err = th.Client.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") @@ -4703,7 +4703,7 @@ func TestGetUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) token, _, err := th.Client.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") require.NoError(t, err) @@ -4751,7 +4751,7 @@ func TestGetUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) token, _, err := th.SystemAdminClient.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") require.NoError(t, err) @@ -4976,7 +4976,7 @@ func TestRevokeUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) token, _, err := th.Client.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") require.NoError(t, err) @@ -5020,7 +5020,7 @@ func TestRevokeUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) token, _, err := th.SystemAdminClient.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") require.NoError(t, err) @@ -5095,7 +5095,7 @@ func TestDisableUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) token, _, err := th.Client.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") require.NoError(t, err) @@ -5139,7 +5139,7 @@ func TestDisableUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) token, _, err := th.SystemAdminClient.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") require.NoError(t, err) @@ -5222,7 +5222,7 @@ func TestEnableUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) token, _, err := th.Client.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") require.NoError(t, err) @@ -5269,7 +5269,7 @@ func TestEnableUserAccessToken(t *testing.T) { }) require.NoError(t, err) CheckCreatedStatus(t, resp) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) token, _, err := th.SystemAdminClient.CreateUserAccessToken(context.Background(), createdBot.UserId, "test token") require.NoError(t, err) diff --git a/server/channels/app/app_iface.go b/server/channels/app/app_iface.go index 84f054d888..57e8bf82fb 100644 --- a/server/channels/app/app_iface.go +++ b/server/channels/app/app_iface.go @@ -86,11 +86,11 @@ type AppIface interface { // ConvertBotToUser converts a bot to user. ConvertBotToUser(c request.CTX, bot *model.Bot, userPatch *model.UserPatch, sysadmin bool) (*model.User, *model.AppError) // ConvertUserToBot converts a user to bot. - ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError) + ConvertUserToBot(rctx request.CTX, user *model.User) (*model.Bot, *model.AppError) // Create/ Update a subscription history event SendSubscriptionHistoryEvent(userID string) (*model.SubscriptionHistory, error) // CreateBot creates the given bot and corresponding user. - CreateBot(c request.CTX, bot *model.Bot) (*model.Bot, *model.AppError) + CreateBot(rctx request.CTX, bot *model.Bot) (*model.Bot, *model.AppError) // CreateChannelScheme creates a new Scheme of scope channel and assigns it to the channel. CreateChannelScheme(c request.CTX, channel *model.Channel) (*model.Scheme, *model.AppError) // CreateDefaultMemberships adds users to teams and channels based on their group memberships and how those groups @@ -165,9 +165,9 @@ type AppIface interface { // filter. GetAllLdapGroupsPage(rctx request.CTX, page int, perPage int, opts model.LdapGroupSearchOpts) ([]*model.Group, int, *model.AppError) // GetBot returns the given bot. - GetBot(botUserId string, includeDeleted bool) (*model.Bot, *model.AppError) + GetBot(rctx request.CTX, botUserId string, includeDeleted bool) (*model.Bot, *model.AppError) // GetBots returns the requested page of bots. - GetBots(options *model.BotGetOptions) (model.BotList, *model.AppError) + GetBots(rctx request.CTX, options *model.BotGetOptions) (model.BotList, *model.AppError) // GetChannelGroupUsers returns the users who are associated to the channel via GroupChannels and GroupMembers. GetChannelGroupUsers(channelID string) ([]*model.User, *model.AppError) // GetChannelModerationsForChannel Gets a channels ChannelModerations from either the higherScoped roles or from the channel scheme roles. @@ -293,7 +293,7 @@ type AppIface interface { // For internal requests, requests are routed directly to a plugin ServerHTTP hook DoActionRequest(c request.CTX, rawURL string, body []byte) (*http.Response, *model.AppError) // PermanentDeleteBot permanently deletes a bot and its corresponding user. - PermanentDeleteBot(botUserId string) *model.AppError + PermanentDeleteBot(rctx request.CTX, botUserId string) *model.AppError // PopulateWebConnConfig checks if the connection id already exists in the hub, // and if so, accordingly populates the other fields of the webconn. PopulateWebConnConfig(s *model.Session, cfg *platform.WebConnConfig, seqVal string) (*platform.WebConnConfig, error) @@ -325,7 +325,7 @@ type AppIface interface { // SessionHasPermissionToManageBot returns nil if the session has access to manage the given bot. // This function deviates from other authorization checks in returning an error instead of just // a boolean, allowing the permission failure to be exposed with more granularity. - SessionHasPermissionToManageBot(session model.Session, botUserId string) *model.AppError + SessionHasPermissionToManageBot(rctx request.CTX, session model.Session, botUserId string) *model.AppError // SessionHasPermissionToTeams returns true only if user has access to all teams. SessionHasPermissionToTeams(c request.CTX, session model.Session, teamIDs []string, permission *model.Permission) bool // SessionIsRegistered determines if a specific session has been registered @@ -378,9 +378,9 @@ type AppIface interface { // This to be used for places we check the users password when they are already logged in DoubleCheckPassword(rctx request.CTX, user *model.User, password string) *model.AppError // UpdateBotActive marks a bot as active or inactive, along with its corresponding user. - UpdateBotActive(c request.CTX, botUserId string, active bool) (*model.Bot, *model.AppError) + UpdateBotActive(rctx request.CTX, botUserId string, active bool) (*model.Bot, *model.AppError) // UpdateBotOwner changes a bot's owner to the given value. - UpdateBotOwner(botUserId, newOwnerId string) (*model.Bot, *model.AppError) + UpdateBotOwner(rctx request.CTX, botUserId, newOwnerId string) (*model.Bot, *model.AppError) // UpdateChannel updates a given channel by its Id. It also publishes the CHANNEL_UPDATED event. UpdateChannel(c request.CTX, channel *model.Channel) (*model.Channel, *model.AppError) // UpdateChannelScheme saves the new SchemeId of the channel passed. @@ -1092,7 +1092,7 @@ type AppIface interface { SessionHasPermissionToReadJob(session model.Session, jobType string) (bool, *model.Permission) SessionHasPermissionToTeam(session model.Session, teamID string, permission *model.Permission) bool SessionHasPermissionToUser(session model.Session, userID string) bool - SessionHasPermissionToUserOrBot(session model.Session, userID string) bool + SessionHasPermissionToUserOrBot(rctx request.CTX, session model.Session, userID string) bool SetActiveChannel(c request.CTX, userID string, channelID string) *model.AppError SetAutoResponderStatus(rctx request.CTX, user *model.User, oldNotifyProps model.StringMap) SetChannels(ch *Channels) diff --git a/server/channels/app/authorization.go b/server/channels/app/authorization.go index 63c93e70bf..6ad4119fa2 100644 --- a/server/channels/app/authorization.go +++ b/server/channels/app/authorization.go @@ -217,12 +217,12 @@ func (a *App) SessionHasPermissionToUser(session model.Session, userID string) b return false } -func (a *App) SessionHasPermissionToUserOrBot(session model.Session, userID string) bool { +func (a *App) SessionHasPermissionToUserOrBot(rctx request.CTX, session model.Session, userID string) bool { if session.IsUnrestricted() { return true } - err := a.SessionHasPermissionToManageBot(session, userID) + err := a.SessionHasPermissionToManageBot(rctx, session, userID) if err == nil { return true } @@ -339,8 +339,8 @@ func (a *App) RolesGrantPermission(roleNames []string, permissionId string) bool // SessionHasPermissionToManageBot returns nil if the session has access to manage the given bot. // This function deviates from other authorization checks in returning an error instead of just // a boolean, allowing the permission failure to be exposed with more granularity. -func (a *App) SessionHasPermissionToManageBot(session model.Session, botUserId string) *model.AppError { - existingBot, err := a.GetBot(botUserId, true) +func (a *App) SessionHasPermissionToManageBot(rctx request.CTX, session model.Session, botUserId string) *model.AppError { + existingBot, err := a.GetBot(rctx, botUserId, true) if err != nil { return err } diff --git a/server/channels/app/authorization_test.go b/server/channels/app/authorization_test.go index 9111d249ed..e2af825c7d 100644 --- a/server/channels/app/authorization_test.go +++ b/server/channels/app/authorization_test.go @@ -256,7 +256,7 @@ func TestSessionHasPermissionToManageBot(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) assert.NotNil(t, bot) t.Run("test my bot", func(t *testing.T) { @@ -264,19 +264,19 @@ func TestSessionHasPermissionToManageBot(t *testing.T) { UserId: th.BasicUser.Id, Roles: model.SystemUserRoleId, } - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.NotNil(t, err) assert.Equal(t, "store.sql_bot.get.missing.app_error", err.Id) assert.NoError(t, err.Unwrap()) th.AddPermissionToRole(model.PermissionReadBots.Id, model.SystemUserRoleId) - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.NotNil(t, err) assert.Equal(t, "api.context.permissions.app_error", err.Id) assert.NoError(t, err.Unwrap()) th.AddPermissionToRole(model.PermissionManageBots.Id, model.SystemUserRoleId) - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.Nil(t, err) th.RemovePermissionFromRole(model.PermissionReadBots.Id, model.SystemUserRoleId) @@ -288,19 +288,19 @@ func TestSessionHasPermissionToManageBot(t *testing.T) { UserId: th.BasicUser2.Id, Roles: model.SystemUserRoleId, } - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.NotNil(t, err) assert.Equal(t, "store.sql_bot.get.missing.app_error", err.Id) assert.NoError(t, err.Unwrap()) th.AddPermissionToRole(model.PermissionReadOthersBots.Id, model.SystemUserRoleId) - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.NotNil(t, err) assert.Equal(t, "api.context.permissions.app_error", err.Id) assert.NoError(t, err.Unwrap()) th.AddPermissionToRole(model.PermissionManageOthersBots.Id, model.SystemUserRoleId) - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.Nil(t, err) th.RemovePermissionFromRole(model.PermissionReadOthersBots.Id, model.SystemUserRoleId) @@ -314,20 +314,20 @@ func TestSessionHasPermissionToManageBot(t *testing.T) { } // test non bot, contains wrapped error - err = th.App.SessionHasPermissionToManageBot(session, "12345") + err = th.App.SessionHasPermissionToManageBot(th.Context, session, "12345") assert.NotNil(t, err) assert.Equal(t, "store.sql_bot.get.missing.app_error", err.Id) assert.Error(t, err.Unwrap()) // test existing bot, without PermissionManageOthersBots - no wrapped error - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.NotNil(t, err) assert.Equal(t, "store.sql_bot.get.missing.app_error", err.Id) assert.NoError(t, err.Unwrap()) // test with correct permissions th.AddPermissionToRole(model.PermissionManageOthersBots.Id, model.SystemUserManagerRoleId) - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.Nil(t, err) th.RemovePermissionFromRole(model.PermissionManageOthersBots.Id, model.SystemUserManagerRoleId) @@ -338,7 +338,7 @@ func TestSessionHasPermissionToManageBot(t *testing.T) { UserId: th.SystemAdminUser.Id, Roles: model.SystemAdminRoleId, } - err = th.App.SessionHasPermissionToManageBot(session, bot.UserId) + err = th.App.SessionHasPermissionToManageBot(th.Context, session, bot.UserId) assert.Nil(t, err) }) @@ -347,7 +347,7 @@ func TestSessionHasPermissionToManageBot(t *testing.T) { UserId: th.SystemAdminUser.Id, Roles: model.SystemUserRoleId, } - err = th.App.SessionHasPermissionToManageBot(session, "12345") + err = th.App.SessionHasPermissionToManageBot(th.Context, session, "12345") assert.NotNil(t, err) assert.Equal(t, "store.sql_bot.get.missing.app_error", err.Id) assert.Error(t, err.Unwrap()) @@ -385,7 +385,7 @@ func TestSessionHasPermissionToUser(t *testing.T) { }) require.Nil(t, err) assert.NotNil(t, bot) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) assert.False(t, th.App.SessionHasPermissionToUser(session, bot.UserId)) }) @@ -410,16 +410,16 @@ func TestSessionHasPermissionToManageUserOrBot(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) t.Run("test basic user access", func(t *testing.T) { session := model.Session{ UserId: th.BasicUser.Id, Roles: model.SystemUserRoleId, } - assert.True(t, th.App.SessionHasPermissionToUserOrBot(session, th.BasicUser.Id)) - assert.False(t, th.App.SessionHasPermissionToUserOrBot(session, bot.UserId)) - assert.False(t, th.App.SessionHasPermissionToUserOrBot(session, th.BasicUser2.Id)) + assert.True(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, th.BasicUser.Id)) + assert.False(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, bot.UserId)) + assert.False(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, th.BasicUser2.Id)) }) t.Run("test user manager access", func(t *testing.T) { @@ -427,18 +427,18 @@ func TestSessionHasPermissionToManageUserOrBot(t *testing.T) { UserId: th.BasicUser2.Id, Roles: model.SystemUserManagerRoleId, } - assert.False(t, th.App.SessionHasPermissionToUserOrBot(session, th.BasicUser.Id)) - assert.True(t, th.App.SessionHasPermissionToUserOrBot(session, th.BasicUser2.Id)) - assert.False(t, th.App.SessionHasPermissionToUserOrBot(session, bot.UserId)) + assert.False(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, th.BasicUser.Id)) + assert.True(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, th.BasicUser2.Id)) + assert.False(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, bot.UserId)) th.AddPermissionToRole(model.PermissionEditOtherUsers.Id, model.SystemUserManagerRoleId) - assert.True(t, th.App.SessionHasPermissionToUserOrBot(session, th.BasicUser.Id)) - assert.False(t, th.App.SessionHasPermissionToUserOrBot(session, bot.UserId)) + assert.True(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, th.BasicUser.Id)) + assert.False(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, bot.UserId)) th.RemovePermissionFromRole(model.PermissionEditOtherUsers.Id, model.SystemUserManagerRoleId) th.AddPermissionToRole(model.PermissionManageOthersBots.Id, model.SystemUserManagerRoleId) - assert.False(t, th.App.SessionHasPermissionToUserOrBot(session, th.BasicUser.Id)) - assert.True(t, th.App.SessionHasPermissionToUserOrBot(session, bot.UserId)) + assert.False(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, th.BasicUser.Id)) + assert.True(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, bot.UserId)) th.RemovePermissionFromRole(model.PermissionManageOthersBots.Id, model.SystemUserManagerRoleId) }) @@ -447,8 +447,8 @@ func TestSessionHasPermissionToManageUserOrBot(t *testing.T) { UserId: th.SystemAdminUser.Id, Roles: model.SystemAdminRoleId, } - assert.True(t, th.App.SessionHasPermissionToUserOrBot(session, bot.UserId)) - assert.True(t, th.App.SessionHasPermissionToUserOrBot(session, th.BasicUser.Id)) + assert.True(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, bot.UserId)) + assert.True(t, th.App.SessionHasPermissionToUserOrBot(th.Context, session, th.BasicUser.Id)) }) } diff --git a/server/channels/app/bot.go b/server/channels/app/bot.go index ac4b4f7e46..44f3bae84b 100644 --- a/server/channels/app/bot.go +++ b/server/channels/app/bot.go @@ -89,13 +89,13 @@ func (a *App) EnsureBot(rctx request.CTX, pluginID string, bot *model.Bot) (stri } // CreateBot creates the given bot and corresponding user. -func (a *App) CreateBot(c request.CTX, bot *model.Bot) (*model.Bot, *model.AppError) { +func (a *App) CreateBot(rctx request.CTX, bot *model.Bot) (*model.Bot, *model.AppError) { vErr := bot.IsValidCreate() if vErr != nil { return nil, vErr } - user, nErr := a.Srv().Store().User().Save(c, model.UserFromBot(bot)) + user, nErr := a.Srv().Store().User().Save(rctx, model.UserFromBot(bot)) if nErr != nil { var appErr *model.AppError var invErr *store.ErrInvalidInput @@ -139,7 +139,7 @@ func (a *App) CreateBot(c request.CTX, bot *model.Bot) (*model.Bot, *model.AppEr } else if ownerUser != nil { // Send a message to the bot's creator to inform them that the bot needs to be added // to a team and channel after it's created - channel, err := a.getOrCreateDirectChannelWithUser(c, user, ownerUser) + channel, err := a.getOrCreateDirectChannelWithUser(rctx, user, ownerUser) if err != nil { return nil, err } @@ -152,7 +152,7 @@ func (a *App) CreateBot(c request.CTX, bot *model.Bot) (*model.Bot, *model.AppEr Message: T("api.bot.teams_channels.add_message_mobile"), } - if _, err := a.CreatePostAsUser(c, botAddPost, c.Session().Id, true); err != nil { + if _, err := a.CreatePostAsUser(rctx, botAddPost, rctx.Session().Id, true); err != nil { return nil, err } } @@ -241,7 +241,7 @@ func (a *App) getOrCreateBot(rctx request.CTX, botDef *model.Bot) (*model.Bot, * } //return the bot for this user - savedBot, appErr := a.GetBot(botUser.Id, false) + savedBot, appErr := a.GetBot(rctx, botUser.Id, false) if appErr != nil { return nil, appErr } @@ -251,7 +251,7 @@ func (a *App) getOrCreateBot(rctx request.CTX, botDef *model.Bot) (*model.Bot, * // PatchBot applies the given patch to the bot and corresponding user. func (a *App) PatchBot(rctx request.CTX, botUserId string, botPatch *model.BotPatch) (*model.Bot, *model.AppError) { - bot, err := a.GetBot(botUserId, true) + bot, err := a.GetBot(rctx, botUserId, true) if err != nil { return nil, err } @@ -320,7 +320,7 @@ func (a *App) PatchBot(rctx request.CTX, botUserId string, botPatch *model.BotPa } // GetBot returns the given bot. -func (a *App) GetBot(botUserId string, includeDeleted bool) (*model.Bot, *model.AppError) { +func (a *App) GetBot(rctx request.CTX, botUserId string, includeDeleted bool) (*model.Bot, *model.AppError) { bot, err := a.Srv().Store().Bot().Get(botUserId, includeDeleted) if err != nil { var nfErr *store.ErrNotFound @@ -335,7 +335,7 @@ func (a *App) GetBot(botUserId string, includeDeleted bool) (*model.Bot, *model. } // GetBots returns the requested page of bots. -func (a *App) GetBots(options *model.BotGetOptions) (model.BotList, *model.AppError) { +func (a *App) GetBots(rctx request.CTX, options *model.BotGetOptions) (model.BotList, *model.AppError) { bots, err := a.Srv().Store().Bot().GetAll(options) if err != nil { return nil, model.NewAppError("GetBots", "app.bot.getbots.internal_error", nil, "", http.StatusInternalServerError).Wrap(err) @@ -344,7 +344,7 @@ func (a *App) GetBots(options *model.BotGetOptions) (model.BotList, *model.AppEr } // UpdateBotActive marks a bot as active or inactive, along with its corresponding user. -func (a *App) UpdateBotActive(c request.CTX, botUserId string, active bool) (*model.Bot, *model.AppError) { +func (a *App) UpdateBotActive(rctx request.CTX, botUserId string, active bool) (*model.Bot, *model.AppError) { user, nErr := a.Srv().Store().User().Get(context.Background(), botUserId) if nErr != nil { var nfErr *store.ErrNotFound @@ -356,7 +356,7 @@ func (a *App) UpdateBotActive(c request.CTX, botUserId string, active bool) (*mo } } - if _, err := a.UpdateActive(c, user, active); err != nil { + if _, err := a.UpdateActive(rctx, user, active); err != nil { return nil, err } @@ -400,7 +400,7 @@ func (a *App) UpdateBotActive(c request.CTX, botUserId string, active bool) (*mo } // PermanentDeleteBot permanently deletes a bot and its corresponding user. -func (a *App) PermanentDeleteBot(botUserId string) *model.AppError { +func (a *App) PermanentDeleteBot(rctx request.CTX, botUserId string) *model.AppError { if err := a.Srv().Store().Bot().PermanentDelete(botUserId); err != nil { var invErr *store.ErrInvalidInput switch { @@ -419,7 +419,7 @@ func (a *App) PermanentDeleteBot(botUserId string) *model.AppError { } // UpdateBotOwner changes a bot's owner to the given value. -func (a *App) UpdateBotOwner(botUserId, newOwnerId string) (*model.Bot, *model.AppError) { +func (a *App) UpdateBotOwner(rctx request.CTX, botUserId, newOwnerId string) (*model.Bot, *model.AppError) { bot, err := a.Srv().Store().Bot().Get(botUserId, true) if err != nil { var nfErr *store.ErrNotFound @@ -451,7 +451,7 @@ func (a *App) UpdateBotOwner(botUserId, newOwnerId string) (*model.Bot, *model.A } // disableUserBots disables all bots owned by the given user. -func (a *App) disableUserBots(c request.CTX, userID string) *model.AppError { +func (a *App) disableUserBots(rctx request.CTX, userID string) *model.AppError { perPage := 20 for { options := &model.BotGetOptions{ @@ -461,15 +461,15 @@ func (a *App) disableUserBots(c request.CTX, userID string) *model.AppError { Page: 0, PerPage: perPage, } - userBots, err := a.GetBots(options) + userBots, err := a.GetBots(rctx, options) if err != nil { return err } for _, bot := range userBots { - _, err := a.UpdateBotActive(c, bot.UserId, false) + _, err := a.UpdateBotActive(rctx, bot.UserId, false) if err != nil { - c.Logger().Warn("Unable to deactivate bot.", mlog.String("bot_user_id", bot.UserId), mlog.Err(err)) + rctx.Logger().Warn("Unable to deactivate bot.", mlog.String("bot_user_id", bot.UserId), mlog.Err(err)) } } @@ -484,7 +484,7 @@ func (a *App) disableUserBots(c request.CTX, userID string) *model.AppError { return nil } -func (a *App) notifySysadminsBotOwnerDeactivated(c request.CTX, userID string) *model.AppError { +func (a *App) notifySysadminsBotOwnerDeactivated(rctx request.CTX, userID string) *model.AppError { perPage := 25 botOptions := &model.BotGetOptions{ OwnerId: userID, @@ -496,7 +496,7 @@ func (a *App) notifySysadminsBotOwnerDeactivated(c request.CTX, userID string) * // get owner bots var userBots []*model.Bot for { - bots, err := a.GetBots(botOptions) + bots, err := a.GetBots(rctx, botOptions) if err != nil { return err } @@ -546,7 +546,7 @@ func (a *App) notifySysadminsBotOwnerDeactivated(c request.CTX, userID string) * // for each sysadmin, notify user that owns bots was disabled for _, sysAdmin := range sysAdmins { - channel, appErr := a.GetOrCreateDirectChannel(c, sysAdmin.Id, sysAdmin.Id) + channel, appErr := a.GetOrCreateDirectChannel(rctx, sysAdmin.Id, sysAdmin.Id) if appErr != nil { return appErr } @@ -558,7 +558,7 @@ func (a *App) notifySysadminsBotOwnerDeactivated(c request.CTX, userID string) * Type: model.PostTypeSystemGeneric, } - _, appErr = a.CreatePost(c, post, channel, false, true) + _, appErr = a.CreatePost(rctx, post, channel, false, true) if appErr != nil { return appErr } @@ -596,7 +596,7 @@ func (a *App) getDisableBotSysadminMessage(user *model.User, userBots model.BotL } // ConvertUserToBot converts a user to bot. -func (a *App) ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError) { +func (a *App) ConvertUserToBot(rctx request.CTX, user *model.User) (*model.Bot, *model.AppError) { bot, err := a.Srv().Store().Bot().Save(model.BotFromUser(user)) if err != nil { var appErr *model.AppError diff --git a/server/channels/app/bot_test.go b/server/channels/app/bot_test.go index 35cda5b70a..65ad78d7f8 100644 --- a/server/channels/app/bot_test.go +++ b/server/channels/app/bot_test.go @@ -79,7 +79,7 @@ func TestCreateBot(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) assert.Equal(t, "username", bot.Username) assert.Equal(t, "a bot", bot.Description) assert.Equal(t, th.BasicUser.Id, bot.OwnerId) @@ -123,7 +123,7 @@ func TestPatchBot(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) botPatch := &model.BotPatch{ Username: sToP("invalid username"), @@ -146,7 +146,7 @@ func TestPatchBot(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) botPatch := &model.BotPatch{ Username: sToP("username"), @@ -172,7 +172,7 @@ func TestPatchBot(t *testing.T) { createdBot, err := th.App.CreateBot(th.Context, bot) require.Nil(t, err) - defer th.App.PermanentDeleteBot(createdBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, createdBot.UserId) botPatch := &model.BotPatch{ Username: sToP("username2"), @@ -204,7 +204,7 @@ func TestPatchBot(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) botPatch := &model.BotPatch{ Username: sToP(th.BasicUser2.Username), @@ -226,7 +226,7 @@ func TestGetBot(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot1.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot1.UserId) bot2, err := th.App.CreateBot(th.Context, &model.Bot{ Username: "username2", @@ -234,7 +234,7 @@ func TestGetBot(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot2.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot2.UserId) deletedBot, err := th.App.CreateBot(th.Context, &model.Bot{ Username: "username3", @@ -244,34 +244,34 @@ func TestGetBot(t *testing.T) { require.Nil(t, err) deletedBot, err = th.App.UpdateBotActive(th.Context, deletedBot.UserId, false) require.Nil(t, err) - defer th.App.PermanentDeleteBot(deletedBot.UserId) + defer th.App.PermanentDeleteBot(th.Context, deletedBot.UserId) t.Run("get unknown bot", func(t *testing.T) { - _, err := th.App.GetBot(model.NewId(), false) + _, err := th.App.GetBot(th.Context, model.NewId(), false) require.NotNil(t, err) require.Equal(t, "store.sql_bot.get.missing.app_error", err.Id) }) t.Run("get bot1", func(t *testing.T) { - bot, err := th.App.GetBot(bot1.UserId, false) + bot, err := th.App.GetBot(th.Context, bot1.UserId, false) require.Nil(t, err) assert.Equal(t, bot1, bot) }) t.Run("get bot2", func(t *testing.T) { - bot, err := th.App.GetBot(bot2.UserId, false) + bot, err := th.App.GetBot(th.Context, bot2.UserId, false) require.Nil(t, err) assert.Equal(t, bot2, bot) }) t.Run("get deleted bot", func(t *testing.T) { - _, err := th.App.GetBot(deletedBot.UserId, false) + _, err := th.App.GetBot(th.Context, deletedBot.UserId, false) require.NotNil(t, err) require.Equal(t, "store.sql_bot.get.missing.app_error", err.Id) }) t.Run("get deleted bot, include deleted", func(t *testing.T) { - bot, err := th.App.GetBot(deletedBot.UserId, true) + bot, err := th.App.GetBot(th.Context, deletedBot.UserId, true) require.Nil(t, err) assert.Equal(t, deletedBot, bot) }) @@ -290,7 +290,7 @@ func TestGetBots(t *testing.T) { OwnerId: OwnerId1, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot1.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot1.UserId) deletedBot1, err := th.App.CreateBot(th.Context, &model.Bot{ Username: "username4", @@ -300,7 +300,7 @@ func TestGetBots(t *testing.T) { require.Nil(t, err) deletedBot1, err = th.App.UpdateBotActive(th.Context, deletedBot1.UserId, false) require.Nil(t, err) - defer th.App.PermanentDeleteBot(deletedBot1.UserId) + defer th.App.PermanentDeleteBot(th.Context, deletedBot1.UserId) bot2, err := th.App.CreateBot(th.Context, &model.Bot{ Username: "username2", @@ -308,7 +308,7 @@ func TestGetBots(t *testing.T) { OwnerId: OwnerId1, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot2.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot2.UserId) bot3, err := th.App.CreateBot(th.Context, &model.Bot{ Username: "username3", @@ -316,7 +316,7 @@ func TestGetBots(t *testing.T) { OwnerId: OwnerId1, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot3.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot3.UserId) bot4, err := th.App.CreateBot(th.Context, &model.Bot{ Username: "username5", @@ -324,7 +324,7 @@ func TestGetBots(t *testing.T) { OwnerId: OwnerId2, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot4.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot4.UserId) deletedBot2, err := th.App.CreateBot(th.Context, &model.Bot{ Username: "username6", @@ -334,10 +334,10 @@ func TestGetBots(t *testing.T) { require.Nil(t, err) deletedBot2, err = th.App.UpdateBotActive(th.Context, deletedBot2.UserId, false) require.Nil(t, err) - defer th.App.PermanentDeleteBot(deletedBot2.UserId) + defer th.App.PermanentDeleteBot(th.Context, deletedBot2.UserId) t.Run("get bots, page=0, perPage=10", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 0, PerPage: 10, OwnerId: "", @@ -348,7 +348,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get bots, page=0, perPage=1", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 0, PerPage: 1, OwnerId: "", @@ -359,7 +359,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get bots, page=1, perPage=2", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 1, PerPage: 2, OwnerId: "", @@ -370,7 +370,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get bots, page=2, perPage=2", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 2, PerPage: 2, OwnerId: "", @@ -381,7 +381,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get bots, page=0, perPage=10, include deleted", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 0, PerPage: 10, OwnerId: "", @@ -392,7 +392,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get bots, page=0, perPage=1, include deleted", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 0, PerPage: 1, OwnerId: "", @@ -403,7 +403,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get bots, page=1, perPage=2, include deleted", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 1, PerPage: 2, OwnerId: "", @@ -414,7 +414,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get bots, page=2, perPage=2, include deleted", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 2, PerPage: 2, OwnerId: "", @@ -425,7 +425,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get offset=0, limit=10, creator id 1", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 0, PerPage: 10, OwnerId: OwnerId1, @@ -436,7 +436,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get offset=0, limit=10, creator id 2", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 0, PerPage: 10, OwnerId: OwnerId2, @@ -447,7 +447,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get offset=0, limit=10, include deleted, creator id 1", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 0, PerPage: 10, OwnerId: OwnerId1, @@ -458,7 +458,7 @@ func TestGetBots(t *testing.T) { }) t.Run("get offset=0, limit=10, include deleted, creator id 2", func(t *testing.T) { - bots, err := th.App.GetBots(&model.BotGetOptions{ + bots, err := th.App.GetBots(th.Context, &model.BotGetOptions{ Page: 0, PerPage: 10, OwnerId: OwnerId2, @@ -489,7 +489,7 @@ func TestUpdateBotActive(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) disabledBot, err := th.App.UpdateBotActive(th.Context, bot.UserId, false) require.Nil(t, err) @@ -522,9 +522,9 @@ func TestPermanentDeleteBot(t *testing.T) { }) require.Nil(t, err) - require.Nil(t, th.App.PermanentDeleteBot(bot.UserId)) + require.Nil(t, th.App.PermanentDeleteBot(th.Context, bot.UserId)) - _, err = th.App.GetBot(bot.UserId, false) + _, err = th.App.GetBot(th.Context, bot.UserId, false) require.NotNil(t, err) require.Equal(t, "store.sql_bot.get.missing.app_error", err.Id) } @@ -539,7 +539,7 @@ func TestDisableUserBots(t *testing.T) { bots := []*model.Bot{} defer func() { for _, bot := range bots { - th.App.PermanentDeleteBot(bot.UserId) + th.App.PermanentDeleteBot(th.Context, bot.UserId) } }() @@ -560,20 +560,20 @@ func TestDisableUserBots(t *testing.T) { OwnerId: ownerId2, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(u2bot1.UserId) + defer th.App.PermanentDeleteBot(th.Context, u2bot1.UserId) err = th.App.disableUserBots(th.Context, ownerId1) require.Nil(t, err) // Check all bots and corresponding users are disabled for creator 1 for _, bot := range bots { - retbot, err2 := th.App.GetBot(bot.UserId, true) + retbot, err2 := th.App.GetBot(th.Context, bot.UserId, true) require.Nil(t, err2) require.NotZero(t, retbot.DeleteAt, bot.Username) } // Check bots and corresponding user not disabled for creator 2 - bot, err := th.App.GetBot(u2bot1.UserId, true) + bot, err := th.App.GetBot(th.Context, u2bot1.UserId, true) require.Nil(t, err) require.Zero(t, bot.DeleteAt) @@ -593,7 +593,7 @@ func TestNotifySysadminsBotOwnerDisabled(t *testing.T) { userBots := []*model.Bot{} defer func() { for _, bot := range userBots { - th.App.PermanentDeleteBot(bot.UserId) + th.App.PermanentDeleteBot(th.Context, bot.UserId) } }() @@ -725,7 +725,7 @@ func TestConvertUserToBot(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - _, err := th.App.ConvertUserToBot(&model.User{ + _, err := th.App.ConvertUserToBot(th.Context, &model.User{ Username: "username", Id: "", }) @@ -737,7 +737,7 @@ func TestConvertUserToBot(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - _, err := th.App.ConvertUserToBot(&model.User{ + _, err := th.App.ConvertUserToBot(th.Context, &model.User{ Username: "invalid username", Id: th.BasicUser.Id, }) @@ -750,12 +750,12 @@ func TestConvertUserToBot(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - bot, err := th.App.ConvertUserToBot(&model.User{ + bot, err := th.App.ConvertUserToBot(th.Context, &model.User{ Username: "username", Id: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) assert.Equal(t, "username", bot.Username) assert.Equal(t, th.BasicUser.Id, bot.OwnerId) }) diff --git a/server/channels/app/channel_test.go b/server/channels/app/channel_test.go index a9f42b5c55..5968f18704 100644 --- a/server/channels/app/channel_test.go +++ b/server/channels/app/channel_test.go @@ -1580,7 +1580,7 @@ func TestAddUserToChannel(t *testing.T) { defer th.App.PermanentDeleteUser(th.Context, &user1) bot := th.CreateBot() botUser, _ := th.App.GetUser(bot.UserId) - defer th.App.PermanentDeleteBot(botUser.Id) + defer th.App.PermanentDeleteBot(th.Context, botUser.Id) th.App.AddTeamMember(th.Context, th.BasicTeam.Id, ruser1.Id) th.App.AddTeamMember(th.Context, th.BasicTeam.Id, bot.UserId) @@ -1662,7 +1662,7 @@ func TestRemoveUserFromChannel(t *testing.T) { bot := th.CreateBot() botUser, _ := th.App.GetUser(bot.UserId) - defer th.App.PermanentDeleteBot(botUser.Id) + defer th.App.PermanentDeleteBot(th.Context, botUser.Id) th.App.AddTeamMember(th.Context, th.BasicTeam.Id, ruser.Id) th.App.AddTeamMember(th.Context, th.BasicTeam.Id, bot.UserId) diff --git a/server/channels/app/helper_test.go b/server/channels/app/helper_test.go index 6a46e11f6d..c494501f33 100644 --- a/server/channels/app/helper_test.go +++ b/server/channels/app/helper_test.go @@ -278,9 +278,9 @@ func (th *TestHelper) InitBasic() *TestHelper { } func (th *TestHelper) DeleteBots() *TestHelper { - preexistingBots, _ := th.App.GetBots(&model.BotGetOptions{Page: 0, PerPage: 100}) + preexistingBots, _ := th.App.GetBots(th.Context, &model.BotGetOptions{Page: 0, PerPage: 100}) for _, bot := range preexistingBots { - th.App.PermanentDeleteBot(bot.UserId) + th.App.PermanentDeleteBot(th.Context, bot.UserId) } return th } diff --git a/server/channels/app/limits_test.go b/server/channels/app/limits_test.go index fb74310487..72d36bc39a 100644 --- a/server/channels/app/limits_test.go +++ b/server/channels/app/limits_test.go @@ -114,7 +114,7 @@ func TestGetUserLimits(t *testing.T) { require.Equal(t, int64(3), userLimits.ActiveUserCount) // now we'll delete the bot - _ = th.App.PermanentDeleteBot(newBot.UserId) + _ = th.App.PermanentDeleteBot(th.Context, newBot.UserId) userLimits, appErr = th.App.GetUserLimits() require.Nil(t, appErr) require.Equal(t, int64(3), userLimits.ActiveUserCount) diff --git a/server/channels/app/notification_test.go b/server/channels/app/notification_test.go index 0dfa677013..424cac692f 100644 --- a/server/channels/app/notification_test.go +++ b/server/channels/app/notification_test.go @@ -2463,7 +2463,7 @@ func TestUserAllowsEmail(t *testing.T) { t.Run("should return false in the case user is a bot", func(t *testing.T) { user := th.CreateUser() - th.App.ConvertUserToBot(user) + th.App.ConvertUserToBot(th.Context, user) channelMemberNotifcationProps := model.StringMap{ model.EmailNotifyProp: model.ChannelNotifyDefault, diff --git a/server/channels/app/opentracing/opentracing_layer.go b/server/channels/app/opentracing/opentracing_layer.go index c214c7d542..78bc11b6fd 100644 --- a/server/channels/app/opentracing/opentracing_layer.go +++ b/server/channels/app/opentracing/opentracing_layer.go @@ -1884,7 +1884,7 @@ func (a *OpenTracingAppLayer) ConvertGroupMessageToChannel(c request.CTX, conver return resultVar0, resultVar1 } -func (a *OpenTracingAppLayer) ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError) { +func (a *OpenTracingAppLayer) ConvertUserToBot(rctx request.CTX, user *model.User) (*model.Bot, *model.AppError) { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.ConvertUserToBot") @@ -1896,7 +1896,7 @@ func (a *OpenTracingAppLayer) ConvertUserToBot(user *model.User) (*model.Bot, *m }() defer span.Finish() - resultVar0, resultVar1 := a.app.ConvertUserToBot(user) + resultVar0, resultVar1 := a.app.ConvertUserToBot(rctx, user) if resultVar1 != nil { span.LogFields(spanlog.Error(resultVar1)) @@ -1950,7 +1950,7 @@ func (a *OpenTracingAppLayer) CopyWranglerPostlist(c request.CTX, wpl *model.Wra return resultVar0, resultVar1 } -func (a *OpenTracingAppLayer) CreateBot(c request.CTX, bot *model.Bot) (*model.Bot, *model.AppError) { +func (a *OpenTracingAppLayer) CreateBot(rctx request.CTX, bot *model.Bot) (*model.Bot, *model.AppError) { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.CreateBot") @@ -1962,7 +1962,7 @@ func (a *OpenTracingAppLayer) CreateBot(c request.CTX, bot *model.Bot) (*model.B }() defer span.Finish() - resultVar0, resultVar1 := a.app.CreateBot(c, bot) + resultVar0, resultVar1 := a.app.CreateBot(rctx, bot) if resultVar1 != nil { span.LogFields(spanlog.Error(resultVar1)) @@ -5138,7 +5138,7 @@ func (a *OpenTracingAppLayer) GetAuthorizedAppsForUser(userID string, page int, return resultVar0, resultVar1 } -func (a *OpenTracingAppLayer) GetBot(botUserId string, includeDeleted bool) (*model.Bot, *model.AppError) { +func (a *OpenTracingAppLayer) GetBot(rctx request.CTX, botUserId string, includeDeleted bool) (*model.Bot, *model.AppError) { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetBot") @@ -5150,7 +5150,7 @@ func (a *OpenTracingAppLayer) GetBot(botUserId string, includeDeleted bool) (*mo }() defer span.Finish() - resultVar0, resultVar1 := a.app.GetBot(botUserId, includeDeleted) + resultVar0, resultVar1 := a.app.GetBot(rctx, botUserId, includeDeleted) if resultVar1 != nil { span.LogFields(spanlog.Error(resultVar1)) @@ -5160,7 +5160,7 @@ func (a *OpenTracingAppLayer) GetBot(botUserId string, includeDeleted bool) (*mo return resultVar0, resultVar1 } -func (a *OpenTracingAppLayer) GetBots(options *model.BotGetOptions) (model.BotList, *model.AppError) { +func (a *OpenTracingAppLayer) GetBots(rctx request.CTX, options *model.BotGetOptions) (model.BotList, *model.AppError) { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetBots") @@ -5172,7 +5172,7 @@ func (a *OpenTracingAppLayer) GetBots(options *model.BotGetOptions) (model.BotLi }() defer span.Finish() - resultVar0, resultVar1 := a.app.GetBots(options) + resultVar0, resultVar1 := a.app.GetBots(rctx, options) if resultVar1 != nil { span.LogFields(spanlog.Error(resultVar1)) @@ -13276,7 +13276,7 @@ func (a *OpenTracingAppLayer) PermanentDeleteAllUsers(c request.CTX) *model.AppE return resultVar0 } -func (a *OpenTracingAppLayer) PermanentDeleteBot(botUserId string) *model.AppError { +func (a *OpenTracingAppLayer) PermanentDeleteBot(rctx request.CTX, botUserId string) *model.AppError { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.PermanentDeleteBot") @@ -13288,7 +13288,7 @@ func (a *OpenTracingAppLayer) PermanentDeleteBot(botUserId string) *model.AppErr }() defer span.Finish() - resultVar0 := a.app.PermanentDeleteBot(botUserId) + resultVar0 := a.app.PermanentDeleteBot(rctx, botUserId) if resultVar0 != nil { span.LogFields(spanlog.Error(resultVar0)) @@ -16150,7 +16150,7 @@ func (a *OpenTracingAppLayer) SessionHasPermissionToGroup(session model.Session, return resultVar0 } -func (a *OpenTracingAppLayer) SessionHasPermissionToManageBot(session model.Session, botUserId string) *model.AppError { +func (a *OpenTracingAppLayer) SessionHasPermissionToManageBot(rctx request.CTX, session model.Session, botUserId string) *model.AppError { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SessionHasPermissionToManageBot") @@ -16162,7 +16162,7 @@ func (a *OpenTracingAppLayer) SessionHasPermissionToManageBot(session model.Sess }() defer span.Finish() - resultVar0 := a.app.SessionHasPermissionToManageBot(session, botUserId) + resultVar0 := a.app.SessionHasPermissionToManageBot(rctx, session, botUserId) if resultVar0 != nil { span.LogFields(spanlog.Error(resultVar0)) @@ -16240,7 +16240,7 @@ func (a *OpenTracingAppLayer) SessionHasPermissionToUser(session model.Session, return resultVar0 } -func (a *OpenTracingAppLayer) SessionHasPermissionToUserOrBot(session model.Session, userID string) bool { +func (a *OpenTracingAppLayer) SessionHasPermissionToUserOrBot(rctx request.CTX, session model.Session, userID string) bool { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SessionHasPermissionToUserOrBot") @@ -16252,7 +16252,7 @@ func (a *OpenTracingAppLayer) SessionHasPermissionToUserOrBot(session model.Sess }() defer span.Finish() - resultVar0 := a.app.SessionHasPermissionToUserOrBot(session, userID) + resultVar0 := a.app.SessionHasPermissionToUserOrBot(rctx, session, userID) return resultVar0 } @@ -17496,7 +17496,7 @@ func (a *OpenTracingAppLayer) UpdateActive(c request.CTX, user *model.User, acti return resultVar0, resultVar1 } -func (a *OpenTracingAppLayer) UpdateBotActive(c request.CTX, botUserId string, active bool) (*model.Bot, *model.AppError) { +func (a *OpenTracingAppLayer) UpdateBotActive(rctx request.CTX, botUserId string, active bool) (*model.Bot, *model.AppError) { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.UpdateBotActive") @@ -17508,7 +17508,7 @@ func (a *OpenTracingAppLayer) UpdateBotActive(c request.CTX, botUserId string, a }() defer span.Finish() - resultVar0, resultVar1 := a.app.UpdateBotActive(c, botUserId, active) + resultVar0, resultVar1 := a.app.UpdateBotActive(rctx, botUserId, active) if resultVar1 != nil { span.LogFields(spanlog.Error(resultVar1)) @@ -17518,7 +17518,7 @@ func (a *OpenTracingAppLayer) UpdateBotActive(c request.CTX, botUserId string, a return resultVar0, resultVar1 } -func (a *OpenTracingAppLayer) UpdateBotOwner(botUserId string, newOwnerId string) (*model.Bot, *model.AppError) { +func (a *OpenTracingAppLayer) UpdateBotOwner(rctx request.CTX, botUserId string, newOwnerId string) (*model.Bot, *model.AppError) { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.UpdateBotOwner") @@ -17530,7 +17530,7 @@ func (a *OpenTracingAppLayer) UpdateBotOwner(botUserId string, newOwnerId string }() defer span.Finish() - resultVar0, resultVar1 := a.app.UpdateBotOwner(botUserId, newOwnerId) + resultVar0, resultVar1 := a.app.UpdateBotOwner(rctx, botUserId, newOwnerId) if resultVar1 != nil { span.LogFields(spanlog.Error(resultVar1)) diff --git a/server/channels/app/plugin_api.go b/server/channels/app/plugin_api.go index 6ee6dc52b2..33ed501bbc 100644 --- a/server/channels/app/plugin_api.go +++ b/server/channels/app/plugin_api.go @@ -1027,11 +1027,11 @@ func (api *PluginAPI) PatchBot(userID string, botPatch *model.BotPatch) (*model. } func (api *PluginAPI) GetBot(userID string, includeDeleted bool) (*model.Bot, *model.AppError) { - return api.app.GetBot(userID, includeDeleted) + return api.app.GetBot(api.ctx, userID, includeDeleted) } func (api *PluginAPI) GetBots(options *model.BotGetOptions) ([]*model.Bot, *model.AppError) { - bots, err := api.app.GetBots(options) + bots, err := api.app.GetBots(api.ctx, options) return []*model.Bot(bots), err } @@ -1041,7 +1041,7 @@ func (api *PluginAPI) UpdateBotActive(userID string, active bool) (*model.Bot, * } func (api *PluginAPI) PermanentDeleteBot(userID string) *model.AppError { - return api.app.PermanentDeleteBot(userID) + return api.app.PermanentDeleteBot(api.ctx, userID) } func (api *PluginAPI) EnsureBotUser(bot *model.Bot) (string, error) { diff --git a/server/channels/app/user_test.go b/server/channels/app/user_test.go index 569c5e3081..e6cdbf5b1e 100644 --- a/server/channels/app/user_test.go +++ b/server/channels/app/user_test.go @@ -353,7 +353,7 @@ func TestUpdateActiveBotsSideEffect(t *testing.T) { OwnerId: th.BasicUser.Id, }) require.Nil(t, err) - defer th.App.PermanentDeleteBot(bot.UserId) + defer th.App.PermanentDeleteBot(th.Context, bot.UserId) // Automatic deactivation disabled th.App.UpdateConfig(func(cfg *model.Config) { @@ -362,7 +362,7 @@ func TestUpdateActiveBotsSideEffect(t *testing.T) { th.App.UpdateActive(th.Context, th.BasicUser, false) - retbot1, err := th.App.GetBot(bot.UserId, true) + retbot1, err := th.App.GetBot(th.Context, bot.UserId, true) require.Nil(t, err) require.Zero(t, retbot1.DeleteAt) user1, err := th.App.GetUser(bot.UserId) @@ -378,7 +378,7 @@ func TestUpdateActiveBotsSideEffect(t *testing.T) { th.App.UpdateActive(th.Context, th.BasicUser, false) - retbot2, err := th.App.GetBot(bot.UserId, true) + retbot2, err := th.App.GetBot(th.Context, bot.UserId, true) require.Nil(t, err) require.NotZero(t, retbot2.DeleteAt) user2, err := th.App.GetUser(bot.UserId) diff --git a/server/cmd/mmctl/commands/bot_e2e_test.go b/server/cmd/mmctl/commands/bot_e2e_test.go index 30e926bbaa..e0200e9503 100644 --- a/server/cmd/mmctl/commands/bot_e2e_test.go +++ b/server/cmd/mmctl/commands/bot_e2e_test.go @@ -22,14 +22,14 @@ func (s *MmctlE2ETestSuite) TestListBotCmdF() { bot, appErr := s.th.App.CreateBot(s.th.Context, &model.Bot{Username: model.NewId(), OwnerId: s.th.BasicUser.Id}) s.Require().Nil(appErr) defer func() { - err := s.th.App.PermanentDeleteBot(bot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, bot.UserId) s.Require().Nil(err) }() deletedBot, appErr := s.th.App.CreateBot(s.th.Context, &model.Bot{Username: model.NewId(), OwnerId: s.th.BasicUser.Id}) s.Require().Nil(appErr) defer func() { - err := s.th.App.PermanentDeleteBot(deletedBot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, deletedBot.UserId) s.Require().Nil(err) }() @@ -59,14 +59,14 @@ func (s *MmctlE2ETestSuite) TestListBotCmdF() { bot, appErr := s.th.App.CreateBot(s.th.Context, &model.Bot{Username: model.NewId(), OwnerId: s.th.BasicUser.Id}) s.Require().Nil(appErr) defer func() { - err := s.th.App.PermanentDeleteBot(bot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, bot.UserId) s.Require().Nil(err) }() deletedBot, appErr := s.th.App.CreateBot(s.th.Context, &model.Bot{Username: model.NewId(), OwnerId: user.Id}) s.Require().Nil(appErr) defer func() { - err := s.th.App.PermanentDeleteBot(deletedBot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, deletedBot.UserId) s.Require().Nil(err) }() @@ -76,7 +76,7 @@ func (s *MmctlE2ETestSuite) TestListBotCmdF() { orphanBot, appErr := s.th.App.CreateBot(s.th.Context, &model.Bot{Username: model.NewId(), OwnerId: user.Id}) s.Require().Nil(appErr) defer func() { - err := s.th.App.PermanentDeleteBot(orphanBot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, orphanBot.UserId) s.Require().Nil(err) }() @@ -106,21 +106,21 @@ func (s *MmctlE2ETestSuite) TestListBotCmdF() { bot, appErr := s.th.App.CreateBot(s.th.Context, &model.Bot{Username: model.NewId(), OwnerId: s.th.BasicUser2.Id}) s.Require().Nil(appErr) defer func() { - err := s.th.App.PermanentDeleteBot(bot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, bot.UserId) s.Require().Nil(err) }() orphanBot, appErr := s.th.App.CreateBot(s.th.Context, &model.Bot{Username: model.NewId(), OwnerId: user.Id}) s.Require().Nil(appErr) defer func() { - err := s.th.App.PermanentDeleteBot(orphanBot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, orphanBot.UserId) s.Require().Nil(err) }() deletedBot, appErr := s.th.App.CreateBot(s.th.Context, &model.Bot{Username: model.NewId(), OwnerId: s.th.BasicUser2.Id}) s.Require().Nil(appErr) defer func() { - err := s.th.App.PermanentDeleteBot(deletedBot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, deletedBot.UserId) s.Require().Nil(err) }() @@ -181,7 +181,7 @@ func (s *MmctlE2ETestSuite) TestBotEnableCmd() { s.Require().Equal(newBot.Username, printedBot.Username) s.Require().Equal(newBot.OwnerId, printedBot.OwnerId) - bot, appErr := s.th.App.GetBot(newBot.UserId, false) + bot, appErr := s.th.App.GetBot(s.th.Context, newBot.UserId, false) s.Require().Nil(appErr) s.Require().Equal(newBot.UserId, bot.UserId) s.Require().Equal(newBot.Username, bot.Username) @@ -235,7 +235,7 @@ func (s *MmctlE2ETestSuite) TestBotEnableCmd() { s.Require().Equal(newBot.Username, printedBot.Username) s.Require().Equal(newBot.OwnerId, printedBot.OwnerId) - bot, appErr := s.th.App.GetBot(newBot.UserId, false) + bot, appErr := s.th.App.GetBot(s.th.Context, newBot.UserId, false) s.Require().Nil(appErr) s.Require().Equal(newBot.UserId, bot.UserId) s.Require().Equal(newBot.Username, bot.Username) @@ -269,7 +269,7 @@ func (s *MmctlE2ETestSuite) TestBotDisableCmd() { s.Require().Equal(newBot.Username, printedBot.Username) s.Require().Equal(newBot.OwnerId, printedBot.OwnerId) - _, appErr = s.th.App.GetBot(newBot.UserId, false) + _, appErr = s.th.App.GetBot(s.th.Context, newBot.UserId, false) s.Require().NotNil(appErr) s.Require().Equal("store.sql_bot.get.missing.app_error", appErr.Id) }) @@ -321,7 +321,7 @@ func (s *MmctlE2ETestSuite) TestBotDisableCmd() { s.Require().Equal(newBot.Username, printedBot.Username) s.Require().Equal(newBot.OwnerId, printedBot.OwnerId) - _, appErr = s.th.App.GetBot(newBot.UserId, false) + _, appErr = s.th.App.GetBot(s.th.Context, newBot.UserId, false) s.Require().NotNil(appErr) s.Require().Equal("store.sql_bot.get.missing.app_error", appErr.Id) }) @@ -351,7 +351,7 @@ func (s *MmctlE2ETestSuite) TestBotAssignCmdF() { s.Require().Nil(appErr) s.Require().Equal(bot.OwnerId, botOwner.Id) defer func() { - err := s.th.App.PermanentDeleteBot(bot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, bot.UserId) s.Require().Nil(err) }() @@ -384,7 +384,7 @@ func (s *MmctlE2ETestSuite) TestBotAssignCmdF() { s.Require().Nil(appErr) s.Require().Equal(bot.OwnerId, botOwner.Id) defer func() { - err := s.th.App.PermanentDeleteBot(bot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, bot.UserId) s.Require().Nil(err) }() @@ -420,7 +420,7 @@ func (s *MmctlE2ETestSuite) TestBotCreateCmdF() { bot, ok := printer.GetLines()[0].(*model.Bot) s.Require().True(ok) defer func() { - err := s.th.App.PermanentDeleteBot(bot.UserId) + err := s.th.App.PermanentDeleteBot(s.th.Context, bot.UserId) s.Require().Nil(err) }() token, ok := printer.GetLines()[1].(*model.UserAccessToken)