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 (
|
2022-02-21 03:53:13 +00:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"sync"
|
2017-11-06 04:02:35 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
qgen "git.tuxpa.in/a/gosora/query_gen"
|
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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
Init() error
|
|
|
|
GetAllMap() (bigMap map[int]map[int]*ForumPerms)
|
|
|
|
Get(fid, gid int) (fp *ForumPerms, err error)
|
|
|
|
GetCopy(fid, gid int) (fp ForumPerms, err error)
|
|
|
|
ReloadAll() error
|
|
|
|
Reload(id 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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
getByForum *sql.Stmt
|
|
|
|
getByForumGroup *sql.Stmt
|
2017-11-13 05:22:37 +00:00
|
|
|
|
2022-02-21 03:32:53 +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) {
|
2022-02-21 03:32:53 +00:00
|
|
|
acc := qgen.NewAcc()
|
|
|
|
fp := "forums_permissions"
|
|
|
|
return &MemoryForumPermsStore{
|
|
|
|
getByForum: acc.Select(fp).Columns("gid,permissions").Where("fid=?").Orderby("gid ASC").Prepare(),
|
|
|
|
getByForumGroup: acc.Select(fp).Columns("permissions").Where("fid=? AND gid=?").Prepare(),
|
|
|
|
|
|
|
|
evenForums: make(map[int]map[int]*ForumPerms),
|
|
|
|
oddForums: make(map[int]map[int]*ForumPerms),
|
|
|
|
}, acc.FirstError()
|
2017-11-02 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:02:45 +00:00
|
|
|
func (s *MemoryForumPermsStore) Init() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
DebugLog("Initialising the forum perms store")
|
|
|
|
return s.ReloadAll()
|
2018-04-23 21:08:31 +00:00
|
|
|
}
|
2017-11-05 01:04:57 +00:00
|
|
|
|
2021-03-22 09:58:13 +00:00
|
|
|
// TODO: Optimise this?
|
2019-06-18 10:02:45 +00:00
|
|
|
func (s *MemoryForumPermsStore) ReloadAll() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
DebugLog("Reloading the forum perms")
|
|
|
|
fids, e := Forums.GetAllIDs()
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
for _, fid := range fids {
|
|
|
|
if e := s.reload(fid); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e := s.recalcCanSeeAll(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
TopicListThaw.Thaw()
|
|
|
|
return nil
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 14:23:11 +00:00
|
|
|
func (s *MemoryForumPermsStore) parseForumPerm(perms []byte) (pperms *ForumPerms, e error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
DebugDetail("perms: ", string(perms))
|
|
|
|
pperms = BlankForumPerms()
|
|
|
|
e = json.Unmarshal(perms, &pperms)
|
|
|
|
pperms.ExtData = make(map[string]bool)
|
|
|
|
pperms.Overrides = true
|
|
|
|
return pperms, e
|
2017-11-13 05:22:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:02:45 +00:00
|
|
|
func (s *MemoryForumPermsStore) Reload(fid int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
e := s.reload(fid)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if e = s.recalcCanSeeAll(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
TopicListThaw.Thaw()
|
|
|
|
return nil
|
2021-03-22 09:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Need a more thread-safe way of doing this. Possibly with sync.Map?
|
|
|
|
func (s *MemoryForumPermsStore) reload(fid int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
DebugLogf("Reloading the forum permissions for forum #%d", fid)
|
|
|
|
rows, err := s.getByForum.Query(fid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
forumPerms := make(map[int]*ForumPerms)
|
|
|
|
for rows.Next() {
|
|
|
|
var gid int
|
|
|
|
var perms []byte
|
|
|
|
err := rows.Scan(&gid, &perms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugLog("gid:", gid)
|
|
|
|
DebugLogf("perms: %+v\n", perms)
|
|
|
|
pperms, err := s.parseForumPerm(perms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
DebugLogf("pperms: %+v\n", pperms)
|
|
|
|
forumPerms[gid] = pperms
|
|
|
|
}
|
|
|
|
DebugLogf("forumPerms: %+v\n", forumPerms)
|
|
|
|
|
|
|
|
if fid%2 == 0 {
|
|
|
|
s.evenLock.Lock()
|
|
|
|
s.evenForums[fid] = forumPerms
|
|
|
|
s.evenLock.Unlock()
|
|
|
|
} else {
|
|
|
|
s.oddLock.Lock()
|
|
|
|
s.oddForums[fid] = forumPerms
|
|
|
|
s.oddLock.Unlock()
|
|
|
|
}
|
|
|
|
return nil
|
2021-03-22 09:58:13 +00:00
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
|
2021-03-22 09:58:13 +00:00
|
|
|
func (s *MemoryForumPermsStore) recalcCanSeeAll() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
groups, err := Groups.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fids, err := Forums.GetAllIDs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gc, ok := Groups.(GroupCache)
|
|
|
|
if !ok {
|
|
|
|
TopicListThaw.Thaw()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A separate loop to avoid contending on the odd-even locks as much
|
|
|
|
fForumPerms := make(map[int]map[int]*ForumPerms)
|
|
|
|
for _, fid := range fids {
|
|
|
|
var forumPerms map[int]*ForumPerms
|
|
|
|
var ok bool
|
|
|
|
if fid%2 == 0 {
|
|
|
|
s.evenLock.RLock()
|
|
|
|
forumPerms, ok = s.evenForums[fid]
|
|
|
|
s.evenLock.RUnlock()
|
|
|
|
} else {
|
|
|
|
s.oddLock.RLock()
|
|
|
|
forumPerms, ok = s.oddForums[fid]
|
|
|
|
s.oddLock.RUnlock()
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
fForumPerms[fid] = forumPerms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Can we recalculate CanSee without calculating every other forum?
|
|
|
|
for _, g := range groups {
|
|
|
|
DebugLogf("Updating the forum permissions for Group #%d", g.ID)
|
|
|
|
canSee := []int{}
|
|
|
|
for _, fid := range fids {
|
|
|
|
DebugDetailf("Forum #%+v\n", fid)
|
|
|
|
forumPerms, ok := fForumPerms[fid]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fp, ok := forumPerms[g.ID]
|
|
|
|
if !ok {
|
|
|
|
if g.Perms.ViewTopic {
|
|
|
|
canSee = append(canSee, fid)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if fp.Overrides {
|
|
|
|
if fp.ViewTopic {
|
|
|
|
canSee = append(canSee, fid)
|
|
|
|
}
|
|
|
|
} else if g.Perms.ViewTopic {
|
|
|
|
canSee = append(canSee, fid)
|
|
|
|
}
|
|
|
|
//DebugDetail("g.ID: ", g.ID)
|
|
|
|
DebugDetailf("forumPerm: %+v\n", fp)
|
|
|
|
DebugDetail("canSee: ", canSee)
|
|
|
|
}
|
|
|
|
DebugDetailf("canSee (length %d): %+v \n", len(canSee), canSee)
|
|
|
|
gc.SetCanSee(g.ID, canSee)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-06-18 04:13:47 +00:00
|
|
|
func (s *MemoryForumPermsStore) GetAllMap() (bigMap map[int]map[int]*ForumPerms) {
|
2022-02-21 03:32:53 +00:00
|
|
|
bigMap = make(map[int]map[int]*ForumPerms)
|
|
|
|
s.evenLock.RLock()
|
|
|
|
for fid, subMap := range s.evenForums {
|
|
|
|
bigMap[fid] = subMap
|
|
|
|
}
|
|
|
|
s.evenLock.RUnlock()
|
|
|
|
s.oddLock.RLock()
|
|
|
|
for fid, subMap := range s.oddForums {
|
|
|
|
bigMap[fid] = subMap
|
|
|
|
}
|
|
|
|
s.oddLock.RUnlock()
|
|
|
|
return bigMap
|
2018-04-24 03:47:39 +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
|
2021-03-22 09:58:13 +00:00
|
|
|
// TODO: Return BlankForumPerms() when the forum permission set doesn't exist?
|
2020-02-18 23:04:14 +00:00
|
|
|
func (s *MemoryForumPermsStore) Get(fid, gid int) (fp *ForumPerms, err error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
var fmap map[int]*ForumPerms
|
|
|
|
var ok bool
|
|
|
|
if fid%2 == 0 {
|
|
|
|
s.evenLock.RLock()
|
|
|
|
fmap, ok = s.evenForums[fid]
|
|
|
|
s.evenLock.RUnlock()
|
|
|
|
} else {
|
|
|
|
s.oddLock.RLock()
|
|
|
|
fmap, ok = s.oddForums[fid]
|
|
|
|
s.oddLock.RUnlock()
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fp, ErrNoRows
|
|
|
|
}
|
|
|
|
|
|
|
|
fp, ok = fmap[gid]
|
|
|
|
if !ok {
|
|
|
|
return fp, ErrNoRows
|
|
|
|
}
|
|
|
|
return fp, 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
|
2021-04-07 14:23:11 +00:00
|
|
|
func (s *MemoryForumPermsStore) GetCopy(fid, gid int) (fp ForumPerms, e error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
fPermsPtr, e := s.Get(fid, gid)
|
|
|
|
if e != nil {
|
|
|
|
return fp, e
|
|
|
|
}
|
|
|
|
return *fPermsPtr, nil
|
2018-01-10 03:32:48 +00:00
|
|
|
}
|