mobile_api_base_controller.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package mobile_api_controllers
  2. import (
  3. "XT_New/controllers"
  4. "XT_New/enums"
  5. "XT_New/models"
  6. "XT_New/service"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. )
  11. type MobileBaseAPIController struct {
  12. controllers.BaseAPIController
  13. }
  14. func (this *MobileBaseAPIController) Prepare() {
  15. this.BaseAPIController.Prepare()
  16. // beego.Trace("============================================================")
  17. // beego.Trace("session ID: %v", this.Ctx.Input.Cookie("beegosessionID"))
  18. // beego.Trace("session : %v", this.GetSession("info"))
  19. // this.SetSession("info", time.Now().Format("2006/01/02 15:04:05"))
  20. // beego.Trace("============================================================")
  21. }
  22. func (this *MobileBaseAPIController) GetMobileAdminUserInfo() *MobileAdminUserInfo {
  23. userInfo := this.GetSession("mobile_admin_user_info")
  24. if userInfo == nil {
  25. return nil
  26. } else {
  27. return userInfo.(*MobileAdminUserInfo)
  28. }
  29. }
  30. type MobileAdminUserInfo struct {
  31. AdminUser *models.AdminUser
  32. Org *models.Org
  33. App *models.OrgApp
  34. AppRole *models.App_Role
  35. Subscibe *models.ServeSubscibe
  36. TemplateInfo *models.GobalTemplate
  37. }
  38. type MobileBaseAPIAuthController struct {
  39. MobileBaseAPIController
  40. }
  41. func (this *MobileBaseAPIAuthController) Prepare() {
  42. token := this.Ctx.GetCookie("token_cookie")
  43. //if len(token) == 0{
  44. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  45. // this.StopRun()
  46. //}
  47. this.MobileBaseAPIController.Prepare()
  48. adminUserInfo := this.GetMobileAdminUserInfo()
  49. if adminUserInfo == nil || len(token) == 0 {
  50. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  51. this.StopRun()
  52. }
  53. //if this.Ctx.Request.Method != "GET" {
  54. // err := service.GetOrgSubscibeState(adminUserInfo.Subscibe)
  55. // if err != nil || adminUserInfo.Subscibe.State == 3 {
  56. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotSubscibe)
  57. // this.StopRun()
  58. // }
  59. //}
  60. if adminUserInfo.AppRole != nil {
  61. app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
  62. if app_role.Status != 1 {
  63. this.DelSession("mobile_admin_user_info")
  64. this.Ctx.SetCookie("token_cookie", "")
  65. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
  66. this.StopRun()
  67. }
  68. }
  69. if this.Ctx.Request.Header.Get("Permission") == "1" {
  70. if !adminUserInfo.AdminUser.IsSuperAdmin || adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator {
  71. isPermission := false
  72. adminUserInfo := this.GetMobileAdminUserInfo()
  73. //该机构下该用户有多少个
  74. role, _ := service.GetUserAllRole(adminUserInfo.Org.Id, adminUserInfo.AdminUser.Id)
  75. var roles []string
  76. if len(role.RoleIds) <= 0 { //该用户没有设置角色
  77. } else {
  78. roles = strings.Split(role.RoleIds, ",")
  79. }
  80. fmt.Println(roles)
  81. //获取该用户下所有角色的权限总集
  82. var userRolePurviews string
  83. var userRolePurviewsArr []string
  84. for _, item := range roles {
  85. role_id, _ := strconv.ParseInt(item, 10, 64)
  86. purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  87. if len(userRolePurviews) == 0 {
  88. userRolePurviews = purviews
  89. } else {
  90. userRolePurviews = userRolePurviews + "," + purviews
  91. }
  92. }
  93. //该用户所拥有角色的权限的总集
  94. userRolePurviewsArr = RemoveRepeatedPurviewElement(strings.Split(userRolePurviews, ","))
  95. fmt.Println(userRolePurviewsArr)
  96. //系统所记录的权限列表
  97. allPermission, _ := service.GetAllFunctionPurview()
  98. for _, item := range allPermission {
  99. fmt.Println(strings.Split(item.Urlfor, ",")[0])
  100. fmt.Println(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  101. //判断当前路由是否在权限路由列表里面
  102. if strings.Split(item.Urlfor, ",")[0] == strings.Split(this.Ctx.Request.RequestURI, "?")[0]+"?"+"mode="+this.GetString("mode") {
  103. //获取该角色的所有权限
  104. for _, items := range userRolePurviewsArr {
  105. id, _ := strconv.ParseInt(items, 10, 64)
  106. fmt.Println(id)
  107. fmt.Println(item.ID)
  108. if id == item.ID {
  109. isPermission = true
  110. }
  111. }
  112. if !isPermission {
  113. msg, _ := service.FindErrorMsgByStr(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  114. json := make(map[string]interface{})
  115. json["msg"] = msg
  116. json["code"] = 0
  117. json["state"] = 0
  118. this.Data["json"] = json
  119. this.ServeJSON()
  120. this.StopRun()
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. func RemoveRepeatedPurviewElement(arr []string) (newArr []string) {
  128. newArr = make([]string, 0)
  129. for i := 0; i < len(arr); i++ {
  130. repeat := false
  131. for j := i + 1; j < len(arr); j++ {
  132. if arr[i] == arr[j] {
  133. repeat = true
  134. break
  135. }
  136. }
  137. if !repeat {
  138. newArr = append(newArr, arr[i])
  139. }
  140. }
  141. return
  142. }