forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_api_delete.go
175 lines (156 loc) · 4.27 KB
/
d_api_delete.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
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
170
171
172
173
174
175
package uadmin
import (
"encoding/json"
"net/http"
"reflect"
"strings"
)
func dAPIDeleteHandler(w http.ResponseWriter, r *http.Request, s *Session) {
var rowsCount int64
urlParts := strings.Split(r.URL.Path, "/")
modelName := r.Context().Value(CKey("modelName")).(string)
model, _ := NewModel(modelName, false)
schema, _ := getSchema(modelName)
tableName := schema.TableName
params := getURLArgs(r)
// Check CSRF
if CheckCSRF(r) {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Failed CSRF protection.",
})
return
}
// Check permission
allow := false
if disableDeleter, ok := model.Interface().(APIDisabledDeleter); ok {
allow = disableDeleter.APIDisabledDelete(r)
// This is a "Disable" method
allow = !allow
if !allow {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Permission denied",
})
return
}
}
if publicDeleter, ok := model.Interface().(APIPublicDeleter); ok {
allow = publicDeleter.APIPublicDelete(r)
}
if !allow && s != nil {
allow = s.User.GetAccess(modelName).Delete
}
if !allow {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Permission denied",
})
return
}
// Check if log is required
log := APILogDelete
if logDeleter, ok := model.Interface().(APILogDeleter); ok {
log = logDeleter.APILogDelete(r)
}
if r.URL.Path == "" {
// Delete Multiple
q, args := getFilters(r, params, tableName, &schema)
modelArray, _ := NewModelArray(modelName, true)
// Block Delete All
if q == "deleted_at IS NULL" {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Delete all is blocked",
})
return
}
if Database.Type == "mysql" {
db := GetDB()
if log {
db.Model(model.Interface()).Where(q, args...).Scan(modelArray.Interface())
}
db = db.Where(q, args...).Delete(model.Addr().Interface())
if db.Error != nil {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Unable to execute DELETE SQL. " + db.Error.Error(),
})
return
}
rowsCount = db.RowsAffected
if log {
for i := 0; i < modelArray.Elem().Len(); i++ {
createAPIDeleteLog(modelName, modelArray.Elem().Index(i).Interface(), &s.User, r)
}
}
} else if Database.Type == "sqlite" {
db := GetDB().Begin()
if log {
db.Model(model.Interface()).Where(q, args...).Scan(modelArray.Interface())
}
db = db.Exec("PRAGMA case_sensitive_like=ON;")
db = db.Where(q, args...).Delete(model.Addr().Interface())
db = db.Exec("PRAGMA case_sensitive_like=OFF;")
db.Commit()
if db.Error != nil {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Unable to COMMIT SQL. " + db.Error.Error(),
})
return
}
rowsCount = db.RowsAffected
if log {
for i := 0; i < modelArray.Elem().Len(); i++ {
createAPIDeleteLog(modelName, modelArray.Elem().Index(i).Interface(), &s.User, r)
}
}
}
returnDAPIJSON(w, r, map[string]interface{}{
"status": "ok",
"rows_count": rowsCount,
}, params, "delete", model.Interface())
} else if len(urlParts) == 1 {
// Delete One
m, _ := NewModel(modelName, true)
db := GetDB()
if log {
db.Model(model.Interface()).Where("id = ?", urlParts[0]).Scan(m.Interface())
}
db = db.Where("id = ?", urlParts[0]).Delete(model.Addr().Interface())
if db.Error != nil {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Unable to execute DELETE SQL. " + db.Error.Error(),
})
return
}
if log {
createAPIDeleteLog(modelName, m.Interface(), &s.User, r)
}
returnDAPIJSON(w, r, map[string]interface{}{
"status": "ok",
"rows_count": db.RowsAffected,
}, params, "delete", model.Interface())
} else {
// Error: Unknown format
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "invalid format (" + r.URL.Path + ")",
})
return
}
}
func createAPIDeleteLog(modelName string, m interface{}, user *User, r *http.Request) {
b, _ := json.Marshal(m)
output := string(b[:len(b)-1]) + `,"_IP":"` + GetRemoteIP(r) + `"}`
log := Log{
Username: user.Username,
Action: Action(0).Deleted(),
TableName: modelName,
TableID: int(GetID(reflect.ValueOf(m))),
Activity: output,
}
log.Save()
}