Deactivating and invalidating sessions of guest users on guest disable (#13007)
Automatic Merge
Этот коммит содержится в:
коммит произвёл
mattermod
родитель
6a75d2fc68
Коммит
7031f51b41
@@ -261,6 +261,21 @@ func NewServer(options ...Option) (*Server, error) {
|
||||
s.StartElasticsearch()
|
||||
}
|
||||
|
||||
s.AddConfigListener(func(oldConfig *model.Config, newConfig *model.Config) {
|
||||
if *oldConfig.GuestAccountsSettings.Enable && !*newConfig.GuestAccountsSettings.Enable {
|
||||
if appErr := s.FakeApp().DeactivateGuests(); appErr != nil {
|
||||
mlog.Error("Unable to deactivate guest accounts", mlog.Err(appErr))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Disable active guest accounts on first run if guest accounts are disabled
|
||||
if !*s.Config().GuestAccountsSettings.Enable {
|
||||
if appErr := s.FakeApp().DeactivateGuests(); appErr != nil {
|
||||
mlog.Error("Unable to deactivate guest accounts", mlog.Err(appErr))
|
||||
}
|
||||
}
|
||||
|
||||
s.initJobs()
|
||||
|
||||
if s.runjobs {
|
||||
|
||||
39
app/user.go
39
app/user.go
@@ -943,28 +943,28 @@ func (a *App) UpdatePasswordAsUser(userId, currentPassword, newPassword string)
|
||||
return a.UpdatePasswordSendEmail(user, newPassword, T("api.user.update_password.menu"))
|
||||
}
|
||||
|
||||
func (a *App) userDeactivated(user *model.User) *model.AppError {
|
||||
if err := a.RevokeAllSessions(user.Id); err != nil {
|
||||
func (a *App) userDeactivated(userId string) *model.AppError {
|
||||
if err := a.RevokeAllSessions(userId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.SetStatusOffline(user.Id, false)
|
||||
a.SetStatusOffline(userId, false)
|
||||
|
||||
if *a.Config().ServiceSettings.DisableBotsWhenOwnerIsDeactivated {
|
||||
a.disableUserBots(user.Id)
|
||||
a.disableUserBots(userId)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) invalidateUserChannelMembersCaches(user *model.User) *model.AppError {
|
||||
teamsForUser, err := a.GetTeamsForUser(user.Id)
|
||||
func (a *App) invalidateUserChannelMembersCaches(userId string) *model.AppError {
|
||||
teamsForUser, err := a.GetTeamsForUser(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, team := range teamsForUser {
|
||||
channelsForUser, err := a.GetChannelsForUser(team.Id, user.Id, false)
|
||||
channelsForUser, err := a.GetChannelsForUser(team.Id, userId, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -992,12 +992,12 @@ func (a *App) UpdateActive(user *model.User, active bool) (*model.User, *model.A
|
||||
ruser := userUpdate.New
|
||||
|
||||
if !active {
|
||||
if err := a.userDeactivated(ruser); err != nil {
|
||||
if err := a.userDeactivated(ruser.Id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
a.invalidateUserChannelMembersCaches(user)
|
||||
a.invalidateUserChannelMembersCaches(user.Id)
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
|
||||
a.sendUpdatedUserEvent(*ruser)
|
||||
@@ -1005,6 +1005,27 @@ func (a *App) UpdateActive(user *model.User, active bool) (*model.User, *model.A
|
||||
return ruser, nil
|
||||
}
|
||||
|
||||
func (a *App) DeactivateGuests() *model.AppError {
|
||||
userIds, err := a.Srv.Store.User().DeactivateGuests()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, userId := range userIds {
|
||||
if err := a.userDeactivated(userId); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
a.Srv.Store.Channel().ClearCaches()
|
||||
a.Srv.Store.User().ClearCaches()
|
||||
|
||||
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_GUESTS_DEACTIVATED, "", "", "", nil)
|
||||
a.Publish(message)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) GetSanitizeOptions(asAdmin bool) map[string]bool {
|
||||
options := a.Config().GetSanitizeOptions()
|
||||
if asAdmin {
|
||||
|
||||
@@ -1164,3 +1164,27 @@ func TestDemoteUserToGuest(t *testing.T) {
|
||||
assert.Len(t, *channelMembers, 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeactivateGuests(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
guest1 := th.CreateGuest()
|
||||
guest2 := th.CreateGuest()
|
||||
user := th.CreateUser()
|
||||
|
||||
err := th.App.DeactivateGuests()
|
||||
require.Nil(t, err)
|
||||
|
||||
guest1, err = th.App.GetUser(guest1.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.NotEqual(t, int64(0), guest1.DeleteAt)
|
||||
|
||||
guest2, err = th.App.GetUser(guest2.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.NotEqual(t, int64(0), guest2.DeleteAt)
|
||||
|
||||
user, err = th.App.GetUser(user.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(0), user.DeleteAt)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user