forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_user.go
180 lines (148 loc) · 3.74 KB
/
router_user.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
package http
import (
"encoding/json"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/didi/nightingale/v5/config"
"github.com/didi/nightingale/v5/models"
)
func userGets(c *gin.Context) {
limit := queryInt(c, "limit", defaultLimit)
query := queryStr(c, "query", "")
total, err := models.UserTotal(query)
dangerous(err)
list, err := models.UserGets(query, limit, offset(c, limit))
dangerous(err)
admin := false
roles := strings.Fields(loginUser(c).RolesForDB)
for i := 0; i < len(roles); i++ {
if roles[i] == "Admin" {
admin = true
break
}
}
renderData(c, gin.H{
"list": list,
"total": total,
"admin": admin,
}, nil)
}
type userAddForm struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
Nickname string `json:"nickname"`
Phone string `json:"phone"`
Email string `json:"email"`
Portrait string `json:"portrait"`
Roles []string `json:"roles"`
Contacts json.RawMessage `json:"contacts"`
}
func userAddPost(c *gin.Context) {
var f userAddForm
bind(c, &f)
password, err := models.CryptoPass(f.Password)
dangerous(err)
now := time.Now().Unix()
username := loginUsername(c)
if len(f.Roles) == 0 {
bomb(200, "roles empty")
}
u := models.User{
Username: f.Username,
Password: password,
Nickname: f.Nickname,
Phone: f.Phone,
Email: f.Email,
Portrait: f.Portrait,
RolesForDB: strings.Join(f.Roles, " "),
Contacts: f.Contacts,
CreateAt: now,
UpdateAt: now,
CreateBy: username,
UpdateBy: username,
}
renderMessage(c, u.Add())
}
func userProfileGet(c *gin.Context) {
renderData(c, User(urlParamInt64(c, "id")), nil)
}
type userProfileForm struct {
Nickname string `json:"nickname"`
Phone string `json:"phone"`
Email string `json:"email"`
Portrait string `json:"portrait"`
Roles []string `json:"roles"`
Status int `json:"status"`
Contacts json.RawMessage `json:"contacts"`
}
func userProfilePut(c *gin.Context) {
var f userProfileForm
bind(c, &f)
if len(f.Roles) == 0 {
bomb(200, "roles empty")
}
target := User(urlParamInt64(c, "id"))
target.Nickname = f.Nickname
target.Phone = f.Phone
target.Email = f.Email
target.Portrait = f.Portrait
target.RolesForDB = strings.Join(f.Roles, " ")
target.Status = f.Status
target.Contacts = f.Contacts
target.UpdateAt = time.Now().Unix()
target.UpdateBy = loginUsername(c)
renderMessage(
c,
target.Update(
"nickname",
"phone",
"email",
"portrait",
"roles",
"status",
"contacts",
"update_at",
"update_by",
),
)
}
type userPasswordForm struct {
Password string `json:"password" binding:"required"`
}
func userPasswordPut(c *gin.Context) {
var f userPasswordForm
bind(c, &f)
target := User(urlParamInt64(c, "id"))
cryptoPass, err := models.CryptoPass(f.Password)
dangerous(err)
target.Password = cryptoPass
target.UpdateAt = time.Now().Unix()
target.UpdateBy = loginUsername(c)
renderMessage(c, target.Update("password", "update_at", "update_by"))
}
type userStatusForm struct {
Status int `json:"status"`
}
func userStatusPut(c *gin.Context) {
var f userStatusForm
bind(c, &f)
target := User(urlParamInt64(c, "id"))
target.Status = f.Status
target.UpdateAt = time.Now().Unix()
target.UpdateBy = loginUsername(c)
renderMessage(c, target.Update("status", "update_at", "update_by"))
}
func userDel(c *gin.Context) {
id := urlParamInt64(c, "id")
target, err := models.UserGet("id=?", id)
dangerous(err)
if target == nil {
renderMessage(c, nil)
return
}
renderMessage(c, target.Del())
}
func contactChannelsGet(c *gin.Context) {
renderData(c, config.Config.ContactKeys, nil)
}