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
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/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 {
|
|
|
|
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) {
|
2018-08-04 11:46:36 +00:00
|
|
|
acc := qgen.NewAcc()
|
2019-10-27 23:13:24 +00:00
|
|
|
fp := "forums_permissions"
|
2017-11-06 04:02:35 +00:00
|
|
|
return &MemoryForumPermsStore{
|
2019-12-07 06:27:01 +00:00
|
|
|
getByForum: acc.Select(fp).Columns("gid,permissions").Where("fid = ?").Orderby("gid ASC").Prepare(),
|
2019-10-27 23:13:24 +00:00
|
|
|
getByForumGroup: acc.Select(fp).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
|
|
|
}
|
|
|
|
|
2019-06-18 10:02:45 +00:00
|
|
|
func (s *MemoryForumPermsStore) Init() error {
|
2018-04-23 21:08:31 +00:00
|
|
|
DebugLog("Initialising the forum perms store")
|
2019-06-18 10:02:45 +00:00
|
|
|
return s.ReloadAll()
|
2018-04-23 21:08:31 +00:00
|
|
|
}
|
2017-11-05 01:04:57 +00:00
|
|
|
|
2019-06-18 10:02:45 +00:00
|
|
|
func (s *MemoryForumPermsStore) ReloadAll() error {
|
2018-04-23 21:08:31 +00:00
|
|
|
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 {
|
2019-06-18 10:02:45 +00:00
|
|
|
err := s.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
|
|
|
}
|
|
|
|
|
2019-06-18 10:02:45 +00:00
|
|
|
func (s *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?
|
2019-06-18 10:02:45 +00:00
|
|
|
func (s *MemoryForumPermsStore) Reload(fid int) error {
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugLogf("Reloading the forum permissions for forum #%d", fid)
|
2019-06-18 10:02:45 +00:00
|
|
|
rows, err := s.getByForum.Query(fid)
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2019-08-28 06:47:54 +00:00
|
|
|
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
|
|
|
|
2018-11-29 07:27:17 +00:00
|
|
|
DebugLog("gid: ", gid)
|
|
|
|
DebugLogf("perms: %+v\n", perms)
|
2019-06-18 10:02:45 +00:00
|
|
|
pperms, err := s.parseForumPerm(perms)
|
2017-11-05 01:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-29 07:27:17 +00:00
|
|
|
DebugLogf("pperms: %+v\n", pperms)
|
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)
|
2019-06-19 03:16:03 +00:00
|
|
|
|
2018-04-23 21:08:31 +00:00
|
|
|
if fid%2 == 0 {
|
2019-06-18 10:02:45 +00:00
|
|
|
s.evenLock.Lock()
|
|
|
|
s.evenForums[fid] = forumPerms
|
|
|
|
s.evenLock.Unlock()
|
2018-04-23 21:08:31 +00:00
|
|
|
} else {
|
2019-06-18 10:02:45 +00:00
|
|
|
s.oddLock.Lock()
|
|
|
|
s.oddForums[fid] = forumPerms
|
|
|
|
s.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
|
|
|
|
}
|
|
|
|
|
2019-06-19 03:16:03 +00:00
|
|
|
gcache, ok := Groups.(GroupCache)
|
|
|
|
if !ok {
|
|
|
|
TopicListThaw.Thaw()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-05 01:04:57 +00:00
|
|
|
for _, group := range groups {
|
2018-02-19 04:26:01 +00:00
|
|
|
DebugLogf("Updating the forum permissions for Group #%d", group.ID)
|
2019-06-19 03:16:03 +00:00
|
|
|
canSee := []int{}
|
2018-04-23 21:08:31 +00:00
|
|
|
for _, fid := range fids {
|
|
|
|
DebugDetailf("Forum #%+v\n", fid)
|
2018-06-17 07:28:18 +00:00
|
|
|
var forumPerms map[int]*ForumPerms
|
2018-04-23 21:08:31 +00:00
|
|
|
var ok bool
|
|
|
|
if fid%2 == 0 {
|
2019-06-18 10:02:45 +00:00
|
|
|
s.evenLock.RLock()
|
|
|
|
forumPerms, ok = s.evenForums[fid]
|
|
|
|
s.evenLock.RUnlock()
|
2018-04-23 21:08:31 +00:00
|
|
|
} else {
|
2019-06-18 10:02:45 +00:00
|
|
|
s.oddLock.RLock()
|
|
|
|
forumPerms, ok = s.oddForums[fid]
|
|
|
|
s.oddLock.RUnlock()
|
2018-04-23 21:08:31 +00:00
|
|
|
}
|
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 {
|
2018-11-29 07:27:17 +00:00
|
|
|
if group.Perms.ViewTopic {
|
2019-06-19 03:16:03 +00:00
|
|
|
canSee = append(canSee, fid)
|
2018-11-29 07:27:17 +00:00
|
|
|
}
|
2018-04-24 03:47:39 +00:00
|
|
|
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 {
|
2019-06-19 03:16:03 +00:00
|
|
|
canSee = append(canSee, fid)
|
2018-04-23 21:08:31 +00:00
|
|
|
}
|
|
|
|
} else if group.Perms.ViewTopic {
|
2019-06-19 03:16:03 +00:00
|
|
|
canSee = append(canSee, fid)
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
2018-04-23 21:08:31 +00:00
|
|
|
DebugDetail("group.ID: ", group.ID)
|
|
|
|
DebugDetailf("forumPerm: %+v\n", forumPerm)
|
2019-06-19 03:16:03 +00:00
|
|
|
DebugDetail("canSee: ", canSee)
|
2018-04-23 21:08:31 +00:00
|
|
|
}
|
2019-06-19 03:16:03 +00:00
|
|
|
DebugDetailf("canSee (length %d): %+v \n", len(canSee), canSee)
|
|
|
|
gcache.SetCanSee(group.ID, canSee)
|
2017-11-05 01:04:57 +00:00
|
|
|
}
|
2018-11-19 23:06:15 +00:00
|
|
|
TopicListThaw.Thaw()
|
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
|
2019-06-18 04:13:47 +00:00
|
|
|
func (s *MemoryForumPermsStore) GetAllMap() (bigMap map[int]map[int]*ForumPerms) {
|
2018-04-24 03:47:39 +00:00
|
|
|
bigMap = make(map[int]map[int]*ForumPerms)
|
2019-06-18 04:13:47 +00:00
|
|
|
s.evenLock.RLock()
|
|
|
|
for fid, subMap := range s.evenForums {
|
2018-04-24 03:47:39 +00:00
|
|
|
bigMap[fid] = subMap
|
|
|
|
}
|
2019-06-18 04:13:47 +00:00
|
|
|
s.evenLock.RUnlock()
|
|
|
|
s.oddLock.RLock()
|
|
|
|
for fid, subMap := range s.oddForums {
|
2018-04-24 03:47:39 +00:00
|
|
|
bigMap[fid] = subMap
|
|
|
|
}
|
2019-06-18 04:13:47 +00:00
|
|
|
s.oddLock.RUnlock()
|
2018-04-24 03:47:39 +00:00
|
|
|
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
|
2019-06-18 04:13:47 +00:00
|
|
|
func (s *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 {
|
2019-06-18 04:13:47 +00:00
|
|
|
s.evenLock.RLock()
|
|
|
|
fmap, ok = s.evenForums[fid]
|
|
|
|
s.evenLock.RUnlock()
|
2018-04-23 21:08:31 +00:00
|
|
|
} else {
|
2019-06-18 04:13:47 +00:00
|
|
|
s.oddLock.RLock()
|
|
|
|
fmap, ok = s.oddForums[fid]
|
|
|
|
s.oddLock.RUnlock()
|
2018-04-23 21:08:31 +00:00
|
|
|
}
|
|
|
|
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
|
2019-06-18 04:13:47 +00:00
|
|
|
func (s *MemoryForumPermsStore) GetCopy(fid int, gid int) (fperms ForumPerms, err error) {
|
|
|
|
fPermsPtr, err := s.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
|
|
|
}
|