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.
This commit is contained in:
Azareal 2017-03-27 08:09:28 +01:00
parent bf19b66a94
commit 42eb509448
14 changed files with 211 additions and 11 deletions

View File

@ -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
}

View File

@ -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!=""
}

View File

@ -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)

View File

@ -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 {

View File

@ -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"})

View File

@ -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
}

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -17,5 +17,34 @@
</div>
{{end}}
</div>
<div class="colstack_item colstack_head">
<div class="rowitem rowhead"><a>Create Group</a></div>
</div>
<div class="colstack_item">
<form action="/panel/groups/create/?session={{.CurrentUser.Session}}" method="post">
<div class="formrow">
<div class="formitem"><a>Name</a></div>
<div class="formitem"><input name="group-name" type="text" /></div>
</div>
<div class="formrow">
<div class="formitem"><a>Type</a></div>
<div class="formitem">
<select name="group-type"{{if not .CurrentUser.Perms.EditGroupGlobalPerms}} disabled{{end}}>
<option selected>Member</option>
<option{{if not .CurrentUser.Perms.EditGroupSuperMod}} disabled{{end}}>Mod</option>
<option{{if not .CurrentUser.Perms.EditGroupAdmin}} disabled{{end}}>Admin</option>
<option>Banned</option>
</select>
</div>
</div>
<div class="formrow">
<div class="formitem"><a>Tag</a></div>
<div class="formitem"><input name="group-tag" type="text" /></div>
</div>
<div class="formrow">
<div class="formitem"><button name="panel-button" class="formbutton">Add Group</button></div>
</div>
</form>
</div>
</div>
{{template "footer.html" . }}