forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
192 lines (160 loc) · 3.87 KB
/
log.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package uadmin
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
"time"
)
// Action !
type Action int
func (a Action) Read() Action {
return 1
}
// Added @
func (a Action) Added() Action {
return 2
}
// Modified !
func (a Action) Modified() Action {
return 3
}
// Deleted !
func (a Action) Deleted() Action {
return 4
}
// LoginSuccessful !
func (a Action) LoginSuccessful() Action {
return 5
}
// LoginDenied !
func (a Action) LoginDenied() Action {
return 6
}
// Logout !
func (a Action) Logout() Action {
return 7
}
// PasswordResetRequest !
func (a Action) PasswordResetRequest() Action {
return 8
}
// PasswordResetDenied !
func (a Action) PasswordResetDenied() Action {
return 9
}
// PasswordResetSuccessful !
func (a Action) PasswordResetSuccessful() Action {
return 10
}
// GetSchema !
func (a Action) GetSchema() Action {
return 11
}
// Custom !
func (a Action) Custom() Action {
return 99
}
// Log !
type Log struct {
Model
Username string `uadmin:"filter;read_only"`
Action Action `uadmin:"filter;read_only"`
TableName string `uadmin:"filter;read_only"`
TableID int `uadmin:"filter;read_only"`
Activity string `uadmin:"code;read_only" gorm:"type:longtext"`
RollBack string `uadmin:"link;"`
CreatedAt time.Time `uadmin:"filter;read_only"`
}
func (l Log) String() string {
return fmt.Sprint(l.ID)
}
// Save !
func (l *Log) Save() {
Save(l)
if l.Action == l.Action.Modified() || l.Action == l.Action.Deleted() {
l.RollBack = RootURL + "revertHandler/?log_id=" + fmt.Sprint(l.ID)
}
Save(l)
}
// ParseRecord !
func (l *Log) ParseRecord(a reflect.Value, modelName string, ID uint, user *User, action Action, r *http.Request) (err error) {
modelName = strings.ToLower(modelName)
s, ok := getSchema(modelName)
if !ok {
errMsg := fmt.Sprintf("Unable to find schema (%s)", modelName)
Trail(ERROR, errMsg)
return fmt.Errorf(errMsg)
}
l.Username = user.Username
l.TableName = modelName
l.TableID = int(ID)
l.Action = action
// Check if the value passed is a pointer
if a.Kind() == reflect.Ptr {
a = a.Elem()
}
jsonifyValue := map[string]string{
"_IP": r.RemoteAddr,
}
for _, f := range s.Fields {
if !f.IsMethod {
if f.Type == cFK {
jsonifyValue[f.Name+"ID"] = fmt.Sprint(a.FieldByName(f.Name + "ID").Interface())
} else if f.Type == cDATE {
val := time.Time{}
if a.FieldByName(f.Name).Type().Kind() == reflect.Ptr {
if a.FieldByName(f.Name).IsNil() {
jsonifyValue[f.Name] = ""
} else {
val, _ = a.FieldByName(f.Name).Elem().Interface().(time.Time)
jsonifyValue[f.Name] = val.Format("2006-01-02 15:04:05 -0700")
}
} else {
val, _ = a.FieldByName(f.Name).Interface().(time.Time)
jsonifyValue[f.Name] = val.Format("2006-01-02 15:04:05 -0700")
}
} else {
jsonifyValue[f.Name] = fmt.Sprint(a.FieldByName(f.Name).Interface())
}
}
}
json, _ := json.Marshal(jsonifyValue)
l.Activity = string(json)
return nil
}
// SignIn !
func (l *Log) SignIn(user string, action Action, r *http.Request) (err error) {
l.Username = user
l.Action = action
loginStatus := ""
if r.Context().Value(CKey("login-status")) != nil {
loginStatus = r.Context().Value(CKey("login-status")).(string)
}
jsonifyValue := map[string]string{
"IP": r.RemoteAddr,
"Login-Status": loginStatus,
}
for k, v := range r.Header {
jsonifyValue[k] = strings.Join(v, ";")
}
json, _ := json.Marshal(jsonifyValue)
l.Activity = string(json)
return nil
}
// PasswordReset !
func (l *Log) PasswordReset(user string, action Action, r *http.Request) (err error) {
l.Username = user
l.Action = action
jsonifyValue := map[string]string{
"IP": r.RemoteAddr,
"Reset-Status": r.FormValue("reset-status"),
}
for k, v := range r.Header {
jsonifyValue[k] = strings.Join(v, ";")
}
json, _ := json.Marshal(jsonifyValue)
l.Activity = string(json)
return nil
}