1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 13:19:43 +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()

View file

@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS `userevent` (
`orgid` CHAR(16) NOT NULL COLLATE utf8_bin,
`userid` CHAR(16) NOT NULL COLLATE utf8_bin,
`eventtype` VARCHAR(100) NOT NULL DEFAULT '',
`ip` VARCHAR(39) NOT NULL COLLATE utf8_bin DEFAULT '',
`created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT pk_id PRIMARY KEY (id),
INDEX `idx_userevent_orgid` (`orgid` ASC),

26
core/utility/net.go Normal file
View file

@ -0,0 +1,26 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package utility
import (
"strings"
)
// GetRemoteIP returns just the IP and not the port number
func GetRemoteIP(ip string) string {
i := strings.LastIndex(ip, ":")
if i == -1 {
return ip
}
return ip[:i]
}