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
|
2018-04-24 03:47:39 +00:00
|
|
|
GetAllMap() (bigMap map[int]map[int]*ForumPerms)
|
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)
|
2018-04-23 21:08:31 +00:00
|
|
|
ReloadAll() error
|
2017-11-06 04:02:35 +00:00
|
|
|
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 {
|
2017-11-13 05:22:37 +00:00
|
|
|
getByForum *sql.Stmt
|
|
|
|
getByForumGroup *sql.Stmt
|
|
|
|
|
2018-04-23 21:08:31 +00:00
|
|
|
evenForums map[int]map[int]*ForumPerms
|
|
|
|
oddForums map[int]map[int]*ForumPerms // [fid][gid]*ForumPerms
|
|
|
|
evenLock sync.RWMutex
|
|
|
|
oddLock sync.RWMutex
|
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
|
|
|
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(),
|
2018-04-23 21:08:31 +00:00
|
|
|
|
|
|
|
evenForums: make(map[int]map[int]*ForumPerms),
|
|
|
|
oddForums: make(map[int]map[int]*ForumPerms),
|
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 {
|
2018-04-23 21:08:31 +00:00
|
|
|
DebugLog("Initialising the forum perms store")
|
|
|
|
return fps.ReloadAll()
|
|
|
|
}
|
2017-11-05 01:04:57 +00:00
|
|
|
|
2018-04-23 21:08:31 +00:00
|
|
|
func (fps *MemoryForumPermsStore) ReloadAll() error {
|
|
|
|
DebugLog("Reloading the forum perms")
|
|
|
|
fids, err := Forums.GetAllIDs()
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
for _, fid := range fids {
|
|
|
|
err := fps.Reload(fid)
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
return nil
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugLogf("Reloading the forum permissions for forum #%d", fid)
|
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()
|
|
|
|
|
2018-04-23 21:08:31 +00:00
|
|
|
var forumPerms = make(map[int]*ForumPerms)
|
2017-11-05 01:04:57 +00:00
|
|
|
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
|
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
forumPerms[gid] = pperms
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
DebugLogf("forumPerms: %+v\n", forumPerms)
|
|
|
|
if fid%2 == 0 {
|
|
|
|
fps.evenLock.Lock()
|
|
|
|
fps.evenForums[fid] = forumPerms
|
|
|
|
fps.evenLock.Unlock()
|
|
|
|
} else {
|
|
|
|
fps.oddLock.Lock()
|
|
|
|
fps.oddForums[fid] = forumPerms
|
|
|
|
fps.oddLock.Unlock()
|
2017-11-13 05:22:37 +00:00
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
|
|
|
|
groups, err := Groups.GetAll()
|
2017-11-13 05:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
fids, err := Forums.GetAllIDs()
|
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-05 01:04:57 +00:00
|
|
|
group.CanSee = []int{}
|
2018-04-23 21:08:31 +00:00
|
|
|
for _, fid := range fids {
|
|
|
|
DebugDetailf("Forum #%+v\n", fid)
|
|
|
|
var forumPerms = make(map[int]*ForumPerms)
|
|
|
|
var ok bool
|
|
|
|
if fid%2 == 0 {
|
|
|
|
fps.evenLock.RLock()
|
|
|
|
forumPerms, ok = fps.evenForums[fid]
|
|
|
|
fps.evenLock.RUnlock()
|
|
|
|
} else {
|
|
|
|
fps.oddLock.RLock()
|
|
|
|
forumPerms, ok = fps.oddForums[fid]
|
|
|
|
fps.oddLock.RUnlock()
|
|
|
|
}
|
2017-11-05 01:04:57 +00:00
|
|
|
|
2018-04-23 21:08:31 +00:00
|
|
|
var forumPerm *ForumPerms
|
|
|
|
if !ok {
|
2018-04-24 03:47:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
forumPerm, ok = forumPerms[group.ID]
|
|
|
|
if !ok {
|
|
|
|
continue
|
2018-04-23 21:08:31 +00:00
|
|
|
}
|
2017-11-05 01:04:57 +00:00
|
|
|
|
2018-04-23 21:08:31 +00:00
|
|
|
if forumPerm.Overrides {
|
|
|
|
if forumPerm.ViewTopic {
|
|
|
|
group.CanSee = append(group.CanSee, fid)
|
|
|
|
}
|
|
|
|
} else if group.Perms.ViewTopic {
|
2017-11-05 01:04:57 +00:00
|
|
|
group.CanSee = append(group.CanSee, fid)
|
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
DebugDetail("group.ID: ", group.ID)
|
|
|
|
DebugDetailf("forumPerm: %+v\n", forumPerm)
|
|
|
|
DebugDetail("group.CanSee: ", group.CanSee)
|
|
|
|
}
|
|
|
|
DebugDetailf("group.CanSee (length %d): %+v \n", len(group.CanSee), group.CanSee)
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
return nil
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 03:47:39 +00:00
|
|
|
// ! Throughput on this might be bad due to the excessive locking
|
|
|
|
func (fps *MemoryForumPermsStore) GetAllMap() (bigMap map[int]map[int]*ForumPerms) {
|
|
|
|
bigMap = make(map[int]map[int]*ForumPerms)
|
|
|
|
fps.evenLock.RLock()
|
|
|
|
for fid, subMap := range fps.evenForums {
|
|
|
|
bigMap[fid] = subMap
|
|
|
|
}
|
|
|
|
fps.evenLock.RUnlock()
|
|
|
|
fps.oddLock.RLock()
|
|
|
|
for fid, subMap := range fps.oddForums {
|
|
|
|
bigMap[fid] = subMap
|
|
|
|
}
|
|
|
|
fps.oddLock.RUnlock()
|
|
|
|
return bigMap
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-04-23 21:08:31 +00:00
|
|
|
var fmap map[int]*ForumPerms
|
|
|
|
var ok bool
|
|
|
|
if fid%2 == 0 {
|
|
|
|
fps.evenLock.RLock()
|
|
|
|
fmap, ok = fps.evenForums[fid]
|
|
|
|
fps.evenLock.RUnlock()
|
|
|
|
} else {
|
|
|
|
fps.oddLock.RLock()
|
|
|
|
fmap, ok = fps.oddForums[fid]
|
|
|
|
fps.oddLock.RUnlock()
|
|
|
|
}
|
|
|
|
if !ok {
|
2017-11-02 02:52:21 +00:00
|
|
|
return fperms, ErrNoRows
|
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
|
|
|
|
fperms, ok = fmap[gid]
|
|
|
|
if !ok {
|
|
|
|
return fperms, ErrNoRows
|
|
|
|
}
|
|
|
|
return fperms, nil
|
2017-11-02 02:52:21 +00:00
|
|
|
}
|
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) {
|
2018-04-23 21:08:31 +00:00
|
|
|
fPermsPtr, err := fps.Get(fid, gid)
|
2018-01-10 03:32:48 +00:00
|
|
|
if err != nil {
|
2018-04-23 21:08:31 +00:00
|
|
|
return fperms, err
|
2018-01-10 03:32:48 +00:00
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
return *fPermsPtr, nil
|
2018-01-10 03:32:48 +00:00
|
|
|
}
|