mobile_api_base_controller.go 6.0KB

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