Skip to content

Commit

Permalink
fix: validate subject type bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgaOzen committed Mar 14, 2023
1 parent c3464ec commit a61494e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 17 deletions.
25 changes: 22 additions & 3 deletions internal/services/relationshipService.go
Expand Up @@ -3,6 +3,8 @@ package services
import (
"context"
"errors"
"fmt"

otelCodes "go.opentelemetry.io/otel/codes"

"github.com/Permify/permify/internal/repositories"
Expand Down Expand Up @@ -62,7 +64,14 @@ func (service *RelationshipService) WriteRelationships(ctx context.Context, tena
version = v
}

relationships := make([]*base.Tuple, 0, len(tuples))

for _, tup := range tuples {
subject := tup.GetSubject()
if !tuple.IsSubjectUser(subject) {
subject.Relation = tuple.ELLIPSIS
}

var entity *base.EntityDefinition
entity, _, err = service.sr.ReadSchemaDefinition(ctx, tenantID, tup.GetEntity().GetType(), version)
if err != nil {
Expand All @@ -84,18 +93,28 @@ func (service *RelationshipService) WriteRelationships(ctx context.Context, tena
return token, err
}
for _, t := range rel.GetRelationReferences() {
vt = append(vt, t.GetType())
if t.GetRelation() != "" {
vt = append(vt, fmt.Sprintf("%s#%s", t.GetType(), t.GetRelation()))
} else {
vt = append(vt, t.GetType())
}
}

err = tuple.ValidateSubjectType(tup.GetSubject(), vt)
err = tuple.ValidateSubjectType(subject, vt)
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
return token, err
}

relationships = append(relationships, &base.Tuple{
Entity: tup.GetEntity(),
Relation: tup.GetRelation(),
Subject: subject,
})
}

return service.rw.WriteRelationships(ctx, tenantID, database.NewTupleCollection(tuples...))
return service.rw.WriteRelationships(ctx, tenantID, database.NewTupleCollection(relationships...))
}

// DeleteRelationships -
Expand Down
14 changes: 0 additions & 14 deletions pkg/tuple/tuple.go
Expand Up @@ -32,20 +32,6 @@ func IsSubjectUser(subject *base.Subject) bool {
return subject.Type == USER
}

// ValidateSubject -
func ValidateSubject(subject *base.Subject) error {
if subject.Type == USER {
if subject.GetRelation() != "" {
return errors.New(base.ErrorCode_ERROR_CODE_SUBJECT_RELATION_MUST_BE_EMPTY.String())
}
} else {
if subject.GetRelation() == "" {
return errors.New(base.ErrorCode_ERROR_CODE_SUBJECT_RELATION_CANNOT_BE_EMPTY.String())
}
}
return nil
}

// AreSubjectsEqual -
func AreSubjectsEqual(s1, s2 *base.Subject) bool {
return s1.GetRelation() == s2.GetRelation() && s1.GetId() == s2.GetId() && s1.GetType() == s2.GetType()
Expand Down
76 changes: 76 additions & 0 deletions pkg/tuple/tuple_test.go
@@ -1,6 +1,7 @@
package tuple

import (
`errors`
"testing"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -335,5 +336,80 @@ var _ = Describe("tuple", func() {
Expect(IsSubjectValid(tt.target)).Should(Equal(tt.expected))
}
})

It("ValidateSubjectType", func() {
tests := []struct {
target *base.Subject
relationTypes []string
expected error
}{
{
target: &base.Subject{
Type: "organization",
Id: "1",
Relation: "member",
},
relationTypes: []string{
"organization#member",
"user",
},
expected: nil,
},
{
target: &base.Subject{
Type: "organization",
Id: "1",
Relation: "",
},
relationTypes: []string{
"organization",
},
expected: nil,
},
{
target: &base.Subject{
Type: "user",
Id: "u82",
Relation: "",
},
relationTypes: []string{
"user",
},
expected: nil,
},
{
target: &base.Subject{
Type: "testrel",
Id: "u82",
Relation: "",
},
relationTypes: []string{
"test",
"user",
},
expected: errors.New(base.ErrorCode_ERROR_CODE_SUBJECT_TYPE_NOT_FOUND.String()),
},
{
target: &base.Subject{
Type: "test",
Id: "u3",
Relation: "mem",
},
relationTypes: []string{
"test#member",
"user",
},
expected: errors.New(base.ErrorCode_ERROR_CODE_SUBJECT_TYPE_NOT_FOUND.String()),
},
}

for _, tt := range tests {
if tt.expected == nil {
Expect(ValidateSubjectType(tt.target, tt.relationTypes)).Should(BeNil())
} else {
Expect(ValidateSubjectType(tt.target, tt.relationTypes)).Should(Equal(tt.expected))
}
}
})
})
})

0 comments on commit a61494e

Please sign in to comment.