Skip to content

Commit

Permalink
chore(graph): expose TTU edges (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
miparnisari authored Oct 1, 2024
2 parents fd967a8 + f034139 commit a12b5ee
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/go/graph/graph_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func parseTupleToUserset(graphBuilder *AuthorizationModelGraphBuilder, parentNod

rewrittenNodeName := fmt.Sprintf("%s#%s", tuplesetType, computedRelation)
nodeSource := graphBuilder.GetOrAddNode(rewrittenNodeName, rewrittenNodeName, SpecificTypeAndRelation)
conditionedOnNodeName := fmt.Sprintf("(%s#%s)", typeDef.GetType(), tuplesetRelation)
conditionedOnNodeName := fmt.Sprintf("%s#%s", typeDef.GetType(), tuplesetRelation)

// new edge from "xxx#admin" to "yyy#viewer" conditioned on "yyy#parent"
graphBuilder.AddEdge(nodeSource, parentNode, TTUEdge, conditionedOnNodeName)
Expand Down
10 changes: 9 additions & 1 deletion pkg/go/graph/graph_edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func (n *AuthorizationModelEdge) EdgeType() EdgeType {
return n.edgeType
}

// ConditionedOn returns the TTU relation. For example, relation
// define viewer: viewer from parent
// gives the graph "document#viewer" -> "document#viewer" and the edge
// is conditioned on "document#parent".
func (n *AuthorizationModelEdge) ConditionedOn() string {
return n.conditionedOn
}

func (n *AuthorizationModelEdge) Attributes() []encoding.Attribute {
switch n.edgeType {
case DirectEdge:
Expand All @@ -55,7 +63,7 @@ func (n *AuthorizationModelEdge) Attributes() []encoding.Attribute {
return []encoding.Attribute{
{
Key: "headlabel",
Value: headLabelAttrValue,
Value: "(" + headLabelAttrValue + ")",
},
}
case RewriteEdge:
Expand Down
45 changes: 45 additions & 0 deletions pkg/go/graph/graph_edge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package graph

import (
"testing"

"github.com/stretchr/testify/require"

language "github.com/openfga/language/pkg/go/transformer"
)

func TestEdgeConditionedOn(t *testing.T) {
t.Parallel()
model := language.MustTransformDSLToProto(`
model
schema 1.1
type user
type folder
type document
relations
define viewer: viewer from parent
define parent: [document, folder]`)
graph, err := NewAuthorizationModelGraph(model)
require.NoError(t, err)

relation, err := graph.GetNodeByLabel("document#viewer")
require.NoError(t, err)

nodesFromRelation := graph.From(relation.ID())
require.Equal(t, 1, nodesFromRelation.Len())
nodesFromRelation.Next()
neighbor, ok := nodesFromRelation.Node().(*AuthorizationModelNode)
require.True(t, ok)

require.Equal(t, relation, neighbor)

edges := graph.Lines(relation.ID(), neighbor.ID())
require.Equal(t, 1, edges.Len())

edges.Next()

edge, ok := edges.Line().(*AuthorizationModelEdge)
require.True(t, ok)

require.Equal(t, "document#parent", edge.ConditionedOn())
}
1 change: 0 additions & 1 deletion pkg/go/validation/validation-rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestValidateObjectID(t *testing.T) {
assert.Equal(t, test.expected, ValidateObjectID(test.value))
})
}

}

func TestValidateObject(t *testing.T) {
Expand Down

0 comments on commit a12b5ee

Please sign in to comment.