forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
revert_log_handler.go
138 lines (126 loc) · 4.54 KB
/
revert_log_handler.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
package uadmin
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
"time"
)
// RevertLogHandler !
func revertLogHandler(w http.ResponseWriter, r *http.Request) {
// Check if the request is coming from an authenticated user
session := IsAuthenticated(r)
if session == nil {
pageErrorHandler(w, r, nil)
return
}
// check if the user has permission to Logs
if !session.User.GetAccess("log").Read {
pageErrorHandler(w, r, nil)
return
}
// Check CSRF
if CheckCSRF(r) {
pageErrorHandler(w, r, nil)
return
}
// retrieve the log
log := Log{}
Get(&log, "id = ?", r.FormValue("log_id"))
if log.ID == 0 {
pageErrorHandler(w, r, nil)
return
}
// Check if the user has perission to edit the model
if !session.User.GetAccess(log.TableName).Edit {
pageErrorHandler(w, r, nil)
return
}
if log.Action == log.Action.Deleted() {
s, _ := getSchema(log.TableName)
tableName := s.TableName //GetDB().Config.NamingStrategy.TableName(log.TableName)
sql := fmt.Sprintf("UPDATE %s SET deleted_at = NULL WHERE id = %d", tableName, log.TableID)
err := db.Exec(sql).Error
if err != nil {
Trail(ERROR, "revertLogHandler was unable to exeture SQL (%s). %s", sql, err.Error())
}
} else if log.Action == log.Action.Modified() {
now := time.Now()
DType := reflect.TypeOf(now)
var langParser map[string]string
err := json.Unmarshal([]byte(log.Activity), &langParser)
if err != nil {
Trail(ERROR, "revertLogHandler.Unmarash Unable to parse JSON from a log: %s", err.Error())
return
}
model, ok := NewModel(log.TableName, true)
if !ok {
Trail(ERROR, "revertLogHandler.NewModel Invalid model name: %s", log.TableName)
}
Get(model.Interface(), "id = ?", log.TableID)
t := reflect.TypeOf(model.Interface()).Elem()
for index := 0; index < t.NumField(); index++ {
if t.Field(index).Type.Kind() == reflect.Int {
_v := string(langParser[t.Field(index).Name])
//_v = fmt.Sprintf("%+v", _v)
i, _ := strconv.ParseInt(_v, 10, 64)
model.Elem().FieldByName(t.Field(index).Name).SetInt(i)
} else if t.Field(index).Type.Kind() == reflect.String {
// Check if Multilingual
val := ""
if t.Field(index).Tag.Get("multilingual") == cTRUE {
tVal := map[string]string{}
langs := []Language{}
Filter(&langs, "`active` = ?", true)
for _, lang := range langs {
tVal[lang.Code] = fmt.Sprint(langParser[lang.Code+"-"+t.Field(index).Name])
}
b, _ := json.Marshal(tVal)
val = string(b)
} else {
val = string(langParser[t.Field(index).Name])
}
model.Elem().FieldByName(t.Field(index).Name).SetString(val)
} else if t.Field(index).Type.Kind() == reflect.Bool {
var val bool
val = false
if string(langParser[t.Field(index).Name]) == "true" {
val = true
}
model.Elem().FieldByName(t.Field(index).Name).SetBool(val)
} else if t.Field(index).Type.Kind() == reflect.Uint {
_v := string(langParser[t.Field(index).Name])
i, _ := strconv.ParseInt(_v, 10, 64)
val := uint(i)
model.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(val))
} else if t.Field(index).Type.Kind() == reflect.Float64 {
_v := string(langParser[t.Field(index).Name])
i, _ := strconv.ParseFloat(_v, 64)
model.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(i))
} else if t.Field(index).Type.Kind() == reflect.Float32 {
_v := string(langParser[t.Field(index).Name])
i, _ := strconv.ParseFloat(_v, 32)
model.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(float32(i)))
} else if t.Field(index).Type.Kind() == reflect.Int32 {
_v := string(langParser[t.Field(index).Name])
i, _ := strconv.ParseInt(_v, 10, 32)
model.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(int32(i)))
} else if t.Field(index).Type.Kind() == reflect.Int64 {
_v := string(langParser[t.Field(index).Name])
i, _ := strconv.ParseInt(_v, 10, 64)
model.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(i))
} else if t.Field(index).Type.Kind() == reflect.Ptr && t.Field(index).Type.Elem() == DType {
if fmt.Sprint(langParser[t.Field(index).Name]) != "" {
tm, _ := time.Parse("2006-01-02 15:04:05 -0700", string(langParser[t.Field(index).Name]))
model.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(&tm))
}
} else if t.Field(index).Type == DType {
_v := string(langParser[t.Field(index).Name])
tm, _ := time.Parse("2006-01-02 15:04:05 -0700", _v)
model.Elem().FieldByName(t.Field(index).Name).Set(reflect.ValueOf(tm))
}
}
Save(model.Elem().Addr().Interface())
}
}