Skip to content

Commit

Permalink
#2367 Prevent custom field creation if it already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Apr 5, 2022
1 parent c7cd8b3 commit bd01858
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions thehive/app/org/thp/thehive/services/CustomFieldSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package org.thp.thehive.services

import akka.actor.typed.ActorRef
import org.apache.tinkerpop.gremlin.structure.Edge
import org.thp.scalligraph.EntityIdOrName
import org.thp.scalligraph.auth.AuthContext
import org.thp.scalligraph.models.{Database, Entity}
import org.thp.scalligraph.query.PropertyUpdater
import org.thp.scalligraph.RichSeq
import org.thp.scalligraph.services.{DedupCheck, IntegrityCheckOps, VertexSrv}
import org.thp.scalligraph.services.{DedupCheck, VertexSrv}
import org.thp.scalligraph.traversal.TraversalOps._
import org.thp.scalligraph.traversal._
import org.thp.scalligraph.{CreateError, EntityIdOrName, RichSeq}
import org.thp.thehive.controllers.v1.Conversion._
import org.thp.thehive.models._
import org.thp.thehive.services.CustomFieldOps._
Expand All @@ -18,7 +17,7 @@ import play.api.libs.json.{JsObject, JsValue}

import java.util.{Map => JMap}
import javax.inject.{Inject, Provider, Singleton}
import scala.util.{Success, Try}
import scala.util.{Failure, Try}

@Singleton
class CustomFieldSrv @Inject() (
Expand All @@ -37,10 +36,13 @@ class CustomFieldSrv @Inject() (
}

def create(e: CustomField)(implicit graph: Graph, authContext: AuthContext): Try[CustomField with Entity] =
for {
created <- createEntity(e)
_ <- auditSrv.customField.create(created, created.toJson)
} yield created
if (startTraversal.getByName(e.name).exists)
Failure(CreateError(s"CustomField ${e.name} already exists"))
else
for {
created <- createEntity(e)
_ <- auditSrv.customField.create(created, created.toJson)
} yield created

override def exists(e: CustomField)(implicit graph: Graph): Boolean = startTraversal.getByName(e.name).exists

Expand Down

0 comments on commit bd01858

Please sign in to comment.