Skip to content

Commit

Permalink
feat(server): Add new policy enforcement for visualizer (#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaforce authored Sep 2, 2024
1 parent ff375f4 commit 32b3be4
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 13 deletions.
13 changes: 13 additions & 0 deletions server/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
help:
@echo "Usage:"
@echo " make <target>"
@echo ""
@echo "Targets:"
@echo " lint Run golangci-lint with auto-fix"
@echo " test Run unit tests with race detector in short mode"
@echo " e2e Run end-to-end tests"
@echo " build Build the project"
@echo " run-app Run the application"
@echo " run-db Run the MongoDB database using Docker Compose"
@echo " gql Generate GraphQL code"

lint:
golangci-lint run --fix

Expand Down
3 changes: 3 additions & 0 deletions server/gql/workspace.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type Policy {
assetStorageSize: FileSize
datasetSchemaCount: Int
datasetCount: Int
nlsLayersCount: Int
pageCount: Int
blocksCount: Int
}

enum Role {
Expand Down
162 changes: 162 additions & 0 deletions server/internal/adapter/gql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions server/internal/adapter/gql/gqlmodel/convert_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@ func ToPolicy(p *policy.Policy) *Policy {
AssetStorageSize: o.AssetStorageSize,
DatasetSchemaCount: o.DatasetSchemaCount,
DatasetCount: o.DatasetCount,
NlsLayersCount: o.NLSLayersCount,
PageCount: o.PageCount,
BlocksCount: o.BlocksCount,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func TestToPolicy(t *testing.T) {
AssetStorageSize: lo.ToPtr(int64(5)),
DatasetCount: lo.ToPtr(6),
DatasetSchemaCount: lo.ToPtr(7),
NlsLayersCount: lo.ToPtr(8),
PageCount: lo.ToPtr(9),
BlocksCount: lo.ToPtr(6),
}, ToPolicy(policy.New(policy.Option{
ID: policy.ID("x"),
Name: "aaa",
Expand All @@ -46,6 +49,9 @@ func TestToPolicy(t *testing.T) {
AssetStorageSize: lo.ToPtr(int64(5)),
DatasetCount: lo.ToPtr(6),
DatasetSchemaCount: lo.ToPtr(7),
NLSLayersCount: lo.ToPtr(8),
PageCount: lo.ToPtr(9),
BlocksCount: lo.ToPtr(6),
})))
assert.Nil(t, ToPolicy(nil))
}
3 changes: 3 additions & 0 deletions server/internal/adapter/gql/gqlmodel/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions server/internal/infrastructure/memory/nlslayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ func (r *NLSLayer) FindNLSLayerSimpleByID(ctx context.Context, id id.NLSLayerID)
return nil, rerror.ErrNotFound
}

func (r *NLSLayer) CountByScene(_ context.Context, sid id.SceneID) (n int, _ error) {
r.lock.Lock()
defer r.lock.Unlock()

if !r.f.CanRead(sid) {
return
}

for _, d := range r.data {
if d.Scene() == sid {
if lg := nlslayer.ToNLSLayerGroup(d); lg == nil && !lg.IsRoot() {
n++
}
}
}
return
}

func (r *NLSLayer) Save(ctx context.Context, l nlslayer.NLSLayer) error {
if !r.f.CanWrite(l.Scene()) {
return repo.ErrOperationDenied
Expand Down
14 changes: 7 additions & 7 deletions server/internal/infrastructure/mongo/migration/migrations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions server/internal/infrastructure/mongo/mongodoc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type Auth struct {

func NewConfig(c config.Config) ConfigDocument {
return ConfigDocument{
Migration: c.Migration,
Auth: NewConfigAuth(c.Auth),
Migration: c.Migration,
Auth: NewConfigAuth(c.Auth),
DefaultPolicy: c.DefaultPolicy,
}
}

Expand All @@ -39,7 +40,8 @@ func (c *ConfigDocument) Model() *config.Config {
}

cfg := &config.Config{
Migration: c.Migration,
Migration: c.Migration,
DefaultPolicy: c.DefaultPolicy,
}

if c.Auth != nil {
Expand Down
6 changes: 6 additions & 0 deletions server/internal/infrastructure/mongo/mongodoc/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type PolicyDocument struct {
DatasetCount *int
DatasetSchemaCount *int
AssetStorageSize *int64
NLSLayersCount *int
PageCount *int
BlocksCount *int
}

func (d PolicyDocument) Model() *policy.Policy {
Expand All @@ -28,6 +31,9 @@ func (d PolicyDocument) Model() *policy.Policy {
DatasetCount: d.DatasetCount,
DatasetSchemaCount: d.DatasetSchemaCount,
AssetStorageSize: d.AssetStorageSize,
NLSLayersCount: d.NLSLayersCount,
PageCount: d.PageCount,
BlocksCount: d.BlocksCount,
})
}

Expand Down
12 changes: 12 additions & 0 deletions server/internal/infrastructure/mongo/nlslayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ func (r *NLSLayer) FindByScene(ctx context.Context, id id.SceneID) (nlslayer.NLS
})
}

func (r *NLSLayer) CountByScene(ctx context.Context, sid id.SceneID) (int, error) {
if !r.f.CanRead(sid) {
return 0, repo.ErrOperationDenied
}

c, err := r.client.Count(ctx, bson.M{
"scene": sid.String(),
"group.root": bson.M{"$ne": true},
})
return int(c), err
}

func (r *NLSLayer) Save(ctx context.Context, layer nlslayer.NLSLayer) error {
if !r.f.CanWrite(layer.Scene()) {
return repo.ErrOperationDenied
Expand Down
Loading

0 comments on commit 32b3be4

Please sign in to comment.