controller.go
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package controllers
import (
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/web/beego"
"github.com/linmadan/egglib-go/web/beego/utils"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/authlib"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/cache"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
"net/http"
"strconv"
"strings"
)
func ResponseGrid(c beego.BaseController, total int64, data interface{}, err error) {
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(c.Ctx, err)
} else {
response = ResponseGridData(c.Ctx, total, data)
}
c.Data["json"] = response
c.ServeJSON()
}
func ResponseGridData(ctx *context.Context, total int64, data interface{}) utils.JsonResponse {
jsonResponse := utils.JsonResponse{}
jsonResponse["code"] = 0
jsonResponse["msg"] = "ok"
jsonResponse["data"] = map[string]interface{}{"grid": map[string]interface{}{
"total": total,
"list": data,
}}
ctx.Input.SetData("outputData", jsonResponse)
return jsonResponse
}
func Must(err error) {
if err != nil {
log.Logger.Error(err.Error())
}
}
func ParseContext(c beego.BaseController) *domain.Context {
var companyId int = 1598224576532189184
var userId int = 1
var userName string = "管理员"
if token := c.Ctx.Input.GetData("UserToken"); token != nil {
userToken, ok := token.(*domain.UserToken)
if ok {
// cache user info
key := cache.KeyCompanyUser(int(userToken.CompanyId), int(userToken.UserId))
if userToken.CheckUserInfo() {
if cacheItem, ok := cache.DefaultCache.Get(key); ok {
v := cacheItem.(*authlib.DataUserMe)
userName = v.User.NickName
} else {
requestToken, _ := userToken.GenerateToken()
authLib := authlib.NewApiAuthLib(constant.AUTH_SERVER_HOST).WithToken(requestToken)
userInfo, err := authLib.MeInfo(authlib.RequestUserMeQuery{
UserId: int(userToken.UserId),
CompanyId: int(userToken.CompanyId),
})
if err != nil || userInfo == nil || userInfo.User == nil {
goto END
}
userName = userInfo.User.NickName
cache.DefaultCache.SetDefault(key, userInfo)
}
}
// assign user
companyId = int(userToken.CompanyId)
userId = int(userToken.UserId)
}
}
END:
ctx := &domain.Context{
CompanyId: companyId,
OperatorId: userId,
OperatorName: userName,
TenantId: 1,
}
return ctx
}
func header(c beego.BaseController, key string) int {
if len(c.Ctx.Input.Header(key)) == 0 {
return 0
}
res, err := strconv.Atoi(c.Ctx.Input.Header(key))
if err != nil {
log.Logger.Error(err.Error())
return 0
}
return res
}
var BlacklistRouters = map[string]bool{
"/data/files": true,
"/data/edit-data-table": true,
"/data/flush-data-table": true,
"/data/generate-main-table": true,
"/data/append-data-to-table": true,
"/data/tables/copy-data-table": true,
"/data/tables/apply-on": true,
"/data/tables/add-sub-table": true,
"/data/tables/row-edit": true,
"/data/mapping-rules": true,
"/data/query-sets": true,
"/data/query-sets/copy": true,
"/data/query-sets/move": true,
"/data/query-sets/rename": true,
"/data/query-sets/change-status": true,
"/data/query-sets/formula": true,
"/data/query-sets/formula/change-status": true,
"/data/query-sets/formula/move": true,
"/data/query-sets/formula/copy": true,
"/data/query-sets/formula/rename": true,
}
func BlacklistFilter(black map[string]bool) func(ctx *context.Context) {
return func(ctx *context.Context) {
if token := ctx.Input.GetData("UserToken"); token != nil {
userToken, ok := token.(*domain.UserToken)
if !ok {
return
}
if userToken.UserId > 0 && userToken.UserId == constant.BlacklistUser {
goto CheckBlackList
} else if userToken.CompanyId > 0 && userToken.UserId == 0 && userToken.CompanyId == constant.BlackListCompany {
goto CheckBlackList
} else {
return
}
CheckBlackList:
var notAllow = false
defer func() {
if notAllow {
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.JSON(map[string]interface{}{
"msg": "测试账户不允许修改数据",
"code": 801,
"data": struct{}{},
}, false, false)
}
}()
if ctx.Request.Method == http.MethodDelete || ctx.Request.Method == http.MethodPut {
notAllow = true
return
}
url := ctx.Request.URL.Path
if v, ok := black[url]; ok && v {
notAllow = true
return
}
if strings.HasSuffix(url, "/") {
url = strings.TrimSuffix(url, "/")
}
if v, ok := black[url]; ok && v {
notAllow = true
return
}
}
}
}