2017-11-10 03:43:38 +00:00
|
|
|
package common
|
2017-11-02 02:52:21 +00:00
|
|
|
|
2017-11-05 01:04:57 +00:00
|
|
|
import (
|
2017-11-06 04:02:35 +00:00
|
|
|
"database/sql"
|
2017-11-05 01:04:57 +00:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2017-11-06 04:02:35 +00:00
|
|
|
|
2017-11-10 03:43:38 +00:00
|
|
|
"../query_gen/lib"
|
2017-11-05 01:04:57 +00:00
|
|
|
)
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
var Fpstore ForumPermsStore
|
2017-11-06 04:02:35 +00:00
|
|
|
|
|
|
|
type ForumPermsStore interface {
|
|
|
|
Init() error
|
|
|
|
Get(fid int, gid int) (fperms ForumPerms, err error)
|
|
|
|
Reload(id int) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type ForumPermsCache interface {
|
|
|
|
}
|
2017-11-02 02:52:21 +00:00
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
type MemoryForumPermsStore struct {
|
|
|
|
get *sql.Stmt
|
|
|
|
getByForum *sql.Stmt
|
2017-11-02 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
func NewMemoryForumPermsStore() (*MemoryForumPermsStore, error) {
|
|
|
|
getPermsStmt, err := qgen.Builder.SimpleSelect("forums_permissions", "gid, fid, permissions", "", "gid ASC, fid ASC", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
getPermsByForumStmt, err := qgen.Builder.SimpleSelect("forums_permissions", "gid, permissions", "fid = ?", "gid ASC", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &MemoryForumPermsStore{
|
|
|
|
get: getPermsStmt,
|
|
|
|
getByForum: getPermsByForumStmt,
|
|
|
|
}, nil
|
2017-11-02 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
func (fps *MemoryForumPermsStore) Init() error {
|
2017-11-11 04:06:16 +00:00
|
|
|
fids, err := Fstore.GetAllIDs()
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.SuperDebug {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Print("fids: ", fids)
|
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
rows, err := fps.get.Query()
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.DebugMode {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Print("Adding the forum permissions")
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.SuperDebug {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Print("forumPerms[gid][fid]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Temporarily store the forum perms in a map before transferring it to a much faster and thread-safe slice
|
|
|
|
forumPerms = make(map[int]map[int]ForumPerms)
|
|
|
|
for rows.Next() {
|
|
|
|
var gid, fid int
|
|
|
|
var perms []byte
|
|
|
|
var pperms ForumPerms
|
|
|
|
err = rows.Scan(&gid, &fid, &perms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.SuperDebug {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Print("perms: ", string(perms))
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(perms, &pperms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pperms.ExtData = make(map[string]bool)
|
|
|
|
pperms.Overrides = true
|
|
|
|
_, ok := forumPerms[gid]
|
|
|
|
if !ok {
|
|
|
|
forumPerms[gid] = make(map[int]ForumPerms)
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.SuperDebug {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Print("gid: ", gid)
|
|
|
|
log.Print("fid: ", fid)
|
|
|
|
log.Printf("perms: %+v\n", pperms)
|
|
|
|
}
|
|
|
|
forumPerms[gid][fid] = pperms
|
|
|
|
}
|
|
|
|
|
|
|
|
return fps.cascadePermSetToGroups(forumPerms, fids)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Need a more thread-safe way of doing this. Possibly with sync.Map?
|
2017-11-06 04:02:35 +00:00
|
|
|
func (fps *MemoryForumPermsStore) Reload(fid int) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.DebugMode {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Printf("Reloading the forum permissions for forum #%d", fid)
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
fids, err := Fstore.GetAllIDs()
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
rows, err := fps.getByForum.Query(fid)
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var gid int
|
|
|
|
var perms []byte
|
|
|
|
var pperms ForumPerms
|
|
|
|
err := rows.Scan(&gid, &perms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(perms, &pperms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pperms.ExtData = make(map[string]bool)
|
|
|
|
pperms.Overrides = true
|
|
|
|
_, ok := forumPerms[gid]
|
|
|
|
if !ok {
|
|
|
|
forumPerms[gid] = make(map[int]ForumPerms)
|
|
|
|
}
|
|
|
|
forumPerms[gid][fid] = pperms
|
|
|
|
}
|
|
|
|
|
|
|
|
return fps.cascadePermSetToGroups(forumPerms, fids)
|
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
func (fps *MemoryForumPermsStore) cascadePermSetToGroups(forumPerms map[int]map[int]ForumPerms, fids []int) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
groups, err := Gstore.GetAll()
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range groups {
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.DebugMode {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Printf("Updating the forum permissions for Group #%d", group.ID)
|
|
|
|
}
|
|
|
|
group.Forums = []ForumPerms{BlankForumPerms}
|
|
|
|
group.CanSee = []int{}
|
|
|
|
fps.cascadePermSetToGroup(forumPerms, group, fids)
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.SuperDebug {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Printf("group.CanSee (length %d): %+v \n", len(group.CanSee), group.CanSee)
|
|
|
|
log.Printf("group.Forums (length %d): %+v\n", len(group.Forums), group.Forums)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
func (fps *MemoryForumPermsStore) cascadePermSetToGroup(forumPerms map[int]map[int]ForumPerms, group *Group, fids []int) {
|
2017-11-05 01:04:57 +00:00
|
|
|
for _, fid := range fids {
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.SuperDebug {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Printf("Forum #%+v\n", fid)
|
|
|
|
}
|
|
|
|
forumPerm, ok := forumPerms[group.ID][fid]
|
|
|
|
if ok {
|
2017-11-11 04:06:16 +00:00
|
|
|
//log.Printf("Overriding permissions for forum #%d",fid)
|
2017-11-05 01:04:57 +00:00
|
|
|
group.Forums = append(group.Forums, forumPerm)
|
|
|
|
} else {
|
|
|
|
//log.Printf("Inheriting from group defaults for forum #%d",fid)
|
|
|
|
forumPerm = BlankForumPerms
|
|
|
|
group.Forums = append(group.Forums, forumPerm)
|
|
|
|
}
|
|
|
|
if forumPerm.Overrides {
|
|
|
|
if forumPerm.ViewTopic {
|
|
|
|
group.CanSee = append(group.CanSee, fid)
|
|
|
|
}
|
|
|
|
} else if group.Perms.ViewTopic {
|
|
|
|
group.CanSee = append(group.CanSee, fid)
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.SuperDebug {
|
2017-11-05 01:04:57 +00:00
|
|
|
log.Print("group.ID: ", group.ID)
|
|
|
|
log.Printf("forumPerm: %+v\n", forumPerm)
|
|
|
|
log.Print("group.CanSee: ", group.CanSee)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
func (fps *MemoryForumPermsStore) Get(fid int, gid int) (fperms ForumPerms, err error) {
|
2017-11-02 04:12:51 +00:00
|
|
|
// TODO: Add a hook here and have plugin_guilds use it
|
2017-11-11 04:06:16 +00:00
|
|
|
group, err := Gstore.Get(gid)
|
2017-11-02 02:52:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return fperms, ErrNoRows
|
|
|
|
}
|
|
|
|
return group.Forums[fid], nil
|
|
|
|
}
|