Fix username collision for shared channels sync update (#25578)

* fix username collision for shared channels sync update

* ensure both return types are handled

* remove unused interface
This commit is contained in:
Doug Lauder 2023-12-01 11:32:14 -05:00 committed by GitHub
parent f6d41bcf5b
commit 57a87d8ca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 11 deletions

View File

@ -70,11 +70,6 @@ type errNotFound interface {
IsErrNotFound() bool
}
// errInvalidInput allows checking against Store.ErrInvalidInput errors without making Store a dependency.
type errInvalidInput interface {
InvalidInputInfo() (entity string, field string, value any)
}
// Service provides shared channel synchronization.
type Service struct {
server ServerIface

View File

@ -248,16 +248,16 @@ func (scs *Service) insertSyncUser(user *model.User, channel *model.Channel, rc
user.Email = mungEmail(rc.Name, model.UserEmailMaxLength)
if userSaved, err = scs.server.GetStore().User().Save(user); err != nil {
e, ok := err.(errInvalidInput)
field, ok := isConflictError(err)
if !ok {
break
}
_, field, value := e.InvalidInputInfo()
if field == "email" || field == "username" {
// username or email collision; try again with different suffix
scs.server.Log().Log(mlog.LvlSharedChannelServiceWarn, "Collision inserting sync user",
mlog.String("field", field),
mlog.Any("value", value),
mlog.String("username", user.Username),
mlog.String("email", user.Email),
mlog.Int("attempt", i),
mlog.Err(err),
)
@ -302,16 +302,16 @@ func (scs *Service) updateSyncUser(patch *model.UserPatch, user *model.User, cha
user.Email = mungEmail(rc.Name, model.UserEmailMaxLength)
if update, err = scs.server.GetStore().User().Update(user, false); err != nil {
e, ok := err.(errInvalidInput)
field, ok := isConflictError(err)
if !ok {
break
}
_, field, value := e.InvalidInputInfo()
if field == "email" || field == "username" {
// username or email collision; try again with different suffix
scs.server.Log().Log(mlog.LvlSharedChannelServiceWarn, "Collision updating sync user",
mlog.String("field", field),
mlog.Any("value", value),
mlog.String("username", user.Username),
mlog.String("email", user.Email),
mlog.Int("attempt", i),
mlog.Err(err),
)

View File

@ -4,10 +4,12 @@
package sharedchannel
import (
"errors"
"fmt"
"strings"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/store"
)
// fixMention replaces any mentions in a post for the user with the user's real username.
@ -99,3 +101,17 @@ func mungEmail(remotename string, maxLen int) string {
}
return s
}
func isConflictError(err error) (string, bool) {
var errConflict *store.ErrConflict
if errors.As(err, &errConflict) {
return strings.ToLower(errConflict.Resource), true
}
var errInput *store.ErrInvalidInput
if errors.As(err, &errInput) {
_, field, _ := errInput.InvalidInputInfo()
return strings.ToLower(field), true
}
return "", false
}