authority.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/growup/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. xtime "go-common/library/time"
  13. )
  14. // const for default groupid
  15. const (
  16. // task group Admin id
  17. AdminID = 1
  18. )
  19. var (
  20. // task role list privilege id
  21. _privilegeTaskRoleList = "12,48"
  22. // admin
  23. _privilegePrivilege = "7,8,9,10,11,12,38,39,40,41,42,43,44,45,46,47,48"
  24. )
  25. func removeDuplicatesAndEmpty(a []string) (ret []string) {
  26. for i := 0; i < len(a); i++ {
  27. if (i > 0 && a[i-1] == a[i]) || len(a[i]) == 0 {
  28. continue
  29. }
  30. ret = append(ret, a[i])
  31. }
  32. return
  33. }
  34. // GetUserPri get user privilege
  35. func (s *Service) GetUserPri(username string) (priMap map[int64]bool, err error) {
  36. priMap = make(map[int64]bool)
  37. query := fmt.Sprintf("username = '%s'", username)
  38. users, err := s.dao.GetAuthorityUsersInfo(query, "id, task_group, task_role")
  39. if err != nil {
  40. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  41. return
  42. }
  43. if len(users) == 0 {
  44. return
  45. }
  46. if len(users) > 1 {
  47. err = errors.New("get user error")
  48. return
  49. }
  50. userPrivileges := []string{}
  51. var taskPrivileges string
  52. user := users[0]
  53. if user.TaskGroup != "" {
  54. groups := strings.Split(user.TaskGroup, ",")
  55. for _, groupID := range groups {
  56. id, _ := strconv.ParseInt(groupID, 10, 64)
  57. taskPrivileges, err = s.dao.GetAuthorityTaskGroupPrivileges(id)
  58. if err != nil {
  59. log.Error("s.dao.GetAuthorityTaskGroupPrivileges Error(%v)", err)
  60. return
  61. }
  62. userPrivileges = append(userPrivileges, strings.Split(taskPrivileges, ",")...)
  63. }
  64. }
  65. if user.TaskRole != "" {
  66. roles := strings.Split(user.TaskRole, ",")
  67. for _, roleID := range roles {
  68. id, _ := strconv.ParseInt(roleID, 10, 64)
  69. taskPrivileges, err = s.dao.GetAuthorityTaskRolePrivileges(id)
  70. if err != nil {
  71. log.Error("s.dao.GetAuthorityTaskRolePrivileges Error(%v)", err)
  72. return
  73. }
  74. userPrivileges = append(userPrivileges, strings.Split(taskPrivileges, ",")...)
  75. }
  76. }
  77. sort.Strings(userPrivileges)
  78. userPrivileges = removeDuplicatesAndEmpty(userPrivileges)
  79. for _, privilegesID := range userPrivileges {
  80. id, _ := strconv.ParseInt(privilegesID, 10, 64)
  81. priMap[id] = true
  82. }
  83. return
  84. }
  85. // GetAuthorityUserPrivileges get user all privileges
  86. func (s *Service) GetAuthorityUserPrivileges(username string) (data interface{}, err error) {
  87. mPrivileges, err := s.GetUserPri(username)
  88. if err != nil {
  89. log.Error("s.GetUserPri Error(%v)", err)
  90. return
  91. }
  92. if len(mPrivileges) == 0 {
  93. data = map[string]interface{}{
  94. "router": []string{},
  95. }
  96. return
  97. }
  98. all, err := s.ListPrivilege()
  99. if err != nil {
  100. log.Error("s.ListPrivilege Error(%v)", err)
  101. return
  102. }
  103. router := []int64{}
  104. for _, level1 := range all {
  105. for _, level2 := range level1.Children {
  106. for _, level3 := range level2.Children {
  107. if _, ok := mPrivileges[level3.ID]; ok {
  108. level3.Selected = true
  109. level2.Selected = true
  110. level1.Selected = true
  111. if level3.IsRouter == 1 {
  112. router = append(router, level3.ID)
  113. }
  114. }
  115. }
  116. }
  117. }
  118. data = map[string]interface{}{
  119. "privileges": all,
  120. "router": router,
  121. }
  122. return
  123. }
  124. // GetAuthorityUserGroup get authority user group
  125. func (s *Service) GetAuthorityUserGroup(username string) (data []*model.Group, err error) {
  126. query := fmt.Sprintf("username = '%s'", username)
  127. users, err := s.dao.GetAuthorityUsersInfo(query, "id, task_group")
  128. if err != nil {
  129. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  130. return
  131. }
  132. if len(users) == 0 {
  133. return
  134. }
  135. if len(users) > 1 {
  136. err = errors.New("get user error")
  137. return
  138. }
  139. if users[0].TaskGroup == "" {
  140. return
  141. }
  142. groups := strings.Split(users[0].TaskGroup, ",")
  143. allGroups, err := s.dao.GetAuthorityTaskGroups("")
  144. if err != nil {
  145. log.Error("s.dao.ListAuthorityTaskGroups Error(%v)", err)
  146. return
  147. }
  148. gMap := make(map[int64]*model.Group)
  149. for _, g := range allGroups {
  150. gMap[g.ID] = g
  151. }
  152. data = make([]*model.Group, 0)
  153. for _, id := range groups {
  154. gID, _ := strconv.ParseInt(id, 10, 64)
  155. if gID == 1 {
  156. data = allGroups
  157. return
  158. }
  159. data = append(data, gMap[gID])
  160. }
  161. return
  162. }
  163. // ListAuthorityUsers list all users in authority-manage
  164. func (s *Service) ListAuthorityUsers(username string, from, limit int, sort string) (users []*model.User, total int, err error) {
  165. query := ""
  166. if len(username) != 0 {
  167. query = fmt.Sprintf("username = '%s'", username)
  168. }
  169. users, total, err = s.dao.ListAuthorityUsers(query, from, limit, sort)
  170. for i := 0; i < len(users); i++ {
  171. taskGroup := users[i].TaskGroup
  172. groups := []*model.Group{}
  173. if len(taskGroup) > 0 {
  174. query = fmt.Sprintf("id in (%s)", taskGroup)
  175. groups, err = s.dao.GetAuthorityTaskGroups(query)
  176. if err != nil {
  177. log.Error("s.dao.GetAuthorityTaskGroups Error(%v)", err)
  178. return
  179. }
  180. }
  181. users[i].Groups = groups
  182. taskRole := users[i].TaskRole
  183. roles := []*model.Role{}
  184. if len(taskRole) > 0 {
  185. query = fmt.Sprintf("id in (%s)", taskRole)
  186. roles, err = s.dao.GetAuthorityTaskRoles(query)
  187. if err != nil {
  188. log.Error("s.dao.GetAuthorityTaskRoles Error(%v)", err)
  189. return
  190. }
  191. }
  192. users[i].Roles = roles
  193. }
  194. return
  195. }
  196. // AddAuthorityUser add user to authority-manage
  197. func (s *Service) AddAuthorityUser(username, nickname string) (err error) {
  198. user := model.User{
  199. Username: username,
  200. Nickname: nickname,
  201. ATime: xtime.Time(time.Now().Unix()),
  202. }
  203. return s.dao.AddAuthorityUser(&user)
  204. }
  205. // UpdateAuthorityUserInfo update user's nickname
  206. func (s *Service) UpdateAuthorityUserInfo(id int64, nickname string) (err error) {
  207. update := map[string]interface{}{
  208. "nickname": nickname,
  209. }
  210. return s.dao.UpdateAuthorityUser(id, update)
  211. }
  212. // UpdateAuthorityUserAuth update user's task group and task role
  213. func (s *Service) UpdateAuthorityUserAuth(id int64, groupID, roleID string) (err error) {
  214. update := map[string]interface{}{
  215. "task_group": groupID,
  216. "task_role": roleID,
  217. }
  218. return s.dao.UpdateAuthorityUser(id, update)
  219. }
  220. // DeleteAuthorityUser delete user from authority-manage from id
  221. func (s *Service) DeleteAuthorityUser(id int64) (err error) {
  222. return s.dao.DeleteAuthorityUser(id)
  223. }
  224. func getAuthorityGroupUsers(groupID string, users []*model.User) (result []*model.SUser) {
  225. result = []*model.SUser{}
  226. for _, user := range users {
  227. groupIDs := strings.Split(user.TaskGroup, ",")
  228. for _, id := range groupIDs {
  229. if id == groupID {
  230. result = append(result, &model.SUser{
  231. ID: user.ID,
  232. Name: user.Username,
  233. })
  234. break
  235. }
  236. }
  237. }
  238. return
  239. }
  240. // ListAuthorityTaskGroups list all task groups in authority-manage
  241. func (s *Service) ListAuthorityTaskGroups(from, limit int, sort string) (groups []*model.TaskGroup, total int, err error) {
  242. groups, total, err = s.dao.ListAuthorityTaskGroups("", from, limit, sort)
  243. if err != nil {
  244. log.Error("s.dao.ListAuthorityTaskGroups Error(%v)", err)
  245. return
  246. }
  247. users, err := s.dao.GetAuthorityUsersInfo("", "id, username, task_group")
  248. if err != nil {
  249. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  250. return
  251. }
  252. for i := 0; i < len(groups); i++ {
  253. groups[i].Users = getAuthorityGroupUsers(strconv.FormatInt(groups[i].ID, 10), users)
  254. }
  255. return
  256. }
  257. // AddAuthorityTaskGroup add new task group to authority-manage and add privilege 48
  258. func (s *Service) AddAuthorityTaskGroup(name, desc string) (err error) {
  259. if len(name) == 0 {
  260. return errors.New("get group name error")
  261. }
  262. group := model.TaskGroup{
  263. Name: name,
  264. Desc: desc,
  265. ATime: xtime.Time(time.Now().Unix()),
  266. }
  267. err = s.dao.AddAuthorityTaskGroup(&group)
  268. if err != nil {
  269. log.Error("s.dao.AddAuthorityTaskGroup Error(%v)", err)
  270. return
  271. }
  272. newGroup, err := s.dao.GetAuthorityTaskGroup(fmt.Sprintf("name = '%s'", name))
  273. if err != nil {
  274. log.Error("s.dao.AddAuthorityTaskGroup Error(%v)", err)
  275. return
  276. }
  277. return s.UpdateAuthorityGroupPrivilege(newGroup.ID, "", "", 0)
  278. }
  279. // AddAuthorityTaskGroupUser add user to task group
  280. func (s *Service) AddAuthorityTaskGroupUser(username, groupID string) (err error) {
  281. if len(username) == 0 {
  282. return errors.New("get username error")
  283. }
  284. var users []*model.User
  285. query := fmt.Sprintf("username = '%s'", username)
  286. users, err = s.dao.GetAuthorityUsersInfo(query, "id, task_group")
  287. if err != nil {
  288. log.Error("s.dao.GetAuthorityUsersInfo error(%v)", err)
  289. return
  290. }
  291. if len(users) != 1 {
  292. return ecode.GrowupAuthorityUserNotFound
  293. }
  294. if len(users[0].TaskGroup) != 0 {
  295. groupID = users[0].TaskGroup + "," + groupID
  296. }
  297. update := map[string]interface{}{"task_group": groupID}
  298. err = s.dao.UpdateAuthorityUser(users[0].ID, update)
  299. if err != nil {
  300. log.Error("s.dao.UpdateAuthorityUser error(%v)", err)
  301. }
  302. return
  303. }
  304. // UpdateAuthorityTaskGroupInfo update task group info
  305. func (s *Service) UpdateAuthorityTaskGroupInfo(groupID int64, name, desc string) (err error) {
  306. update := make(map[string]interface{})
  307. if len(desc) != 0 {
  308. update["desc"] = desc
  309. }
  310. if len(name) != 0 {
  311. update["name"] = name
  312. }
  313. return s.dao.UpdateAuthorityTaskGroup(groupID, update)
  314. }
  315. func spliceStrs(strs []string) (ret string) {
  316. for _, str := range strs {
  317. ret += str + ","
  318. }
  319. if len(ret) == 0 {
  320. return
  321. }
  322. return ret[:len(ret)-1]
  323. }
  324. func (s *Service) updateAuthorityUsersGroup(groupID string, users []*model.User) (err error) {
  325. for _, user := range users {
  326. groupIDs := strings.Split(user.TaskGroup, ",")
  327. for i := 0; i < len(groupIDs); i++ {
  328. if groupIDs[i] == groupID {
  329. groupIDs = append(groupIDs[:i], groupIDs[i+1:]...)
  330. err = s.dao.UpdateAuthorityUser(user.ID, map[string]interface{}{"task_group": spliceStrs(groupIDs)})
  331. if err != nil {
  332. log.Error("s.dao.UpdateAuthorityUser error(%v)", err)
  333. return
  334. }
  335. break
  336. }
  337. }
  338. }
  339. return
  340. }
  341. // DeleteAuthorityTaskGroup delete task group
  342. func (s *Service) DeleteAuthorityTaskGroup(groupID int64) (err error) {
  343. err = s.dao.DeleteAuthorityTaskGroup(groupID)
  344. if err != nil {
  345. log.Error("s.dao.DeleteAuthorityTaskGroup error(%v)", err)
  346. return
  347. }
  348. // update users task group
  349. users, err := s.dao.GetAuthorityUsersInfo("", "id, task_group")
  350. if err != nil {
  351. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  352. return
  353. }
  354. err = s.updateAuthorityUsersGroup(strconv.FormatInt(groupID, 10), users)
  355. if err != nil {
  356. log.Error("s.updateAuthorityUsersGroup error(%v)", err)
  357. return
  358. }
  359. // delete task role which belong this group
  360. query := fmt.Sprintf("group_id = %d", groupID)
  361. roles, err := s.dao.GetAuthorityTaskRoles(query)
  362. for _, role := range roles {
  363. err = s.DeleteAuthorityTaskRole(role.ID)
  364. if err != nil {
  365. log.Error("s.DeleteAuthorityTaskRole error(%v)", err)
  366. return
  367. }
  368. }
  369. return
  370. }
  371. // DeleteAuthorityTaskGroupUser delete user from task group
  372. func (s *Service) DeleteAuthorityTaskGroupUser(id, groupID int64) (err error) {
  373. query := fmt.Sprintf("id = %d", id)
  374. users, err := s.dao.GetAuthorityUsersInfo(query, "id, task_group")
  375. if err != nil {
  376. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  377. return
  378. }
  379. err = s.updateAuthorityUsersGroup(strconv.FormatInt(groupID, 10), users)
  380. if err != nil {
  381. log.Error("s.updateAuthorityUsersGroup error(%v)", err)
  382. }
  383. return
  384. }
  385. // ListAuthorityGroupPrivilege list task group's privileges
  386. func (s *Service) ListAuthorityGroupPrivilege(groupID int64, fatherID int64) (ret *model.SPrivilege, err error) {
  387. var privilege string
  388. privilege, err = s.dao.GetAuthorityTaskGroupPrivileges(groupID)
  389. if err != nil {
  390. log.Error("s.dao.GetAuthorityTaskGroupPrivileges Error(%v)", err)
  391. return
  392. }
  393. privileges := strings.Split(privilege, ",")
  394. var data []*model.SPrivilege
  395. data, err = s.ListPrivilege()
  396. if err != nil {
  397. log.Error("s.ListPrivilege Error(%v)", err)
  398. return
  399. }
  400. for i := 0; i < len(data); i++ {
  401. if data[i].ID == fatherID {
  402. ret = data[i]
  403. for _, idStr := range privileges {
  404. id, _ := strconv.ParseInt(idStr, 10, 64)
  405. for _, level2 := range ret.Children {
  406. for _, level3 := range level2.Children {
  407. if level3.ID == id {
  408. level3.Selected = true
  409. level2.Selected = true
  410. }
  411. }
  412. }
  413. }
  414. ret.Selected = true
  415. break
  416. }
  417. }
  418. return
  419. }
  420. // UpdateAuthorityGroupPrivilege update group task privileges
  421. func (s *Service) UpdateAuthorityGroupPrivilege(groupID int64, add, minus string, authType int) (err error) {
  422. // get old privilege by group id
  423. privilege, err := s.dao.GetAuthorityTaskGroupPrivileges(groupID)
  424. if err != nil {
  425. log.Error("s.dao.GetAuthorityTaskGroupPrivileges Error(%v)", err)
  426. return
  427. }
  428. newP := make(map[string]struct{})
  429. privilegeSli := strings.Split(privilege, ",")
  430. for _, p := range privilegeSli {
  431. if p == "" {
  432. continue
  433. }
  434. newP[p] = struct{}{}
  435. }
  436. // default add task role list privilege
  437. // 数据源权限不需要添加
  438. if authType == 0 {
  439. add += "," + _privilegeTaskRoleList
  440. if groupID == 1 {
  441. add += "," + _privilegePrivilege
  442. }
  443. add = strings.TrimPrefix(add, ",")
  444. }
  445. for _, p := range strings.Split(add, ",") {
  446. if p == "" {
  447. continue
  448. }
  449. newP[p] = struct{}{}
  450. }
  451. // minus
  452. for _, p := range strings.Split(minus, ",") {
  453. if p == "" {
  454. continue
  455. }
  456. delete(newP, p)
  457. }
  458. privileges := ""
  459. for p := range newP {
  460. privileges += p + ","
  461. }
  462. update := map[string]interface{}{
  463. "privileges": strings.TrimSuffix(privileges, ","),
  464. }
  465. return s.dao.UpdateAuthorityTaskGroup(groupID, update)
  466. }
  467. func getAuthorityRoleUsers(roleID string, users []*model.User) (result []*model.SUser) {
  468. result = []*model.SUser{}
  469. for _, user := range users {
  470. roleIDs := strings.Split(user.TaskRole, ",")
  471. for _, id := range roleIDs {
  472. if id == roleID {
  473. result = append(result, &model.SUser{
  474. ID: user.ID,
  475. Name: user.Username,
  476. })
  477. break
  478. }
  479. }
  480. }
  481. return
  482. }
  483. // ListAuthorityTaskRoles list user's task roles
  484. func (s *Service) ListAuthorityTaskRoles(username string, from, limit int, sort string) (roles []*model.TaskRole, total int, err error) {
  485. query := fmt.Sprintf("username = '%s'", username)
  486. users, err := s.dao.GetAuthorityUsersInfo(query, "task_group")
  487. if err != nil {
  488. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  489. return
  490. }
  491. if len(users) == 0 || len(users) > 1 {
  492. err = ecode.GrowupAuthorityUserNotFound
  493. return
  494. }
  495. user := users[0]
  496. query = fmt.Sprintf("group_id in (%s)", user.TaskGroup)
  497. groupIDs := strings.Split(user.TaskGroup, ",")
  498. for _, groupID := range groupIDs {
  499. if groupID == strconv.Itoa(AdminID) { // Admin
  500. query = ""
  501. break
  502. }
  503. }
  504. roles, total, err = s.dao.ListAuthorityTaskRoles(query, from, limit, sort)
  505. if err != nil {
  506. log.Error("s.dao.ListAuthorityTaskRoles Error(%v)", err)
  507. return
  508. }
  509. users, err = s.dao.GetAuthorityUsersInfo("", "id, username, task_role")
  510. if err != nil {
  511. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  512. return
  513. }
  514. for i := 0; i < len(roles); i++ {
  515. roles[i].Users = getAuthorityRoleUsers(strconv.FormatInt(roles[i].ID, 10), users)
  516. roles[i].GroupName, err = s.dao.GetAuthorityTaskGroupName(roles[i].GroupID)
  517. if err != nil {
  518. log.Error("s.dao.GetAuthorityTaskGroupName Error(%v)", err)
  519. return
  520. }
  521. }
  522. return
  523. }
  524. // AddAuthorityTaskRole add task role to authority-manage
  525. func (s *Service) AddAuthorityTaskRole(groupID int64, name, desc string) (err error) {
  526. if len(name) == 0 {
  527. return errors.New("get role name error")
  528. }
  529. role := model.TaskRole{
  530. Name: name,
  531. Desc: desc,
  532. GroupID: groupID,
  533. ATime: xtime.Time(time.Now().Unix()),
  534. }
  535. return s.dao.AddAuthorityTaskRole(&role)
  536. }
  537. // AddAuthorityTaskRoleUser add user to task group
  538. func (s *Service) AddAuthorityTaskRoleUser(username, roleID string) (err error) {
  539. if len(username) == 0 {
  540. return errors.New("get username error")
  541. }
  542. var users []*model.User
  543. query := fmt.Sprintf("username = '%s'", username)
  544. users, err = s.dao.GetAuthorityUsersInfo(query, "id, task_role")
  545. if err != nil {
  546. log.Error("s.dao.GetAuthorityUsersInfo error(%v)", err)
  547. return
  548. }
  549. if len(users) != 1 {
  550. return ecode.GrowupAuthorityUserNotFound
  551. }
  552. if len(users[0].TaskRole) != 0 {
  553. roleID = users[0].TaskRole + "," + roleID
  554. }
  555. update := map[string]interface{}{"task_role": roleID}
  556. err = s.dao.UpdateAuthorityUser(users[0].ID, update)
  557. if err != nil {
  558. log.Error("s.dao.UpdateAuthorityUser error(%v)", err)
  559. }
  560. return
  561. }
  562. // UpdateAuthorityTaskRoleInfo update task role info
  563. func (s *Service) UpdateAuthorityTaskRoleInfo(roleID int64, name, desc string) (err error) {
  564. update := make(map[string]interface{})
  565. if len(desc) != 0 {
  566. update["desc"] = desc
  567. }
  568. if len(name) != 0 {
  569. update["name"] = name
  570. }
  571. return s.dao.UpdateAuthorityTaskRole(roleID, update)
  572. }
  573. func (s *Service) updateAuthorityUsersRole(roleID string, users []*model.User) (err error) {
  574. for _, user := range users {
  575. roleIDs := strings.Split(user.TaskRole, ",")
  576. for i := 0; i < len(roleIDs); i++ {
  577. if roleIDs[i] == roleID {
  578. roleIDs = append(roleIDs[:i], roleIDs[i+1:]...)
  579. err = s.dao.UpdateAuthorityUser(user.ID, map[string]interface{}{"task_role": spliceStrs(roleIDs)})
  580. if err != nil {
  581. log.Error("s.dao.UpdateAuthorityUser error(%v)", err)
  582. return
  583. }
  584. break
  585. }
  586. }
  587. }
  588. return
  589. }
  590. // DeleteAuthorityTaskRole delete task role
  591. func (s *Service) DeleteAuthorityTaskRole(roleID int64) (err error) {
  592. err = s.dao.DeleteAuthorityTaskRole(roleID)
  593. if err != nil {
  594. log.Error("s.dao.DeleteAuthorityTaskRole error(%v)", err)
  595. return
  596. }
  597. // update users task role
  598. users, err := s.dao.GetAuthorityUsersInfo("", "id, task_role")
  599. if err != nil {
  600. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  601. return
  602. }
  603. err = s.updateAuthorityUsersRole(strconv.FormatInt(roleID, 10), users)
  604. if err != nil {
  605. log.Error("s.updateAuthorityUsersRole error(%v)", err)
  606. }
  607. return
  608. }
  609. // DeleteAuthorityTaskRoleUser delete user from task role
  610. func (s *Service) DeleteAuthorityTaskRoleUser(id, roleID int64) (err error) {
  611. query := fmt.Sprintf("id = %d", id)
  612. users, err := s.dao.GetAuthorityUsersInfo(query, "id, task_role")
  613. if err != nil {
  614. log.Error("s.dao.GetAuthorityUsersInfo Error(%v)", err)
  615. return
  616. }
  617. err = s.updateAuthorityUsersRole(strconv.FormatInt(roleID, 10), users)
  618. if err != nil {
  619. log.Error("s.updateAuthorityUsersRole error(%v)", err)
  620. }
  621. return
  622. }
  623. // ListAuthorityRolePrivilege list task role's privileges
  624. func (s *Service) ListAuthorityRolePrivilege(groupID, roleID int64, fatherID int64) (data *model.SPrivilege, err error) {
  625. var privilege string
  626. privilege, err = s.dao.GetAuthorityTaskRolePrivileges(roleID)
  627. if err != nil {
  628. log.Error("s.dao.GetAuthorityTaskRolePrivileges Error(%v)", err)
  629. return
  630. }
  631. privileges := strings.Split(privilege, ",")
  632. data, err = s.ListAuthorityGroupPrivilege(groupID, fatherID)
  633. if err != nil {
  634. log.Error("s.ListAuthorityGroupPrivilege Error(%v)", err)
  635. return
  636. }
  637. level2 := data.Children
  638. for i := 0; i < len(level2); i++ {
  639. if !level2[i].Selected {
  640. level2 = append(level2[:i], level2[i+1:]...)
  641. i--
  642. } else {
  643. level3 := level2[i].Children
  644. for j := 0; j < len(level3); j++ {
  645. if !level3[j].Selected {
  646. level3 = append(level3[:j], level3[j+1:]...)
  647. j--
  648. } else {
  649. level3[j].Selected = false
  650. }
  651. }
  652. level2[i].Children = level3
  653. level2[i].Selected = false
  654. }
  655. }
  656. data.Children = level2
  657. data.Selected = false
  658. for _, idStr := range privileges {
  659. id, _ := strconv.ParseInt(idStr, 10, 64)
  660. for _, level2 := range data.Children {
  661. for _, level3 := range level2.Children {
  662. if level3.ID == id {
  663. level3.Selected = true
  664. level2.Selected = true
  665. }
  666. }
  667. }
  668. }
  669. data.Selected = true
  670. return
  671. }
  672. // UpdateAuthorityRolePrivilege update role task privileges
  673. func (s *Service) UpdateAuthorityRolePrivilege(roleID int64, add, minus string) (err error) {
  674. privilege, err := s.dao.GetAuthorityTaskRolePrivileges(roleID)
  675. if err != nil {
  676. log.Error("s.dao.GetAuthorityTaskRolePrivileges Error(%v)", err)
  677. return
  678. }
  679. newP := make(map[string]struct{})
  680. privilegeSli := strings.Split(privilege, ",")
  681. for _, p := range privilegeSli {
  682. if p == "" {
  683. continue
  684. }
  685. newP[p] = struct{}{}
  686. }
  687. // add
  688. for _, p := range strings.Split(add, ",") {
  689. if p == "" {
  690. continue
  691. }
  692. newP[p] = struct{}{}
  693. }
  694. // minus
  695. for _, p := range strings.Split(minus, ",") {
  696. if p == "" {
  697. continue
  698. }
  699. delete(newP, p)
  700. }
  701. privileges := ""
  702. for p := range newP {
  703. privileges += p + ","
  704. }
  705. update := map[string]interface{}{
  706. "privileges": strings.TrimSuffix(privileges, ","),
  707. }
  708. return s.dao.UpdateAuthorityTaskRole(roleID, update)
  709. }
  710. // ListGroupAndRole list all task groups and task roles to admin
  711. func (s *Service) ListGroupAndRole() (groups []*model.Group, roles []*model.Role, err error) {
  712. groups, err = s.dao.GetAuthorityTaskGroups("")
  713. if err != nil {
  714. log.Error("s.dao.GetAuthorityTaskGroups Error(%v)", err)
  715. return
  716. }
  717. roles, err = s.dao.GetAuthorityTaskRoles("")
  718. if err != nil {
  719. log.Error("s.dao.GetAuthorityTaskRoles Error(%v)", err)
  720. return
  721. }
  722. return
  723. }
  724. // AddPrivilege all privilege
  725. func (s *Service) AddPrivilege(name string, level, fatherID int64, isRouter uint8) (err error) {
  726. privilege := model.Privilege{
  727. Name: name,
  728. Level: level,
  729. FatherID: fatherID,
  730. IsRouter: isRouter,
  731. }
  732. return s.dao.AddPrivilege(&privilege)
  733. }
  734. // UpdatePrivilege update privilege info
  735. func (s *Service) UpdatePrivilege(id int64, name string, level, fatherID int64, isRouter uint8) (err error) {
  736. update := map[string]interface{}{
  737. "name": name,
  738. "level": level,
  739. "father_id": fatherID,
  740. "is_router": isRouter,
  741. }
  742. return s.dao.UpdatePrivilege(id, update)
  743. }
  744. // ListPrivilege list privilege by level
  745. func (s *Service) ListPrivilege() (data []*model.SPrivilege, err error) {
  746. var level1, level2, level3 []*model.SPrivilege
  747. query := fmt.Sprintf("level = 1")
  748. level1, err = s.dao.GetLevelPrivileges(query)
  749. if err != nil {
  750. log.Error("s.dao.GetLevelPrivileges Error(%v)", err)
  751. return
  752. }
  753. for _, p1 := range level1 {
  754. query = fmt.Sprintf("level = 2 AND father_id = %d", p1.ID)
  755. level2, err = s.dao.GetLevelPrivileges(query)
  756. if err != nil {
  757. log.Error("s.dao.GetLevelPrivileges Error(%v)", err)
  758. return
  759. }
  760. for _, p2 := range level2 {
  761. query = fmt.Sprintf("level = 3 AND father_id = %d", p2.ID)
  762. level3, err = s.dao.GetLevelPrivileges(query)
  763. if err != nil {
  764. log.Error("s.dao.GetLevelPrivileges Error(%v)", err)
  765. return
  766. }
  767. p2.Children = level3
  768. p2.Level = 2
  769. }
  770. p1.Children = level2
  771. p1.Level = 1
  772. }
  773. data = level1
  774. return
  775. }