rbac_group.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package rbac
  2. import (
  3. "context"
  4. "gd_admin/apis"
  5. "gd_admin/common.in/jsonrpc2"
  6. "gd_admin/common.in/utils"
  7. "gd_admin/errors"
  8. "gd_admin/impl/dbmodel"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/astaxie/beego/orm"
  12. "go.uber.org/zap"
  13. "strings"
  14. "time"
  15. )
  16. // 获取所有的权限列表
  17. func GetGroupList(ctx context.Context, req *apis.GetRbacGroupListReq, reply *apis.GetRbacGroupListReply) error {
  18. // 捕获各个task中的异常并返回给调用者
  19. defer func() {
  20. if r := recover(); r != nil {
  21. err := fmt.Errorf("%+v", r)
  22. e := &jsonrpc2.Error{}
  23. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  24. l.Error("err",
  25. zap.String("system_err", err.Error()),
  26. zap.Stack("stacktrace"))
  27. }
  28. }
  29. }()
  30. // where
  31. filter := map[string]interface{}{
  32. "custom": 0,
  33. }
  34. p := dbmodel.TGdAdminRbacGroup{}
  35. // 获取权限分组信息
  36. list, err := p.FetchAll(orm.NewOrm(), filter, []string{"id", "name", "rbac_node_list"})
  37. if err != nil {
  38. if err == orm.ErrNoRows {
  39. reply.List = make([]apis.RbacGroupList, 0)
  40. return nil
  41. }
  42. l.Error("mysql",
  43. zap.String("sql", fmt.Sprintf("SELECT * FROM %s", p.TableName())),
  44. zap.String("fields", utils.MarshalJsonString(filter)),
  45. zap.String("error", err.Error()))
  46. return errors.DataBaseError
  47. }
  48. for k := range list {
  49. reply.List = append(reply.List, apis.RbacGroupList{
  50. Id: int(list[k].Id),
  51. Name: list[k].Name,
  52. Node: strings.Split(list[k].RbacNodeList, ","),
  53. })
  54. }
  55. return nil
  56. }
  57. // 修改分组权限
  58. func UpdateGroup(ctx context.Context, req *apis.UpdateRbacGroupReq, reply *apis.UpdateRbacGroupReply) error {
  59. // 捕获各个task中的异常并返回给调用者
  60. defer func() {
  61. if r := recover(); r != nil {
  62. err := fmt.Errorf("%+v", r)
  63. e := &jsonrpc2.Error{}
  64. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  65. l.Error("err",
  66. zap.String("system_err", err.Error()),
  67. zap.Stack("stacktrace"))
  68. }
  69. }
  70. }()
  71. // 参数验证
  72. if req.Id <= 0 || req.NodeId == "" || req.Name == "" {
  73. return errors.ArgsError
  74. }
  75. // 去除重复的id
  76. ids := strings.Split(req.NodeId, ",")
  77. newIds := utils.StrArrRemoveDuplicates(ids)
  78. // where
  79. filter := map[string]interface{}{
  80. "id": req.Id,
  81. }
  82. // value
  83. value := map[string]interface{}{
  84. "name": req.Name,
  85. "rbac_node_list": strings.Join(newIds, ","),
  86. "updated_at": time.Now().Format("2006-01-02 15:04:05"),
  87. }
  88. p := dbmodel.TGdAdminRbacGroup{}
  89. if _, err := p.Save(orm.NewOrm(), filter, value); err != nil {
  90. l.Error("mysql",
  91. zap.String("sql", fmt.Sprintf("Update %s", p.TableName())),
  92. zap.String("fields", utils.MarshalJsonString(filter, value)),
  93. zap.String("error", err.Error()))
  94. return errors.DataBaseError
  95. }
  96. return nil
  97. }
  98. // 删除分组
  99. func DeleteGroup(ctx context.Context, req *apis.DeleteRbacGroupReq, reply *apis.DeleteRbacGroupReply) error {
  100. // 捕获各个task中的异常并返回给调用者
  101. defer func() {
  102. if r := recover(); r != nil {
  103. err := fmt.Errorf("%+v", r)
  104. e := &jsonrpc2.Error{}
  105. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  106. l.Error("err",
  107. zap.String("system_err", err.Error()),
  108. zap.Stack("stacktrace"))
  109. }
  110. }
  111. }()
  112. // 参数验证
  113. if req.Id <= 0 {
  114. return errors.ArgsError
  115. }
  116. // where
  117. where := map[string]interface{}{
  118. "group_id": req.Id,
  119. }
  120. n := dbmodel.TGdAdminRbacAccess{}
  121. if err := n.Fetch(orm.NewOrm(), where); err != nil && err != orm.ErrNoRows {
  122. l.Error("mysql",
  123. zap.String("sql", fmt.Sprintf("SELECT * FROM %s", n.TableName())),
  124. zap.String("fields", utils.MarshalJsonString(where)),
  125. zap.String("error", err.Error()))
  126. return errors.DataBaseError
  127. }
  128. // 已存在使用用户,不允许删除
  129. if n.Uid != 0 {
  130. return errors.DelGroupErr
  131. }
  132. // where
  133. filter := map[string]interface{}{
  134. "id": req.Id,
  135. }
  136. p := dbmodel.TGdAdminRbacGroup{}
  137. if _, err := p.Delete(orm.NewOrm(), filter); err != nil {
  138. l.Error("mysql",
  139. zap.String("sql", fmt.Sprintf("Delete %s", p.TableName())),
  140. zap.String("fields", utils.MarshalJsonString(filter)),
  141. zap.String("error", err.Error()))
  142. return errors.DataBaseError
  143. }
  144. return nil
  145. }
  146. // 新增分组
  147. func AddGroup(ctx context.Context, req *apis.AddRbacGroupReq, reply *apis.AddRbacGroupReply) error {
  148. // 捕获各个task中的异常并返回给调用者
  149. defer func() {
  150. if r := recover(); r != nil {
  151. err := fmt.Errorf("%+v", r)
  152. e := &jsonrpc2.Error{}
  153. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  154. l.Error("err",
  155. zap.String("system_err", err.Error()),
  156. zap.Stack("stacktrace"))
  157. }
  158. }
  159. }()
  160. // 参数验证
  161. if req.Name == "" || req.NodeId == "" {
  162. return errors.ArgsError
  163. }
  164. // 去除重复的id
  165. ids := strings.Split(req.NodeId, ",")
  166. newIds := utils.StrArrRemoveDuplicates(ids)
  167. p := dbmodel.TGdAdminRbacGroup{
  168. Name: req.Name,
  169. RbacNodeList: strings.Join(newIds, ","),
  170. Custom: 0,
  171. CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
  172. UpdatedAt: time.Now().Format("2006-01-02 15:04:05"),
  173. }
  174. if _, err := p.Create(orm.NewOrm()); err != nil {
  175. l.Error("mysql",
  176. zap.String("sql", fmt.Sprintf("Insert %s", p.TableName())),
  177. zap.String("fields", utils.MarshalJsonString(p)),
  178. zap.String("error", err.Error()))
  179. return errors.DataBaseError
  180. }
  181. return nil
  182. }
  183. // 新增自定义分组
  184. func AddCostomGroup(db orm.Ormer, nodeIds string) (int64, error) {
  185. p := dbmodel.TGdAdminRbacGroup{}
  186. // 去除重复的id
  187. ids := strings.Split(nodeIds, ",")
  188. newIds := utils.StrArrRemoveDuplicates(ids)
  189. // where
  190. filter := map[string]interface{}{
  191. "rbac_node_list": strings.Join(newIds, ","),
  192. }
  193. err := p.Fetch(db, filter)
  194. switch {
  195. case err != nil && err != orm.ErrNoRows:
  196. l.Error("mysql",
  197. zap.String("sql", fmt.Sprintf("SELECT * FROM %s", p.TableName())),
  198. zap.String("fields", utils.MarshalJsonString(filter)),
  199. zap.String("error", err.Error()))
  200. return 0, errors.DataBaseError
  201. case err == orm.ErrNoRows:
  202. // 不存在分组,新增分组
  203. p = dbmodel.TGdAdminRbacGroup{
  204. Name: "自定义",
  205. RbacNodeList: nodeIds,
  206. Custom: 1,
  207. CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
  208. UpdatedAt: time.Now().Format("2006-01-02 15:04:05"),
  209. }
  210. id, err := p.Create(db)
  211. if err != nil {
  212. l.Error("mysql",
  213. zap.String("sql", fmt.Sprintf("INSERT %s", p.TableName())),
  214. zap.String("fields", utils.MarshalJsonString(p)),
  215. zap.String("error", err.Error()))
  216. return 0, errors.DataBaseError
  217. }
  218. return id, nil
  219. }
  220. return p.Id, nil
  221. }