forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_api_schema.go
95 lines (85 loc) · 2.09 KB
/
d_api_schema.go
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
package uadmin
import (
"encoding/json"
"net/http"
)
func dAPISchemaHandler(w http.ResponseWriter, r *http.Request, s *Session) {
modelName := r.Context().Value(CKey("modelName")).(string)
model, _ := NewModel(modelName, false)
params := getURLArgs(r)
// Check permission
allow := false
if disableSchemer, ok := model.Interface().(APIDisabledSchemer); ok {
allow = disableSchemer.APIDisabledSchema(r)
// This is a "Disable" method
allow = !allow
if !allow {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Permission denied",
})
return
}
}
if publicSchemer, ok := model.Interface().(APIPublicSchemer); ok {
allow = publicSchemer.APIPublicSchema(r)
}
if !allow && s != nil {
allow = s.User.GetAccess(modelName).Read
}
if !allow {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Permission denied",
})
return
}
schema, _ := getSchema(modelName)
// Get Language
lang := r.URL.Query().Get("language")
if lang == "" {
if langC, err := r.Cookie("language"); err != nil || (langC != nil && langC.Value == "") {
lang = GetDefaultLanguage().Code
} else {
lang = langC.Value
}
}
// Translation
TranslateSchema(&schema, lang)
if r.URL.Query().Get("$choices") == "1" {
// Load Choices for FK
for i := range schema.Fields {
if schema.Fields[i].Type == cFK || schema.Fields[i].Type == cM2M {
choices := getChoices(schema.Fields[i].TypeName)
schema.Fields[i].Choices = choices
}
}
}
returnDAPIJSON(w, r, map[string]interface{}{
"status": "ok",
"result": schema,
}, params, "schema", model.Interface())
go func() {
// Check if log is required
log := APILogSchema
if logSchemer, ok := model.Interface().(APILogSchemer); ok {
log = logSchemer.APILogSchema(r)
}
if log {
user := ""
if s != nil {
user = s.User.Username
}
activity, _ := json.Marshal(map[string]interface{}{
"_IP": GetRemoteIP(r),
})
log := Log{
Username: user,
Action: Action(0).GetSchema(),
TableName: modelName,
Activity: string(activity),
}
log.Save()
}
}()
}