-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
init_mysql_data.go
667 lines (647 loc) · 14.1 KB
/
init_mysql_data.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package common
import (
"errors"
"github.com/eryajf/xirang/config"
"github.com/eryajf/xirang/model/system"
"github.com/eryajf/xirang/public/tools"
"github.com/thoas/go-funk"
"gorm.io/gorm"
)
// 初始化mysql数据
func InitData() {
// 是否初始化数据
if !config.Conf.System.InitData {
return
}
// 1.写入角色数据
newRoles := make([]*system.Role, 0)
roles := []*system.Role{
{
Model: gorm.Model{ID: 1},
Name: "管理员",
Keyword: "admin",
Remark: "",
Sort: 1,
Status: 1,
Creator: "系统",
},
{
Model: gorm.Model{ID: 2},
Name: "普通用户",
Keyword: "user",
Remark: "",
Sort: 3,
Status: 1,
Creator: "系统",
},
{
Model: gorm.Model{ID: 3},
Name: "访客",
Keyword: "guest",
Remark: "",
Sort: 5,
Status: 1,
Creator: "系统",
},
}
for _, role := range roles {
err := DB.First(&role, role.ID).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
newRoles = append(newRoles, role)
}
}
if len(newRoles) > 0 {
err := DB.Create(&newRoles).Error
if err != nil {
Log.Errorf("写入系统角色数据失败:%v", err)
}
}
// 2写入菜单
newMenus := make([]system.Menu, 0)
var uint0 uint = 0
var uint1 uint = 1
var uint7 uint = 7
var uint9 uint = 9
menus := []system.Menu{
{
Model: gorm.Model{ID: 1},
Name: "System",
Title: "系统管理",
Icon: "system",
Path: "/system",
Component: "Layout",
Redirect: "/system/user",
Sort: 1,
ParentId: uint0,
Roles: roles[:1],
Creator: "系统",
},
{
Model: gorm.Model{ID: 2},
Name: "User",
Title: "用户管理",
Icon: "user",
Path: "user",
Component: "/system/user/index",
Sort: 2,
ParentId: uint1,
Roles: roles[:1],
Creator: "系统",
},
{
Model: gorm.Model{ID: 3},
Name: "Group",
Title: "分组管理",
Icon: "peoples",
Path: "group",
Component: "/system/group/index",
Sort: 3,
ParentId: uint1,
NoCache: 1,
Roles: roles[:1],
Creator: "系统",
},
{
Model: gorm.Model{ID: 4},
Name: "Role",
Title: "角色管理",
Icon: "eye-open",
Path: "role",
Component: "/system/role/index",
Sort: 4,
ParentId: uint1,
Roles: roles[:1],
Creator: "系统",
},
{
Model: gorm.Model{ID: 5},
Name: "Menu",
Title: "菜单管理",
Icon: "tree-table",
Path: "menu",
Component: "/system/menu/index",
Sort: 5,
ParentId: uint1,
Roles: roles[:1],
Creator: "系统",
},
{
Model: gorm.Model{ID: 6},
Name: "Api",
Title: "接口管理",
Icon: "tree",
Path: "api",
Component: "/system/api/index",
Sort: 6,
ParentId: uint1,
Roles: roles[:1],
Creator: "系统",
},
{
Model: gorm.Model{ID: 7},
Name: "Log",
Title: "日志管理",
Icon: "log",
Path: "log",
Component: "/system/log/index",
Redirect: "/system/log/operationLog",
Sort: 7,
ParentId: uint1,
Roles: roles[:2],
Creator: "系统",
},
{
Model: gorm.Model{ID: 8},
Name: "OperationLog",
Title: "操作日志",
Icon: "documentation",
Path: "operationLog",
Component: "/system/log/operationLog/index",
Sort: 8,
ParentId: uint7,
Roles: roles[:2],
Creator: "系统",
},
{
Model: gorm.Model{ID: 9},
Name: "Example",
Title: "示例模块",
Icon: "example",
Path: "/example",
Component: "Layout",
Sort: 9,
ParentId: uint0,
Roles: roles[:2],
Creator: "系统",
},
{
Model: gorm.Model{ID: 10},
Name: "CloudAccount",
Title: "云账户",
Icon: "peoples",
Path: "CloudAccount",
Component: "/example/cloudAccount/index",
Sort: 10,
ParentId: uint9,
Roles: roles[:2],
Creator: "系统",
},
}
for _, menu := range menus {
err := DB.First(&menu, menu.ID).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
newMenus = append(newMenus, menu)
}
}
if len(newMenus) > 0 {
err := DB.Create(&newMenus).Error
if err != nil {
Log.Errorf("写入系统菜单数据失败:%v", err)
}
}
// 3.写入用户
newUsers := make([]*system.User, 0)
users := []*system.User{
{
Model: gorm.Model{ID: 1},
Username: "admin",
Password: tools.NewGenPasswd("123456"),
Nickname: "管理员",
GivenName: "最强后台",
Mail: "admin@eryajf.net",
JobNumber: "0000",
Mobile: "18888888888",
Avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
PostalAddress: "中国河南省南阳市",
Departments: "ops",
Position: "系统管理员",
Introduction: "最强后台的管理员",
Status: 1,
Creator: "系统",
Roles: roles[:1],
},
}
for _, user := range users {
err := DB.First(&user, user.ID).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
newUsers = append(newUsers, user)
}
}
if len(newUsers) > 0 {
err := DB.Create(&newUsers).Error
if err != nil {
Log.Errorf("写入用户数据失败:%v", err)
}
}
// 4.写入api
apis := []system.Api{
{
Method: "POST",
Path: "/system/base/login",
Category: "base",
Remark: "用户登录",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/base/logout",
Category: "base",
Remark: "用户登出",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/base/refreshToken",
Category: "base",
Remark: "刷新JWT令牌",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/user/info",
Category: "user",
Remark: "获取当前登录用户信息",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/user/list",
Category: "user",
Remark: "获取用户列表",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/user/changePwd",
Category: "user",
Remark: "更新用户登录密码",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/user/add",
Category: "user",
Remark: "创建用户",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/user/update",
Category: "user",
Remark: "更新用户",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/user/delete",
Category: "user",
Remark: "批量删除用户",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/user/changeUserStatus",
Category: "user",
Remark: "更改用户状态",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/group/list",
Category: "group",
Remark: "获取分组列表",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/group/tree",
Category: "group",
Remark: "获取分组列表树",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/group/add",
Category: "group",
Remark: "创建分组",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/group/update",
Category: "group",
Remark: "更新分组",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/group/delete",
Category: "group",
Remark: "批量删除分组",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/group/adduser",
Category: "group",
Remark: "添加用户到分组",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/group/removeuser",
Category: "group",
Remark: "将用户从分组移出",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/group/useringroup",
Category: "group",
Remark: "获取在分组内的用户列表",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/group/usernoingroup",
Category: "group",
Remark: "获取不在分组内的用户列表",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/role/list",
Category: "role",
Remark: "获取角色列表",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/role/add",
Category: "role",
Remark: "创建角色",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/role/update",
Category: "role",
Remark: "更新角色",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/role/getmenulist",
Category: "role",
Remark: "获取角色的权限菜单",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/role/updatemenus",
Category: "role",
Remark: "更新角色的权限菜单",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/role/getapilist",
Category: "role",
Remark: "获取角色的权限接口",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/role/updateapis",
Category: "role",
Remark: "更新角色的权限接口",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/role/delete",
Category: "role",
Remark: "批量删除角色",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/menu/list",
Category: "menu",
Remark: "获取菜单列表",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/menu/tree",
Category: "menu",
Remark: "获取菜单树",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/menu/access/tree",
Category: "menu",
Remark: "获取用户菜单树",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/menu/add",
Category: "menu",
Remark: "创建菜单",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/menu/update",
Category: "menu",
Remark: "更新菜单",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/menu/delete",
Category: "menu",
Remark: "批量删除菜单",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/api/list",
Category: "api",
Remark: "获取接口列表",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/api/tree",
Category: "api",
Remark: "获取接口树",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/api/add",
Category: "api",
Remark: "创建接口",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/api/update",
Category: "api",
Remark: "更新接口",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/api/delete",
Category: "api",
Remark: "批量删除接口",
Creator: "系统",
},
{
Method: "GET",
Path: "/system/log/operation/list",
Category: "log",
Remark: "获取操作日志列表",
Creator: "系统",
},
{
Method: "POST",
Path: "/system/log/operation/delete",
Category: "log",
Remark: "批量删除操作日志",
Creator: "系统",
},
{
Method: "GET",
Path: "/example/cloudaccount/list",
Category: "cloudAccount",
Remark: "获取云账户列表",
Creator: "系统",
},
{
Method: "POST",
Path: "/example/cloudaccount/add",
Category: "cloudAccount",
Remark: "添加云账户",
Creator: "系统",
},
{
Method: "POST",
Path: "/example/cloudaccount/update",
Category: "cloudAccount",
Remark: "更新云账户信息",
Creator: "系统",
},
{
Method: "POST",
Path: "/example/cloudaccount/delete",
Category: "cloudAccount",
Remark: "批量删除云账户",
Creator: "系统",
},
}
// 5. 将角色绑定给菜单
newApi := make([]system.Api, 0)
newRoleCasbin := make([]system.RoleCasbin, 0)
for i, api := range apis {
api.ID = uint(i + 1)
err := DB.First(&api, api.ID).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
newApi = append(newApi, api)
// 管理员拥有所有API权限
newRoleCasbin = append(newRoleCasbin, system.RoleCasbin{
Keyword: roles[0].Keyword,
Path: api.Path,
Method: api.Method,
})
// 非管理员拥有基础权限
basePaths := []string{
"/system/base/login",
"/system/base/logout",
"/system/base/refreshToken",
"/system/base/changePwd",
"/system/base/dashboard",
"/system/user/info",
"/system/user/changePwd",
"/system/menu/access/tree",
"/system/log/operation/list",
}
if funk.ContainsString(basePaths, api.Path) {
newRoleCasbin = append(newRoleCasbin, system.RoleCasbin{
Keyword: roles[1].Keyword,
Path: api.Path,
Method: api.Method,
})
}
}
}
if len(newApi) > 0 {
if err := DB.Create(&newApi).Error; err != nil {
Log.Errorf("写入api数据失败:%v", err)
}
}
if len(newRoleCasbin) > 0 {
rules := make([][]string, 0)
for _, c := range newRoleCasbin {
rules = append(rules, []string{
c.Keyword, c.Path, c.Method,
})
}
isAdd, err := CasbinEnforcer.AddPolicies(rules)
if !isAdd {
Log.Errorf("写入casbin数据失败:%v", err)
}
}
// 6.写入分组
newGroups := make([]system.Group, 0)
groups := []system.Group{
{
Model: gorm.Model{ID: 1},
GroupName: "root",
Remark: "根部门",
Creator: "system",
ParentId: 0,
Source: "system",
},
{
Model: gorm.Model{ID: 2},
GroupName: "backend",
Remark: "后端部",
Creator: "system",
ParentId: 1,
Source: "system",
},
{
Model: gorm.Model{ID: 3},
GroupName: "test",
Remark: "测试部",
Creator: "system",
ParentId: 1,
Source: "system",
},
{
Model: gorm.Model{ID: 4},
GroupName: "ops",
Remark: "运维部",
Creator: "system",
ParentId: 1,
Source: "system",
},
}
for _, group := range groups {
err := DB.First(&group, group.ID).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
newGroups = append(newGroups, group)
}
}
if len(newGroups) > 0 {
err := DB.Create(&newGroups).Error
if err != nil {
Log.Errorf("写入分组数据失败:%v", err)
}
}
}