-
Notifications
You must be signed in to change notification settings - Fork 3
/
member.go
175 lines (147 loc) · 3.91 KB
/
member.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
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func fetchMembers(c *gin.Context) {
_, pageSize, cookie := findPageControl(c)
sr, err := org.Members(pageSize, cookie)
if err != nil {
sendError(c, err)
return
}
sendResult(c, sr)
}
func mapToMemberInfo(body map[string]interface{}) map[string][]string {
memberInfo := make(map[string][]string, 0)
if name, ok := body[`name`].(string); ok {
memberInfo[`name`] = []string{name}
}
if cn, ok := body[`cn`].(string); ok {
memberInfo[`cn`] = []string{cn}
}
if email, ok := body[`email`].(string); ok {
memberInfo[`email`] = []string{email}
}
if gender, ok := body[`gender`].(string); ok {
memberInfo[`gender`] = []string{gender}
}
if priority, ok := body[`priority`].(string); ok {
memberInfo[`priority`] = []string{priority}
} else {
memberInfo[`priority`] = []string{`0`}
}
if telephoneNumber, ok := body[`telephoneNumber`].(string); ok {
memberInfo[`telephoneNumber`] = []string{telephoneNumber}
}
if title, ok := body[`title`].(string); ok {
memberInfo[`title`] = []string{title}
}
if unitIDs, ok := body[`unitID`].([]interface{}); ok {
var ids []string
for _, id := range unitIDs {
ids = append(ids, id.(string))
}
memberInfo[`unitID`] = ids
}
if rbacType, ok := body[`rbacType`].(string); ok {
memberInfo[`rbacType`] = []string{rbacType}
}
if rbacRoles, ok := body[`rbacRole`].([]interface{}); ok {
var roles []string
for _, r := range rbacRoles {
roles = append(roles, r.(string))
}
memberInfo[`rbacRole`] = roles
}
return memberInfo
}
func createMember(c *gin.Context) {
var body map[string]interface{}
err := c.BindJSON(&body) // 会发送错误信息
if err != nil {
return
}
memberInfo := mapToMemberInfo(body)
m := md5.New()
m.Write([]byte(`123456`))
pwd := m.Sum(nil)
memberInfo[`userPassword`] = []string{hex.EncodeToString(pwd)}
id, err := org.AddMember(memberInfo)
if err != nil {
sendError(c, err)
return
}
thirdPwd := newPassword()
// 去环信注册
err = em.RegisterSignelUser(id, thirdPwd)
if err != nil {
log.WithField(`resource`, `member`).Warn(fmt.Sprintf(`register user failed %v`, err))
}
// 将用户名和密码更新到ldapserver
err = org.ModifyMember(id, map[string][]string{
`thirdAccount`: []string{id},
`thirdPassword`: []string{thirdPwd},
`labeledURI`: []string{id}, // 顺便在同一个请求中添加默认头像 如果创建用户成功,则会在后台自动生成默认头像
})
if err != nil {
log.WithField(`resource`, `memeber`).Warn(fmt.Sprintf(`modify member third account info occured error %v`, err))
}
// 生成默认头像
go func(n, id string) {
a, err := avatarGenerator.Gen(n)
if err != nil {
log.WithField(`resource`, `generate avatar`).Warn(err)
return
}
go func(string, string) {
_, err = uploadFileToQiNiu(a, id)
if err != nil {
log.WithField(`resource`, `upload avatar`).Warn(err)
return
}
}(a, id)
}(memberInfo[`name`][0], id)
c.JSON(http.StatusOK, map[string]string{`id`: id})
}
func editMember(c *gin.Context) {
var body map[string]interface{}
err := c.BindJSON(&body) // 会发送错误信息
if err != nil {
return
}
memberInfo := mapToMemberInfo(body)
id := c.Param(`id`)
err = org.ModifyMember(id, memberInfo)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, map[string]string{`id`: id})
}
func memberByID(c *gin.Context) {
ms, err := org.MemberByID(c.Param(`id`), true, false)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, ms)
}
func delMember(c *gin.Context) {
id := c.Param(`id`)
err := org.DelMember(id)
if err != nil {
sendError(c, err)
return
}
// 去环信删除
err = em.DeleteUser(id)
if err != nil {
log.WithField(`resource`, `member`).Warn(fmt.Sprintf(`delete user[id:%v] failed %v`, id, err))
}
c.JSON(http.StatusOK, map[string]string{`id`: id})
}