From fbd746aef8c78067593f901afb0f255574392499 Mon Sep 17 00:00:00 2001 From: rtz12 Date: Tue, 11 Oct 2016 20:53:22 +0200 Subject: NSA implementiert diff --git a/.gitignore b/.gitignore index 76e7090..5d77737 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ node_modules/ package.json .npm_update *.d +eventlogging/events.go #Gebuildete Dateien assets/js/* diff --git a/Makefile b/Makefile index fbef6fa..452e1d5 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ LESSC := ./node_modules/less/bin/lessc -include $(DEPFILES) -all: $(CSS_FILES) $(LESS_FILES) $(JS_FILES) +all: $(CSS_FILES) $(LESS_FILES) $(JS_FILES) eventlogging/events.go print_info: @echo $(CSS_FILES) @@ -45,12 +45,16 @@ assets/js/%.js: assets_src/js/%.js assets_src/js/%.d .babelrc .npm_update @echo browserify $< -o $@ @$(BROWSERIFY) --debug $< -o $@ -t babelify +eventlogging/events.go: eventlogging/events.txt + @./bin/generate-events < $< > $@ + clean: -rm -- $(CSS_FILES) -rm -- $(LESS_FILES) -rm -- $(JS_FILES) -rm -- $(DEPFILES) -rm -- importer + -rm -- eventlogging/events.go superclean: clean -rm -- .npm_update 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 @@ +#!/bin/bash +events=$(cat) +echo 'package eventlogging +/* + * THIS FILE IS AUTOGENERATED + * DO NOT EDIT MANUALLY!!! + */ + +import ( + "fagott.pw/charakterin" +) + +' + +echo "var Events = []string{" +while read event +do + cat < 0 && forwardedIPs[0] != "" { + ip = forwardedIPs[0] + } + host, _, err := net.SplitHostPort(ip) + if err != nil { + log.Printf("Could not split IP %s\n", ip) + log.Println(err) + host = "0.0.0.0" + } + return eventlogging.NewEventLogger(host) +} + // Module ist ein Modul für Grilist. type Module interface { Init(*Grilist) diff --git a/main.go b/main.go index 68a9ede..e1c06e6 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,10 @@ import ( "fmt" "log" "net/http" + "strings" "fagott.pw/charakterin" + "fagott.pw/grilist/eventlogging" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/grils" @@ -14,8 +16,7 @@ import ( "fagott.pw/grilist/modules/search" "fagott.pw/grilist/modules/tags" "fagott.pw/grilist/modules/user" - - "strings" + "fagott.pw/nsa" "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" @@ -52,6 +53,13 @@ func main() { log.Println("database connection established") + if err := nsa.Init(db); err != nil { + panic(err) + } + if err := nsa.SetEvents(eventlogging.Events); err != nil { + panic(err) + } + renderer := frontend.New("views") login := charakterin.New(db) login.UseRenderer(renderer) diff --git a/modules/grils/grils.go b/modules/grils/grils.go index 4913e11..debb77b 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go @@ -11,6 +11,7 @@ import ( "fagott.pw/charakterin" "fagott.pw/grilist/cache" + "fagott.pw/grilist/eventlogging" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" @@ -241,6 +242,7 @@ func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { user, _ := m.g.Charakterin.GetUserFromRequest(r) + el := m.g.EventLogger(r) sid := p.ByName("id") id, err := strconv.Atoi(sid) @@ -292,6 +294,9 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou data["SimilarGrils"] = similar m.g.Renderer.RenderPage("gril", w, data) + el.ViewGril(user, eventlogging.ViewGrilData{ + GrilID: gril.ID, + }) } func pgArray(array []byte) []string { diff --git a/modules/lists/lists.go b/modules/lists/lists.go index d1913be..9c9eaf4 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -14,6 +14,7 @@ import ( "fagott.pw/charakterin" "fagott.pw/grilist/cache" + "fagott.pw/grilist/eventlogging" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/grils" @@ -259,6 +260,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { user, _ := m.g.Charakterin.GetUserFromRequest(r) + el := m.g.EventLogger(r) sid := p.ByName("id") id, err := strconv.Atoi(sid) @@ -285,9 +287,13 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P } m.g.Renderer.RenderPage("list", w, data) + el.ViewList(user, eventlogging.ViewListData{ + ListID: list.ID, + }) } func (m *Module) deleteList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + el := m.g.EventLogger(r) user, err := m.g.Charakterin.GetUserFromRequest(r) if err != nil { log.Println("invalid deleteList user") @@ -333,6 +339,9 @@ func (m *Module) deleteList(w http.ResponseWriter, r *http.Request, p httprouter log.Printf("list %d has been deleted by the owner %d(%s)", list.ID, list.Owner.ID, list.Owner.GetName()) http.Redirect(w, r, "/", 302) + el.DeleteList(user, eventlogging.DeleteListData{ + ListID: list.ID, + }) } func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) { @@ -371,6 +380,7 @@ func (m *Module) viewListSettings(w http.ResponseWriter, r *http.Request, p http } func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + el := m.g.EventLogger(r) user, err := m.g.Charakterin.GetUserFromRequest(r) if err != nil { log.Println("invalid updateListSettings user") @@ -435,6 +445,18 @@ func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p ht return } + rows, err := m.g.DB.Query("SELECT name, description FROM grilist.lists WHERE id = $1;", id) + if err != nil { + log.Println("could not get list info", id) + renderWithError("interner fehler", false, false) + return + } + defer rows.Close() + + var oldName string + var oldDescription string + rows.Scan(&oldName, &oldDescription) + if list.Name != name && list.Description != description { _, err = m.g.DB.Query(`UPDATE grilist.lists SET name = $2, description = $3 WHERE id = $1`, id, name, description) if err != nil { @@ -462,9 +484,17 @@ func (m *Module) updateListSettings(w http.ResponseWriter, r *http.Request, p ht list.Description = description m.viewListSettings(w, r, p) + el.EditList(user, eventlogging.EditListData{ + ListID: id, + OldName: oldName, + OldDescription: oldDescription, + NewName: name, + NewDescription: description, + }) } func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + el := m.g.EventLogger(r) slistID := p.ByName("id") user, err := m.g.Charakterin.GetUserFromRequest(r) @@ -531,7 +561,10 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou data["Value"] = value list.Grils = append(list.Grils, lg) m.g.Renderer.RenderPage("list_gril", w, data) - return + el.AddGrilToList(user, eventlogging.AddGrilToListData{ + ListID: list.ID, + GrilID: gril.ID, + }) } func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { @@ -548,6 +581,7 @@ func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p htt } func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + el := m.g.EventLogger(r) user, err := m.g.Charakterin.GetUserFromRequest(r) if err != nil { log.Println(err) @@ -571,9 +605,15 @@ func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter } http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302) + el.CreateList(user, eventlogging.CreateListData{ + ListID: id, + Name: values.Get("name"), + Description: values.Get("description"), + }) } func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + el := m.g.EventLogger(r) slistID := p.ByName("id") user, err := m.g.Charakterin.GetUserFromRequest(r) @@ -606,6 +646,16 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr return } + var oldOrder int + err = m.g.DB.QueryRow( + `SELECT "order" FROM grilist.lists_grils WHERE list_id = $1 AND gril_id = $2;`, + listID, grilID).Scan(&oldOrder) + if err != nil { + log.Println(err) + http.Error(w, "Internal Server Error", 500) + return + } + // rein in die DB damit _, err = m.g.DB.Exec(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, pos) if err != nil { @@ -622,17 +672,30 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr w.WriteHeader(200) w.Write([]byte("ok")) - return + el.ChangeGrilOrder(user, eventlogging.ChangeGrilOrderData{ + ListID: listID, + GrilID: grilID, + OldOrder: oldOrder, + NewOrder: pos, + }) } func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + el := m.g.EventLogger(r) slistID := p.ByName("id") + user, err := m.g.Charakterin.GetUserFromRequest(r) + if err != nil { + http.Error(w, "Unauthorized", 401) + return + } + listID, err := strconv.Atoi(slistID) if err != nil { http.Error(w, "invalid list ID (type mismatch)", 400) return } + //TODO: noch mal gucken ob der User Rechte hat values, err := readBody(r) if err != nil { @@ -666,7 +729,10 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht w.WriteHeader(200) w.Write([]byte("ok")) - return + el.DeleteGrilFromList(user, eventlogging.DeleteGrilFromListData{ + ListID: listID, + GrilID: grilID, + }) } func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) { -- cgit v0.10.1