1
+ "use strict" ;
2
+
3
+ const {
4
+ Redprint,
5
+ paginate,
6
+ routeMetaInfo,
7
+ adminRequired,
8
+ Success,
9
+ ParametersException,
10
+ NotFound,
11
+ Failed
12
+ } = require ( "lin-cms-test" ) ;
13
+
14
+ const {
15
+ has,
16
+ set,
17
+ get,
18
+ toSafeInteger,
19
+ isInteger
20
+ } = require ( "lodash" ) ;
21
+ const {
22
+ ResetPasswordForm,
23
+ UpdateUserInfoForm,
24
+ NewGroupForm,
25
+ UpdateGroupForm,
26
+ DispatchAuthForm,
27
+ DispatchAuthsForm,
28
+ RemoveAuthsForm
29
+ } = require ( "../../validators/forms" ) ;
30
+ const {
31
+ AdminDao
32
+ } = require ( "../../dao/cms/admin" ) ;
33
+ const {
34
+ getSafeParamId
35
+ } = require ( "../../libs/util" ) ;
36
+
37
+ const admin = new Redprint ( {
38
+ prefix : "/admin"
39
+ } ) ;
40
+
41
+ exports . admin = admin ;
42
+ const adminDao = new AdminDao ( ) ;
43
+
44
+ admin . redGet ( "getAuthority" , "/authority" , {
45
+ auth : "查询所有可分配的权限" ,
46
+ module : "管理员" ,
47
+ mount : false
48
+ } , adminRequired , ctx => {
49
+ const res = { } ;
50
+ routeMetaInfo . forEach ( ( v , k ) => {
51
+ const au = v [ "auth" ] ;
52
+ if ( ! has ( res , `${ v [ "module" ] } .${ au } ` ) ) {
53
+ set ( res , `${ v [ "module" ] } .${ au } ` , [ k ] ) ;
54
+ } else {
55
+ res [ v [ "module" ] ] [ au ] . push ( k ) ;
56
+ }
57
+ } ) ;
58
+ ctx . json ( res ) ;
59
+ } ) ;
60
+
61
+ admin . redGet ( "getAdminUsers" , "/users" , {
62
+ auth : "查询所有用户" ,
63
+ module : "管理员" ,
64
+ mount : false
65
+ } , adminRequired , async ( ctx ) => {
66
+ const groupId = get ( ctx . request . query , "group_id" ) ;
67
+ const {
68
+ start,
69
+ count
70
+ } = paginate ( ctx ) ;
71
+ const {
72
+ users,
73
+ total
74
+ } = await adminDao . getUsers ( ctx , groupId , start , count ) ;
75
+ ctx . json ( {
76
+ collection : users ,
77
+ // 超级管理员不算入总数
78
+ total_nums : total
79
+ } ) ;
80
+ } ) ;
81
+
82
+ admin . redPut ( "changeUserPassword" , "/password/:id" , {
83
+ auth : "修改用户密码" ,
84
+ module : "管理员" ,
85
+ mount : false
86
+ } , adminRequired , async ( ctx ) => {
87
+ const form = new ResetPasswordForm ( ctx ) . validate ( ) ;
88
+ const id = toSafeInteger ( get ( ctx . params , "id" ) ) ;
89
+ if ( ! isInteger ( id ) ) {
90
+ throw new ParametersException ( {
91
+ msg : "路由参数错误"
92
+ } ) ;
93
+ }
94
+ await adminDao . changeUserPassword ( ctx , form , id ) ;
95
+ ctx . json ( new Success ( {
96
+ msg : "密码修改成功"
97
+ } ) ) ;
98
+ } ) ;
99
+
100
+ admin . redDelete ( "deleteUser" , "/:id" , {
101
+ auth : "删除用户" ,
102
+ module : "管理员" ,
103
+ mount : false
104
+ } , adminRequired , async ( ctx ) => {
105
+ const id = toSafeInteger ( get ( ctx . params , "id" ) ) ;
106
+ if ( ! isInteger ( id ) ) {
107
+ throw new ParametersException ( {
108
+ msg : "路由参数错误"
109
+ } ) ;
110
+ }
111
+ await adminDao . deleteUser ( ctx , id ) ;
112
+ ctx . json ( new Success ( {
113
+ msg : "操作成功"
114
+ } ) ) ;
115
+ } ) ;
116
+
117
+ admin . redPut ( "updateUser" , "/:id" , {
118
+ auth : "管理员更新用户信息" ,
119
+ module : "管理员" ,
120
+ mount : false
121
+ } , adminRequired , async ( ctx ) => {
122
+ const form = new UpdateUserInfoForm ( ctx ) . validate ( ) ;
123
+ const id = toSafeInteger ( get ( ctx . params , "id" ) ) ;
124
+ if ( ! isInteger ( id ) ) {
125
+ throw new ParametersException ( {
126
+ msg : "路由参数错误"
127
+ } ) ;
128
+ }
129
+ await adminDao . updateUserInfo ( ctx , form , id ) ;
130
+ ctx . json ( new Success ( {
131
+ msg : "操作成功"
132
+ } ) ) ;
133
+ } ) ;
134
+
135
+ admin . redGet ( "getAdminGroups" , "/groups" , {
136
+ auth : "查询所有权限组及其权限" ,
137
+ module : "管理员" ,
138
+ mount : false
139
+ } , adminRequired , async ( ctx ) => {
140
+ const {
141
+ start,
142
+ count
143
+ } = paginate ( ctx ) ;
144
+ const {
145
+ groups,
146
+ total
147
+ } = await adminDao . getGroups ( ctx , start , count ) ;
148
+ if ( total < 1 ) {
149
+ throw new NotFound ( {
150
+ msg : "未找到任何权限组"
151
+ } ) ;
152
+ }
153
+ ctx . json ( {
154
+ collection : groups ,
155
+ total_nums : total
156
+ } ) ;
157
+ } ) ;
158
+
159
+ admin . redGet ( "getAllGroup" , "/group/all" , {
160
+ auth : "查询所有权限组" ,
161
+ module : "管理员" ,
162
+ mount : false
163
+ } , adminRequired , async ( ctx ) => {
164
+ const groups = await ctx . manager . groupModel . find ( ) ;
165
+ if ( ! groups || groups . length < 1 ) {
166
+ throw new NotFound ( {
167
+ msg : "未找到任何权限组"
168
+ } ) ;
169
+ }
170
+ ctx . json ( groups ) ;
171
+ } ) ;
172
+
173
+ admin . redGet ( "getGroup" , "/group/:id" , {
174
+ auth : "查询一个权限组及其权限" ,
175
+ module : "管理员" ,
176
+ mount : false
177
+ } , adminRequired , async ( ctx ) => {
178
+ const id = toSafeInteger ( get ( ctx . params , "id" ) ) ;
179
+ if ( ! isInteger ( id ) ) {
180
+ throw new ParametersException ( {
181
+ msg : "路由参数错误"
182
+ } ) ;
183
+ }
184
+ const group = await adminDao . getGroup ( ctx , id ) ;
185
+ ctx . json ( group ) ;
186
+ } ) ;
187
+
188
+ admin . redPost ( "createGroup" , "/group" , {
189
+ auth : "新建权限组" ,
190
+ module : "管理员" ,
191
+ mount : false
192
+ } , adminRequired , async ( ctx ) => {
193
+ const form = new NewGroupForm ( ctx ) . validate ( ) ;
194
+ const ok = await adminDao . createGroup ( ctx , form ) ;
195
+ if ( ok ) {
196
+ ctx . json ( new Failed ( {
197
+ msg : "新建分组失败"
198
+ } ) ) ;
199
+ } else {
200
+ ctx . json ( new Success ( {
201
+ msg : "新建分组成功"
202
+ } ) ) ;
203
+ }
204
+ } ) ;
205
+
206
+ admin . redPut ( "updateGroup" , "/group/:id" , {
207
+ auth : "更新一个权限组" ,
208
+ module : "管理员" ,
209
+ mount : false
210
+ } , adminRequired , async ( ctx ) => {
211
+ const id = getSafeParamId ( ctx ) ;
212
+ const form = new UpdateGroupForm ( ctx ) . validate ( ) ;
213
+ await adminDao . updateGroup ( ctx , form , id ) ;
214
+ ctx . json ( new Success ( {
215
+ msg : "更新分组成功"
216
+ } ) ) ;
217
+ } ) ;
218
+
219
+ admin . redDelete ( "deleteGroup" , "/group/:id" , {
220
+ auth : "删除一个权限组" ,
221
+ module : "管理员" ,
222
+ mount : false
223
+ } , adminRequired , async ( ctx ) => {
224
+ const id = getSafeParamId ( ctx ) ;
225
+ await adminDao . deleteGroup ( ctx , id ) ;
226
+ ctx . json ( new Success ( {
227
+ msg : "删除分组成功"
228
+ } ) ) ;
229
+ } ) ;
230
+
231
+ admin . redPost ( "dispatchAuth" , "/dispatch" , {
232
+ auth : "分配单个权限" ,
233
+ module : "管理员" ,
234
+ mount : false
235
+ } , adminRequired , async ( ctx ) => {
236
+ const form = new DispatchAuthForm ( ctx ) . validate ( ) ;
237
+ await adminDao . dispatchAuth ( ctx , form ) ;
238
+ ctx . json ( new Success ( {
239
+ msg : "添加权限成功"
240
+ } ) ) ;
241
+ } ) ;
242
+
243
+ admin . redPost ( "dispatchAuths" , "/dispatch/patch" , {
244
+ auth : "分配多个权限" ,
245
+ module : "管理员" ,
246
+ mount : false
247
+ } , adminRequired , async ( ctx ) => {
248
+ const form = new DispatchAuthsForm ( ctx ) . validate ( ) ;
249
+ await adminDao . dispatchAuths ( ctx , form ) ;
250
+ ctx . json ( new Success ( {
251
+ msg : "添加权限成功"
252
+ } ) ) ;
253
+ } ) ;
254
+
255
+ admin . redPost ( "removeAuths" , "/remove" , {
256
+ auth : "删除多个权限" ,
257
+ module : "管理员" ,
258
+ mount : false
259
+ } , adminRequired , async ( ctx ) => {
260
+ const form = new RemoveAuthsForm ( ctx ) . validate ( ) ;
261
+ await adminDao . removeAuths ( ctx , form ) ;
262
+ ctx . json ( new Success ( {
263
+ msg : "删除权限成功"
264
+ } ) ) ;
265
+ } ) ;
0 commit comments