aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile21
-rwxr-xr-xbin/generate-events38
-rw-r--r--eventlogging/eventlogger.go18
-rw-r--r--eventlogging/events.txt8
-rw-r--r--eventlogging/types.go52
-rw-r--r--grilist/grilist.go20
-rw-r--r--main.go12
-rw-r--r--modules/grils/grils.go5
-rw-r--r--modules/lists/lists.go72
10 files changed, 235 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore
index e915543..1216dc4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@ node_modules/
5package.json 5package.json
6.npm_update 6.npm_update
7*.d 7*.d
8eventlogging/events.go
8 9
9#Gebuildete Dateien 10#Gebuildete Dateien
10assets/js/* 11assets/js/*
diff --git a/Makefile b/Makefile
index 12f5f53..754b1e4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
1include config.mk 1include config.mk
2 2
3BINARY_PATH=./bin 3BINARY_PATH = ./bin
4TARGET = $(BINARY_PATH)/$(NAME)
4IMPORTER_FILES = $(shell find tools/importer/ -type f -name '*.go') 5IMPORTER_FILES = $(shell find tools/importer/ -type f -name '*.go')
5 6
6CSS_FILES = $(patsubst assets_src/%, assets/%, $(shell find assets_src/css/ -type f -name '*.css')) 7CSS_FILES = $(patsubst assets_src/%, assets/%, $(shell find assets_src/css/ -type f -name '*.css'))
@@ -13,7 +14,7 @@ LESSC := ./node_modules/less/bin/lessc
13 14
14-include $(DEPFILES) 15-include $(DEPFILES)
15 16
16all: assets app 17all: assets $(TARGET) $(IMPORTER)
17 18
18print_info: 19print_info:
19 @echo $(CSS_FILES) 20 @echo $(CSS_FILES)
@@ -21,8 +22,10 @@ print_info:
21 @echo $(JS_FILES) 22 @echo $(JS_FILES)
22 @echo $(DEPFILES) 23 @echo $(DEPFILES)
23 24
24app: 25$(NAME): $(TARGET)
25 go build -o $(BINARY_PATH)/$(NAME) 26
27$(TARGET): eventlogging/events.go
28 go build -o $@
26 29
27.npm_update: package.json 30.npm_update: package.json
28 touch $@ 31 touch $@
@@ -51,6 +54,9 @@ assets/js/%.js: assets_src/js/%.js assets_src/js/%.d .babelrc .npm_update
51 @echo browserify $< -o $@ 54 @echo browserify $< -o $@
52 @$(BROWSERIFY) --debug $< -o $@ -t babelify 55 @$(BROWSERIFY) --debug $< -o $@ -t babelify
53 56
57eventlogging/events.go: eventlogging/events.txt
58 ./bin/generate-events < $< > $@
59
54clean: 60clean:
55 -rm -- $(BINARY_PATH)/$(NAME) 61 -rm -- $(BINARY_PATH)/$(NAME)
56 -rm -- $(CSS_FILES) 62 -rm -- $(CSS_FILES)
@@ -58,6 +64,7 @@ clean:
58 -rm -- $(JS_FILES) 64 -rm -- $(JS_FILES)
59 -rm -- $(DEPFILES) 65 -rm -- $(DEPFILES)
60 -rm -- importer 66 -rm -- importer
67 -rm -- eventlogging/events.go
61 68
62superclean: clean 69superclean: clean
63 -rm -- .npm_update 70 -rm -- .npm_update
@@ -66,11 +73,11 @@ superclean: clean
66ultraclean: superclean 73ultraclean: superclean
67 -rm -r -- node_modules 74 -rm -r -- node_modules
68 75
69run: assets 76run: assets $(TARGET)
70 go run *.go 77 $(TARGET)
71 78
72importer: $(IMPORTER_FILES) 79importer: $(IMPORTER_FILES)
73 go build -o importer tools/importer/*.go 80 go build -o importer tools/importer/*.go
74 81
75.PHONY: all assets app print_info clean superclean ultraclean run 82.PHONY: all assets $(NAME) $(TARGET) print_info clean superclean ultraclean run
76 83
diff --git a/bin/generate-events b/bin/generate-events
new file mode 100755
index 0000000..c4a0c1c
--- /dev/null
+++ b/bin/generate-events
@@ -0,0 +1,38 @@
1#!/bin/bash
2events=$(cat)
3echo 'package eventlogging
4/*
5 * THIS FILE IS AUTOGENERATED
6 * DO NOT EDIT MANUALLY!!!
7 */
8
9import (
10 "fagott.pw/charakterin"
11)
12
13'
14
15echo "var Events = []string{"
16while read event
17do
18 cat <<EOT
19 "$event",
20EOT
21done <<<"$events"
22echo '}
23'
24
25while read event
26do
27 funcName=$(sed -r 's/(^|_)(.)([^_]*)/\2\L\3/g' <<<$event)
28 cat <<EOT
29func (l *EventLogger) ${funcName}(user *charakterin.User, data ${funcName}Data) {
30 if user != nil {
31 data.User = user.Name
32 data.UserAgent = user.Agent
33 }
34 l.base.Log("$event", data)
35}
36
37EOT
38done <<<"$events"
diff --git a/eventlogging/eventlogger.go b/eventlogging/eventlogger.go
new file mode 100644
index 0000000..e5a1b2b
--- /dev/null
+++ b/eventlogging/eventlogger.go
@@ -0,0 +1,18 @@
1package eventlogging
2
3import "fagott.pw/nsa"
4
5type EventLoggerData struct {
6 User string `json:"user"`
7 UserAgent string `json:"userAgent"`
8}
9
10type EventLogger struct {
11 base *nsa.Logger
12}
13
14func NewEventLogger(ip string) *EventLogger {
15 return &EventLogger{
16 base: nsa.NewLogger(ip),
17 }
18}
diff --git a/eventlogging/events.txt b/eventlogging/events.txt
new file mode 100644
index 0000000..0e170dc
--- /dev/null
+++ b/eventlogging/events.txt
@@ -0,0 +1,8 @@
1VIEW_GRIL
2VIEW_LIST
3CREATE_LIST
4ADD_GRIL_TO_LIST
5DELETE_GRIL_FROM_LIST
6DELETE_LIST
7EDIT_LIST
8CHANGE_GRIL_ORDER
diff --git a/eventlogging/types.go b/eventlogging/types.go
new file mode 100644
index 0000000..96d0ce0
--- /dev/null
+++ b/eventlogging/types.go
@@ -0,0 +1,52 @@
1package eventlogging
2
3type ViewGrilData struct {
4 EventLoggerData
5 GrilID int
6}
7
8type ViewListData struct {
9 EventLoggerData
10 ListID int
11}
12
13type CreateListData struct {
14 EventLoggerData
15 ListID int
16 Name string
17 Description string
18}
19
20type AddGrilToListData struct {
21 EventLoggerData
22 GrilID int
23 ListID int
24}
25
26type DeleteGrilFromListData struct {
27 EventLoggerData
28 ListID int
29 GrilID int
30}
31
32type DeleteListData struct {
33 EventLoggerData
34 ListID int
35}
36
37type EditListData struct {
38 EventLoggerData
39 ListID int
40 OldName string
41 OldDescription string
42 NewName string
43 NewDescription string
44}
45
46type ChangeGrilOrderData struct {
47 EventLoggerData
48 ListID int
49 GrilID int
50 OldOrder int
51 NewOrder int
52}
diff --git a/grilist/grilist.go b/grilist/grilist.go
index f9a79b2..aebe45c 100644
--- a/grilist/grilist.go
+++ b/grilist/grilist.go
@@ -2,8 +2,13 @@ package grilist
2 2
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 "log"
6 "net"
7 "net/http"
8 "strings"
5 9
6 "fagott.pw/charakterin" 10 "fagott.pw/charakterin"
11 "fagott.pw/grilist/eventlogging"
7 "fagott.pw/grilist/frontend" 12 "fagott.pw/grilist/frontend"
8 13
9 "github.com/julienschmidt/httprouter" 14 "github.com/julienschmidt/httprouter"
@@ -20,6 +25,21 @@ type Grilist struct {
20 Router *httprouter.Router 25 Router *httprouter.Router
21} 26}
22 27
28func (g *Grilist) EventLogger(r *http.Request) *eventlogging.EventLogger {
29 forwardedIPs := strings.Split(r.Header.Get("X-Forwarded-For"), ", ")
30 ip := r.RemoteAddr
31 if len(forwardedIPs) > 0 && forwardedIPs[0] != "" {
32 ip = forwardedIPs[0]
33 }
34 host, _, err := net.SplitHostPort(ip)
35 if err != nil {
36 log.Printf("Could not split IP %s\n", ip)
37 log.Println(err)
38 host = "0.0.0.0"
39 }
40 return eventlogging.NewEventLogger(host)
41}
42
23// Module ist ein Modul für Grilist. 43// Module ist ein Modul für Grilist.
24type Module interface { 44type Module interface {
25 Init(*Grilist) 45 Init(*Grilist)
diff --git a/main.go b/main.go
index 7ee05ad..75eee68 100644
--- a/main.go
+++ b/main.go
@@ -5,8 +5,10 @@ import (
5 "fmt" 5 "fmt"
6 "log" 6 "log"
7 "net/http" 7 "net/http"
8 "strings"
8 9
9 "fagott.pw/charakterin" 10 "fagott.pw/charakterin"
11 "fagott.pw/grilist/eventlogging"
10 "fagott.pw/grilist/frontend" 12 "fagott.pw/grilist/frontend"
11 "fagott.pw/grilist/grilist" 13 "fagott.pw/grilist/grilist"
12 "fagott.pw/grilist/modules/grils" 14 "fagott.pw/grilist/modules/grils"
@@ -15,8 +17,7 @@ import (
15 "fagott.pw/grilist/modules/series" 17 "fagott.pw/grilist/modules/series"
16 "fagott.pw/grilist/modules/tags" 18 "fagott.pw/grilist/modules/tags"
17 "fagott.pw/grilist/modules/user" 19 "fagott.pw/grilist/modules/user"
18 20 "fagott.pw/nsa"
19 "strings"
20 21
21 "github.com/julienschmidt/httprouter" 22 "github.com/julienschmidt/httprouter"
22 _ "github.com/lib/pq" 23 _ "github.com/lib/pq"
@@ -53,6 +54,13 @@ func main() {
53 54
54 log.Println("database connection established") 55 log.Println("database connection established")
55 56
57 if err := nsa.Init(db); err != nil {
58 panic(err)
59 }
60 if err := nsa.SetEvents(eventlogging.Events); err != nil {
61 panic(err)
62 }
63
56 renderer := frontend.New("views") 64 renderer := frontend.New("views")
57 login := charakterin.New(db) 65 login := charakterin.New(db)
58 login.UseRenderer(renderer) 66 login.UseRenderer(renderer)
diff --git a/modules/grils/grils.go b/modules/grils/grils.go
index 1b1bb5f..4a9990f 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -11,6 +11,7 @@ import (
11 11
12 "fagott.pw/charakterin" 12 "fagott.pw/charakterin"
13 "fagott.pw/grilist/cache" 13 "fagott.pw/grilist/cache"
14 "fagott.pw/grilist/eventlogging"
14 "fagott.pw/grilist/frontend" 15 "fagott.pw/grilist/frontend"
15 "fagott.pw/grilist/grilist" 16 "fagott.pw/grilist/grilist"
16 "fagott.pw/grilist/models" 17 "fagott.pw/grilist/models"
@@ -237,6 +238,7 @@ func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) {
237 238
238func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 239func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
239 user, _ := m.g.Charakterin.GetUserFromRequest(r) 240 user, _ := m.g.Charakterin.GetUserFromRequest(r)
241 el := m.g.EventLogger(r)
240 sid := p.ByName("id") 242 sid := p.ByName("id")
241 243
242 id, err := strconv.Atoi(sid) 244 id, err := strconv.Atoi(sid)
@@ -288,6 +290,9 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou
288 data["SimilarGrils"] = similar 290 data["SimilarGrils"] = similar
289 291
290 m.g.Renderer.RenderPage("gril", w, data) 292 m.g.Renderer.RenderPage("gril", w, data)
293 el.ViewGril(user, eventlogging.ViewGrilData{
294 GrilID: gril.ID,
295 })
291} 296}
292 297
293func pgArray(array []byte) []string { 298func pgArray(array []byte) []string {
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 5934e9a..65e969f 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -14,6 +14,7 @@ import (
14 14
15 "fagott.pw/charakterin" 15 "fagott.pw/charakterin"
16 "fagott.pw/grilist/cache" 16 "fagott.pw/grilist/cache"
17 "fagott.pw/grilist/eventlogging"
17 "fagott.pw/grilist/frontend" 18 "fagott.pw/grilist/frontend"
18 "fagott.pw/grilist/grilist" 19 "fagott.pw/grilist/grilist"
19 "fagott.pw/grilist/models" 20 "fagott.pw/grilist/models"
@@ -260,6 +261,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
260 261
261func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 262func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
262 user, _ := m.g.Charakterin.GetUserFromRequest(r) 263 user, _ := m.g.Charakterin.GetUserFromRequest(r)
264 el := m.g.EventLogger(r)
263 sid := p.ByName("id") 265 sid := p.ByName("id")
264 266
265 id, err := strconv.Atoi(sid) 267 id, err := strconv.Atoi(sid)
@@ -286,9 +288,13 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P
286 } 288 }
287 289
288 m.g.Renderer.RenderPage("list", w, data) 290 m.g.Renderer.RenderPage("list", w, data)
291 el.ViewList(user, eventlogging.ViewListData{
292 ListID: list.ID,
293 })
289} 294}
290 295
291func (m *Module) deleteList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 296func (m *Module) deleteList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
297 el := m.g.EventLogger(r)
292 user, err := m.g.Charakterin.GetUserFromRequest(r) 298 user, err := m.g.Charakterin.GetUserFromRequest(r)
293 if err != nil { 299 if err != nil {
294 log.Println("invalid deleteList user") 300 log.Println("invalid deleteList user")
@@ -334,6 +340,9 @@ func (m *Module) deleteList(w http.ResponseWriter, r *http.Request, p httprouter
334 340
335 log.Printf("list %d has been deleted by the owner %d(%s)", list.ID, list.Owner.ID, list.Owner.GetName()) 341 log.Printf("list %d has been deleted by the owner %d(%s)", list.ID, list.Owner.ID, list.Owner.GetName())
336 http.Redirect(w, r, "/", 302) 342 http.Redirect(w, r, "/", 302)
343 el.DeleteList(user, eventlogging.DeleteListData{
344 ListID: list.ID,
345 })
337} 346}
338 347
339func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 348func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@@ -372,6 +381,7 @@ func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p http
372} 381}
373 382
374func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 383func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
384 el := m.g.EventLogger(r)
375 user, err := m.g.Charakterin.GetUserFromRequest(r) 385 user, err := m.g.Charakterin.GetUserFromRequest(r)
376 if err != nil { 386 if err != nil {
377 log.Println("invalid updateListSettings user") 387 log.Println("invalid updateListSettings user")
@@ -436,6 +446,18 @@ func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p ht
436 return 446 return
437 } 447 }
438 448
449 rows, err := m.g.DB.Query("SELECT name, description FROM grilist.lists WHERE id = $1;", id)
450 if err != nil {
451 log.Println("could not get list info", id)
452 renderWithError("interner fehler", false, false)
453 return
454 }
455 defer rows.Close()
456
457 var oldName string
458 var oldDescription string
459 rows.Scan(&oldName, &oldDescription)
460
439 if list.Name != name && list.Description != description { 461 if list.Name != name && list.Description != description {
440 _, err = m.g.DB.Query(`UPDATE grilist.lists SET name = $2, description = $3 WHERE id = $1`, id, name, description) 462 _, err = m.g.DB.Query(`UPDATE grilist.lists SET name = $2, description = $3 WHERE id = $1`, id, name, description)
441 if err != nil { 463 if err != nil {
@@ -463,9 +485,17 @@ func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p ht
463 list.Description = description 485 list.Description = description
464 486
465 m.viewListSettings(w, r, p) 487 m.viewListSettings(w, r, p)
488 el.EditList(user, eventlogging.EditListData{
489 ListID: id,
490 OldName: oldName,
491 OldDescription: oldDescription,
492 NewName: name,
493 NewDescription: description,
494 })
466} 495}
467 496
468func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 497func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
498 el := m.g.EventLogger(r)
469 slistID := p.ByName("id") 499 slistID := p.ByName("id")
470 500
471 user, err := m.g.Charakterin.GetUserFromRequest(r) 501 user, err := m.g.Charakterin.GetUserFromRequest(r)
@@ -532,7 +562,10 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou
532 data["Value"] = value 562 data["Value"] = value
533 list.Grils = append(list.Grils, lg) 563 list.Grils = append(list.Grils, lg)
534 m.g.Renderer.RenderPage("list_gril", w, data) 564 m.g.Renderer.RenderPage("list_gril", w, data)
535 return 565 el.AddGrilToList(user, eventlogging.AddGrilToListData{
566 ListID: list.ID,
567 GrilID: gril.ID,
568 })
536} 569}
537 570
538func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 571func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@@ -549,6 +582,7 @@ func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p htt
549} 582}
550 583
551func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 584func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
585 el := m.g.EventLogger(r)
552 user, err := m.g.Charakterin.GetUserFromRequest(r) 586 user, err := m.g.Charakterin.GetUserFromRequest(r)
553 if err != nil { 587 if err != nil {
554 log.Println(err) 588 log.Println(err)
@@ -572,9 +606,15 @@ func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter
572 } 606 }
573 607
574 http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302) 608 http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302)
609 el.CreateList(user, eventlogging.CreateListData{
610 ListID: id,
611 Name: values.Get("name"),
612 Description: values.Get("description"),
613 })
575} 614}
576 615
577func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 616func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
617 el := m.g.EventLogger(r)
578 slistID := p.ByName("id") 618 slistID := p.ByName("id")
579 619
580 user, err := m.g.Charakterin.GetUserFromRequest(r) 620 user, err := m.g.Charakterin.GetUserFromRequest(r)
@@ -607,6 +647,16 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr
607 return 647 return
608 } 648 }
609 649
650 var oldOrder int
651 err = m.g.DB.QueryRow(
652 `SELECT "order" FROM grilist.lists_grils WHERE list_id = $1 AND gril_id = $2;`,
653 listID, grilID).Scan(&oldOrder)
654 if err != nil {
655 log.Println(err)
656 http.Error(w, "Internal Server Error", 500)
657 return
658 }
659
610 // rein in die DB damit 660 // rein in die DB damit
611 _, err = m.g.DB.Exec(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, pos) 661 _, err = m.g.DB.Exec(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, pos)
612 if err != nil { 662 if err != nil {
@@ -623,17 +673,30 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr
623 673
624 w.WriteHeader(200) 674 w.WriteHeader(200)
625 w.Write([]byte("ok")) 675 w.Write([]byte("ok"))
626 return 676 el.ChangeGrilOrder(user, eventlogging.ChangeGrilOrderData{
677 ListID: listID,
678 GrilID: grilID,
679 OldOrder: oldOrder,
680 NewOrder: pos,
681 })
627} 682}
628 683
629func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 684func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
685 el := m.g.EventLogger(r)
630 slistID := p.ByName("id") 686 slistID := p.ByName("id")
631 687
688 user, err := m.g.Charakterin.GetUserFromRequest(r)
689 if err != nil {
690 http.Error(w, "Unauthorized", 401)
691 return
692 }
693
632 listID, err := strconv.Atoi(slistID) 694 listID, err := strconv.Atoi(slistID)
633 if err != nil { 695 if err != nil {
634 http.Error(w, "invalid list ID (type mismatch)", 400) 696 http.Error(w, "invalid list ID (type mismatch)", 400)
635 return 697 return
636 } 698 }
699 //TODO: noch mal gucken ob der User Rechte hat
637 700
638 values, err := readBody(r) 701 values, err := readBody(r)
639 if err != nil { 702 if err != nil {
@@ -667,7 +730,10 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht
667 730
668 w.WriteHeader(200) 731 w.WriteHeader(200)
669 w.Write([]byte("ok")) 732 w.Write([]byte("ok"))
670 return 733 el.DeleteGrilFromList(user, eventlogging.DeleteGrilFromListData{
734 ListID: listID,
735 GrilID: grilID,
736 })
671} 737}
672 738
673func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 739func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) {