1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00

record IP in events

This commit is contained in:
Harvey Kandola 2017-05-05 10:44:52 +01:00
parent 2d5b8e652f
commit c797a87eb9
5 changed files with 41 additions and 3 deletions

View file

@ -515,6 +515,7 @@ type AppEvent struct {
OrgID string `json:"orgId"`
UserID string `json:"userId"`
Type string `json:"eventType"`
IP string `json:"ip"`
Created time.Time `json:"created"`
}

View file

@ -21,6 +21,7 @@ import (
"github.com/jmoiron/sqlx"
"github.com/documize/community/core/log"
"github.com/documize/community/core/utility"
)
var rc = Context{}
@ -39,6 +40,7 @@ type Context struct {
SSL bool
AppURL string // e.g. https://{url}.documize.com
Subdomain string
ClientIP string
Expires time.Time
Transaction *sqlx.Tx
}
@ -60,7 +62,6 @@ func NewContext() Context {
}
func getContext(r *http.Request) Context {
if value := context.Get(r, rc); value != nil {
return value.(Context)
}
@ -74,6 +75,14 @@ func SetContext(r *http.Request, c Context) {
c.Subdomain = GetSubdomainFromHost(r)
c.SSL = r.TLS != nil
// get user IP from request
c.ClientIP = utility.GetRemoteIP(r.RemoteAddr)
fip := r.Header.Get("X-Forwarded-For")
if len(fip) > 0 {
c.ClientIP = fip
}
context.Set(r, rc, c)
}

View file

@ -24,6 +24,7 @@ func (p *Persister) RecordEvent(t entity.EventType) {
e.OrgID = p.Context.OrgID
e.UserID = p.Context.UserID
e.Created = time.Now().UTC()
e.IP = p.Context.ClientIP
e.Type = string(t)
if e.OrgID == "" || e.UserID == "" {
@ -37,14 +38,14 @@ func (p *Persister) RecordEvent(t entity.EventType) {
return
}
stmt, err := tx.Preparex("INSERT INTO userevent (orgid, userid, eventtype, created) VALUES (?, ?, ?, ?)")
stmt, err := tx.Preparex("INSERT INTO userevent (orgid, userid, eventtype, ip, created) VALUES (?, ?, ?, ?, ?)")
if err != nil {
tx.Rollback()
log.Error("Unable to prepare insert RecordEvent", err)
return
}
_, err = stmt.Exec(e.OrgID, e.UserID, e.Type, e.Created)
_, err = stmt.Exec(e.OrgID, e.UserID, e.Type, e.IP, e.Created)
if err != nil {
log.Error("Unable to execute insert RecordEvent", err)
tx.Rollback()