Skip to content

Commit

Permalink
Prevent edits to deleted entities (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash authored Dec 20, 2024
1 parent 1ebd890 commit d9be7b9
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 21 deletions.
16 changes: 16 additions & 0 deletions pkg/manager/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var ErrEditAlreadyApplied = errors.New("edit already applied")
var ErrNoChanges = errors.New("edit contains no changes")
var ErrMergeIDMissing = errors.New("merge target ID is required")
var ErrEntityNotFound = errors.New("entity not found")
var ErrEntityDeleted = errors.New("entity is deleted")
var ErrMergeTargetIsSource = errors.New("merge target cannot be used as source")
var ErrNoMergeSources = errors.New("no merge sources found")

Expand Down Expand Up @@ -309,3 +310,18 @@ func ResolveVotingThreshold(fac models.Repo, edit *models.Edit) (models.VoteStat

return models.VoteStatusEnumPending, nil
}

type editEntity interface {
IsDeleted() bool
}

func validateEditEntity(entity *editEntity, id uuid.UUID, typeName string) error {
if entity == nil {
return fmt.Errorf("%w: %s %s", ErrEntityNotFound, typeName, id.String())
}
if (*entity).IsDeleted() {
return fmt.Errorf("%w: %s %s", ErrEntityDeleted, typeName, id.String())
}

return nil
}
14 changes: 10 additions & 4 deletions pkg/manager/edit/performer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func (m *PerformerEditProcessor) modifyEdit(input models.PerformerEditInput, inp
return err
}

if performer == nil {
return fmt.Errorf("performer with id %v not found", performerID)
var entity editEntity = *performer
if err := validateEditEntity(&entity, *input.Edit.ID, "performer"); err != nil {
return err
}

// perform a diff against the input and the current object
Expand Down Expand Up @@ -163,9 +164,14 @@ func (m *PerformerEditProcessor) destroyEdit(input models.PerformerEditInput) er
pqb := m.fac.Performer()

// get the existing performer
_, err := pqb.Find(*input.Edit.ID)
performerID := *input.Edit.ID
performer, err := pqb.Find(performerID)
if err != nil {
return err
}

return err
var entity editEntity = *performer
return validateEditEntity(&entity, performerID, "performer")
}

func (m *PerformerEditProcessor) CreateJoin(input models.PerformerEditInput) error {
Expand Down
15 changes: 9 additions & 6 deletions pkg/manager/edit/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func (m *SceneEditProcessor) modifyEdit(input models.SceneEditInput, inputArgs u
return err
}

if scene == nil {
return fmt.Errorf("%w: scene %s", ErrEntityNotFound, sceneID.String())
var entity editEntity = *scene
if err := validateEditEntity(&entity, sceneID, "scene"); err != nil {
return err
}

// perform a diff against the input and the current object
Expand Down Expand Up @@ -304,12 +305,14 @@ func (m *SceneEditProcessor) destroyEdit(input models.SceneEditInput, inputArgs
tqb := m.fac.Scene()

// get the existing scene
scene, err := tqb.Find(*input.Edit.ID)
if scene == nil {
return fmt.Errorf("scene with id %v not found", *input.Edit.ID)
sceneID := *input.Edit.ID
scene, err := tqb.Find(sceneID)
if err != nil {
return err
}

return err
var entity editEntity = *scene
return validateEditEntity(&entity, sceneID, "scene")
}

func (m *SceneEditProcessor) CreateJoin(input models.SceneEditInput) error {
Expand Down
13 changes: 8 additions & 5 deletions pkg/manager/edit/studio.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func (m *StudioEditProcessor) modifyEdit(input models.StudioEditInput, inputArgs
return err
}

if studio == nil {
return fmt.Errorf("%w: studio %s", ErrEntityNotFound, studioID.String())
var entity editEntity = studio
if err := validateEditEntity(&entity, studioID, "studio"); err != nil {
return err
}

// perform a diff against the input and the current object
Expand Down Expand Up @@ -175,12 +176,14 @@ func (m *StudioEditProcessor) destroyEdit(input models.StudioEditInput) error {
tqb := m.fac.Studio()

// Get the existing studio
studioID := *input.Edit.ID
studio, err := tqb.Find(*input.Edit.ID)
if studio == nil {
return fmt.Errorf("scene with id %v not found", *input.Edit.ID)
if err != nil {
return err
}

return err
var entity editEntity = studio
return validateEditEntity(&entity, studioID, "studio")
}

func (m *StudioEditProcessor) CreateJoin(input models.StudioEditInput) error {
Expand Down
16 changes: 10 additions & 6 deletions pkg/manager/edit/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func (m *TagEditProcessor) modifyEdit(input models.TagEditInput, inputArgs utils
return err
}

if tag == nil {
return fmt.Errorf("%w: tag %s", ErrEntityNotFound, tagID.String())
var entity editEntity = tag
if err := validateEditEntity(&entity, tagID, "tag"); err != nil {
return err
}

// perform a diff against the input and the current object
Expand Down Expand Up @@ -139,12 +140,15 @@ func (m *TagEditProcessor) destroyEdit(input models.TagEditInput) error {
tqb := m.fac.Tag()

// Get the existing tag
tag, err := tqb.Find(*input.Edit.ID)
if tag == nil {
return fmt.Errorf("tag with id %v not found", *input.Edit.ID)
tagID := *input.Edit.ID
tag, err := tqb.Find(tagID)

if err != nil {
return err
}

return err
var entity editEntity = tag
return validateEditEntity(&entity, tagID, "tag")
}

func (m *TagEditProcessor) CreateJoin(input models.TagEditInput) error {
Expand Down
4 changes: 4 additions & 0 deletions pkg/models/model_performer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (p Performer) GetID() uuid.UUID {
return p.ID
}

func (p Performer) IsDeleted() bool {
return p.Deleted
}

type Performers []*Performer

func (p Performers) Each(fn func(interface{})) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/models/model_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (p Scene) GetID() uuid.UUID {
return p.ID
}

func (p Scene) IsDeleted() bool {
return p.Deleted
}

type Scenes []*Scene

func (p Scenes) Each(fn func(interface{})) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/models/model_studio.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (s Studio) GetID() uuid.UUID {
return s.ID
}

func (s Studio) IsDeleted() bool {
return s.Deleted
}

type Studios []*Studio

func (s Studios) Each(fn func(interface{})) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/models/model_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (p Tag) GetID() uuid.UUID {
return p.ID
}

func (p Tag) IsDeleted() bool {
return p.Deleted
}

type Tags []*Tag

func (p Tags) Each(fn func(interface{})) {
Expand Down

0 comments on commit d9be7b9

Please sign in to comment.