2021-09-13 21:36:36 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
package plugindelivery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
mm_model "github.com/mattermost/mattermost-server/v6/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
usernameSpecialChars = ".-_ "
|
|
|
|
)
|
|
|
|
|
2021-09-20 23:32:47 +02:00
|
|
|
func teamMemberFromUsername(api PluginAPI, username string, teamID string) (*mm_model.TeamMember, error) {
|
|
|
|
// check for usernames that might have trailing punctuation
|
|
|
|
var user *mm_model.User
|
|
|
|
var err error
|
|
|
|
ok := true
|
|
|
|
trimmed := username
|
|
|
|
for ok {
|
|
|
|
user, err = api.GetUserByUsername(trimmed)
|
|
|
|
if err != nil && !isErrNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
trimmed, ok = trimUsernameSpecialChar(trimmed)
|
2021-09-13 21:36:36 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 23:32:47 +02:00
|
|
|
if user == nil {
|
2021-09-13 21:36:36 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-20 23:32:47 +02:00
|
|
|
// make sure user is member of team.
|
|
|
|
member, err := api.GetTeamMember(teamID, user.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-09-13 21:36:36 +02:00
|
|
|
}
|
2021-09-20 23:32:47 +02:00
|
|
|
|
|
|
|
return member, nil
|
2021-09-13 21:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// trimUsernameSpecialChar tries to remove the last character from word if it
|
|
|
|
// is a special character for usernames (dot, dash or underscore). If not, it
|
|
|
|
// returns the same string.
|
|
|
|
func trimUsernameSpecialChar(word string) (string, bool) {
|
|
|
|
len := len(word)
|
|
|
|
|
|
|
|
if len > 0 && strings.LastIndexAny(word, usernameSpecialChars) == (len-1) {
|
|
|
|
return word[:len-1], true
|
|
|
|
}
|
|
|
|
|
|
|
|
return word, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isErrNotFound returns true if the error is a plugin.ErrNotFound. The pluginAPI converts
|
|
|
|
// AppError to the plugin.ErrNotFound var.
|
|
|
|
// TODO: add a `IsErrNotFound` method to the plugin API.
|
|
|
|
func isErrNotFound(err error) bool {
|
|
|
|
return err != nil && err.Error() == "not found"
|
|
|
|
}
|