2016-12-04 06:16:59 +00:00
|
|
|
package main
|
|
|
|
|
2017-03-18 07:23:02 +00:00
|
|
|
import "sync"
|
2017-03-27 07:09:28 +00:00
|
|
|
import "encoding/json"
|
|
|
|
import "database/sql"
|
|
|
|
import _ "github.com/go-sql-driver/mysql"
|
2017-03-18 07:23:02 +00:00
|
|
|
|
|
|
|
var group_update_mutex sync.Mutex
|
|
|
|
|
|
|
|
type GroupAdmin struct
|
|
|
|
{
|
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Rank string
|
|
|
|
RankEmoji string
|
|
|
|
CanEdit bool
|
|
|
|
CanDelete bool
|
|
|
|
}
|
|
|
|
|
2016-12-04 06:16:59 +00:00
|
|
|
type Group struct
|
|
|
|
{
|
|
|
|
ID int
|
|
|
|
Name string
|
2016-12-07 13:46:14 +00:00
|
|
|
Is_Mod bool
|
2016-12-04 06:16:59 +00:00
|
|
|
Is_Admin bool
|
|
|
|
Is_Banned bool
|
2016-12-07 09:34:09 +00:00
|
|
|
Tag string
|
2017-01-31 05:13:38 +00:00
|
|
|
Perms Perms
|
|
|
|
PermissionsText []byte
|
|
|
|
Forums []ForumPerms
|
|
|
|
CanSee []int // The IDs of the forums this group can see
|
2016-12-04 06:16:59 +00:00
|
|
|
}
|
2017-03-18 07:23:02 +00:00
|
|
|
|
2017-03-27 07:09:28 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-18 07:23:02 +00:00
|
|
|
func group_exists(gid int) bool {
|
|
|
|
return (gid <= groupCapCount) && (gid > 0) && groups[gid].Name!=""
|
|
|
|
}
|