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
|
2017-05-29 14:52:37 +00:00
|
|
|
RankClass string
|
2017-03-18 07:23:02 +00:00
|
|
|
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-06-15 11:40:35 +00:00
|
|
|
var group_create_mutex sync.Mutex
|
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
|
|
|
|
}
|
2017-06-15 11:40:35 +00:00
|
|
|
|
2017-03-27 07:09:28 +00:00
|
|
|
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
|
2017-06-15 11:40:35 +00:00
|
|
|
|
2017-03-27 07:09:28 +00:00
|
|
|
group_update_mutex.Unlock()
|
|
|
|
return gid, nil
|
|
|
|
}
|
2017-06-15 11:40:35 +00:00
|
|
|
|
|
|
|
group_create_mutex.Lock()
|
2017-03-27 07:09:28 +00:00
|
|
|
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
|
|
|
|
}
|
2017-06-15 11:40:35 +00:00
|
|
|
|
2017-03-27 07:09:28 +00:00
|
|
|
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})
|
2017-06-15 11:40:35 +00:00
|
|
|
group_create_mutex.Unlock()
|
|
|
|
|
2017-03-27 07:09:28 +00:00
|
|
|
// 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"
|
|
|
|
}
|
2017-06-15 11:40:35 +00:00
|
|
|
|
2017-03-27 07:09:28 +00:00
|
|
|
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
|
|
|
|
}
|
2017-06-15 11:40:35 +00:00
|
|
|
|
2017-03-27 07:09:28 +00:00
|
|
|
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!=""
|
|
|
|
}
|