role.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package v1
  2. import (
  3. "github.com/Anderson-Lu/gofasion/gofasion"
  4. "github.com/kptyun/KPTCOMM/middleware/inject"
  5. "github.com/kptyun/KPTCOMM/pkg/setting"
  6. "github.com/kptyun/KPTCOMM/pkg/util"
  7. "github.com/unknwon/com"
  8. "io/ioutil"
  9. "net/http"
  10. "github.com/astaxie/beego/validation"
  11. "github.com/gin-gonic/gin"
  12. "github.com/kptyun/KPTCOMM/pkg/app"
  13. "github.com/kptyun/KPTCOMM/pkg/e"
  14. )
  15. // @Summary 获取单个角色
  16. // @Tags role
  17. // @Accept json
  18. // @Produce json
  19. // @Param id path string true "id"
  20. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  21. // @Router /authdata/roles/:id [GET]
  22. func GetRole(c *gin.Context) {
  23. appG := app.Gin{C: c}
  24. id := com.StrTo(c.Param("id")).MustInt()
  25. valid := validation.Validation{}
  26. valid.Min(id, 1, "id").Message("ID必须大于0")
  27. if valid.HasErrors() {
  28. app.MarkErrors(valid.Errors)
  29. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  30. return
  31. }
  32. RoleService := Role_service.Role{ID: id}
  33. exists, err := RoleService.ExistByID()
  34. if err != nil {
  35. appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)
  36. return
  37. }
  38. if !exists {
  39. appG.Response(http.StatusOK, e.ERROR_NOT_EXIST, nil)
  40. return
  41. }
  42. article, err := RoleService.Get()
  43. if err != nil {
  44. appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)
  45. return
  46. }
  47. appG.Response(http.StatusOK, e.SUCCESS, article)
  48. }
  49. // @Summary 获取所有角色
  50. // @Tags role
  51. // @Accept json
  52. // @Produce json
  53. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  54. // @Router /authdata/roles [GET]
  55. func GetRoles(c *gin.Context) {
  56. appG := app.Gin{C: c}
  57. name := c.Query("name")
  58. valid := validation.Validation{}
  59. if valid.HasErrors() {
  60. app.MarkErrors(valid.Errors)
  61. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  62. return
  63. }
  64. RoleService := Role_service.Role{
  65. Name: name,
  66. PageNum: util.GetPage(c),
  67. PageSize: setting.AppSetting.PageSize,
  68. }
  69. total, err := RoleService.Count()
  70. if err != nil {
  71. appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_FAIL, nil)
  72. return
  73. }
  74. articles, err := RoleService.GetAll()
  75. if err != nil {
  76. appG.Response(http.StatusInternalServerError, e.ERROR_GET_S_FAIL, nil)
  77. return
  78. }
  79. data := make(map[string]interface{})
  80. data["rows"] = articles
  81. data["total"] = total
  82. appG.Response(http.StatusOK, e.SUCCESS, data)
  83. }
  84. // @Summary 增加角色
  85. // @Tags role
  86. // @Accept json
  87. // @Produce json
  88. // @Param name query string true "name"
  89. // @Param menu_id query string true "menu_id"
  90. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  91. // @Router /authdata/roles [POST]
  92. func AddRole(c *gin.Context) {
  93. var (
  94. appG = app.Gin{C: c}
  95. )
  96. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  97. fsion := gofasion.NewFasion(string(dataByte))
  98. name := fsion.Get("username").ValueStr()
  99. menuId := com.StrTo(fsion.Get("menu_id").ValueInt()).MustInt()
  100. valid := validation.Validation{}
  101. valid.MaxSize(name, 100, "path").Message("名称最长为100字符")
  102. if valid.HasErrors() {
  103. app.MarkErrors(valid.Errors)
  104. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  105. return
  106. }
  107. RoleService := Role_service.Role{
  108. Name: name,
  109. Menu: menuId,
  110. }
  111. if id, err := RoleService.Add(); err != nil {
  112. err = inject.Obj.Common.RoleAPI.LoadPolicy(id)
  113. if err != nil {
  114. appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)
  115. return
  116. }
  117. appG.Response(http.StatusOK, e.SUCCESS, nil)
  118. } else {
  119. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  120. return
  121. }
  122. }
  123. // @Summary 更新角色
  124. // @Tags role
  125. // @Accept json
  126. // @Produce json
  127. // @Param id path string true "id"
  128. // @Param name query string true "name"
  129. // @Param menu_id query string true "menu_id"
  130. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  131. // @Router /authdata/roles/:id [PUT]
  132. func EditRole(c *gin.Context) {
  133. var (
  134. appG = app.Gin{C: c}
  135. )
  136. id := com.StrTo(c.Param("id")).MustInt()
  137. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  138. fsion := gofasion.NewFasion(string(dataByte))
  139. name := fsion.Get("username").ValueStr()
  140. menuId := com.StrTo(fsion.Get("menu_id").ValueInt()).MustInt()
  141. valid := validation.Validation{}
  142. valid.MaxSize(name, 100, "path").Message("名称最长为100字符")
  143. if valid.HasErrors() {
  144. app.MarkErrors(valid.Errors)
  145. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  146. return
  147. }
  148. RoleService := Role_service.Role{
  149. ID: id,
  150. Name: name,
  151. Menu: menuId,
  152. }
  153. exists, err := RoleService.ExistByID()
  154. if err != nil {
  155. appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_FAIL, nil)
  156. return
  157. }
  158. if !exists {
  159. appG.Response(http.StatusOK, e.ERROR_EXIST_FAIL, nil)
  160. return
  161. }
  162. err = RoleService.Edit()
  163. if err != nil {
  164. appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)
  165. return
  166. }
  167. err = inject.Obj.Common.RoleAPI.LoadPolicy(id)
  168. if err != nil {
  169. appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)
  170. return
  171. }
  172. appG.Response(http.StatusOK, e.SUCCESS, nil)
  173. }
  174. // @Summary 删除角色
  175. // @Tags role
  176. // @Accept json
  177. // @Produce json
  178. // @Param id path string true "id"
  179. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  180. // @Router /authdata/roles/:id [DELETE]
  181. func DeleteRole(c *gin.Context) {
  182. appG := app.Gin{C: c}
  183. valid := validation.Validation{}
  184. id := com.StrTo(c.Param("id")).MustInt()
  185. valid.Min(id, 1, "id").Message("ID必须大于0")
  186. if valid.HasErrors() {
  187. app.MarkErrors(valid.Errors)
  188. appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
  189. return
  190. }
  191. RoleService := Role_service.Role{ID: id}
  192. exists, err := RoleService.ExistByID()
  193. if err != nil {
  194. appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_FAIL, nil)
  195. return
  196. }
  197. if !exists {
  198. appG.Response(http.StatusOK, e.ERROR_EXIST_FAIL, nil)
  199. return
  200. }
  201. role, err := RoleService.Get()
  202. err = RoleService.Delete()
  203. if err != nil {
  204. appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_FAIL, nil)
  205. return
  206. }
  207. inject.Obj.Enforcer.DeleteUser(role.Name)
  208. appG.Response(http.StatusOK, e.SUCCESS, nil)
  209. }