From d3d76d407bff1e5b0b942c1e8ec34cb46b9de7fe Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 5 Apr 2017 15:05:37 +0100 Subject: [PATCH] Modlogs / Adminlogs WIP, not much to see here. I'm just dumping this here, as I'm going to be moving a lot of code into another file, and I didn't want it to mess with the diffs. --- data.sql | 16 ++++++++++++++++ mod_routes.go | 23 +++++++++++++++++++++++ mysql.go | 14 ++++++++++++++ routes.go | 1 + utils.go | 16 ++++++++++++++++ 5 files changed, 70 insertions(+) diff --git a/data.sql b/data.sql index 22e6b6cb..7a924f8d 100644 --- a/data.sql +++ b/data.sql @@ -169,6 +169,22 @@ CREATE TABLE `themes`( unique(`uname`) ); +CREATE TABLE `moderation_logs`( + `action` varchar(100) not null, + `elementID` int not null, + `elementType` varchar(100) not null, + `ipaddress` varchar(200) not null, + `actorID` int not null +); + +CREATE TABLE `administration_logs`( + `action` varchar(100) not null, + `elementID` int not null, + `elementType` varchar(100) not null, + `ipaddress` varchar(200) not null, + `actorID` int not null +); + INSERT INTO settings(`name`,`content`,`type`) VALUES ('url_tags','1','bool'); INSERT INTO settings(`name`,`content`,`type`,`constraints`) VALUES ('activation_type','1','list','1-3'); INSERT INTO settings(`name`,`content`,`type`) VALUES ('bigpost_min_chars','250','int'); diff --git a/mod_routes.go b/mod_routes.go index 2f67d42f..10671a28 100644 --- a/mod_routes.go +++ b/mod_routes.go @@ -73,6 +73,12 @@ func route_edit_topic(w http.ResponseWriter, r *http.Request) { } else { action = "unlock" } + + err = addModLog(action,tid,"topic",ipaddress,user.ID) + if err != nil { + InternalError(err,w,r) + return + } _, err = create_action_reply_stmt.Exec(tid,action,ipaddress,user.ID) if err != nil { InternalError(err,w,r) @@ -137,6 +143,23 @@ func route_delete_topic(w http.ResponseWriter, r *http.Request) { InternalError(err,w,r) return } + + ipaddress, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + LocalError("Bad IP",w,r,user) + return + } + err = addModLog("delete",tid,"topic",ipaddress,user.ID) + if err != nil { + InternalError(err,w,r) + return + } + /*_, err = create_action_reply_stmt.Exec(tid,"delete",ipaddress,user.ID) + if err != nil { + InternalError(err,w,r) + return + }*/ + log.Print("The topic '" + strconv.Itoa(tid) + "' was deleted by User ID #" + strconv.Itoa(user.ID) + ".") http.Redirect(w,r,"/",http.StatusSeeOther) diff --git a/mysql.go b/mysql.go index a2e30564..99dbfffa 100644 --- a/mysql.go +++ b/mysql.go @@ -91,6 +91,8 @@ var update_group_stmt *sql.Stmt var create_group_stmt *sql.Stmt var add_theme_stmt *sql.Stmt var update_theme_stmt *sql.Stmt +var add_modlog_entry_stmt *sql.Stmt +var add_adminlog_entry_stmt *sql.Stmt func init_database(err error) { if(dbpassword != ""){ @@ -594,6 +596,18 @@ func init_database(err error) { log.Fatal(err) } + log.Print("Preparing add_modlog_entry statement.") + add_modlog_entry_stmt, err = db.Prepare("INSERT INTO moderation_logs(action,elementID,elementType,ipaddress,actorID) VALUES(?,?,?,?,?)") + if err != nil { + log.Fatal(err) + } + + log.Print("Preparing add_adminlog_entry statement.") + add_adminlog_entry_stmt, err = db.Prepare("INSERT INTO moderation_logs(action,elementID,elementType,actorID) VALUES(?,?,?,?)") + if err != nil { + log.Fatal(err) + } + log.Print("Loading the usergroups.") groups = append(groups, Group{ID:0,Name:"System"}) diff --git a/routes.go b/routes.go index 3a60b764..f6af4b4b 100644 --- a/routes.go +++ b/routes.go @@ -15,6 +15,7 @@ import "net/http" import "html" import "html/template" import "database/sql" + import _ "github.com/go-sql-driver/mysql" import "golang.org/x/crypto/bcrypt" diff --git a/utils.go b/utils.go index 487b69e6..4ed1aa78 100644 --- a/utils.go +++ b/utils.go @@ -223,3 +223,19 @@ func fill_group_id_gap(biggerID int, smallerID int) { groups = append(groups, dummy) } } + +func addModLog(action string, elementID int, elementType string, ipaddress string, actorID int) error { + _, err := add_modlog_entry_stmt.Exec(action,elementID,elementType,ipaddress,actorID) + if err != nil { + return err + } + return nil +} + +func addAdminLog(action string, elementID string, elementType int, ipaddress string, actorID int) error { + _, err := add_adminlog_entry_stmt.Exec(action,elementID,elementType,ipaddress,actorID) + if err != nil { + return err + } + return nil +}