forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_user_group.go
173 lines (128 loc) · 3.52 KB
/
router_user_group.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
package http
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/didi/nightingale/v5/models"
)
func userGroupListGet(c *gin.Context) {
limit := queryInt(c, "limit", defaultLimit)
query := queryStr(c, "query", "")
total, err := models.UserGroupTotal(query)
dangerous(err)
list, err := models.UserGroupGets(query, limit, offset(c, limit))
dangerous(err)
renderData(c, gin.H{
"list": list,
"total": total,
}, nil)
}
// 与我相关的用户组,我创建的,或者我是其中一员
// 这个量不大,搜索和分页都放在前端来做,后端搞起来比较麻烦
func userGroupMineGet(c *gin.Context) {
list, err := loginUser(c).MyUserGroups()
renderData(c, list, err)
}
type userGroupForm struct {
Name string `json:"name"`
Note string `json:"note"`
}
func userGroupAdd(c *gin.Context) {
var f userGroupForm
bind(c, &f)
me := loginUser(c)
ug := models.UserGroup{
Name: f.Name,
Note: f.Note,
CreateBy: me.Username,
UpdateBy: me.Username,
}
dangerous(ug.Add())
// 顺便把创建者也作为团队的一员,失败了也没关系,用户会重新添加成员
models.UserGroupMemberAdd(ug.Id, me.Id)
renderData(c, ug.Id, nil)
}
func userGroupPut(c *gin.Context) {
var f userGroupForm
bind(c, &f)
me := loginUser(c)
ug := UserGroup(urlParamInt64(c, "id"))
can, err := me.CanModifyUserGroup(ug)
dangerous(err)
if !can {
bomb(http.StatusForbidden, "forbidden")
}
if ug.Name != f.Name {
// 如果name发生变化,需要检查这个新name是否与别的group重名
num, err := models.UserGroupCount("name=? and id<>?", f.Name, ug.Id)
dangerous(err)
if num > 0 {
bomb(200, "UserGroup %s already exists", f.Name)
}
}
ug.Name = f.Name
ug.Note = f.Note
ug.UpdateBy = me.Username
ug.UpdateAt = time.Now().Unix()
renderMessage(c, ug.Update("name", "note", "update_at", "update_by"))
}
// 不但返回UserGroup的信息,也把成员信息返回,成员不会特别多,所以,
// 成员全部返回,由前端分页、查询
func userGroupGet(c *gin.Context) {
ug := UserGroup(urlParamInt64(c, "id"))
ids, err := ug.MemberIds()
dangerous(err)
users, err := models.UserGetsByIds(ids)
renderData(c, gin.H{
"users": users,
"user_group": ug,
}, err)
}
func userGroupMemberAdd(c *gin.Context) {
var f idsForm
bind(c, &f)
f.Validate()
me := loginUser(c)
ug := UserGroup(urlParamInt64(c, "id"))
can, err := me.CanModifyUserGroup(ug)
dangerous(err)
if !can {
bomb(http.StatusForbidden, "forbidden")
}
dangerous(ug.AddMembers(f.Ids))
// 用户组的成员发生变化,相当于更新了用户组
// 如果更新失败了直接忽略,不是啥大事
ug.UpdateAt = time.Now().Unix()
ug.UpdateBy = me.Username
ug.Update("update_at", "update_by")
renderMessage(c, nil)
}
func userGroupMemberDel(c *gin.Context) {
var f idsForm
bind(c, &f)
f.Validate()
me := loginUser(c)
ug := UserGroup(urlParamInt64(c, "id"))
can, err := me.CanModifyUserGroup(ug)
dangerous(err)
if !can {
bomb(http.StatusForbidden, "forbidden")
}
dangerous(ug.DelMembers(f.Ids))
// 用户组的成员发生变化,相当于更新了用户组
// 如果更新失败了直接忽略,不是啥大事
ug.UpdateAt = time.Now().Unix()
ug.UpdateBy = me.Username
ug.Update("update_at", "update_by")
renderMessage(c, nil)
}
func userGroupDel(c *gin.Context) {
me := loginUser(c)
ug := UserGroup(urlParamInt64(c, "id"))
can, err := me.CanModifyUserGroup(ug)
dangerous(err)
if !can {
bomb(http.StatusForbidden, "forbidden")
}
renderMessage(c, ug.Del())
}