Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/api/author/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package author

import (
"encoding/json"

error2 "github.com/ether/etherpad-go/lib/api/error"
"github.com/ether/etherpad-go/lib/author"
"github.com/ether/etherpad-go/lib/db"
"github.com/gofiber/fiber/v2"
)

Expand All @@ -15,8 +17,8 @@ type CreateDtoResponse struct {
authorId string
}

func Init(c *fiber.App) {
var authorManager author.Manager = author.NewManager()
func Init(c *fiber.App, db db.DataStore) {
var authorManager = author.NewManager(db)

c.Post("/author", func(c *fiber.Ctx) error {
var dto CreateDto
Expand Down
8 changes: 5 additions & 3 deletions lib/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"github.com/ether/etherpad-go/lib/api/groups"
"github.com/ether/etherpad-go/lib/api/pad"
"github.com/ether/etherpad-go/lib/api/static"
"github.com/ether/etherpad-go/lib/db"
"github.com/ether/etherpad-go/lib/settings"
"github.com/ether/etherpad-go/lib/ws"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
)

func InitAPI(c *fiber.App, uiAssets embed.FS, settings settings.Settings, cookieStore *session.Store) {
author.Init(c)
pad.Init(c)
func InitAPI(c *fiber.App, uiAssets embed.FS, settings settings.Settings, cookieStore *session.Store, store db.DataStore, handler *ws.PadMessageHandler) {
author.Init(c, store)
pad.Init(c, store, handler)
groups.Init(c)
static.Init(c, uiAssets, settings, cookieStore)
}
16 changes: 9 additions & 7 deletions lib/api/pad/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package pad

import (
"errors"

apiError "github.com/ether/etherpad-go/lib/api/error"
utils2 "github.com/ether/etherpad-go/lib/api/utils"
"github.com/ether/etherpad-go/lib/apool"
"github.com/ether/etherpad-go/lib/db"
"github.com/ether/etherpad-go/lib/utils"
"github.com/ether/etherpad-go/lib/ws"
"github.com/gofiber/fiber/v2"
)

func getText(padId string, rev *string) (*string, error) {
func getText(padId string, rev *string, store db.DataStore) (*string, error) {
var revNum *int = nil
if rev != nil {
revPoint, err := utils.CheckValidRev(*rev)
Expand All @@ -20,7 +22,7 @@ func getText(padId string, rev *string) (*string, error) {
}
}

pad, err := utils2.GetPadSafe(padId, true, nil, nil)
pad, err := utils2.GetPadSafe(padId, true, nil, nil, store)

if err != nil {
return nil, err
Expand All @@ -44,13 +46,13 @@ type AttributePoolResponse struct {
Pool apool.APool `json:"pool"`
}

func Init(c *fiber.App) {
func Init(c *fiber.App, store db.DataStore, handler *ws.PadMessageHandler) {
c.Get("/pads/:padId/text", func(c *fiber.Ctx) error {
return c.SendStatus(200)
})
c.Get("/pads/:padId/attributePool", func(ctx *fiber.Ctx) error {
var padIdToFind = ctx.Params("padId")
var padFound, err = utils2.GetPadSafe(padIdToFind, true, nil, nil)
var padFound, err = utils2.GetPadSafe(padIdToFind, true, nil, nil, store)
if err != nil {
return ctx.Status(404).JSON(apiError.Error{
Message: "Pad not found",
Expand All @@ -74,7 +76,7 @@ func Init(c *fiber.App) {
})
}

var pad, errorForPad2 = utils2.GetPadSafe(padId, true, nil, nil)
var pad, errorForPad2 = utils2.GetPadSafe(padId, true, nil, nil, store)
if errorForPad2 != nil {
return ctx.Status(404).JSON(apiError.Error{
Message: "Pad not found",
Expand Down Expand Up @@ -118,7 +120,7 @@ func Init(c *fiber.App) {
})
}

var pad, errPadSafe = utils2.GetPadSafe(padId, true, nil, nil)
var pad, errPadSafe = utils2.GetPadSafe(padId, true, nil, nil, store)
if errPadSafe != nil {
return ctx.Status(404).JSON(apiError.Error{
Message: "Pad not found",
Expand All @@ -132,7 +134,7 @@ func Init(c *fiber.App) {
Error: 500,
})
}
ws.UpdatePadClients(pad)
handler.UpdatePadClients(pad)
return ctx.SendStatus(200)
})
}
6 changes: 4 additions & 2 deletions lib/api/utils/pad.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package utils

import (
"errors"

"github.com/ether/etherpad-go/lib/db"
pad2 "github.com/ether/etherpad-go/lib/models/pad"
"github.com/ether/etherpad-go/lib/pad"
)

func GetPadSafe(padID string, shouldExist bool, text *string, authorId *string) (*pad2.Pad, error) {
var padManager = pad.NewManager()
func GetPadSafe(padID string, shouldExist bool, text *string, authorId *string, db db.DataStore) (*pad2.Pad, error) {
var padManager = pad.NewManager(db)

if !padManager.IsValidPadId(padID) {
return nil, errors.New("padID is not valid")
Expand Down
6 changes: 3 additions & 3 deletions lib/author/authorManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type Manager struct {
Db db.DataStore
}

func NewManager() Manager {
return Manager{
Db: utils.GetDB(),
func NewManager(db db.DataStore) *Manager {
return &Manager{
Db: db,
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/db/DirtyDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,8 @@ func (d SQLiteDB) GetPadMetaData(padId string, revNum int) (*db.PadMetaData, err
return &padMetaData, nil
}

// NewDirtyDB This function creates a new SQLiteDB and returns a pointer to it.
func NewDirtyDB(path string) (*SQLiteDB, error) {
// NewSQLiteDB This function creates a new SQLiteDB and returns a pointer to it.
func NewSQLiteDB(path string) (*SQLiteDB, error) {
sqlDb, err := sql.Open("sqlite", path)
if err != nil {
panic(err)
Expand Down
16 changes: 6 additions & 10 deletions lib/models/pad/Pad.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ import (
"github.com/ether/etherpad-go/lib/utils"
)

var authorManager author.Manager

func init() {
authorManager = author.NewManager()
}

type Pad struct {
db db.DataStore
authorManager *author.Manager
Id string
ChatHead int
Head int
Expand All @@ -35,10 +30,10 @@ type Pad struct {
AText apool.AText
}

func NewPad(id string) Pad {
func NewPad(id string, db db.DataStore) Pad {
p := new(Pad)
p.Id = id
p.db = utils.GetDB()
p.db = db
p.Pool = apool.NewAPool()
p.Head = -1
p.ChatHead = -1
Expand Down Expand Up @@ -155,7 +150,8 @@ func CleanText(context string) *string {
return &context
}

func (p *Pad) Init(text *string, author *string) error {
func (p *Pad) Init(text *string, author *string, authorManager *author.Manager) error {
p.authorManager = authorManager
if author == nil {
author = new(string)
*author = ""
Expand Down Expand Up @@ -329,7 +325,7 @@ func (p *Pad) AppendRevision(cs string, authorId *string) int {
if authorId != nil {
var clonedAuthorId = *authorId
if clonedAuthorId != "" {
authorManager.AddPad(*authorId, p.Id)
p.authorManager.AddPad(*authorId, p.Id)
}
}

Expand Down
16 changes: 9 additions & 7 deletions lib/pad/SecurityManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@ package pad

import (
"errors"
"strings"

"github.com/ether/etherpad-go/lib/author"
"github.com/ether/etherpad-go/lib/db"
"github.com/ether/etherpad-go/lib/models/webaccess"
"github.com/ether/etherpad-go/lib/settings"
"github.com/ether/etherpad-go/lib/utils"
"strings"
)

type SecurityManager struct {
ReadOnlyManager *ReadOnlyManager
PadManager Manager
AuthorManager author.Manager
AuthorManager *author.Manager
SessionManager *SessionManager
}

func NewSecurityManager() SecurityManager {
func NewSecurityManager(db db.DataStore) SecurityManager {
return SecurityManager{
ReadOnlyManager: NewReadOnlyManager(),
PadManager: NewManager(),
AuthorManager: author.NewManager(),
SessionManager: NewSessionManager(),
ReadOnlyManager: NewReadOnlyManager(db),
PadManager: NewManager(db),
AuthorManager: author.NewManager(db),
SessionManager: NewSessionManager(db),
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/pad/SessionManager.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package pad

import (
"github.com/ether/etherpad-go/lib/db"
"github.com/ether/etherpad-go/lib/utils"
"regexp"
"strings"

"github.com/ether/etherpad-go/lib/db"
)

type SessionManager struct {
db db.DataStore
}

func NewSessionManager() *SessionManager {
func NewSessionManager(db db.DataStore) *SessionManager {
return &SessionManager{
db: utils.GetDB(),
db,
}
}

Expand Down
32 changes: 17 additions & 15 deletions lib/pad/padManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"regexp"
"unicode/utf8"

"github.com/ether/etherpad-go/lib/author"
"github.com/ether/etherpad-go/lib/db"
"github.com/ether/etherpad-go/lib/models/pad"
"github.com/ether/etherpad-go/lib/utils"
)

var globalPadCache *GlobalPadCache
Expand All @@ -20,12 +20,12 @@ type List struct {
db db.DataStore
}

func NewList() List {
func NewList(db db.DataStore) List {
return List{
_cachedList: make([]string, 0),
_list: make(map[string]interface{}),
_loaded: false,
db: utils.GetDB(),
db: db,
}
}

Expand Down Expand Up @@ -58,11 +58,13 @@ func (l *List) GetPads() []string {
return l._cachedList
}

var padRegex *regexp.Regexp

func init() {
globalPadCache = &GlobalPadCache{
padCache: make(map[string]*pad.Pad),
}
padList = NewList()
padRegex, _ = regexp.Compile("^(g.[a-zA-Z0-9]{16}$)?[^$]{1,50}$")
}

type GlobalPadCache struct {
Expand All @@ -81,19 +83,19 @@ func (g *GlobalPadCache) DeletePad(padID string) {
delete(g.padCache, padID)
}

var padRegex *regexp.Regexp

func init() {
padRegex, _ = regexp.Compile("^(g.[a-zA-Z0-9]{16}$)?[^$]{1,50}$")
}

type Manager struct {
store db.DataStore
store db.DataStore
author *author.Manager
padList List
}

func NewManager() Manager {
func NewManager(db db.DataStore) Manager {
return Manager{
store: utils.GetDB(),
store: db,
author: &author.Manager{
Db: db,
},
padList: NewList(db),
}
}

Expand Down Expand Up @@ -140,11 +142,11 @@ func (m *Manager) GetPad(padID string, text *string, authorId *string) (*pad.Pad
}

// try to load pad
var newPad = pad.NewPad(padID)
var newPad = pad.NewPad(padID, m.store)

// initialize the pad

newPad.Init(text, authorId)
newPad.Init(text, authorId, m.author)
globalPadCache.SetPad(padID, &newPad)

return &newPad, nil
Expand Down
4 changes: 2 additions & 2 deletions lib/pad/readOnlyManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type IdRequest struct {
ReadOnly bool
}

func NewReadOnlyManager() *ReadOnlyManager {
func NewReadOnlyManager(db db.DataStore) *ReadOnlyManager {
return &ReadOnlyManager{
Store: utils.GetDB(),
Store: db,
}
}

Expand Down
6 changes: 2 additions & 4 deletions lib/pad/webaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import (
"go.uber.org/zap"
)

var readOnlyManager = NewReadOnlyManager()

func UserCanModify(padId *string, req *webaccess.SocketClientRequest) bool {
func UserCanModify(padId *string, req *webaccess.SocketClientRequest, readOnlyManager ReadOnlyManager) bool {
if readOnlyManager.isReadOnlyID(padId) {
return false
}
Expand All @@ -43,7 +41,7 @@ func UserCanModify(padId *string, req *webaccess.SocketClientRequest) bool {
return level != nil && *level != "readOnly"
}

func CheckAccess(ctx *fiber.Ctx, logger *zap.SugaredLogger, retrievedSettings *settings.Settings) error {
func CheckAccess(ctx *fiber.Ctx, logger *zap.SugaredLogger, retrievedSettings *settings.Settings, readOnlyManager *ReadOnlyManager) error {
var requireAdmin = strings.HasPrefix(strings.ToLower(ctx.Path()), "/admin-auth")
//FIXME this needs to be set
// ///////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Loading
Loading