mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
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:
parent
f6d41bcf5b
commit
57a87d8ca5
@ -70,11 +70,6 @@ type errNotFound interface {
|
|||||||
IsErrNotFound() bool
|
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.
|
// Service provides shared channel synchronization.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
server ServerIface
|
server ServerIface
|
||||||
|
@ -248,16 +248,16 @@ func (scs *Service) insertSyncUser(user *model.User, channel *model.Channel, rc
|
|||||||
user.Email = mungEmail(rc.Name, model.UserEmailMaxLength)
|
user.Email = mungEmail(rc.Name, model.UserEmailMaxLength)
|
||||||
|
|
||||||
if userSaved, err = scs.server.GetStore().User().Save(user); err != nil {
|
if userSaved, err = scs.server.GetStore().User().Save(user); err != nil {
|
||||||
e, ok := err.(errInvalidInput)
|
field, ok := isConflictError(err)
|
||||||
if !ok {
|
if !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
_, field, value := e.InvalidInputInfo()
|
|
||||||
if field == "email" || field == "username" {
|
if field == "email" || field == "username" {
|
||||||
// username or email collision; try again with different suffix
|
// username or email collision; try again with different suffix
|
||||||
scs.server.Log().Log(mlog.LvlSharedChannelServiceWarn, "Collision inserting sync user",
|
scs.server.Log().Log(mlog.LvlSharedChannelServiceWarn, "Collision inserting sync user",
|
||||||
mlog.String("field", field),
|
mlog.String("field", field),
|
||||||
mlog.Any("value", value),
|
mlog.String("username", user.Username),
|
||||||
|
mlog.String("email", user.Email),
|
||||||
mlog.Int("attempt", i),
|
mlog.Int("attempt", i),
|
||||||
mlog.Err(err),
|
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)
|
user.Email = mungEmail(rc.Name, model.UserEmailMaxLength)
|
||||||
|
|
||||||
if update, err = scs.server.GetStore().User().Update(user, false); err != nil {
|
if update, err = scs.server.GetStore().User().Update(user, false); err != nil {
|
||||||
e, ok := err.(errInvalidInput)
|
field, ok := isConflictError(err)
|
||||||
if !ok {
|
if !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
_, field, value := e.InvalidInputInfo()
|
|
||||||
if field == "email" || field == "username" {
|
if field == "email" || field == "username" {
|
||||||
// username or email collision; try again with different suffix
|
// username or email collision; try again with different suffix
|
||||||
scs.server.Log().Log(mlog.LvlSharedChannelServiceWarn, "Collision updating sync user",
|
scs.server.Log().Log(mlog.LvlSharedChannelServiceWarn, "Collision updating sync user",
|
||||||
mlog.String("field", field),
|
mlog.String("field", field),
|
||||||
mlog.Any("value", value),
|
mlog.String("username", user.Username),
|
||||||
|
mlog.String("email", user.Email),
|
||||||
mlog.Int("attempt", i),
|
mlog.Int("attempt", i),
|
||||||
mlog.Err(err),
|
mlog.Err(err),
|
||||||
)
|
)
|
||||||
|
@ -4,10 +4,12 @@
|
|||||||
package sharedchannel
|
package sharedchannel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mattermost/mattermost/server/public/model"
|
"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.
|
// 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
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user