From 42eb50944840b3983426fe3ee2e9be99aa22195b Mon Sep 17 00:00:00 2001 From: Azareal Date: Mon, 27 Mar 2017 08:09:28 +0100 Subject: [PATCH] Added the Group Creator. Generated files should now be detected by Github. I'll work on making the detection method a little less ugly once Github lets me put that thing on something other than the first line. Fixed a race condition in the Forum Creator. Fixed a few forgotten error checks in permmap_to_query. --- forum.go | 5 +++ group.go | 86 +++++++++++++++++++++++++++++++++++-- main.go | 1 + mod_routes.go | 53 +++++++++++++++++++++++ mysql.go | 20 +++++++-- permissions.go | 14 +++++- template_forum.go | 1 + template_forums.go | 3 +- template_profile.go | 3 +- template_topic.go | 3 +- template_topic_alt.go | 1 + template_topics.go | 1 + templates.go | 2 +- templates/panel-groups.html | 29 +++++++++++++ 14 files changed, 211 insertions(+), 11 deletions(-) diff --git a/forum.go b/forum.go index 7561eaf6..7b2fad0b 100644 --- a/forum.go +++ b/forum.go @@ -1,5 +1,7 @@ package main + //import "fmt" +import "sync" import "strconv" import "database/sql" import _ "github.com/go-sql-driver/mysql" @@ -38,6 +40,7 @@ type ForumSimple struct Preset string } +var forum_update_mutex sync.Mutex func create_forum(forum_name string, active bool, preset string) (int, error) { var fid int err := forum_entry_exists_stmt.QueryRow().Scan(&fid) @@ -45,6 +48,7 @@ func create_forum(forum_name string, active bool, preset string) (int, error) { return 0, err } if err != sql.ErrNoRows { + forum_update_mutex.Lock() _, err = update_forum_stmt.Exec(forum_name, active, preset, fid) if err != nil { return fid, err @@ -52,6 +56,7 @@ func create_forum(forum_name string, active bool, preset string) (int, error) { forums[fid].Name = forum_name forums[fid].Active = active forums[fid].Preset = preset + forum_update_mutex.Unlock() return fid, nil } diff --git a/group.go b/group.go index 9fdf0ca8..63754b0a 100644 --- a/group.go +++ b/group.go @@ -1,6 +1,9 @@ package main import "sync" +import "encoding/json" +import "database/sql" +import _ "github.com/go-sql-driver/mysql" var group_update_mutex sync.Mutex @@ -28,9 +31,86 @@ type Group struct CanSee []int // The IDs of the forums this group can see } +func create_group(group_name string, tag string, is_admin bool, is_mod bool, is_banned bool) (int, error) { + var gid int + err := group_entry_exists_stmt.QueryRow().Scan(&gid) + if err != nil && err != sql.ErrNoRows { + return 0, err + } + if err != sql.ErrNoRows { + group_update_mutex.Lock() + _, err = update_group_rank_stmt.Exec(is_admin, is_mod, is_banned, gid) + if err != nil { + return gid, err + } + _, err = update_group_stmt.Exec(group_name, tag, gid) + if err != nil { + return gid, err + } + + groups[gid].Name = group_name + groups[gid].Tag = tag + groups[gid].Is_Banned = is_banned + groups[gid].Is_Mod = is_mod + groups[gid].Is_Admin = is_admin + + group_update_mutex.Unlock() + return gid, nil + } + + var permstr string = "{}" + res, err := create_group_stmt.Exec(group_name, tag, is_admin, is_mod, is_banned, permstr) + if err != nil { + return 0, err + } + + gid64, err := res.LastInsertId() + if err != nil { + return 0, err + } + gid = int(gid64) + perms := BlankPerms + var blankForums []ForumPerms + var blankIntList []int + groups = append(groups, Group{gid,group_name,is_mod,is_admin,is_banned,tag,perms,[]byte(permstr),blankForums,blankIntList}) + + // Generate the forum permissions based on the presets... + fdata := forums + permupdate_mutex.Lock() + for _, forum := range fdata { + var thePreset string + if is_admin { + thePreset = "admins" + } else if is_mod { + thePreset = "staff" + } else if is_banned { + thePreset = "banned" + } else { + thePreset = "members" + } + + permmap := preset_to_permmap(forum.Preset) + permitem := permmap[thePreset] + permitem.Overrides = true + permstr, err := json.Marshal(permitem) + if err != nil { + return gid, err + } + perms := string(permstr) + _, err = add_forum_perms_to_group_stmt.Exec(gid,forum.ID,forum.Preset,perms) + if err != nil { + return gid, err + } + + err = rebuild_forum_permissions(forum.ID) + if err != nil { + return gid, err + } + } + permupdate_mutex.Unlock() + return gid, nil +} + func group_exists(gid int) bool { - //fmt.Println(gid <= groupCapCount) - //fmt.Println(gid > 0) - //fmt.Println(groups[gid].Name!="") return (gid <= groupCapCount) && (gid > 0) && groups[gid].Name!="" } diff --git a/main.go b/main.go index ec962da0..caaad5be 100644 --- a/main.go +++ b/main.go @@ -263,6 +263,7 @@ func main(){ router.HandleFunc("/panel/groups/edit/perms/", route_panel_groups_edit_perms) router.HandleFunc("/panel/groups/edit/submit/", route_panel_groups_edit_submit) router.HandleFunc("/panel/groups/edit/perms/submit/", route_panel_groups_edit_perms_submit) + router.HandleFunc("/panel/groups/create/", route_panel_groups_create_submit) router.HandleFunc("/api/", route_api) //router.HandleFunc("/exit/", route_exit) diff --git a/mod_routes.go b/mod_routes.go index aca0760e..482a0ca1 100644 --- a/mod_routes.go +++ b/mod_routes.go @@ -1775,6 +1775,59 @@ func route_panel_groups_edit_perms_submit(w http.ResponseWriter, r *http.Request http.Redirect(w,r,"/panel/groups/edit/perms/" + strconv.Itoa(gid),http.StatusSeeOther) } +func route_panel_groups_create_submit(w http.ResponseWriter, r *http.Request){ + user, ok := SimpleSessionCheck(w,r) + if !ok { + return + } + if !user.Is_Super_Mod || !user.Perms.EditGroup { + NoPermissions(w,r,user) + return + } + if r.FormValue("session") != user.Session { + SecurityError(w,r,user) + return + } + + group_name := r.PostFormValue("group-name") + if group_name == "" { + LocalError("You need a name for this group!",w,r,user) + return + } + group_tag := r.PostFormValue("group-tag") + + var is_admin bool + var is_mod bool + var is_banned bool + if user.Perms.EditGroupGlobalPerms { + group_type := r.PostFormValue("group-type") + if group_type == "Admin" { + if !user.Perms.EditGroupAdmin { + LocalError("You need the EditGroupAdmin permission can create admin groups",w,r,user) + return + } + is_admin = true + is_mod = true + } else if group_type == "Mod" { + if !user.Perms.EditGroupSuperMod { + LocalError("You need the EditGroupSuperMod permission can create admin groups",w,r,user) + return + } + is_mod = true + } else if group_type == "Banned" { + is_banned = true + } + } + + gid, err := create_group(group_name, group_tag, is_admin, is_mod, is_banned) + if err != nil { + InternalError(err,w,r) + return + } + fmt.Println(groups) + http.Redirect(w,r,"/panel/groups/edit/" + strconv.Itoa(gid),http.StatusSeeOther) +} + func route_panel_themes(w http.ResponseWriter, r *http.Request){ user, noticeList, ok := SessionCheck(w,r) if !ok { diff --git a/mysql.go b/mysql.go index 3c637266..22a9e071 100644 --- a/mysql.go +++ b/mysql.go @@ -73,12 +73,13 @@ var create_forum_stmt *sql.Stmt var delete_forum_stmt *sql.Stmt var update_forum_stmt *sql.Stmt var forum_entry_exists_stmt *sql.Stmt +var group_entry_exists_stmt *sql.Stmt var delete_forum_perms_by_forum_stmt *sql.Stmt var add_forum_perms_to_forum_stmt *sql.Stmt var add_forum_perms_to_forum_admins_stmt *sql.Stmt var add_forum_perms_to_forum_staff_stmt *sql.Stmt var add_forum_perms_to_forum_members_stmt *sql.Stmt -var add_forum_perms_to_forum_guests_stmt *sql.Stmt +var add_forum_perms_to_group_stmt *sql.Stmt var update_setting_stmt *sql.Stmt var add_plugin_stmt *sql.Stmt var update_plugin_stmt *sql.Stmt @@ -86,6 +87,7 @@ var update_user_stmt *sql.Stmt var update_group_perms_stmt *sql.Stmt var update_group_rank_stmt *sql.Stmt var update_group_stmt *sql.Stmt +var create_group_stmt *sql.Stmt var add_theme_stmt *sql.Stmt var update_theme_stmt *sql.Stmt @@ -483,6 +485,12 @@ func init_database(err error) { log.Fatal(err) } + log.Print("Preparing group_entry_exists statement.") + group_entry_exists_stmt, err = db.Prepare("SELECT `gid` FROM `users_groups` WHERE `name` = '' order by gid asc limit 1") + if err != nil { + log.Fatal(err) + } + log.Print("Preparing delete_forum_perms_by_forum statement.") delete_forum_perms_by_forum_stmt, err = db.Prepare("DELETE FROM forums_permissions WHERE fid = ?") if err != nil { @@ -513,8 +521,8 @@ func init_database(err error) { log.Fatal(err) } - log.Print("Preparing add_forum_perms_to_forum_guests statement.") - add_forum_perms_to_forum_guests_stmt, err = db.Prepare("INSERT INTO forums_permissions(gid,fid,preset,permissions) VALUES(6,?,?,?)") + log.Print("Preparing add_forum_perms_to_group statement.") + add_forum_perms_to_group_stmt, err = db.Prepare("INSERT INTO forums_permissions(gid,fid,preset,permissions) VALUES(?,?,?,?)") if err != nil { log.Fatal(err) } @@ -573,6 +581,12 @@ func init_database(err error) { log.Fatal(err) } + log.Print("Preparing create_group statement.") + create_group_stmt, err = db.Prepare("INSERT INTO users_groups(name,tag,is_admin,is_mod,is_banned,permissions) VALUES(?,?,?,?,?,?)") + if err != nil { + log.Fatal(err) + } + log.Print("Loading the usergroups.") groups = append(groups, Group{ID:0,Name:"System"}) diff --git a/permissions.go b/permissions.go index 589e6c68..29a1f4da 100644 --- a/permissions.go +++ b/permissions.go @@ -266,25 +266,37 @@ func permmap_to_query(permmap map[string]ForumPerms, fid int) error { } perms, err := json.Marshal(permmap["admins"]) + if err != nil { + return err + } _, err = add_forum_perms_to_forum_admins_stmt.Exec(fid,"",perms) if err != nil { return err } perms, err = json.Marshal(permmap["staff"]) + if err != nil { + return err + } _, err = add_forum_perms_to_forum_staff_stmt.Exec(fid,"",perms) if err != nil { return err } perms, err = json.Marshal(permmap["members"]) + if err != nil { + return err + } _, err = add_forum_perms_to_forum_members_stmt.Exec(fid,"",perms) if err != nil { return err } perms, err = json.Marshal(permmap["guests"]) - _, err = add_forum_perms_to_forum_guests_stmt.Exec(fid,"",perms) + if err != nil { + return err + } + _, err = add_forum_perms_to_group_stmt.Exec(6,fid,"",perms) if err != nil { return err } diff --git a/template_forum.go b/template_forum.go index 55a5e609..3f607036 100644 --- a/template_forum.go +++ b/template_forum.go @@ -1,3 +1,4 @@ +// Code generated by. DO NOT EDIT. /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main import "io" diff --git a/template_forums.go b/template_forums.go index d912d703..01a6ab08 100644 --- a/template_forums.go +++ b/template_forums.go @@ -1,7 +1,8 @@ +// Code generated by. DO NOT EDIT. /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main -import "strconv" import "io" +import "strconv" func init() { template_forums_handle = template_forums diff --git a/template_profile.go b/template_profile.go index 1e4dc78d..4fb4d95f 100644 --- a/template_profile.go +++ b/template_profile.go @@ -1,7 +1,8 @@ +// Code generated by. DO NOT EDIT. /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main -import "strconv" import "io" +import "strconv" func init() { template_profile_handle = template_profile diff --git a/template_topic.go b/template_topic.go index 80201881..4e87987d 100644 --- a/template_topic.go +++ b/template_topic.go @@ -1,7 +1,8 @@ +// Code generated by. DO NOT EDIT. /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main -import "io" import "strconv" +import "io" func init() { template_topic_handle = template_topic diff --git a/template_topic_alt.go b/template_topic_alt.go index c8518fc9..481c888f 100644 --- a/template_topic_alt.go +++ b/template_topic_alt.go @@ -1,3 +1,4 @@ +// Code generated by. DO NOT EDIT. /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main import "io" diff --git a/template_topics.go b/template_topics.go index e42c7c91..eaf64471 100644 --- a/template_topics.go +++ b/template_topics.go @@ -1,3 +1,4 @@ +// Code generated by. DO NOT EDIT. /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main import "io" diff --git a/templates.go b/templates.go index e8126475..8b2a39b8 100644 --- a/templates.go +++ b/templates.go @@ -150,7 +150,7 @@ func (c *CTemplateSet) compile_template(name string, dir string, expects string, varString += "var " + varItem.Name + " " + varItem.Type + " = " + varItem.Destination + "\n" } - fout := "/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */\n" + fout := "// Code generated by. DO NOT EDIT.\n/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */\n" fout += "package main\n" + importList + c.pVarList + "\n" fout += "func init() {\n\ttemplate_" + fname +"_handle = template_" + fname + "\n\t//o_template_" + fname +"_handle = template_" + fname + "\n\tctemplates = append(ctemplates,\"" + fname + "\")\n\ttmpl_ptr_map[\"" + fname + "\"] = &template_" + fname + "_handle\n\ttmpl_ptr_map[\"o_" + fname + "\"] = template_" + fname + "\n}\n\n" fout += "func template_" + fname + "(tmpl_" + fname + "_vars " + expects + ", w io.Writer) {\n" + varString + out + "}\n" diff --git a/templates/panel-groups.html b/templates/panel-groups.html index 48304b48..f22bfffb 100644 --- a/templates/panel-groups.html +++ b/templates/panel-groups.html @@ -17,5 +17,34 @@ {{end}} +
+ +
+
+
+
+ +
+
+
+ +
+ +
+
+
+ +
+
+
+
+
+
+
{{template "footer.html" . }} \ No newline at end of file