user.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. package http
  2. import (
  3. "strings"
  4. "go-common/app/admin/main/apm/conf"
  5. "go-common/app/admin/main/apm/model/user"
  6. "go-common/library/conf/env"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "github.com/jinzhu/gorm"
  11. )
  12. func name(ctx *bm.Context) (name string) {
  13. usernameI, _ := ctx.Get("username")
  14. name, _ = usernameI.(string)
  15. return
  16. }
  17. func userAuth(c *bm.Context) {
  18. var (
  19. usr = &user.User{}
  20. username = name(c)
  21. err error
  22. mdls []*user.Module
  23. rls []*user.Rule
  24. super bool
  25. )
  26. if usr, err = apmSvc.GetUser(c, username); err != nil {
  27. log.Error("apmSvc.userAuth error(%v)", err)
  28. c.JSON(nil, err)
  29. return
  30. }
  31. // err := apmSvc.DB.Where("username = ?", username).First(usr).Error
  32. // if err == gorm.ErrRecordNotFound {
  33. // usr.UserName = username
  34. // usr.NickName = username
  35. // err = apmSvc.DB.Create(usr).Error
  36. // }
  37. // if err != nil {
  38. // log.Error("apmSvc.userAuth error(%v)", err)
  39. // c.JSON(nil, err)
  40. // return
  41. // }
  42. for _, u := range conf.Conf.Superman {
  43. if u == username {
  44. super = true
  45. break
  46. }
  47. }
  48. var (
  49. ms []string
  50. rs []string
  51. )
  52. if super {
  53. for m := range user.Modules {
  54. ms = append(ms, m)
  55. for rl := range user.Rules {
  56. if strings.HasPrefix(rl+"_", m) {
  57. rs = append(rs, rl)
  58. }
  59. }
  60. }
  61. } else {
  62. ms, rs = apmSvc.GetDefaultPermission(c)
  63. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&mdls).Error; err != nil {
  64. log.Error("apmSvc.userAuth modules error(%v)", err)
  65. c.JSON(nil, err)
  66. return
  67. }
  68. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&rls).Error; err != nil {
  69. log.Error("apmSvc.userAuth rules error(%v)", err)
  70. c.JSON(nil, err)
  71. return
  72. }
  73. for _, m := range mdls {
  74. ms = append(ms, m.Module)
  75. }
  76. for _, r := range rls {
  77. rs = append(rs, r.Rule)
  78. }
  79. }
  80. data := user.Result{
  81. Super: super,
  82. User: usr,
  83. Env: env.DeployEnv,
  84. Rules: append(ms, rs...),
  85. }
  86. c.JSON(data, nil)
  87. }
  88. func userRuleStates(c *bm.Context) {
  89. username := name(c)
  90. usr := &user.User{}
  91. err := apmSvc.DB.Where("username = ?", username).First(usr).Error
  92. if err != nil {
  93. log.Error("apmSvc.userRuleStates error(%v)", err)
  94. c.JSON(nil, err)
  95. return
  96. }
  97. for _, u := range conf.Conf.Superman {
  98. if u == username {
  99. c.JSONMap(map[string]interface{}{
  100. "message": "超级管理员拥有所有权限",
  101. }, nil)
  102. return
  103. }
  104. }
  105. var (
  106. //app *user.Apply
  107. rls []*user.Rule
  108. )
  109. app := &user.Apply{}
  110. if err = apmSvc.DB.Where("user_id=? AND status=?", usr.ID, 1).First(app).Error; err != nil && err != gorm.ErrRecordNotFound {
  111. log.Error("apm.Svc.userRuleStates error(%v)", err)
  112. c.JSON(nil, err)
  113. return
  114. }
  115. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&rls).Error; err != nil && err != gorm.ErrRecordNotFound {
  116. log.Error("apm.Svc.userRuleStates error(%v)", err)
  117. c.JSON(nil, err)
  118. return
  119. }
  120. trs := strings.Split(app.Rules, ",")
  121. type ruleRes struct {
  122. Rule string `json:"rule"`
  123. Name string `json:"name"`
  124. State int `json:"state"`
  125. }
  126. data := map[string][]*ruleRes{}
  127. for module := range user.Modules {
  128. if env.DeployEnv != env.DeployEnvProd && module == "CONFIG" {
  129. continue
  130. }
  131. if user.Modules[module].Permit == user.PermitSuper {
  132. continue
  133. }
  134. // if module == "USER" {
  135. // continue
  136. // }
  137. NEXTRULE:
  138. for rule := range user.Rules {
  139. if !strings.HasPrefix(rule, module) {
  140. continue
  141. }
  142. rr := &ruleRes{Rule: rule, Name: user.Rules[rule].Des, State: 0}
  143. _, rdft := apmSvc.GetDefaultPermission(c)
  144. for _, rl := range rdft {
  145. if rule == rl {
  146. rr.State = 1
  147. data[module] = append(data[module], rr)
  148. continue NEXTRULE
  149. }
  150. }
  151. for _, rl := range rls {
  152. if rule == rl.Rule {
  153. rr.State = 1
  154. data[module] = append(data[module], rr)
  155. continue NEXTRULE
  156. }
  157. }
  158. for _, tr := range trs {
  159. if rule == tr {
  160. rr.State = 2
  161. data[module] = append(data[module], rr)
  162. continue NEXTRULE
  163. }
  164. }
  165. data[module] = append(data[module], rr)
  166. }
  167. }
  168. c.JSON(map[string]interface{}{
  169. "user": usr,
  170. "rule_states": data,
  171. }, nil)
  172. }
  173. func userApply(c *bm.Context) {
  174. username := name(c)
  175. usr := &user.User{}
  176. if err := apmSvc.DB.Where("username = ?", username).First(usr).Error; err != nil {
  177. log.Error("apmSvc.userApply error(%v)", err)
  178. c.JSON(nil, err)
  179. return
  180. }
  181. v := new(struct {
  182. Rules []string `form:"rules,split" validate:"required"`
  183. })
  184. if err := c.Bind(v); err != nil {
  185. c.JSON(nil, ecode.RequestErr)
  186. return
  187. }
  188. for _, rule := range v.Rules {
  189. if _, ok := user.Rules[rule]; !ok {
  190. c.JSONMap(map[string]interface{}{
  191. "message": "申请的操作不存在",
  192. }, nil)
  193. return
  194. }
  195. }
  196. istr := strings.Join(v.Rules, ",")
  197. m := &user.Apply{
  198. UserID: usr.ID,
  199. Rules: istr,
  200. Status: 1,
  201. }
  202. db := apmSvc.DB.Model(&user.Apply{}).Create(m)
  203. if err := db.Error; err != nil {
  204. log.Error("apmSvc.userApply error(%v)", err)
  205. c.JSON(nil, err)
  206. return
  207. }
  208. c.JSONMap(map[string]interface{}{
  209. "message": "申请成功",
  210. }, nil)
  211. }
  212. func userApplyEdit(c *bm.Context) {
  213. v := new(struct {
  214. ID int64 `form:"id" validate:"required"`
  215. Rules []string `form:"rules,split" validate:"required"`
  216. })
  217. var err error
  218. if err = c.Bind(v); err != nil {
  219. return
  220. }
  221. for _, r := range v.Rules {
  222. if _, ok := user.Rules[r]; !ok {
  223. c.JSONMap(map[string]interface{}{
  224. "message": "申请的操作不存在",
  225. }, nil)
  226. return
  227. }
  228. }
  229. username := name(c)
  230. if err = apmSvc.DB.Model(&user.Apply{}).Where("status = 1 AND id = ?", v.ID).Update(map[string]interface{}{
  231. "rules": strings.Join(v.Rules, ","), "admin": username}).Error; err != nil {
  232. log.Error("apmSvc.userApplyEdit error(%v)", err)
  233. c.JSON(nil, err)
  234. return
  235. }
  236. c.JSONMap(map[string]interface{}{
  237. "message": "修改成功",
  238. }, nil)
  239. }
  240. func userAudit(c *bm.Context) {
  241. username := name(c)
  242. super := false
  243. for _, u := range conf.Conf.Superman {
  244. if u == username {
  245. super = true
  246. break
  247. }
  248. }
  249. if !super {
  250. c.JSON(nil, ecode.AccessDenied)
  251. return
  252. }
  253. v := new(struct {
  254. ID int64 `form:"id" validate:"required"`
  255. Status int8 `form:"status" validate:"required"`
  256. })
  257. if err := c.Bind(v); err != nil {
  258. c.JSON(nil, ecode.RequestErr)
  259. return
  260. }
  261. if !(v.Status == 2 || v.Status == 3) {
  262. log.Error("apmSvc.userAudit error(%v)", v.Status)
  263. c.JSONMap(map[string]interface{}{
  264. "message": "status值范围为2,3",
  265. }, ecode.RequestErr)
  266. return
  267. }
  268. if err := apmSvc.DB.Model(&user.Apply{}).Where("id = ? AND status = ?", v.ID, 1).Updates(map[string]interface{}{"status": v.Status, "admin": username}).Error; err != nil {
  269. log.Error("apmSvc.userAudit update user_apply error(%v)", err)
  270. c.JSON(nil, err)
  271. return
  272. }
  273. if v.Status == 3 {
  274. c.JSONMap(map[string]interface{}{
  275. "message": "权限审核不通过",
  276. }, nil)
  277. return
  278. }
  279. apps := &user.Apply{}
  280. if err := apmSvc.DB.Where("id=?", v.ID).First(apps).Error; err != nil {
  281. log.Error("apmSvc.userAudit find user_apply error(%v)", err)
  282. c.JSON(nil, err)
  283. return
  284. }
  285. rules := strings.Split(apps.Rules, ",")
  286. for _, rule := range rules {
  287. r := &user.Rule{}
  288. apmSvc.DB.FirstOrCreate(r, &user.Rule{UserID: apps.UserID, Rule: rule})
  289. for module := range user.Modules {
  290. if strings.HasPrefix(rule, module) {
  291. m := &user.Module{}
  292. apmSvc.DB.FirstOrCreate(m, &user.Module{UserID: apps.UserID, Module: module})
  293. }
  294. }
  295. }
  296. c.JSONMap(map[string]interface{}{
  297. "message": "权限审核通过",
  298. }, nil)
  299. }
  300. func userApplies(c *bm.Context) {
  301. username := name(c)
  302. v := new(struct {
  303. Pn int `form:"pn" default:"1" validate:"min=1"`
  304. Ps int `form:"ps" default:"20" validate:"min=1"`
  305. Name string `form:"name"`
  306. })
  307. err := c.Bind(v)
  308. if err != nil {
  309. c.JSON(nil, ecode.RequestErr)
  310. return
  311. }
  312. var (
  313. super bool
  314. total int
  315. applies []*user.Applies
  316. )
  317. for _, u := range conf.Conf.Superman {
  318. if u == username {
  319. super = true
  320. break
  321. }
  322. }
  323. if !super {
  324. c.JSON(nil, ecode.AccessDenied)
  325. return
  326. }
  327. if v.Name != "" {
  328. err = apmSvc.DB.Raw(`SELECT user_apply.id, user_apply.user_id,user.username,user_apply.rules,user_apply.status
  329. FROM user_apply LEFT JOIN user ON user_apply.user_id=user.id WHERE user_apply.status=? AND (user.username like ? OR user.nickname like ?)`,
  330. 1, "%"+v.Name+"%", "%"+v.Name+"%").Order("user_apply.id desc").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&applies).Error
  331. } else {
  332. err = apmSvc.DB.Raw(`SELECT user_apply.id, user_apply.user_id,user.username,user_apply.rules,user_apply.status
  333. FROM user_apply LEFT JOIN user ON user_apply.user_id=user.id WHERE user_apply.status=?`,
  334. 1).Order("user_apply.id desc").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&applies).Error
  335. }
  336. // err := apmSvc.DB.Raw(`SELECT user_apply.id, user_apply.user_id,user.username,user_apply.rules,user_apply.status
  337. // FROM user_apply LEFT JOIN user ON user_apply.user_id=user.id WHERE user_apply.status=?`, 1).Scan(&applies).Error
  338. if err == gorm.ErrRecordNotFound {
  339. c.JSONMap(map[string]interface{}{
  340. "message": "当前没有任何申请",
  341. }, nil)
  342. return
  343. }
  344. if v.Name != "" {
  345. err = apmSvc.DB.Model(&user.Apply{}).Joins("LEFT JOIN user ON user_apply.user_id=user.id").Where(`user_apply.status=?
  346. AND (user.username like ? OR user.nickname like ?)`, 1, "%"+v.Name+"%", "%"+v.Name+"%").Count(&total).Error
  347. } else {
  348. err = apmSvc.DB.Model(&user.Apply{}).Joins(`LEFT JOIN user ON user_apply.user_id=user.id`).Where(`user_apply.status=?`, 1).Count(&total).Error
  349. }
  350. if err != nil {
  351. log.Error("apmSvc.userApplies error(%v)", err)
  352. c.JSON(nil, err)
  353. return
  354. }
  355. data := &Paper{
  356. Pn: v.Pn,
  357. Ps: v.Ps,
  358. Items: applies,
  359. Total: total,
  360. }
  361. c.JSON(data, nil)
  362. }
  363. func userList(c *bm.Context) {
  364. v := new(struct {
  365. Pn int `form:"pn" default:"1" validate:"min=1"`
  366. Ps int `form:"ps" default:"20" validate:"min=1"`
  367. Name string `form:"name"`
  368. })
  369. var err error
  370. if err = c.Bind(v); err != nil {
  371. return
  372. }
  373. var (
  374. pts []*user.User
  375. total int
  376. )
  377. s := "%" + v.Name + "%"
  378. if v.Name != "" {
  379. err = apmSvc.DB.Where("username LIKE ? OR nickname LIKE ?", s, s).Order("id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&pts).Error
  380. } else {
  381. err = apmSvc.DB.Order("id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&pts).Error
  382. }
  383. if err != nil {
  384. log.Error("apmSvc.Users error(%v)", err)
  385. c.JSON(nil, err)
  386. return
  387. }
  388. if v.Name != "" {
  389. err = apmSvc.DB.Where("username LIKE ? OR nickname LIKE ?", s, s).Model(&user.User{}).Count(&total).Error
  390. } else {
  391. err = apmSvc.DB.Model(&user.User{}).Count(&total).Error
  392. }
  393. if err != nil {
  394. log.Error("apmSvc.Users count error(%v)", err)
  395. c.JSON(nil, err)
  396. return
  397. }
  398. data := &Paper{
  399. Pn: v.Pn,
  400. Ps: v.Ps,
  401. Items: pts,
  402. Total: total,
  403. }
  404. c.JSON(data, nil)
  405. }
  406. func userInfo(c *bm.Context) {
  407. v := new(struct {
  408. ID int64 `form:"id" validate:"required"`
  409. })
  410. var err error
  411. if err = c.Bind(v); err != nil {
  412. return
  413. }
  414. usr := &user.User{}
  415. if err = apmSvc.DB.First(usr, v.ID).Error; err != nil {
  416. log.Error("apmSvc.userInfo error(%v)", err)
  417. c.JSON(nil, err)
  418. return
  419. }
  420. c.JSON(usr, nil)
  421. }
  422. func userEdit(c *bm.Context) {
  423. v := new(struct {
  424. ID int64 `form:"id" validate:"required"`
  425. Nickname string `form:"nickname"`
  426. Email string `form:"email"`
  427. Phone string `form:"phone"`
  428. })
  429. var err error
  430. if err = c.Bind(v); err != nil {
  431. return
  432. }
  433. usr := &user.User{}
  434. if err = apmSvc.DB.First(usr, v.ID).Error; err != nil {
  435. c.JSON(nil, err)
  436. return
  437. }
  438. if err = apmSvc.DB.Model(&user.User{}).Where("id = ?", v.ID).Omit("id").UpdateColumns(v).Error; err != nil {
  439. log.Error("apmSvc.userEdit error(%v)", err)
  440. c.JSON(nil, err)
  441. return
  442. }
  443. sqlLog := &map[string]interface{}{
  444. "SQLType": "update",
  445. "Where": "id = ?",
  446. "Value1": v.ID,
  447. "Update": v,
  448. "Old": usr,
  449. }
  450. username := name(c)
  451. apmSvc.SendLog(*c, username, 0, 2, int64(v.ID), "apmSvc.userEdit", sqlLog)
  452. c.JSON(nil, err)
  453. }
  454. func userModules(c *bm.Context) {
  455. v := new(struct {
  456. ID int64 `form:"id" validate:"required"`
  457. })
  458. var err error
  459. if err = c.Bind(v); err != nil {
  460. return
  461. }
  462. usr := &user.User{}
  463. if err = apmSvc.DB.First(usr, v.ID).Error; err != nil {
  464. log.Error("apmSvc.userInfo error(%v)", err)
  465. c.JSON(nil, err)
  466. return
  467. }
  468. var mdls []*user.Module
  469. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&mdls).Error; err != nil {
  470. log.Error("apmSvc.userAuth modules error(%v)", err)
  471. c.JSON(nil, err)
  472. return
  473. }
  474. var ms []string
  475. for _, m := range mdls {
  476. ms = append(ms, m.Module)
  477. }
  478. allMds := make(map[string]string)
  479. for module := range user.Modules {
  480. allMds[module] = user.Modules[module].Des
  481. }
  482. data := map[string]interface{}{
  483. "owns": ms,
  484. "modules": allMds,
  485. }
  486. c.JSON(data, nil)
  487. }
  488. func userRules(c *bm.Context) {
  489. v := new(struct {
  490. ID int64 `form:"id" validate:"required"`
  491. })
  492. var err error
  493. if err = c.Bind(v); err != nil {
  494. return
  495. }
  496. usr := &user.User{}
  497. if err = apmSvc.DB.First(usr, v.ID).Error; err != nil {
  498. log.Error("apmSvc.userInfo error(%v)", err)
  499. c.JSON(nil, err)
  500. return
  501. }
  502. var (
  503. mdls []*user.Module
  504. rls []*user.Rule
  505. )
  506. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&mdls).Error; err != nil {
  507. log.Error("apmSvc.userAuth modules error(%v)", err)
  508. c.JSON(nil, err)
  509. return
  510. }
  511. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&rls).Error; err != nil {
  512. log.Error("apmSvc.userAuth rules error(%v)", err)
  513. c.JSON(nil, err)
  514. return
  515. }
  516. var rs []string
  517. for _, r := range rls {
  518. rs = append(rs, r.Rule)
  519. }
  520. allRls := map[string]string{}
  521. for _, mdl := range mdls {
  522. for rl, rlM := range user.Rules {
  523. if strings.HasPrefix(rl+"_", mdl.Module) {
  524. allRls[rl] = rlM.Des
  525. }
  526. }
  527. }
  528. data := map[string]interface{}{
  529. "owns": rs,
  530. "rules": allRls,
  531. }
  532. c.JSON(data, nil)
  533. }
  534. func userModulesEdit(c *bm.Context) {
  535. v := new(struct {
  536. ID int64 `form:"id" validate:"required"`
  537. Modules []string `form:"modules,split"`
  538. })
  539. var err error
  540. if err = c.Bind(v); err != nil {
  541. return
  542. }
  543. usr := &user.User{}
  544. if err = apmSvc.DB.First(usr, v.ID).Error; err != nil {
  545. c.JSON(nil, err)
  546. return
  547. }
  548. var mdls []*user.Module
  549. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&mdls).Error; err != nil {
  550. log.Error("apmSvc.moduleEdit modules error(%v)", err)
  551. c.JSON(nil, err)
  552. return
  553. }
  554. var (
  555. ns []*user.Module
  556. ds []string
  557. )
  558. for _, m := range v.Modules {
  559. if len(mdls) == 0 {
  560. ns = append(ns, &user.Module{UserID: usr.ID, Module: m})
  561. continue
  562. }
  563. for j, mdl := range mdls {
  564. if m == mdl.Module {
  565. break
  566. }
  567. if j+1 == len(mdls) {
  568. ns = append(ns, &user.Module{UserID: usr.ID, Module: m})
  569. }
  570. }
  571. }
  572. for _, mdl := range mdls {
  573. if len(v.Modules) == 0 {
  574. ds = append(ds, mdl.Module)
  575. continue
  576. }
  577. for j, m := range v.Modules {
  578. if m == mdl.Module {
  579. break
  580. }
  581. if j+1 == len(v.Modules) {
  582. ds = append(ds, mdl.Module)
  583. }
  584. }
  585. }
  586. if err = apmSvc.DB.Exec("DELETE FROM user_module WHERE user_id=? AND module IN (?)", usr.ID, ds).Error; err != nil {
  587. log.Error("apmSvc.moduleEdit delModule error(%v)", err)
  588. c.JSON(nil, err)
  589. return
  590. }
  591. var sqlLogs []*map[string]interface{}
  592. sqlLog := &map[string]interface{}{
  593. "SQLType": "delete",
  594. "Where": "DELETE FROM user_module WHERE user_id=? AND module IN (?)",
  595. "Value1": usr.ID,
  596. "Value2": ds,
  597. "Update": "",
  598. "Old": "",
  599. }
  600. sqlLogs = append(sqlLogs, sqlLog)
  601. username := name(c)
  602. // apmSvc.SendLog(c, username, 0, 2, int64(v.ID), "apmSvc.moduleEdit", sqlLog)
  603. for _, d := range ds {
  604. if err = apmSvc.DB.Exec("DELETE FROM user_rule WHERE user_id=? AND rule LIKE ?", usr.ID, d+"_%").Error; err != nil {
  605. log.Error("apmSvc.moduleEdit delModule error(%v)", err)
  606. c.JSON(nil, err)
  607. apmSvc.SendLog(*c, username, 0, 2, 0, "apmSvc.moduleEdit", sqlLogs)
  608. return
  609. }
  610. sqlLog := &map[string]interface{}{
  611. "SQLType": "delete",
  612. "Where": "DELETE FROM user_rule WHERE user_id=? AND rule LIKE ?",
  613. "Value1": usr.ID,
  614. "Value2": d + "_%",
  615. "Update": "",
  616. "Old": "",
  617. }
  618. sqlLogs = append(sqlLogs, sqlLog)
  619. }
  620. for _, n := range ns {
  621. if err = apmSvc.DB.Create(n).Error; err != nil {
  622. log.Error("apmSvc.moduleEdit addModule error(%v)", err)
  623. c.JSON(nil, err)
  624. apmSvc.SendLog(*c, username, 0, 2, 0, "apmSvc.moduleEdit", sqlLogs)
  625. return
  626. }
  627. sqlLog := &map[string]interface{}{
  628. "SQLType": "add",
  629. "Content": n,
  630. }
  631. sqlLogs = append(sqlLogs, sqlLog)
  632. }
  633. apmSvc.SendLog(*c, username, 0, 2, 0, "apmSvc.moduleEdit", sqlLogs)
  634. c.JSON(nil, err)
  635. }
  636. func userRulesEdit(c *bm.Context) {
  637. v := new(struct {
  638. ID int64 `form:"id" validate:"required"`
  639. Rules []string `form:"rules,split"`
  640. })
  641. var err error
  642. if err = c.Bind(v); err != nil {
  643. return
  644. }
  645. usr := &user.User{}
  646. if err = apmSvc.DB.First(usr, v.ID).Error; err != nil {
  647. c.JSON(nil, err)
  648. return
  649. }
  650. var mdls []*user.Module
  651. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&mdls).Error; err != nil {
  652. log.Error("apmSvc.moduleEdit modules error(%v)", err)
  653. c.JSON(nil, err)
  654. return
  655. }
  656. if len(mdls) == 0 {
  657. log.Error("apmSvc.moduleEdit have not module error(%v)", err)
  658. c.JSON(nil, ecode.RequestErr)
  659. return
  660. }
  661. for _, rl := range v.Rules {
  662. var has bool
  663. for _, mdl := range mdls {
  664. if has = strings.HasPrefix(rl, mdl.Module); has {
  665. break
  666. }
  667. }
  668. if !has {
  669. log.Error("apmSvc.moduleEdit have not module error(%v)", err)
  670. c.JSON(nil, ecode.RequestErr)
  671. return
  672. }
  673. }
  674. var rls []*user.Rule
  675. if err = apmSvc.DB.Where("user_id=?", usr.ID).Find(&rls).Error; err != nil {
  676. log.Error("apmSvc.ruleEdit modules error(%v)", err)
  677. c.JSON(nil, err)
  678. return
  679. }
  680. var (
  681. ns []*user.Rule
  682. ds []string
  683. )
  684. for _, m := range v.Rules {
  685. if len(rls) == 0 {
  686. ns = append(ns, &user.Rule{UserID: usr.ID, Rule: m})
  687. continue
  688. }
  689. for j, rl := range rls {
  690. if m == rl.Rule {
  691. break
  692. }
  693. if j+1 == len(rls) {
  694. ns = append(ns, &user.Rule{UserID: usr.ID, Rule: m})
  695. }
  696. }
  697. }
  698. for _, rl := range rls {
  699. if len(v.Rules) == 0 {
  700. ds = append(ds, rl.Rule)
  701. continue
  702. }
  703. for j, m := range v.Rules {
  704. if m == rl.Rule {
  705. break
  706. }
  707. if j+1 == len(v.Rules) {
  708. ds = append(ds, rl.Rule)
  709. }
  710. }
  711. }
  712. var sqlLogs []*map[string]interface{}
  713. if err = apmSvc.DB.Exec("DELETE FROM user_rule WHERE user_id=? AND rule IN (?)", usr.ID, ds).Error; err != nil {
  714. log.Error("apmSvc.ruleEdit delModule error(%v)", err)
  715. c.JSON(nil, err)
  716. return
  717. }
  718. sqlLog := &map[string]interface{}{
  719. "SQLType": "delete",
  720. "Where": "DELETE FROM user_rule WHERE user_id=? AND rule IN (?)",
  721. "Value1": usr.ID,
  722. "Value2": ds,
  723. "Update": "",
  724. "Old": "",
  725. }
  726. username := name(c)
  727. sqlLogs = append(sqlLogs, sqlLog)
  728. for _, n := range ns {
  729. if err = apmSvc.DB.Create(n).Error; err != nil {
  730. log.Error("apmSvc.ruleEdit addModule error(%v)", err)
  731. c.JSON(nil, err)
  732. apmSvc.SendLog(*c, username, 0, 2, 0, "apmSvc.ruleEdit", sqlLogs)
  733. return
  734. }
  735. sqlLog := &map[string]interface{}{
  736. "SQLType": "add",
  737. "Content": n,
  738. }
  739. sqlLogs = append(sqlLogs, sqlLog)
  740. }
  741. apmSvc.SendLog(*c, username, 0, 2, 0, "apmSvc.ruleEdit", sqlLogs)
  742. c.JSON(nil, err)
  743. }
  744. func userSyncTree(c *bm.Context) {
  745. username := name(c)
  746. apmSvc.TreeSync(c, username, c.Request.Header.Get("Cookie"))
  747. c.JSON(nil, nil)
  748. }
  749. func userTreeAppids(c *bm.Context) {
  750. username := name(c)
  751. appids, err := apmSvc.Appids(c, username, c.Request.Header.Get("Cookie"))
  752. if err != nil {
  753. log.Error("%v", err)
  754. c.JSON(nil, err)
  755. return
  756. }
  757. c.JSON(appids, nil)
  758. }
  759. func userTreeDiscovery(c *bm.Context) {
  760. username := name(c)
  761. appids, err := apmSvc.DiscoveryID(c, username, c.Request.Header.Get("Cookie"))
  762. if err != nil {
  763. log.Error("%v", err)
  764. c.JSON(nil, err)
  765. return
  766. }
  767. c.JSON(appids, nil)
  768. }