2df4cd547b0f364288478a7fe1c2d4f21b275fa0.svn-base 6.0 KB

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