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"
|
2017-11-13 05:22:37 +00:00
|
|
|
"sync"
|
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-23 05:37:08 +00:00
|
|
|
var FPStore ForumPermsStore
|
2017-11-06 04:02:35 +00:00
|
|
|
|
|
|
|
type ForumPermsStore interface {
|
|
|
|
Init() error
|
2017-11-13 05:22:37 +00:00
|
|
|
Get(fid int, gid int) (fperms *ForumPerms, err error)
|
2018-01-10 03:32:48 +00:00
|
|
|
GetCopy(fid int, gid int) (fperms ForumPerms, err error)
|
2017-11-06 04:02:35 +00:00
|
|
|
Reload(id int) error
|
2017-11-13 05:22:37 +00:00
|
|
|
ReloadGroup(fid int, gid int) error
|
2017-11-06 04:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ForumPermsCache interface {
|
|
|
|
}
|
2017-11-02 02:52:21 +00:00
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
type MemoryForumPermsStore struct {
|
2017-11-13 05:22:37 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
getByForum *sql.Stmt
|
|
|
|
getByForumGroup *sql.Stmt
|
|
|
|
|
|
|
|
updateMutex sync.Mutex
|
2017-11-02 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
func NewMemoryForumPermsStore() (*MemoryForumPermsStore, error) {
|
2017-11-11 23:34:27 +00:00
|
|
|
acc := qgen.Builder.Accumulator()
|
2017-11-06 04:02:35 +00:00
|
|
|
return &MemoryForumPermsStore{
|
2017-11-13 05:22:37 +00:00
|
|
|
get: acc.Select("forums_permissions").Columns("gid, fid, permissions").Orderby("gid ASC, fid ASC").Prepare(),
|
|
|
|
getByForum: acc.Select("forums_permissions").Columns("gid, permissions").Where("fid = ?").Orderby("gid ASC").Prepare(),
|
|
|
|
getByForumGroup: acc.Select("forums_permissions").Columns("permissions").Where("fid = ? AND gid = ?").Prepare(),
|
2017-11-11 23:34:27 +00:00
|
|
|
}, acc.FirstError()
|
2017-11-02 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 04:02:35 +00:00
|
|
|
func (fps *MemoryForumPermsStore) Init() error {
|
2017-11-13 05:22:37 +00:00
|
|
|
fps.updateMutex.Lock()
|
|
|
|
defer fps.updateMutex.Unlock()
|
2017-11-23 05:37:08 +00:00
|
|
|
fids, err := Forums.GetAllIDs()
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugDetail("fids: ", fids)
|
2017-11-05 01:04:57 +00:00
|
|
|
|
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()
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugLog("Adding the forum permissions")
|
|
|
|
DebugDetail("forumPerms[gid][fid]")
|
2017-11-05 01:04:57 +00:00
|
|
|
|
2017-11-13 05:22:37 +00:00
|
|
|
forumPerms = make(map[int]map[int]*ForumPerms)
|
2017-11-05 01:04:57 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var gid, fid int
|
|
|
|
var perms []byte
|
|
|
|
err = rows.Scan(&gid, &fid, &perms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-13 05:22:37 +00:00
|
|
|
pperms, err := fps.parseForumPerm(perms)
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, ok := forumPerms[gid]
|
|
|
|
if !ok {
|
2017-11-13 05:22:37 +00:00
|
|
|
forumPerms[gid] = make(map[int]*ForumPerms)
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugDetail("gid: ", gid)
|
|
|
|
DebugDetail("fid: ", fid)
|
|
|
|
DebugDetailf("perms: %+v\n", pperms)
|
2017-11-05 01:04:57 +00:00
|
|
|
forumPerms[gid][fid] = pperms
|
|
|
|
}
|
|
|
|
|
|
|
|
return fps.cascadePermSetToGroups(forumPerms, fids)
|
|
|
|
}
|
|
|
|
|
2017-11-13 05:22:37 +00:00
|
|
|
func (fps *MemoryForumPermsStore) parseForumPerm(perms []byte) (pperms *ForumPerms, err error) {
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugDetail("perms: ", string(perms))
|
2017-11-13 05:22:37 +00:00
|
|
|
pperms = BlankForumPerms()
|
|
|
|
err = json.Unmarshal(perms, &pperms)
|
|
|
|
pperms.ExtData = make(map[string]bool)
|
|
|
|
pperms.Overrides = true
|
|
|
|
return pperms, err
|
|
|
|
}
|
|
|
|
|
2017-11-05 01:04:57 +00:00
|
|
|
// 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-13 05:22:37 +00:00
|
|
|
fps.updateMutex.Lock()
|
|
|
|
defer fps.updateMutex.Unlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugLogf("Reloading the forum permissions for forum #%d", fid)
|
2017-11-23 05:37:08 +00:00
|
|
|
fids, err := Forums.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
|
|
|
|
err := rows.Scan(&gid, &perms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-13 05:22:37 +00:00
|
|
|
|
|
|
|
pperms, err := fps.parseForumPerm(perms)
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, ok := forumPerms[gid]
|
|
|
|
if !ok {
|
2017-11-13 05:22:37 +00:00
|
|
|
forumPerms[gid] = make(map[int]*ForumPerms)
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
2017-11-13 05:22:37 +00:00
|
|
|
|
2017-11-05 01:04:57 +00:00
|
|
|
forumPerms[gid][fid] = pperms
|
|
|
|
}
|
|
|
|
|
|
|
|
return fps.cascadePermSetToGroups(forumPerms, fids)
|
|
|
|
}
|
|
|
|
|
2017-11-13 05:22:37 +00:00
|
|
|
func (fps *MemoryForumPermsStore) ReloadGroup(fid int, gid int) (err error) {
|
|
|
|
fps.updateMutex.Lock()
|
|
|
|
defer fps.updateMutex.Unlock()
|
|
|
|
var perms []byte
|
|
|
|
err = fps.getByForumGroup.QueryRow(fid, gid).Scan(&perms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fperms, err := fps.parseForumPerm(perms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-23 05:37:08 +00:00
|
|
|
group, err := Groups.Get(gid)
|
2017-11-13 05:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: Refactor this
|
|
|
|
group.Forums[fid] = fperms
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fps *MemoryForumPermsStore) cascadePermSetToGroups(forumPerms map[int]map[int]*ForumPerms, fids []int) error {
|
2017-11-23 05:37:08 +00:00
|
|
|
groups, err := Groups.GetAll()
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range groups {
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugLogf("Updating the forum permissions for Group #%d", group.ID)
|
2017-11-13 05:22:37 +00:00
|
|
|
group.Forums = []*ForumPerms{BlankForumPerms()}
|
2017-11-05 01:04:57 +00:00
|
|
|
group.CanSee = []int{}
|
|
|
|
fps.cascadePermSetToGroup(forumPerms, group, fids)
|
|
|
|
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugDetailf("group.CanSee (length %d): %+v \n", len(group.CanSee), group.CanSee)
|
|
|
|
DebugDetailf("group.Forums (length %d): %+v\n", len(group.Forums), group.Forums)
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 05:22:37 +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 {
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugDetailf("Forum #%+v\n", fid)
|
2017-11-05 01:04:57 +00:00
|
|
|
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)
|
2017-11-13 05:22:37 +00:00
|
|
|
forumPerm = BlankForumPerms()
|
2017-11-05 01:04:57 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugDetail("group.ID: ", group.ID)
|
|
|
|
DebugDetailf("forumPerm: %+v\n", forumPerm)
|
|
|
|
DebugDetail("group.CanSee: ", group.CanSee)
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 05:22:37 +00:00
|
|
|
// TODO: Add a hook here and have plugin_guilds use it
|
2018-01-10 03:32:48 +00:00
|
|
|
// TODO: Check if the forum exists?
|
|
|
|
// TODO: Fix the races
|
2017-11-13 05:22:37 +00:00
|
|
|
func (fps *MemoryForumPermsStore) Get(fid int, gid int) (fperms *ForumPerms, err error) {
|
2017-11-23 05:37:08 +00:00
|
|
|
group, err := Groups.Get(gid)
|
2017-11-02 02:52:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return fperms, ErrNoRows
|
|
|
|
}
|
|
|
|
return group.Forums[fid], nil
|
|
|
|
}
|
2018-01-10 03:32:48 +00:00
|
|
|
|
|
|
|
// TODO: Check if the forum exists?
|
|
|
|
// TODO: Fix the races
|
|
|
|
func (fps *MemoryForumPermsStore) GetCopy(fid int, gid int) (fperms ForumPerms, err error) {
|
|
|
|
group, err := Groups.Get(gid)
|
|
|
|
if err != nil {
|
|
|
|
return fperms, ErrNoRows
|
|
|
|
}
|
|
|
|
return *group.Forums[fid], nil
|
|
|
|
}
|