From 7bba8db65f5c56050ed3e5d9e1384d842d8948ca Mon Sep 17 00:00:00 2001 From: Christopher Poile Date: Wed, 3 Jun 2020 09:27:00 -0400 Subject: [PATCH] MM-25595 - Edits to bot username and display name fail to save (#14699) * invalidate the user profile cache bc we know it's now outdated * invalidate cache when updating user roles; test * tests fix -- experimental * fixing linter errors * revert to original solution Co-authored-by: mattermod --- api4/bot_test.go | 16 ++++++++++++++++ app/bot.go | 1 + app/user.go | 6 +++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/api4/bot_test.go b/api4/bot_test.go index a5c8ef9e68..0ff029c948 100644 --- a/api4/bot_test.go +++ b/api4/bot_test.go @@ -220,6 +220,22 @@ func TestPatchBot(t *testing.T) { require.Equal(t, *botPatch.DisplayName, patchedBot.DisplayName) require.Equal(t, *botPatch.Description, patchedBot.Description) require.Equal(t, th.SystemAdminUser.Id, patchedBot.OwnerId) + + // Continue through the bot update process (call UpdateUserRoles), then + // get the bot, to make sure the patched bot was correctly saved. + th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.TEAM_USER_ROLE_ID) + th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID) + th.AddPermissionToRole(model.PERMISSION_MANAGE_ROLES.Id, model.TEAM_USER_ROLE_ID) + th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false) + + success, resp := th.Client.UpdateUserRoles(createdBot.UserId, model.SYSTEM_USER_ROLE_ID) + CheckOKStatus(t, resp) + require.True(t, success) + + bots, resp := th.Client.GetBots(0, 2, "") + CheckOKStatus(t, resp) + require.Len(t, bots, 1) + require.Equal(t, []*model.Bot{patchedBot}, bots) }) t.Run("patch my bot without permission", func(t *testing.T) { diff --git a/app/bot.go b/app/bot.go index 09f96d3262..9b53a4e5da 100644 --- a/app/bot.go +++ b/app/bot.go @@ -89,6 +89,7 @@ func (a *App) PatchBot(botUserId string, botPatch *model.BotPatch) (*model.Bot, if err != nil { return nil, err } + a.InvalidateCacheForUser(user.Id) ruser := userUpdate.New a.sendUpdatedUserEvent(*ruser) diff --git a/app/user.go b/app/user.go index 62c057e948..5fd8dcc042 100644 --- a/app/user.go +++ b/app/user.go @@ -1395,8 +1395,8 @@ func (a *App) UpdateUserRoles(userId string, newRoles string, sendWebSocketEvent schan := make(chan store.StoreResult, 1) go func() { - userId, err := a.Srv().Store.Session().UpdateRoles(user.Id, newRoles) - schan <- store.StoreResult{Data: userId, Err: err} + id, err := a.Srv().Store.Session().UpdateRoles(user.Id, newRoles) + schan <- store.StoreResult{Data: id, Err: err} close(schan) }() @@ -1411,7 +1411,7 @@ func (a *App) UpdateUserRoles(userId string, newRoles string, sendWebSocketEvent mlog.Error("Failed during updating user roles", mlog.Err(result.Err)) } - a.InvalidateCacheForUser(user.Id) + a.InvalidateCacheForUser(userId) a.ClearSessionCacheForUser(user.Id) if sendWebSocketEvent {