-
Notifications
You must be signed in to change notification settings - Fork 0
/
mattermost_adapter.go
148 lines (120 loc) · 3.8 KB
/
mattermost_adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"github.com/mattermost/mattermost-server/model"
"log"
"strconv"
"strings"
)
func (auth *AuthenticatorWithSync) getAllOAuthUsers() ([]*model.User, error) {
curPage := 0
terminate := false
var result []*model.User
for !terminate {
users, resp := auth.Mattermost().GetUsers(curPage, 50, "")
if resp.Error != nil {
return nil, resp.Error
}
for _, user := range users {
if user.AuthService == model.USER_AUTH_SERVICE_GITLAB {
result = append(result, user)
}
}
curPage++
terminate = len(users) == 0
}
return result, nil
}
func (auth *AuthenticatorWithSync) syncOAuthUsersWithBackend([]*model.User) error {
users, err := auth.getAllOAuthUsers()
if err != nil {
return err
}
for _, user := range users {
// strip off username prefix to obtain the actual username to feed into backend
username := strings.Replace(user.Username, auth.transformer.UsernamePrefix, "", 1)
log.Printf("Syncing user %s with backend.\n", username)
auth.syncMattermostForUser(username)
}
return nil
}
func (auth *AuthenticatorWithSync) syncAllOAuthUsers() {
users, err := auth.getAllOAuthUsers()
if err != nil {
log.Printf("Error while syncing all OAuth users: %+v", err)
}
auth.syncOAuthUsersWithBackend(users)
}
func (auth *AuthenticatorWithSync) checkMattermostUser(id int64, username, name, mail string) {
user, resp := auth.Mattermost().GetUserByEmail(mail, "")
if resp.Error != nil && resp.StatusCode != 404 {
log.Printf("ERROR: %+v", resp.Error)
return
}
created := false
userID := strconv.FormatInt(id, 10)
if resp.StatusCode == 404 {
log.Println("Creating new user.")
// auth user does not exist
var newUser model.User
newUser.AuthService = model.USER_AUTH_SERVICE_GITLAB
newUser.AuthData = &userID
newUser.Email = mail
newUser.FirstName = name
newUser.Username = username
newUser.EmailVerified = true
user, resp = auth.Mattermost().CreateUser(&newUser)
if resp.Error != nil {
log.Printf("Could not create user with email %s, got error: %+v.", mail, resp.Error)
return
}
created = true
}
// Update user if not just created
if !created {
var patch model.UserPatch
patch.Username = &username
patch.Email = &mail
patch.FirstName = &strings.Split(name, " ")[0]
if len(strings.Split(name, " ")) > 1 {
patch.LastName = &strings.Split(name, " ")[1]
}
_, resp = auth.Mattermost().PatchUser(user.Id, &patch)
if resp.Error != nil {
log.Printf("Could not update existing user, got Error %+v", resp.Error)
return
}
}
}
func (auth *AuthenticatorWithSync) checkGroupForMattermostUser(group group, mail string) {
group.uid = strings.Replace(group.uid, "_", "-", -1)
team, resp := auth.Mattermost().GetTeamByName(group.uid, "")
if resp.Error != nil && resp.StatusCode != 404 {
log.Printf("ERROR: Could not find team %+v, got error: %+v.", group, resp.Error)
}
if resp.StatusCode == 404 {
newTeam := model.Team{}
newTeam.Name = auth.normalizeGroupName(group.uid)
newTeam.DisplayName = group.name
newTeam.Type = "I"
team, resp = auth.Mattermost().CreateTeam(&newTeam)
if resp.Error != nil {
log.Printf("ERROR: Could not create Team %+v, got error %+v", group, resp.Error)
return
}
log.Printf("Created new Team %s.\n", team.DisplayName)
}
user, userResp := auth.Mattermost().GetUserByEmail(mail, "")
if userResp.Error != nil {
log.Printf("ERROR: Could not fetch user when adding to team %+v, got error: %+v", group, userResp.Error)
return
}
_, err := auth.Mattermost().AddTeamMember(team.Id, user.Id)
if err.Error != nil {
log.Printf("ERROR: Could add user to team %+v, got error: %+v", group, err.Error)
return
}
log.Printf("Added user %s to team %s \n", user.Email, team.DisplayName)
}
func (auth *AuthenticatorWithSync) normalizeGroupName(name string) string {
return strings.Replace(name, "_", "-", -1)
}