Skip to content

Commit

Permalink
fix: writing models with conditions (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Jan 23, 2024
2 parents a52bee4 + 3e9edca commit 9690505
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/model/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func Write(
fgaClient client.SdkClient,
inputModel authorizationmodel.AuthzModel,
) (*client.ClientWriteAuthorizationModelResponse, error) {
body := &client.ClientWriteAuthorizationModelRequest{
body := client.ClientWriteAuthorizationModelRequest{
SchemaVersion: inputModel.GetSchemaVersion(),
TypeDefinitions: inputModel.GetTypeDefinitions(),
Conditions: inputModel.GetConditions(),
}

model, err := fgaClient.WriteAuthorizationModel(context.Background()).Body(*body).Execute()
model, err := fgaClient.WriteAuthorizationModel(context.Background()).Body(body).Execute()
if err != nil {
return nil, fmt.Errorf("failed to write model due to %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/model/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestWriteModelFail(t *testing.T) {
defer mockCtrl.Finish()
mockFgaClient := mockclient.NewMockSdkClient(mockCtrl)

modelJSONTxt := `{"schema_version":"1.1","type_definitions":[{"relations":{"viewer":{"this":{}}},"type":"github-repo"}]}` //nolint:lll
modelJSONTxt := `{"schema_version":"1.1","type_definitions":[{"relations":{"viewer":{"this":{}}},"type":"github-repo"}],"conditions":{}}` //nolint:lll
body := &client.ClientWriteAuthorizationModelRequest{}

err := json.Unmarshal([]byte(modelJSONTxt), &body)
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestWriteModel(t *testing.T) {
defer mockCtrl.Finish()
mockFgaClient := mockclient.NewMockSdkClient(mockCtrl)

modelJSONTxt := `{"schema_version":"1.1","type_definitions":[{"relations":{"viewer":{"this":{}}},"type":"github-repo"}]}` //nolint:lll
modelJSONTxt := `{"schema_version":"1.1","type_definitions":[{"relations":{"viewer":{"this":{}}},"type":"github-repo"}],"conditions":{}}` //nolint:lll

body := &client.ClientWriteAuthorizationModelRequest{}

Expand Down
14 changes: 14 additions & 0 deletions internal/authorizationmodel/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ func (model *AuthzModel) GetTypeDefinitions() []openfga.TypeDefinition {
return *model.TypeDefinitions
}

func (model *AuthzModel) GetConditions() *map[string]openfga.Condition {
conditions := make(map[string]openfga.Condition)

if model == nil || model.Conditions == nil {
return &conditions
}

for conditionName, condition := range model.Conditions {
conditions[conditionName] = *condition
}

return &conditions
}

func (model *AuthzModel) GetProtoModel() *pb.AuthorizationModel {
if model == nil {
return nil
Expand Down

0 comments on commit 9690505

Please sign in to comment.