/
user_admin.go
356 lines (314 loc) · 8.59 KB
/
user_admin.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ucauth
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/modules/openapi/conf"
"github.com/erda-project/erda/pkg/http/httpclient"
"github.com/erda-project/erda/pkg/strutil"
)
// User 用户中心用户数据结构
type User struct {
ID string `json:"user_id"`
Name string `json:"username"`
Nick string `json:"nickname"`
AvatarURL string `json:"avatar_url"`
Phone string `json:"phone_number"`
Email string `json:"email"`
State string `json:"state"`
}
type UcUser struct {
ID int `json:"user_id"`
Name string `json:"username"`
Nick string `json:"nickname"`
AvatarURL string `json:"avatar_url"`
Phone string `json:"phone_number"`
Email string `json:"email"`
}
type UserIDModel struct {
ID string
UserID string
}
// UCClient UC客户端\
type UCClient struct {
baseURL string
isOry bool
client *httpclient.HTTPClient
ucTokenAuth *UCTokenAuth
db *gorm.DB
}
func (c *UCClient) SetDBClient(db *gorm.DB) {
c.db = db
}
// NewUCClient 初始化UC客户端
func NewUCClient(baseURL, clientID, clientSecret string) *UCClient {
if clientID == OryCompatibleClientId {
// TODO: it's a hack
return &UCClient{
baseURL: baseURL,
isOry: true,
}
}
tokenAuth, err := NewUCTokenAuth(baseURL, clientID, clientSecret)
if err != nil {
panic(err)
}
return &UCClient{
baseURL: baseURL,
client: httpclient.New(),
ucTokenAuth: tokenAuth,
isOry: false,
}
}
func (c *UCClient) oryEnabled() bool {
return c.isOry
}
func (c *UCClient) oryKratosPrivateAddr() string {
return c.baseURL
}
// InvalidateServerToken 使 server token 失效
func (c *UCClient) InvalidateServerToken() {
c.ucTokenAuth.ExpireServerToken()
}
// FindUsers 根据用户ID查找用户信息
func (c *UCClient) FindUsers(ids []string) ([]User, error) {
if len(ids) == 0 {
return nil, nil
}
if c.oryEnabled() {
// get ordered uuid list
userIDs, userMap, err := c.ConvertUserIDs(ids)
if err != nil {
return nil, err
}
users, err := getUserByIDs(c.oryKratosPrivateAddr(), userIDs)
if err != nil {
return nil, err
}
// revert uuid to id for old uc users
for i, u := range users {
if userID, ok := userMap[u.ID]; ok {
users[i].ID = userID
}
}
return users, nil
}
parts := make([]string, len(ids))
for _, id := range ids {
parts = append(parts, "user_id:"+id)
}
query := strings.Join(parts, " OR ")
// 保证返回的用户顺序为 id 列表顺序
return c.findUsersByQuery(query, ids...)
}
const DIALECT = "mysql"
const BULK_INSERT_CHUNK_SIZE = 3000
func NewDB() (*gorm.DB, error) {
url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=%s",
conf.MySQLUsername(), conf.MySQLPassword(), conf.MySQLHost(), conf.MySQLPort(), conf.MySQLDatabase(), conf.MySQLLoc())
logrus.Infof("Initialize db with %s, url: %s", DIALECT, url)
db, err := gorm.Open(DIALECT, url)
if err != nil {
return nil, err
}
if conf.Debug() {
db.LogMode(true)
}
// connection pool
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(50)
db.DB().SetConnMaxLifetime(time.Hour)
return db, nil
}
func (c *UCClient) ConvertUserIDs(ids []string) ([]string, map[string]string, error) {
users, err := c.GetUserIDMapping(ids)
if err != nil {
return nil, nil, err
}
ucKratosMap := make(map[string]string)
kratosUcMap := make(map[string]string)
for _, u := range users {
ucKratosMap[u.ID] = u.UserID
kratosUcMap[u.UserID] = u.ID
}
return filterUserIDs(ids, ucKratosMap), kratosUcMap, nil
}
func (c *UCClient) GetUserIDMapping(ids []string) ([]UserIDModel, error) {
var users []UserIDModel
if err := c.db.Table("kratos_uc_userid_mapping").Where("id in (?)", ids).Find(&users).Error; err != nil {
return nil, err
}
return users, nil
}
func filterUserIDs(ids []string, users map[string]string) []string {
userIDs := make([]string, 0, len(ids))
for _, id := range ids {
if userID, ok := users[id]; ok {
userIDs = append(userIDs, userID)
} else {
userIDs = append(userIDs, id)
}
}
return userIDs
}
// FindUsersByKey 根据key查找用户,key可匹配用户名/邮箱/手机号
func (c *UCClient) FindUsersByKey(key string) ([]User, error) {
if key == "" {
return nil, nil
}
if c.oryEnabled() {
return getUserByKey(c.oryKratosPrivateAddr(), key)
}
query := fmt.Sprintf("username:%s OR nickname:%s OR phone_number:%s OR email:%s", key, key, key, key)
return c.findUsersByQuery(query)
}
func (c *UCClient) FuzzSearchUserByName(name string) ([]User, error) {
if name == "" {
return nil, nil
}
if c.oryEnabled() {
return getUserByKey(c.oryKratosPrivateAddr(), name)
}
query := fmt.Sprintf("username:%s OR nickname:%s", name, name)
return c.findUsersByQuery(query)
}
func userPagingListMapper(user *userPaging) []User {
userList := make([]User, 0)
for _, u := range user.Data {
userList = append(userList, User{
ID: strutil.String(u.Id),
Name: u.Username,
Nick: u.Nickname,
AvatarURL: u.Avatar,
Phone: u.Mobile,
Email: u.Email,
})
}
return userList
}
// GetUser 获取用户详情
func (c *UCClient) GetUser(userID string) (*User, error) {
if c.oryEnabled() {
userIDs, userMap, err := c.ConvertUserIDs([]string{userID})
if err != nil || len(userIDs) == 0 {
return nil, err
}
user, err := getUserByID(c.oryKratosPrivateAddr(), userIDs[0])
if err != nil {
return nil, err
}
if userID, ok := userMap[user.ID]; ok {
user.ID = userID
}
return user, nil
}
var (
user *User
err error
)
// 增加重试机制,防止因 uc 升级 serverToken 格式不兼容,无法获取用户信息
for i := 0; i < 3; i++ {
user, err = c.getUser(userID)
if err != nil {
continue
}
return user, nil
}
return nil, err
}
func (c *UCClient) getUser(userID string) (*User, error) {
token, err := c.ucTokenAuth.GetServerToken(false)
if err != nil {
return nil, errors.Wrapf(err, "failed to get token when finding user")
}
var user UcUser
r, err := c.client.Get(c.baseURL).
Path(strutil.Concat("/api/open/v1/users/", userID)).
Header("Authorization", strutil.Concat("Bearer ", token.AccessToken)).
Do().JSON(&user)
if err != nil {
return nil, err
}
if !r.IsOK() {
if r.StatusCode() == http.StatusUnauthorized {
c.InvalidateServerToken()
}
return nil, errors.Errorf("failed to find user, status code: %d", r.StatusCode())
}
if user.ID == 0 {
return nil, errors.Errorf("failed to find user %s", userID)
}
return userMapper(&user), nil
}
func userMapper(user *UcUser) *User {
return &User{
ID: strconv.Itoa(user.ID),
Name: user.Name,
Nick: user.Nick,
AvatarURL: user.AvatarURL,
Phone: user.Phone,
Email: user.Email,
}
}
func (c *UCClient) findUsersByQuery(query string, idOrder ...string) ([]User, error) {
// 获取token
token, err := c.ucTokenAuth.GetServerToken(false)
if err != nil {
return nil, errors.Wrapf(err, "failed to get token when finding users")
}
// 批量查询用户
var users []UcUser
var b bytes.Buffer
r, err := c.client.Get(c.baseURL).
Path("/api/open/v1/users").
Param("query", query).
Header("Authorization", strutil.Concat("Bearer ", token.AccessToken)).
Do().Body(&b)
if err != nil {
return nil, errors.Wrapf(err, "failed to find users")
}
if !r.IsOK() {
return nil, errors.Errorf("failed to find users, status code: %d", r.StatusCode())
}
content := b.Bytes()
if err := json.Unmarshal(content, &users); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %v", string(content))
}
// 保证顺序
if len(idOrder) > 0 {
userMap := make(map[string]User)
for _, user := range users {
userMap[strconv.Itoa(user.ID)] = *userMapper(&user)
}
var orderedUsers []User
for _, id := range idOrder {
orderedUsers = append(orderedUsers, userMap[id])
}
return orderedUsers, nil
}
userList := make([]User, len(users))
for i, user := range users {
userList[i] = *userMapper(&user)
}
return userList, nil
}