role_controller.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package role
  2. import (
  3. base_ctl "SCRM/controllers"
  4. "SCRM/enums"
  5. "SCRM/service/role_service"
  6. "time"
  7. "github.com/astaxie/beego"
  8. )
  9. func RoleCtlRegistRouters() {
  10. beego.Router("/api/roles", &RoleAPIController{}, "get:GetRoles")
  11. beego.Router("/api/role/create", &RoleAPIController{}, "post:CreateRole")
  12. beego.Router("/api/role/modify", &RoleAPIController{}, "post:ModifyRole")
  13. beego.Router("/api/role/setstatus", &RoleAPIController{}, "post:ModifyRoleStatus")
  14. beego.Router("/role/purview/editinit", &RoleAPIController{}, "get:EditPurviewInitData")
  15. beego.Router("/role/purview/edit", &RoleAPIController{}, "post:EditPurview")
  16. }
  17. type RoleAPIController struct {
  18. base_ctl.BaseAuthAPIController
  19. }
  20. // /api/roles [get]
  21. // @param page?:int
  22. func (this *RoleAPIController) GetRoles() {
  23. page, _ := this.GetInt("page")
  24. adminUserInfo := this.GetAdminUserInfo()
  25. //beego.Alert(adminUserInfo.AdminUser)
  26. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  27. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  28. return
  29. }
  30. if page <= 0 {
  31. page = 1
  32. }
  33. roles, total, getRoleErr := role_service.GetRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 10)
  34. if getRoleErr != nil {
  35. //beego.Error("获取角色列表失败:", getRoleErr)
  36. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  37. } else {
  38. this.ServeSuccessJSON(map[string]interface{}{
  39. "roles": roles,
  40. "total_count": total,
  41. })
  42. }
  43. }
  44. // /api/role/create [post]
  45. // @param name:string
  46. // @param intro:string
  47. func (this *RoleAPIController) CreateRole() {
  48. name := this.GetString("name")
  49. intro := this.GetString("intro")
  50. if len(name) == 0 || len(intro) == 0 {
  51. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  52. return
  53. }
  54. adminUserInfo := this.GetAdminUserInfo()
  55. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  56. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  57. return
  58. }
  59. role, createErr := role_service.CreateRole(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, intro)
  60. if createErr != nil {
  61. //beego.Error("创建角色失败:", createErr)
  62. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  63. } else {
  64. this.ServeSuccessJSON(map[string]interface{}{
  65. "id": role.Id,
  66. "name": role.RoleName,
  67. "intro": role.RoleIntro,
  68. "status": role.Status,
  69. })
  70. }
  71. }
  72. // /api/role/modify
  73. // @param role_id:int
  74. // @param name:string
  75. // @param intro:string
  76. func (this *RoleAPIController) ModifyRole() {
  77. roleID, _ := this.GetInt64("role_id")
  78. name := this.GetString("name")
  79. intro := this.GetString("intro")
  80. if roleID <= 0 || len(name) == 0 || len(intro) == 0 {
  81. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  82. return
  83. }
  84. adminUserInfo := this.GetAdminUserInfo()
  85. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  86. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  87. return
  88. }
  89. role, getRoleErr := role_service.GetRoleByRoleID(roleID)
  90. if getRoleErr != nil {
  91. //beego.Error("获取角色失败:", getRoleErr)
  92. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  93. return
  94. } else if role == nil {
  95. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  96. return
  97. }
  98. role.RoleName = name
  99. role.RoleIntro = intro
  100. role.ModifyTime = time.Now().Unix()
  101. saveErr := role_service.ModifyRole(role)
  102. if saveErr != nil {
  103. //beego.Error("修改角色失败:", role.Id, saveErr)
  104. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  105. } else {
  106. this.ServeSuccessJSON(nil)
  107. }
  108. }
  109. // /api/role/setstatus
  110. // @param role_id:int
  111. // @param enable:bool
  112. func (this *RoleAPIController) ModifyRoleStatus() {
  113. roleID, _ := this.GetInt64("role_id")
  114. enable, _ := this.GetBool("enable")
  115. if roleID <= 0 {
  116. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  117. return
  118. }
  119. adminUserInfo := this.GetAdminUserInfo()
  120. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  121. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  122. return
  123. }
  124. role, getRoleErr := role_service.GetRoleByRoleID(roleID)
  125. if getRoleErr != nil {
  126. //beego.Error("获取角色失败:", getRoleErr)
  127. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  128. return
  129. } else if role == nil {
  130. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  131. return
  132. }
  133. if enable == false {
  134. if count, _ := role_service.RoleAdminUserCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleID); count != 0 {
  135. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCannotRemoveRole)
  136. return
  137. }
  138. }
  139. if enable {
  140. role.Status = 1
  141. } else {
  142. role.Status = 2
  143. }
  144. role.ModifyTime = time.Now().Unix()
  145. saveErr := role_service.ModifyRole(role)
  146. if saveErr != nil {
  147. //beego.Error("修改角色失败:", role.Id, saveErr)
  148. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  149. } else {
  150. this.ServeSuccessJSON(nil)
  151. }
  152. }
  153. // /role/purview/editinit [get]
  154. // @param role_id:int
  155. func (this *RoleAPIController) EditPurviewInitData() {
  156. adminUserInfo := this.GetAdminUserInfo()
  157. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  158. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  159. return
  160. }
  161. roleId, _ := this.GetInt64("role_id")
  162. if roleId <= 0 {
  163. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  164. return
  165. }
  166. purviews, getPurviewsErr := role_service.GetAllGeneralPurviewVMsProcessed()
  167. if getPurviewsErr != nil {
  168. //beego.Error("获取所有权限时出错:", getPurviewsErr)
  169. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  170. return
  171. }
  172. rolePurviewIdStr, getRPIdsErr := role_service.GetRolePurviewIds(roleId)
  173. if getRPIdsErr != nil {
  174. //beego.Error("获取角色的权限时出错:", getRPIdsErr)
  175. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  176. return
  177. }
  178. this.ServeSuccessJSON(map[string]interface{}{
  179. "purviews": purviews,
  180. "role_purview_ids": rolePurviewIdStr,
  181. })
  182. }
  183. // /role/purview/edit [post]
  184. // @param role_id:int
  185. // @param purview_ids:string
  186. func (this *RoleAPIController) EditPurview() {
  187. adminUserInfo := this.GetAdminUserInfo()
  188. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  189. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  190. return
  191. }
  192. roleId, _ := this.GetInt64("role_id")
  193. purviewIds := this.GetString("purview_ids")
  194. if roleId <= 0 {
  195. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  196. return
  197. }
  198. err := role_service.SaveRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, purviewIds)
  199. if err != nil {
  200. //beego.Error("设置角色的权限时出错:", err)
  201. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  202. } else {
  203. this.ServeSuccessJSON(nil)
  204. }
  205. }