Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation to tag an ent field as an additionalSubject #185

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions entx/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,33 @@ package entx
// EventsHookAnnotationName is the value of the annotation when read during ent compilation
var EventsHookAnnotationName = "INFRA9_EVENTHOOKS"

// EventsHookAnnotation provides a ent.Annotation spec. These shouldn't be set directly, you should use EventsHookAdditionalSubject() and EventsHookSubjectName instead
// EventsHookAnnotation provides a ent.Annotation spec.
// These shouldn't be set directly, you should use EventsHookAdditionalSubject(), EventsHookAdditionalSubjectField() and EventsHookSubjectName() instead
type EventsHookAnnotation struct {
SubjectName string
AdditionalSubjectRelation string
IsAdditionalSubjectField bool
}

// Name implements the ent Annotation interface.
func (a EventsHookAnnotation) Name() string {
return EventsHookAnnotationName
}

// EventsHookAdditionalSubject marks this field as a field to return as an additional subject
// EventsHookAdditionalSubject marks this ent field as a field to return as an additional subject relationship
func EventsHookAdditionalSubject(relation string) *EventsHookAnnotation {
return &EventsHookAnnotation{
AdditionalSubjectRelation: relation,
}
}

// EventsHookAdditionalSubjectField marks this ent field as a field to return an additional subject in the change event
func EventsHookAdditionalSubjectField() *EventsHookAnnotation {
return &EventsHookAnnotation{
IsAdditionalSubjectField: true,
}
}

// EventsHookSubjectName sets the subject name that is where the messages for this object will be sent
func EventsHookSubjectName(s string) *EventsHookAnnotation {
return &EventsHookAnnotation{
Expand Down
14 changes: 11 additions & 3 deletions entx/template/event_hooks.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{{ $currentValue }} := ""
{{ $f.Name }}, ok := m.{{ $f.MutationGet }}()
{{- $annotation := $f.Annotations.INFRA9_EVENTHOOKS }}
{{- if $annotation.AdditionalSubjectRelation }}
{{- if or $annotation.AdditionalSubjectRelation $annotation.IsAdditionalSubjectField }}
if !ok && !m.Op().Is(ent.OpCreate) {
// since we are doing an update or delete and these fields didn't change, load the "old" value
{{ $f.Name }}, err = m.{{ $f.MutationGetOld }}(ctx)
Expand All @@ -55,18 +55,22 @@
if {{ $f.Name }} != gidx.NullPrefixedID {
additionalSubjects = append(additionalSubjects, {{ $f.Name }})

{{- if $annotation.AdditionalSubjectRelation }}
relationships = append(relationships, events.AuthRelationshipRelation{
Relation: "{{ $annotation.AdditionalSubjectRelation }}",
SubjectID: {{ $f.Name }},
})
{{- end }}
}
{{- else }}
additionalSubjects = append(additionalSubjects, {{ $f.Name }})

{{- if $annotation.AdditionalSubjectRelation }}
relationships = append(relationships, events.AuthRelationshipRelation{
Relation: "{{ $annotation.AdditionalSubjectRelation }}",
SubjectID: {{ $f.Name }},
})
{{- end }}
{{- end }}
{{ end }}

Expand Down Expand Up @@ -127,7 +131,7 @@
return retValue, err
}

if len(relationships) != 0 {
if len(relationships) != 0 && m.Op().Is(ent.OpCreate) {
if err := permissions.CreateAuthRelationships(ctx, "{{ $nodeAnnotation.SubjectName }}", objID, relationships...); err != nil {
return nil, fmt.Errorf("relationship request failed with error: %w", err)
}
Expand Down Expand Up @@ -162,23 +166,27 @@
{{- range $f := $node.Fields }}
{{- if not $f.Sensitive }}
{{- $annotation := $f.Annotations.INFRA9_EVENTHOOKS }}
{{- if $annotation.AdditionalSubjectRelation }}
{{- if or $annotation.AdditionalSubjectRelation $annotation.IsAdditionalSubjectField }}
{{- if $f.Optional }}
if dbObj.{{ $f.MutationGet }} != gidx.NullPrefixedID {
additionalSubjects = append(additionalSubjects, dbObj.{{ $f.MutationGet }})

{{- if $annotation.AdditionalSubjectRelation }}
relationships = append(relationships, events.AuthRelationshipRelation{
Relation: "{{ $annotation.AdditionalSubjectRelation }}",
SubjectID: dbObj.{{ $f.MutationGet }},
})
{{- end }}
}
{{- else }}
additionalSubjects = append(additionalSubjects, dbObj.{{ $f.MutationGet }})

{{- if $annotation.AdditionalSubjectRelation }}
relationships = append(relationships, events.AuthRelationshipRelation{
Relation: "{{ $annotation.AdditionalSubjectRelation }}",
SubjectID: dbObj.{{ $f.MutationGet }},
})
{{- end }}
{{- end }}
{{ end }}
{{ end }}
Expand Down
Loading