matterbridge/vendor/github.com/mattermost/platform/model/gitlab/gitlab.go

115 lines
2.2 KiB
Go
Raw Normal View History

2017-08-16 23:37:37 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2016-04-10 23:39:38 +02:00
// See License.txt for license information.
package oauthgitlab
import (
"encoding/json"
"io"
"strconv"
"strings"
2018-02-09 00:11:04 +01:00
"github.com/mattermost/mattermost-server/einterfaces"
"github.com/mattermost/mattermost-server/model"
2016-04-10 23:39:38 +02:00
)
type GitLabProvider struct {
}
type GitLabUser struct {
Id int64 `json:"id"`
Username string `json:"username"`
Login string `json:"login"`
Email string `json:"email"`
Name string `json:"name"`
}
func init() {
provider := &GitLabProvider{}
2016-05-15 23:02:30 +02:00
einterfaces.RegisterOauthProvider(model.USER_AUTH_SERVICE_GITLAB, provider)
2016-04-10 23:39:38 +02:00
}
func userFromGitLabUser(glu *GitLabUser) *model.User {
user := &model.User{}
username := glu.Username
if username == "" {
username = glu.Login
}
user.Username = model.CleanUsername(username)
splitName := strings.Split(glu.Name, " ")
if len(splitName) == 2 {
user.FirstName = splitName[0]
user.LastName = splitName[1]
} else if len(splitName) >= 2 {
user.FirstName = splitName[0]
user.LastName = strings.Join(splitName[1:], " ")
} else {
user.FirstName = glu.Name
}
user.Email = glu.Email
2016-06-23 20:28:05 +02:00
userId := strconv.FormatInt(glu.Id, 10)
user.AuthData = &userId
2016-05-15 23:02:30 +02:00
user.AuthService = model.USER_AUTH_SERVICE_GITLAB
2016-04-10 23:39:38 +02:00
return user
}
func gitLabUserFromJson(data io.Reader) *GitLabUser {
decoder := json.NewDecoder(data)
var glu GitLabUser
err := decoder.Decode(&glu)
if err == nil {
return &glu
} else {
return nil
}
}
2017-03-25 21:04:10 +01:00
func (glu *GitLabUser) ToJson() string {
b, err := json.Marshal(glu)
if err != nil {
return ""
} else {
return string(b)
}
}
2016-04-10 23:39:38 +02:00
func (glu *GitLabUser) IsValid() bool {
if glu.Id == 0 {
return false
}
if len(glu.Email) == 0 {
return false
}
return true
}
func (glu *GitLabUser) getAuthData() string {
return strconv.FormatInt(glu.Id, 10)
}
func (m *GitLabProvider) GetIdentifier() string {
2016-05-15 23:02:30 +02:00
return model.USER_AUTH_SERVICE_GITLAB
2016-04-10 23:39:38 +02:00
}
func (m *GitLabProvider) GetUserFromJson(data io.Reader) *model.User {
glu := gitLabUserFromJson(data)
if glu.IsValid() {
return userFromGitLabUser(glu)
}
return &model.User{}
}
func (m *GitLabProvider) GetAuthDataFromJson(data io.Reader) string {
glu := gitLabUserFromJson(data)
if glu.IsValid() {
return glu.getAuthData()
}
return ""
}