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
Этот коммит содержится в:
Doug Lauder
2023-12-01 11:32:14 -05:00
коммит произвёл GitHub
родитель f6d41bcf5b
Коммит 57a87d8ca5
3 изменённых файлов: 22 добавлений и 11 удалений

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

@@ -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

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

@@ -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),
)

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

@@ -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
}