authority.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/library/log"
  5. bm "go-common/library/net/http/blademaster"
  6. "go-common/library/net/http/blademaster/render"
  7. "go-common/library/ecode"
  8. )
  9. // check username and dashboard sessionid
  10. func checkCookie(c *bm.Context) (username, sid string, err error) {
  11. var r = c.Request
  12. var name *http.Cookie
  13. if name, err = r.Cookie("username"); err == nil {
  14. username = name.Value
  15. }
  16. var session *http.Cookie
  17. if session, err = r.Cookie("_AJSESSIONID"); err == nil {
  18. sid = session.Value
  19. }
  20. if username == "" || sid == "" {
  21. err = ecode.Unauthorized
  22. }
  23. return
  24. }
  25. func getAuthorityUserPrivileges(c *bm.Context) {
  26. username, _, err := checkCookie(c)
  27. if err != nil {
  28. c.JSON(nil, err)
  29. log.Error("growup checkCookie error(%v)", err)
  30. return
  31. }
  32. data, err := svr.GetAuthorityUserPrivileges(username)
  33. if err != nil {
  34. c.JSON(nil, err)
  35. log.Error("growup svr.GetAuthorityUserPrivileges error(%v)", err)
  36. return
  37. }
  38. c.JSON(data, nil)
  39. }
  40. func getAuthorityUserGroup(c *bm.Context) {
  41. username, _, err := checkCookie(c)
  42. if err != nil {
  43. c.JSON(nil, err)
  44. log.Error("growup checkCookie error(%v)", err)
  45. return
  46. }
  47. data, err := svr.GetAuthorityUserGroup(username)
  48. if err != nil {
  49. c.JSON(nil, err)
  50. log.Error("growup svr.GetAuthorityUserGroup error(%v)", err)
  51. return
  52. }
  53. c.JSON(data, nil)
  54. }
  55. func listAuthorityUsers(c *bm.Context) {
  56. v := new(struct {
  57. Username string `form:"username"`
  58. From int `form:"from" validate:"min=0" default:"0"`
  59. Limit int `form:"limit" validate:"min=1" default:"20"`
  60. Sort string `form:"sort"`
  61. })
  62. if err := c.Bind(v); err != nil {
  63. return
  64. }
  65. users, total, err := svr.ListAuthorityUsers(v.Username, v.From, v.Limit, v.Sort)
  66. if err != nil {
  67. c.JSON(nil, err)
  68. log.Error("growup svr.ListAuthorityUsers error(%v)", err)
  69. return
  70. }
  71. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  72. "code": 0,
  73. "message": "0",
  74. "data": users,
  75. "paging": map[string]int{
  76. "page_size": v.Limit,
  77. "total": total,
  78. },
  79. }))
  80. }
  81. func addAuthorityUser(c *bm.Context) {
  82. v := new(struct {
  83. Username string `form:"username"`
  84. Nickname string `form:"nickname"`
  85. })
  86. if err := c.Bind(v); err != nil {
  87. return
  88. }
  89. err := svr.AddAuthorityUser(v.Username, v.Nickname)
  90. if err != nil {
  91. log.Error("growup svr.AddAuthorityUser error(%v)", err)
  92. }
  93. c.JSON(nil, err)
  94. }
  95. func updateAuthorityUserInfo(c *bm.Context) {
  96. v := new(struct {
  97. ID int64 `form:"id"`
  98. Nickname string `form:"nickname"`
  99. })
  100. if err := c.Bind(v); err != nil {
  101. return
  102. }
  103. err := svr.UpdateAuthorityUserInfo(v.ID, v.Nickname)
  104. if err != nil {
  105. log.Error("growup svr.UpdateAuthorityUserInfo error(%v)", err)
  106. }
  107. c.JSON(nil, err)
  108. }
  109. func updateAuthorityUserAuth(c *bm.Context) {
  110. v := new(struct {
  111. ID int64 `form:"id"`
  112. GroupID string `form:"group_id"`
  113. RoleID string `form:"role_id"`
  114. })
  115. if err := c.Bind(v); err != nil {
  116. return
  117. }
  118. err := svr.UpdateAuthorityUserAuth(v.ID, v.GroupID, v.RoleID)
  119. if err != nil {
  120. log.Error("growup svr.UpdateAuthorityUserAuth error(%v)", err)
  121. }
  122. c.JSON(nil, err)
  123. }
  124. func deleteAuthorityUser(c *bm.Context) {
  125. v := new(struct {
  126. ID int64 `form:"id"`
  127. })
  128. if err := c.Bind(v); err != nil {
  129. return
  130. }
  131. err := svr.DeleteAuthorityUser(v.ID)
  132. if err != nil {
  133. log.Error("growup svr.DeleteAuthorityUser error(%v)", err)
  134. }
  135. c.JSON(nil, err)
  136. }
  137. func listAuthorityTaskGroups(c *bm.Context) {
  138. v := new(struct {
  139. From int `form:"from" validate:"min=0" default:"0"`
  140. Limit int `form:"limit" validate:"min=1" default:"20"`
  141. Sort string `form:"sort"`
  142. })
  143. if err := c.Bind(v); err != nil {
  144. return
  145. }
  146. groups, total, err := svr.ListAuthorityTaskGroups(v.From, v.Limit, v.Sort)
  147. if err != nil {
  148. c.JSON(nil, err)
  149. log.Error("growup svr.ListAuthorityTaskGroups error(%v)", err)
  150. return
  151. }
  152. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  153. "code": 0,
  154. "message": "0",
  155. "data": groups,
  156. "paging": map[string]int{
  157. "page_size": v.Limit,
  158. "total": total,
  159. },
  160. }))
  161. }
  162. func addAuthorityTaskGroup(c *bm.Context) {
  163. v := new(struct {
  164. Name string `form:"name"`
  165. Desc string `form:"desc"`
  166. })
  167. if err := c.Bind(v); err != nil {
  168. return
  169. }
  170. err := svr.AddAuthorityTaskGroup(v.Name, v.Desc)
  171. if err != nil {
  172. log.Error("growup svr.AddAuthorityTaskGroup error(%v)", err)
  173. }
  174. c.JSON(nil, err)
  175. }
  176. func addAuthorityTaskGroupUser(c *bm.Context) {
  177. v := new(struct {
  178. Username string `form:"username"`
  179. GroupID string `form:"group_id"`
  180. })
  181. if err := c.Bind(v); err != nil {
  182. return
  183. }
  184. err := svr.AddAuthorityTaskGroupUser(v.Username, v.GroupID)
  185. if err != nil {
  186. log.Error("growup svr.AddAuthorityTaskGroupUser error(%v)", err)
  187. }
  188. c.JSON(nil, err)
  189. }
  190. func updateAuthorityTaskGroupInfo(c *bm.Context) {
  191. v := new(struct {
  192. GroupID int64 `form:"group_id"`
  193. Name string `form:"name"`
  194. Desc string `form:"desc"`
  195. })
  196. if err := c.Bind(v); err != nil {
  197. return
  198. }
  199. err := svr.UpdateAuthorityTaskGroupInfo(v.GroupID, v.Name, v.Desc)
  200. if err != nil {
  201. log.Error("growup svr.UpdateAuthorityTaskGroupInfo error(%v)", err)
  202. }
  203. c.JSON(nil, err)
  204. }
  205. func deleteAuthorityTaskGroup(c *bm.Context) {
  206. v := new(struct {
  207. GroupID int64 `form:"group_id"`
  208. })
  209. if err := c.Bind(v); err != nil {
  210. return
  211. }
  212. err := svr.DeleteAuthorityTaskGroup(v.GroupID)
  213. if err != nil {
  214. log.Error("growup svr.DeleteAuthorityTaskGroup error(%v)", err)
  215. }
  216. c.JSON(nil, err)
  217. }
  218. func deleteAuthorityTaskGroupUser(c *bm.Context) {
  219. v := new(struct {
  220. ID int64 `form:"id"`
  221. GroupID int64 `form:"group_id"`
  222. })
  223. if err := c.Bind(v); err != nil {
  224. return
  225. }
  226. err := svr.DeleteAuthorityTaskGroupUser(v.ID, v.GroupID)
  227. if err != nil {
  228. log.Error("growup svr.DeleteAuthorityTaskGroupUser error(%v)", err)
  229. }
  230. c.JSON(nil, err)
  231. }
  232. func listAuthorityGroupPrivilege(c *bm.Context) {
  233. v := new(struct {
  234. GroupID int64 `form:"group_id"`
  235. FatherID int64 `form:"father_id" validate:"required"`
  236. })
  237. if err := c.Bind(v); err != nil {
  238. return
  239. }
  240. data, err := svr.ListAuthorityGroupPrivilege(v.GroupID, v.FatherID)
  241. if err != nil {
  242. log.Error("growup svr.ListAuthorityGroupPrivilege error(%v)", err)
  243. c.JSON(nil, err)
  244. return
  245. }
  246. c.JSON(data, nil)
  247. }
  248. func updateAuthorityGroupPrivilege(c *bm.Context) {
  249. v := new(struct {
  250. Add string `form:"add"`
  251. Minus string `form:"minus"`
  252. GroupID int64 `form:"group_id"`
  253. Type int `form:"type"` // 1 数据源
  254. })
  255. if err := c.Bind(v); err != nil {
  256. return
  257. }
  258. err := svr.UpdateAuthorityGroupPrivilege(v.GroupID, v.Add, v.Minus, v.Type)
  259. if err != nil {
  260. log.Error("growup svr.UpdateAuthorityGroupPrivilege error(%v)", err)
  261. }
  262. c.JSON(nil, err)
  263. }
  264. func listAuthorityTaskRoles(c *bm.Context) {
  265. username, _, err := checkCookie(c)
  266. if err != nil {
  267. c.JSON(nil, err)
  268. log.Error("growup checkCookie error(%v)", err)
  269. return
  270. }
  271. v := new(struct {
  272. From int `form:"from" validate:"min=0" default:"0"`
  273. Limit int `form:"limit" validate:"min=1" default:"20"`
  274. Sort string `form:"sort"`
  275. })
  276. if err = c.Bind(v); err != nil {
  277. return
  278. }
  279. roles, total, err := svr.ListAuthorityTaskRoles(username, v.From, v.Limit, v.Sort)
  280. if err != nil {
  281. c.JSON(nil, err)
  282. log.Error("growup svr.ListAuthorityTaskRoles error(%v)", err)
  283. return
  284. }
  285. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  286. "code": 0,
  287. "message": "0",
  288. "data": roles,
  289. "paging": map[string]int{
  290. "page_size": v.Limit,
  291. "total": total,
  292. },
  293. }))
  294. }
  295. func addAuthorityTaskRole(c *bm.Context) {
  296. v := new(struct {
  297. GroupID int64 `form:"group_id" validate:"required"`
  298. Name string `form:"name"`
  299. Desc string `form:"desc"`
  300. })
  301. if err := c.Bind(v); err != nil {
  302. return
  303. }
  304. err := svr.AddAuthorityTaskRole(v.GroupID, v.Name, v.Desc)
  305. if err != nil {
  306. log.Error("growup svr.AddAuthorityTaskRole error(%v)", err)
  307. }
  308. c.JSON(nil, err)
  309. }
  310. func addAuthorityTaskRoleUser(c *bm.Context) {
  311. v := new(struct {
  312. Username string `form:"username"`
  313. RoleID string `form:"role_id"`
  314. })
  315. if err := c.Bind(v); err != nil {
  316. return
  317. }
  318. err := svr.AddAuthorityTaskRoleUser(v.Username, v.RoleID)
  319. if err != nil {
  320. log.Error("growup svr.AddAuthorityTaskRoleUser error(%v)", err)
  321. }
  322. c.JSON(nil, err)
  323. }
  324. func updateAuthorityTaskRoleInfo(c *bm.Context) {
  325. v := new(struct {
  326. RoleID int64 `form:"role_id"`
  327. Name string `form:"name"`
  328. Desc string `form:"desc"`
  329. })
  330. if err := c.Bind(v); err != nil {
  331. return
  332. }
  333. err := svr.UpdateAuthorityTaskRoleInfo(v.RoleID, v.Name, v.Desc)
  334. if err != nil {
  335. log.Error("growup svr.UpdateAuthorityTaskRoleInfo error(%v)", err)
  336. }
  337. c.JSON(nil, err)
  338. }
  339. func deleteAuthorityTaskRole(c *bm.Context) {
  340. v := new(struct {
  341. RoleID int64 `form:"role_id"`
  342. })
  343. if err := c.Bind(v); err != nil {
  344. return
  345. }
  346. err := svr.DeleteAuthorityTaskRole(v.RoleID)
  347. if err != nil {
  348. log.Error("growup svr.DeleteAuthorityTaskRole error(%v)", err)
  349. }
  350. c.JSON(nil, err)
  351. }
  352. func deleteAuthorityTaskRoleUser(c *bm.Context) {
  353. v := new(struct {
  354. ID int64 `form:"id"`
  355. RoleID int64 `form:"role_id"`
  356. })
  357. if err := c.Bind(v); err != nil {
  358. return
  359. }
  360. err := svr.DeleteAuthorityTaskRoleUser(v.ID, v.RoleID)
  361. if err != nil {
  362. log.Error("growup svr.DeleteAuthorityTaskRoleUser error(%v)", err)
  363. }
  364. c.JSON(nil, err)
  365. }
  366. func listAuthorityRolePrivilege(c *bm.Context) {
  367. v := new(struct {
  368. GroupID int64 `form:"group_id"`
  369. RoleID int64 `form:"role_id"`
  370. FatherID int64 `form:"father_id" validate:"required"`
  371. })
  372. if err := c.Bind(v); err != nil {
  373. return
  374. }
  375. data, err := svr.ListAuthorityRolePrivilege(v.GroupID, v.RoleID, v.FatherID)
  376. if err != nil {
  377. c.JSON(nil, err)
  378. log.Error("growup svr.ListAuthorityRolePrivilege error(%v)", err)
  379. return
  380. }
  381. c.JSON(data, nil)
  382. }
  383. func updateAuthorityRolePrivilege(c *bm.Context) {
  384. v := new(struct {
  385. Add string `form:"add"`
  386. Minus string `form:"minus"`
  387. RoleID int64 `form:"role_id"`
  388. })
  389. if err := c.Bind(v); err != nil {
  390. return
  391. }
  392. err := svr.UpdateAuthorityRolePrivilege(v.RoleID, v.Add, v.Minus)
  393. if err != nil {
  394. log.Error("growup svr.UpdateAuthorityRolePrivilege error(%v)", err)
  395. }
  396. c.JSON(nil, err)
  397. }
  398. func listAuthorityGroupAndRole(c *bm.Context) {
  399. groups, roles, err := svr.ListGroupAndRole()
  400. if err != nil {
  401. c.JSON(nil, err)
  402. log.Error("growup svr.ListGroupAndRole error(%v)", err)
  403. return
  404. }
  405. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  406. "code": 0,
  407. "message": "0",
  408. "data": map[string]interface{}{
  409. "groups": groups,
  410. "roles": roles,
  411. },
  412. }))
  413. }
  414. func listPrivilege(c *bm.Context) {
  415. data, err := svr.ListPrivilege()
  416. if err != nil {
  417. c.JSON(nil, err)
  418. log.Error("growup svr.AddPrivilege error(%v)", err)
  419. return
  420. }
  421. c.JSON(data, nil)
  422. }
  423. func addPrivilege(c *bm.Context) {
  424. v := new(struct {
  425. Level int64 `form:"level" validate:"required"`
  426. Name string `form:"name" validate:"required"`
  427. FatherID int64 `form:"father_id"`
  428. IsRouter uint8 `form:"is_router"`
  429. })
  430. if err := c.Bind(v); err != nil {
  431. return
  432. }
  433. if v.Level > 1 && v.FatherID == 0 {
  434. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  435. "code": ecode.RequestErr,
  436. "message": "privilege > 1 but father_id = 0",
  437. }))
  438. return
  439. }
  440. err := svr.AddPrivilege(v.Name, v.Level, v.FatherID, v.IsRouter)
  441. if err != nil {
  442. log.Error("growup svr.AddPrivilege error(%v)", err)
  443. }
  444. c.JSON(nil, err)
  445. }
  446. func updatePrivilege(c *bm.Context) {
  447. v := new(struct {
  448. ID int64 `form:"id" validate:"required"`
  449. Level int64 `form:"level" validate:"required"`
  450. Name string `form:"name" validate:"required"`
  451. FatherID int64 `form:"father_id"`
  452. IsRouter uint8 `form:"is_router"`
  453. })
  454. if err := c.Bind(v); err != nil {
  455. return
  456. }
  457. if v.Level > 1 && v.FatherID == 0 {
  458. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  459. "code": ecode.RequestErr,
  460. "message": "privilege > 1 but father_id = 0",
  461. }))
  462. return
  463. }
  464. err := svr.UpdatePrivilege(v.ID, v.Name, v.Level, v.FatherID, v.IsRouter)
  465. if err != nil {
  466. log.Error("growup svr.UpdatePrivilege error(%v)", err)
  467. }
  468. c.JSON(nil, err)
  469. }
  470. func busPrivilege(c *bm.Context) {
  471. username, _, err := checkCookie(c)
  472. if err != nil {
  473. c.JSON(nil, err)
  474. log.Error("growup checkCookie error(%v)", err)
  475. return
  476. }
  477. v := new(struct {
  478. Type string `form:"type"`
  479. })
  480. if err = c.Bind(v); err != nil {
  481. return
  482. }
  483. data, err := svr.BusPrivilege(c, username, v.Type)
  484. if err != nil {
  485. c.JSON(nil, err)
  486. log.Error("growup svr.BusPrivilege error(%v)", err)
  487. return
  488. }
  489. c.JSON(data, nil)
  490. }