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:
parent
2d5b8e652f
commit
c797a87eb9
5 changed files with 41 additions and 3 deletions
|
@ -515,6 +515,7 @@ type AppEvent struct {
|
||||||
OrgID string `json:"orgId"`
|
OrgID string `json:"orgId"`
|
||||||
UserID string `json:"userId"`
|
UserID string `json:"userId"`
|
||||||
Type string `json:"eventType"`
|
Type string `json:"eventType"`
|
||||||
|
IP string `json:"ip"`
|
||||||
Created time.Time `json:"created"`
|
Created time.Time `json:"created"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
|
"github.com/documize/community/core/utility"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rc = Context{}
|
var rc = Context{}
|
||||||
|
@ -39,6 +40,7 @@ type Context struct {
|
||||||
SSL bool
|
SSL bool
|
||||||
AppURL string // e.g. https://{url}.documize.com
|
AppURL string // e.g. https://{url}.documize.com
|
||||||
Subdomain string
|
Subdomain string
|
||||||
|
ClientIP string
|
||||||
Expires time.Time
|
Expires time.Time
|
||||||
Transaction *sqlx.Tx
|
Transaction *sqlx.Tx
|
||||||
}
|
}
|
||||||
|
@ -60,7 +62,6 @@ func NewContext() Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getContext(r *http.Request) Context {
|
func getContext(r *http.Request) Context {
|
||||||
|
|
||||||
if value := context.Get(r, rc); value != nil {
|
if value := context.Get(r, rc); value != nil {
|
||||||
return value.(Context)
|
return value.(Context)
|
||||||
}
|
}
|
||||||
|
@ -74,6 +75,14 @@ func SetContext(r *http.Request, c Context) {
|
||||||
c.Subdomain = GetSubdomainFromHost(r)
|
c.Subdomain = GetSubdomainFromHost(r)
|
||||||
c.SSL = r.TLS != nil
|
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)
|
context.Set(r, rc, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ func (p *Persister) RecordEvent(t entity.EventType) {
|
||||||
e.OrgID = p.Context.OrgID
|
e.OrgID = p.Context.OrgID
|
||||||
e.UserID = p.Context.UserID
|
e.UserID = p.Context.UserID
|
||||||
e.Created = time.Now().UTC()
|
e.Created = time.Now().UTC()
|
||||||
|
e.IP = p.Context.ClientIP
|
||||||
e.Type = string(t)
|
e.Type = string(t)
|
||||||
|
|
||||||
if e.OrgID == "" || e.UserID == "" {
|
if e.OrgID == "" || e.UserID == "" {
|
||||||
|
@ -37,14 +38,14 @@ func (p *Persister) RecordEvent(t entity.EventType) {
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
log.Error("Unable to prepare insert RecordEvent", err)
|
log.Error("Unable to prepare insert RecordEvent", err)
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
log.Error("Unable to execute insert RecordEvent", err)
|
log.Error("Unable to execute insert RecordEvent", err)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|
|
@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS `userevent` (
|
||||||
`orgid` CHAR(16) NOT NULL COLLATE utf8_bin,
|
`orgid` CHAR(16) NOT NULL COLLATE utf8_bin,
|
||||||
`userid` CHAR(16) NOT NULL COLLATE utf8_bin,
|
`userid` CHAR(16) NOT NULL COLLATE utf8_bin,
|
||||||
`eventtype` VARCHAR(100) NOT NULL DEFAULT '',
|
`eventtype` VARCHAR(100) NOT NULL DEFAULT '',
|
||||||
|
`ip` VARCHAR(39) NOT NULL COLLATE utf8_bin DEFAULT '',
|
||||||
`created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
`created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
CONSTRAINT pk_id PRIMARY KEY (id),
|
CONSTRAINT pk_id PRIMARY KEY (id),
|
||||||
INDEX `idx_userevent_orgid` (`orgid` ASC),
|
INDEX `idx_userevent_orgid` (`orgid` ASC),
|
||||||
|
|
26
core/utility/net.go
Normal file
26
core/utility/net.go
Normal 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]
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue