Skip to content

Commit c797a87

Browse files
committed
record IP in events
1 parent 2d5b8e6 commit c797a87

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

core/api/entity/objects.go

+1
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ type AppEvent struct {
515515
OrgID string `json:"orgId"`
516516
UserID string `json:"userId"`
517517
Type string `json:"eventType"`
518+
IP string `json:"ip"`
518519
Created time.Time `json:"created"`
519520
}
520521

core/api/request/context.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/jmoiron/sqlx"
2222

2323
"github.com/documize/community/core/log"
24+
"github.com/documize/community/core/utility"
2425
)
2526

2627
var rc = Context{}
@@ -39,6 +40,7 @@ type Context struct {
3940
SSL bool
4041
AppURL string // e.g. https://{url}.documize.com
4142
Subdomain string
43+
ClientIP string
4244
Expires time.Time
4345
Transaction *sqlx.Tx
4446
}
@@ -60,7 +62,6 @@ func NewContext() Context {
6062
}
6163

6264
func getContext(r *http.Request) Context {
63-
6465
if value := context.Get(r, rc); value != nil {
6566
return value.(Context)
6667
}
@@ -74,6 +75,14 @@ func SetContext(r *http.Request, c Context) {
7475
c.Subdomain = GetSubdomainFromHost(r)
7576
c.SSL = r.TLS != nil
7677

78+
// get user IP from request
79+
c.ClientIP = utility.GetRemoteIP(r.RemoteAddr)
80+
81+
fip := r.Header.Get("X-Forwarded-For")
82+
if len(fip) > 0 {
83+
c.ClientIP = fip
84+
}
85+
7786
context.Set(r, rc, c)
7887
}
7988

core/api/request/event.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (p *Persister) RecordEvent(t entity.EventType) {
2424
e.OrgID = p.Context.OrgID
2525
e.UserID = p.Context.UserID
2626
e.Created = time.Now().UTC()
27+
e.IP = p.Context.ClientIP
2728
e.Type = string(t)
2829

2930
if e.OrgID == "" || e.UserID == "" {
@@ -37,14 +38,14 @@ func (p *Persister) RecordEvent(t entity.EventType) {
3738
return
3839
}
3940

40-
stmt, err := tx.Preparex("INSERT INTO userevent (orgid, userid, eventtype, created) VALUES (?, ?, ?, ?)")
41+
stmt, err := tx.Preparex("INSERT INTO userevent (orgid, userid, eventtype, ip, created) VALUES (?, ?, ?, ?, ?)")
4142
if err != nil {
4243
tx.Rollback()
4344
log.Error("Unable to prepare insert RecordEvent", err)
4445
return
4546
}
4647

47-
_, err = stmt.Exec(e.OrgID, e.UserID, e.Type, e.Created)
48+
_, err = stmt.Exec(e.OrgID, e.UserID, e.Type, e.IP, e.Created)
4849
if err != nil {
4950
log.Error("Unable to execute insert RecordEvent", err)
5051
tx.Rollback()

core/database/scripts/autobuild/db_00013.sql

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS `userevent` (
66
`orgid` CHAR(16) NOT NULL COLLATE utf8_bin,
77
`userid` CHAR(16) NOT NULL COLLATE utf8_bin,
88
`eventtype` VARCHAR(100) NOT NULL DEFAULT '',
9+
`ip` VARCHAR(39) NOT NULL COLLATE utf8_bin DEFAULT '',
910
`created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1011
CONSTRAINT pk_id PRIMARY KEY (id),
1112
INDEX `idx_userevent_orgid` (`orgid` ASC),

core/utility/net.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 Documize Inc. <[email protected]>. All rights reserved.
2+
//
3+
// This software (Documize Community Edition) is licensed under
4+
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
5+
//
6+
// You can operate outside the AGPL restrictions by purchasing
7+
// Documize Enterprise Edition and obtaining a commercial license
8+
// by contacting <[email protected]>.
9+
//
10+
// https://documize.com
11+
12+
package utility
13+
14+
import (
15+
"strings"
16+
)
17+
18+
// GetRemoteIP returns just the IP and not the port number
19+
func GetRemoteIP(ip string) string {
20+
i := strings.LastIndex(ip, ":")
21+
if i == -1 {
22+
return ip
23+
}
24+
25+
return ip[:i]
26+
}

0 commit comments

Comments
 (0)