mng_auth.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. _authRelationSQL = `SELECT rid,auth_id FROM auth_relation`
  10. _authsSQL = `SELECT id,system,name,flag,ctime,mtime FROM auth`
  11. // auth.
  12. _inAuthSQL = `INSERT INTO auth (system,name,flag) VALUES(?,?,?)`
  13. _upAuthSQL = `UPDATE auth SET name=? WHERE id=?`
  14. _delAuthSQL = `DELETE FROM auth WHERE id=?`
  15. // auth_relation.
  16. _inAuthRelationSQL = `INSERT INTO auth_relation (rid,auth_id) VALUES(?,?)`
  17. _delAuthRelationSQL = `DELETE FROM auth_relation WHERE rid=? AND auth_id=?`
  18. _cleanRelationByAuth = "DELETE FROM auth_relation WHERE auth_id=?"
  19. )
  20. // Auths select all auth from db.
  21. func (d *Dao) Auths(c context.Context) (res map[string]map[int64]*model.Auth, err error) {
  22. rows, err := d.db.Query(c, _authsSQL)
  23. if err != nil {
  24. log.Error("Auths d.db.Query(%d) error(%v)", err)
  25. return
  26. }
  27. defer rows.Close()
  28. res = make(map[string]map[int64]*model.Auth)
  29. for rows.Next() {
  30. var (
  31. auths map[int64]*model.Auth
  32. ok bool
  33. )
  34. auth := &model.Auth{}
  35. if err = rows.Scan(&auth.AuthID, &auth.System, &auth.AuthName, &auth.AuthFlag, &auth.CTime, &auth.MTime); err != nil {
  36. log.Error("Auths rows.Scan error(%v)", err)
  37. return
  38. }
  39. if auths, ok = res[auth.System]; !ok {
  40. auths = make(map[int64]*model.Auth)
  41. res[auth.System] = auths
  42. }
  43. auths[auth.AuthID] = auth
  44. }
  45. return
  46. }
  47. // AddAuth insert auth.
  48. func (d *Dao) AddAuth(c context.Context, system, authName, authFlag string) (rows int64, err error) {
  49. res, err := d.db.Exec(c, _inAuthSQL, system, authName, authFlag)
  50. if err != nil {
  51. log.Error("AddAuth d.db.Exec() error(%v)", err)
  52. return
  53. }
  54. rows, err = res.RowsAffected()
  55. return
  56. }
  57. // UpAuth update auth.
  58. func (d *Dao) UpAuth(c context.Context, authName string, authID int64) (rows int64, err error) {
  59. res, err := d.db.Exec(c, _upAuthSQL, authName, authID)
  60. if err != nil {
  61. log.Error("UpAuth d.db.Exec() error(%v)", err)
  62. return
  63. }
  64. rows, err = res.RowsAffected()
  65. return
  66. }
  67. // DelAuth del auth.
  68. func (d *Dao) DelAuth(c context.Context, authID int64) (rows int64, err error) {
  69. res, err := d.db.Exec(c, _delAuthSQL, authID)
  70. if err != nil {
  71. log.Error("DelAuth d.db.Exec() error(%v)", err)
  72. return
  73. }
  74. rows, err = res.RowsAffected()
  75. return
  76. }
  77. // CleanAuthRelationByAuth del all auth relation by auth.
  78. func (d *Dao) CleanAuthRelationByAuth(c context.Context, authID int64) (rows int64, err error) {
  79. res, err := d.db.Exec(c, _cleanRelationByAuth, authID)
  80. if err != nil {
  81. log.Error("CleanAuthRelationByAuth d.db.Exec() error(%v)", err)
  82. return
  83. }
  84. rows, err = res.RowsAffected()
  85. return
  86. }
  87. // AuthRelation select all auth_relation from db.
  88. func (d *Dao) AuthRelation(c context.Context) (res map[int64][]int64, err error) {
  89. rows, err := d.db.Query(c, _authRelationSQL)
  90. if err != nil {
  91. log.Error("Roles d.db.Query(%d) error(%v)", err)
  92. return
  93. }
  94. defer rows.Close()
  95. res = make(map[int64][]int64)
  96. for rows.Next() {
  97. var (
  98. roleID, authID int64
  99. )
  100. if err = rows.Scan(&roleID, &authID); err != nil {
  101. log.Error("Roles rows.Scan error(%v)", err)
  102. return
  103. }
  104. res[roleID] = append(res[roleID], authID)
  105. }
  106. return
  107. }
  108. // AddAuthRelation insert auth_relation.
  109. func (d *Dao) AddAuthRelation(c context.Context, roleID, authID int64) (rows int64, err error) {
  110. res, err := d.db.Exec(c, _inAuthRelationSQL, roleID, authID)
  111. if err != nil {
  112. log.Error("AddAuthRelation d.db.Exec() error(%v)", err)
  113. return
  114. }
  115. rows, err = res.RowsAffected()
  116. return
  117. }
  118. // DelAuthRelation del auth_relation.
  119. func (d *Dao) DelAuthRelation(c context.Context, roleID, authID int64) (rows int64, err error) {
  120. res, err := d.db.Exec(c, _delAuthRelationSQL, roleID, authID)
  121. if err != nil {
  122. log.Error("DelAuthRelation d.db.Exec() error(%v)", err)
  123. return
  124. }
  125. rows, err = res.RowsAffected()
  126. return
  127. }