mng_role.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package dao
  2. import (
  3. "context"
  4. model "go-common/app/admin/main/macross/model/manager"
  5. "go-common/library/log"
  6. )
  7. const (
  8. // load cache(get all).
  9. _rolesSQL = `SELECT id,system,name,ctime,mtime FROM role`
  10. // role.
  11. _inRoleSQL = `INSERT INTO role (system,name) VALUES(?,?)`
  12. _upRoleSQL = `UPDATE role SET name=? WHERE id=?`
  13. _delRoleSQL = `DELETE FROM role WHERE id=?`
  14. _cleanRelationByRole = "DELETE FROM auth_relation WHERE rid=?"
  15. )
  16. // Roles select all role from db.
  17. func (d *Dao) Roles(c context.Context) (res map[string]map[int64]*model.Role, err error) {
  18. rows, err := d.db.Query(c, _rolesSQL)
  19. if err != nil {
  20. log.Error("Roles d.db.Query(%d) error(%v)", err)
  21. return
  22. }
  23. defer rows.Close()
  24. res = make(map[string]map[int64]*model.Role)
  25. for rows.Next() {
  26. var (
  27. roles map[int64]*model.Role
  28. ok bool
  29. )
  30. role := &model.Role{}
  31. if err = rows.Scan(&role.RoleID, &role.System, &role.RoleName, &role.CTime, &role.MTime); err != nil {
  32. log.Error("Roles rows.Scan error(%v)", err)
  33. return
  34. }
  35. if roles, ok = res[role.System]; !ok {
  36. roles = make(map[int64]*model.Role)
  37. res[role.System] = roles
  38. }
  39. roles[role.RoleID] = role
  40. }
  41. return
  42. }
  43. // AddRole insert role.
  44. func (d *Dao) AddRole(c context.Context, system, roleName string) (rows int64, err error) {
  45. res, err := d.db.Exec(c, _inRoleSQL, system, roleName)
  46. if err != nil {
  47. log.Error("AddRole d.db.Exec() error(%v)", err)
  48. return
  49. }
  50. rows, err = res.RowsAffected()
  51. return
  52. }
  53. // UpRole update role.
  54. func (d *Dao) UpRole(c context.Context, roleName string, roleID int64) (rows int64, err error) {
  55. res, err := d.db.Exec(c, _upRoleSQL, roleName, roleID)
  56. if err != nil {
  57. log.Error("UpRole d.db.Exec() error(%v)", err)
  58. return
  59. }
  60. rows, err = res.RowsAffected()
  61. return
  62. }
  63. // DelRole del role.
  64. func (d *Dao) DelRole(c context.Context, roleID int64) (rows int64, err error) {
  65. res, err := d.db.Exec(c, _delRoleSQL, roleID)
  66. if err != nil {
  67. log.Error("DelRole d.db.Exec() error(%v)", err)
  68. return
  69. }
  70. rows, err = res.RowsAffected()
  71. return
  72. }
  73. // CleanAuthRelationByRole del all auth relation by role.
  74. func (d *Dao) CleanAuthRelationByRole(c context.Context, roleID int64) (rows int64, err error) {
  75. res, err := d.db.Exec(c, _cleanRelationByRole, roleID)
  76. if err != nil {
  77. log.Error("CleanAuthRelationByRole d.db.Exec() error(%v)", err)
  78. return
  79. }
  80. rows, err = res.RowsAffected()
  81. return
  82. }