Skip to content

Commit

Permalink
add nodes first to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
miparnisari committed Sep 20, 2024
1 parent 5dde31f commit 16d2daf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
16 changes: 15 additions & 1 deletion pkg/go/graph/weighted_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@ type WeightedAuthorizationModelGraph struct {
drawingDirection DrawingDirection
}

//nolint: cyclop
func NewWeightedAuthorizationModelGraph(model *openfgav1.AuthorizationModel) (*WeightedAuthorizationModelGraph, error) {
g, err := NewAuthorizationModelGraph(model)
if err != nil {
return nil, err
}
g, err = g.Reversed() // we want direction of Check
g, err = g.Reversed() // we want edges to have the direction of Check when doing the weight assignments later
if err != nil {
return nil, err
}

graphBuilder := &WeightedAuthorizationModelGraphBuilder{multi.NewDirectedGraph()}

// Add all nodes
iterNodes := g.Nodes()
for iterNodes.Next() {
nextNode := iterNodes.Node()
from, ok := nextNode.(*AuthorizationModelNode)
if !ok {
return nil, fmt.Errorf("%w: could not cast to WeightedAuthorizationModelNode", ErrBuildingGraph)
}
newNode := &WeightedAuthorizationModelNode{from, make(WeightMap), false}
graphBuilder.AddNode(newNode)
}

// Add all the edges
iterEdges := g.Edges()
for iterEdges.Next() {
nextEdge, ok := iterEdges.Edge().(multi.Edge)
Expand Down
22 changes: 1 addition & 21 deletions pkg/go/graph/weighted_graph_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,9 @@ type WeightedAuthorizationModelGraphBuilder struct {
}

func (wb *WeightedAuthorizationModelGraphBuilder) AddEdgeWithWeights(edge *AuthorizationModelEdge) error {
isLoop := edge.From().ID() == edge.To().ID()

// create "from" node
fromNode := wb.Node(edge.From().ID())
if fromNode == nil {
from, ok := edge.From().(*AuthorizationModelNode)
if !ok {
return fmt.Errorf("%w: could not cast to WeightedAuthorizationModelNode", ErrBuildingGraph)
}
fromNode = &WeightedAuthorizationModelNode{from, make(WeightMap), false}
wb.AddNode(fromNode)
}

// create "to" node
toNode := wb.Node(edge.To().ID())
if toNode == nil {
to, ok := edge.To().(*AuthorizationModelNode)
if !ok {
return fmt.Errorf("%w: could not cast to WeightedAuthorizationModelNode", ErrBuildingGraph)
}
toNode = &WeightedAuthorizationModelNode{to, make(WeightMap), false}
wb.AddNode(toNode)
}
isLoop := fromNode == toNode

// update "isNested" field for the node
if isLoop {
Expand Down
10 changes: 10 additions & 0 deletions pkg/go/graph/weighted_graph_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rankdir=TB
];
// Node definitions.
0 [label=folder];
1 [label="folder#viewer - weights:[user=1]"];
2 [label=user];
Expand All @@ -53,6 +54,7 @@ rankdir=TB
];
// Node definitions.
0 [label=folder];
1 [label="folder#viewer - weights:[user1=1,user2=1]"];
2 [label=user1];
3 [label=user2];
Expand All @@ -79,6 +81,7 @@ rankdir=TB
];
// Node definitions.
0 [label=folder];
1 [label="folder#viewer - weights:[user=1]"];
2 [label=user];
3 [label="user:*"];
Expand All @@ -103,6 +106,7 @@ rankdir=TB
];
// Node definitions.
0 [label=folder];
1 [label="folder#rewrite - weights:[user=1]"];
2 [label="folder#viewer - weights:[user=1]"];
3 [label=user];
Expand Down Expand Up @@ -130,6 +134,7 @@ rankdir=TB
];
// Node definitions.
0 [label=folder];
1 [label="folder#viewer - weights:[user=+∞]"];
2 [label=user];
Expand Down Expand Up @@ -159,6 +164,7 @@ rankdir=TB
0 [label=company];
1 [label="company#approved_member - weights:[user=1]"];
2 [label=user];
3 [label=license];
4 [label="license#member - weights:[user=2]"];
5 [label="license#owner - weights:[company=1]"];
Expand Down Expand Up @@ -197,6 +203,7 @@ rankdir=TB
];
// Node definitions.
0 [label=folder];
1 [label="folder#org - weights:[org=1]"];
2 [label=org];
3 [label="folder#viewer - weights:[user=4]"];
Expand Down Expand Up @@ -255,6 +262,7 @@ rankdir=TB
];
// Node definitions.
0 [label=group];
1 [label="group#max_owner - weights:[user=+∞]"];
2 [label="group#owner - weights:[user=3]"];
3 [label=user];
Expand All @@ -263,6 +271,7 @@ rankdir=TB
6 [label="state#can_view - weights:[user=1]"];
7 [label="union - weights:[user=1]"];
8 [label="state#member - weights:[user=1]"];
9 [label=transition];
10 [label="intersection - weights:[user=2]"];
11 [label="transition#end - weights:[state=1]"];
12 [label="transition#start - weights:[state=1]"];
Expand Down Expand Up @@ -333,6 +342,7 @@ rankdir=TB
];
// Node definitions.
0 [label=folder];
1 [label="folder#a - weights:[user1=1,user2=1]"];
2 [label=user1];
3 [label=user2];
Expand Down

0 comments on commit 16d2daf

Please sign in to comment.