mobile_api_base_controller.go 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. //logout_cookie := this.Ctx.GetCookie("logout_cookie")
  44. //if len(token) == 0{
  45. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  46. // this.StopRun()
  47. //}
  48. this.MobileBaseAPIController.Prepare()
  49. adminUserInfo := this.GetMobileAdminUserInfo()
  50. if adminUserInfo == nil {
  51. this.DelSession("mobile_admin_user_info")
  52. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  53. this.StopRun()
  54. }
  55. if len(token) == 0 {
  56. this.DelSession("mobile_admin_user_info")
  57. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  58. this.StopRun()
  59. }
  60. //if this.Ctx.Request.Method != "GET" {
  61. // err := service.GetOrgSubscibeState(adminUserInfo.Subscibe)
  62. // if err != nil || adminUserInfo.Subscibe.State == 3 {
  63. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotSubscibe)
  64. // this.StopRun()
  65. // }
  66. //}
  67. if adminUserInfo.AppRole != nil {
  68. if adminUserInfo.AppRole.Id > 0 {
  69. app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
  70. if app_role != nil {
  71. if app_role.Status != 1 {
  72. this.DelSession("mobile_admin_user_info")
  73. this.Ctx.SetCookie("token_cookie", "")
  74. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
  75. this.StopRun()
  76. }
  77. }
  78. }
  79. }
  80. if this.Ctx.Request.Header.Get("Permission") == "1" {
  81. if adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator { //超级管理员不受此限制
  82. isPermission := false
  83. adminUserInfo := this.GetMobileAdminUserInfo()
  84. //该机构下该用户有多少个
  85. role, _ := service.GetUserAllRole(adminUserInfo.Org.Id, adminUserInfo.AdminUser.Id)
  86. var roles []string
  87. if len(role.RoleIds) <= 0 { //该用户没有设置角色
  88. } else {
  89. roles = strings.Split(role.RoleIds, ",")
  90. }
  91. fmt.Println(roles)
  92. //获取该用户下所有角色的权限总集
  93. var userRolePurviews string
  94. var userRolePurviewsArr []string
  95. for _, item := range roles {
  96. role_id, _ := strconv.ParseInt(item, 10, 64)
  97. purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  98. if len(userRolePurviews) == 0 {
  99. userRolePurviews = purviews
  100. } else {
  101. userRolePurviews = userRolePurviews + "," + purviews
  102. }
  103. }
  104. //该用户所拥有角色的权限的总集
  105. userRolePurviewsArr = RemoveRepeatedPurviewElement(strings.Split(userRolePurviews, ","))
  106. fmt.Println(userRolePurviewsArr)
  107. //系统所记录的权限列表
  108. allPermission, _ := service.GetAllFunctionPurview()
  109. for _, item := range allPermission {
  110. fmt.Println(strings.Split(item.Urlfor, ",")[0])
  111. fmt.Println(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  112. //判断当前路由是否在权限路由列表里面
  113. if strings.Split(item.Urlfor, ",")[0] == strings.Split(this.Ctx.Request.RequestURI, "?")[0]+"?"+"mode="+this.GetString("mode") {
  114. //获取该角色的所有权限
  115. for _, items := range userRolePurviewsArr {
  116. id, _ := strconv.ParseInt(items, 10, 64)
  117. fmt.Println(id)
  118. fmt.Println(item.ID)
  119. if id == item.ID {
  120. isPermission = true
  121. }
  122. }
  123. if !isPermission {
  124. msg, _ := service.FindErrorMsgByStr(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  125. json := make(map[string]interface{})
  126. json["msg"] = msg
  127. json["code"] = 0
  128. json["state"] = 0
  129. this.Data["json"] = json
  130. this.ServeJSON()
  131. this.StopRun()
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. func RemoveRepeatedPurviewElement(arr []string) (newArr []string) {
  139. newArr = make([]string, 0)
  140. for i := 0; i < len(arr); i++ {
  141. repeat := false
  142. for j := i + 1; j < len(arr); j++ {
  143. if arr[i] == arr[j] {
  144. repeat = true
  145. break
  146. }
  147. }
  148. if !repeat {
  149. newArr = append(newArr, arr[i])
  150. }
  151. }
  152. return
  153. }
  154. type AdminUserInfo struct {
  155. AdminUser *models.AdminUser `json:"user"`
  156. CurrentOrgId int64 `json:"current_org_id"`
  157. CurrentAppId int64 `json:"current_app_id"`
  158. OrgIds []int64 `json:"org_ids"`
  159. Orgs map[int64]*models.Org `json:"orgs"`
  160. OrgAppIds map[int64][]int64 `json:"org_app_ids"`
  161. OrgApps map[int64](map[int64]*models.OrgApp) `json:"org_apps"`
  162. App2OrgIds map[int64]int64 `json:"app_to_org_ids"`
  163. AppRoles map[int64]*models.App_Role `json:"app_roles"`
  164. AppPurviews map[int64][]*models.Purview `json:"app_purviews"`
  165. AppUrlfors map[int64][]string `json:"app_urlfors"`
  166. Subscibes map[int64]*models.ServeSubscibe `json:"org_subscibes"`
  167. }