Skip to content

Commit

Permalink
fix(account): fix account repo Filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Mar 8, 2024
1 parent c576140 commit 34752fb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
4 changes: 4 additions & 0 deletions account/accountinfrastructure/accountmemory/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func NewWorkspaceWith(workspaces ...*workspace.Workspace) *Workspace {
return r
}

func (r *Workspace) Filtered(f accountrepo.WorkspaceFilter) accountrepo.Workspace {
return r // TODO
}

func (r *Workspace) FindByUser(_ context.Context, i accountdomain.UserID) (workspace.List, error) {
if r.err != nil {
return nil, r.err
Expand Down
43 changes: 43 additions & 0 deletions account/accountinfrastructure/accountmongo/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/reearth/reearthx/account/accountinfrastructure/accountmongo/mongodoc"
"github.com/reearth/reearthx/account/accountusecase/accountrepo"
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/rerror"
"go.mongodb.org/mongo-driver/bson"
)

Expand All @@ -19,6 +20,7 @@ var (

type Workspace struct {
client *mongox.Collection
f accountrepo.WorkspaceFilter
}

func NewWorkspace(client *mongox.Client) accountrepo.Workspace {
Expand All @@ -33,6 +35,13 @@ func (r *Workspace) Init() error {
return createIndexes(context.Background(), r.client, nil, workspaceUniqueIndexes)
}

func (r *Workspace) Filtered(f accountrepo.WorkspaceFilter) accountrepo.Workspace {
return &Workspace{
client: r.client,
f: r.f.Merge(f),
}
}

func (r *Workspace) FindByUser(ctx context.Context, id user.ID) (workspace.List, error) {
return r.find(ctx, bson.M{
"members." + strings.Replace(id.String(), ".", "", -1): bson.M{
Expand All @@ -53,6 +62,13 @@ func (r *Workspace) FindByIDs(ctx context.Context, ids accountdomain.WorkspaceID
if len(ids) == 0 {
return nil, nil
}

for _, id := range ids {
if !r.f.CanRead(id) {
return nil, rerror.ErrNotFound
}
}

res, err := r.find(ctx, bson.M{
"id": bson.M{"$in": ids.Strings()},
})
Expand All @@ -63,10 +79,18 @@ func (r *Workspace) FindByIDs(ctx context.Context, ids accountdomain.WorkspaceID
}

func (r *Workspace) FindByID(ctx context.Context, id accountdomain.WorkspaceID) (*workspace.Workspace, error) {
if !r.f.CanRead(id) {
return nil, rerror.ErrNotFound
}

return r.findOne(ctx, bson.M{"id": id.String()})
}

func (r *Workspace) Save(ctx context.Context, workspace *workspace.Workspace) error {
if !r.f.CanWrite(workspace.ID()) {
return accountrepo.ErrOperationDenied
}

doc, id := mongodoc.NewWorkspace(workspace)
return r.client.SaveOne(ctx, id, doc)
}
Expand All @@ -75,6 +99,13 @@ func (r *Workspace) SaveAll(ctx context.Context, workspaces workspace.List) erro
if len(workspaces) == 0 {
return nil
}

for _, w := range workspaces {
if !r.f.CanWrite(w.ID()) {
return accountrepo.ErrOperationDenied
}
}

docs, ids := mongodoc.NewWorkspaces(workspaces)
docs2 := make([]any, 0, len(workspaces))
for _, d := range docs {
Expand All @@ -84,20 +115,31 @@ func (r *Workspace) SaveAll(ctx context.Context, workspaces workspace.List) erro
}

func (r *Workspace) Remove(ctx context.Context, id accountdomain.WorkspaceID) error {
if !r.f.CanWrite(id) {
return accountrepo.ErrOperationDenied
}
return r.client.RemoveOne(ctx, bson.M{"id": id.String()})
}

func (r *Workspace) RemoveAll(ctx context.Context, ids accountdomain.WorkspaceIDList) error {
if len(ids) == 0 {
return nil
}

for _, id := range ids {
if !r.f.CanWrite(id) {
return accountrepo.ErrOperationDenied
}
}

return r.client.RemoveAll(ctx, bson.M{
"id": bson.M{"$in": ids.Strings()},
})
}

func (r *Workspace) find(ctx context.Context, filter any) (workspace.List, error) {
c := mongodoc.NewWorkspaceConsumer()
filter = r.f.Filter(filter)
if err := r.client.Find(ctx, filter, c); err != nil {
return nil, err
}
Expand All @@ -106,6 +148,7 @@ func (r *Workspace) find(ctx context.Context, filter any) (workspace.List, error

func (r *Workspace) findOne(ctx context.Context, filter any) (*workspace.Workspace, error) {
c := mongodoc.NewWorkspaceConsumer()
filter = r.f.Filter(filter)
if err := r.client.FindOne(ctx, filter, c); err != nil {
return nil, err
}
Expand Down
17 changes: 16 additions & 1 deletion account/accountusecase/accountrepo/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/reearth/reearthx/i18n"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
"go.mongodb.org/mongo-driver/bson"
)

type Container struct {
Expand All @@ -24,8 +25,9 @@ func (c *Container) Filtered(workspace WorkspaceFilter) *Container {
return c
}
return &Container{
Workspace: c.Workspace,
Workspace: c.Workspace.Filtered(workspace),
User: c.User,
Users: c.Users,
}
}

Expand Down Expand Up @@ -77,3 +79,16 @@ func (f WorkspaceFilter) CanRead(id accountdomain.WorkspaceID) bool {
func (f WorkspaceFilter) CanWrite(id accountdomain.WorkspaceID) bool {
return f.Writable == nil || f.Writable.Has(id)
}

func (f WorkspaceFilter) Filter(q any) any {
if f.Readable == nil {
return q
}

return bson.M{
"$and": bson.A{
bson.M{"id": bson.M{"$in": f.Readable.Strings()}},
q,
},
}
}
1 change: 1 addition & 0 deletions account/accountusecase/accountrepo/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

type Workspace interface {
Filtered(WorkspaceFilter) Workspace
FindByID(context.Context, workspace.ID) (*workspace.Workspace, error)
FindByIDs(context.Context, workspace.IDList) (workspace.List, error)
FindByUser(context.Context, user.ID) (workspace.List, error)
Expand Down

0 comments on commit 34752fb

Please sign in to comment.