Skip to content

Commit

Permalink
#1410 Fix organisation in user list
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jul 22, 2020
1 parent 9e862db commit fc9262d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions thehive/app/org/thp/thehive/controllers/v0/UserCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class UserCtrl @Inject() (
override val pageQuery: ParamQuery[OutputParam] = Query.withParam[OutputParam, UserSteps, PagedResult[RichUser]](
"page",
FieldsParser[OutputParam],
(range, userSteps, authContext) => userSteps.richUser(authContext.organisation).page(range.from, range.to, withTotal = true)
(range, userSteps, authContext) => userSteps.richUser(authContext).page(range.from, range.to, withTotal = true)
)
override val outputQuery: Query =
Query.outputWithContext[RichUser, UserSteps]((userSteps, authContext) => userSteps.richUser(authContext.organisation))
Query.outputWithContext[RichUser, UserSteps]((userSteps, authContext) => userSteps.richUser(authContext))

override val extraQueries: Seq[ParamQuery[_]] = Seq()

Expand Down
4 changes: 2 additions & 2 deletions thehive/app/org/thp/thehive/controllers/v1/UserCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class UserCtrl @Inject() (
override val pageQuery: ParamQuery[OutputParam] = Query.withParam[OutputParam, UserSteps, PagedResult[RichUser]](
"page",
FieldsParser[OutputParam],
(range, userSteps, authContext) => userSteps.richUser(authContext.organisation).page(range.from, range.to, range.extraData.contains("total"))
(range, userSteps, authContext) => userSteps.richUser(authContext).page(range.from, range.to, range.extraData.contains("total"))
)
override val outputQuery: Query =
Query.outputWithContext[RichUser, UserSteps]((userSteps, authContext) => userSteps.richUser(authContext.organisation))
Query.outputWithContext[RichUser, UserSteps]((userSteps, authContext) => userSteps.richUser(authContext))

override val extraQueries: Seq[ParamQuery[_]] = Seq(
Query.init[UserSteps]("currentUser", (graph, authContext) => userSrv.current(graph, authContext)),
Expand Down
24 changes: 23 additions & 1 deletion thehive/app/org/thp/thehive/services/UserSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.thp.thehive.services
import java.util.regex.Pattern
import java.util.{List => JList}

import scala.collection.JavaConverters._
import akka.actor.ActorRef
import gremlin.scala._
import javax.inject.{Inject, Named, Singleton}
Expand All @@ -14,7 +15,7 @@ import org.thp.scalligraph.query.PropertyUpdater
import org.thp.scalligraph.services._
import org.thp.scalligraph.steps.StepsOps._
import org.thp.scalligraph.steps.{Traversal, TraversalLike, VertexSteps}
import org.thp.scalligraph.{AuthorizationError, BadRequestError, EntitySteps, RichOptionTry}
import org.thp.scalligraph.{AuthorizationError, BadRequestError, EntitySteps, InternalError, RichOptionTry}
import org.thp.thehive.controllers.v1.Conversion._
import org.thp.thehive.models._
import play.api.Configuration
Expand Down Expand Up @@ -266,6 +267,27 @@ class UserSteps(raw: GremlinScala[Vertex])(implicit @Named("with-thehive-schema"
RichUser(user.as[User], avatar, "", Set.empty, organisation)
}

def richUser(implicit authContext: AuthContext): Traversal[RichUser, RichUser] =
this
.project(
_.by
.by(_.avatar.fold)
.by(_.role.project(_.by(_.profile).by(_.organisation.name)).fold)
)
.map {
case (user, attachment, profileOrganisations) =>
val po = profileOrganisations.asScala.map {
case (profile, organisationName) => profile.as[Profile] -> organisationName
}
po.find(_._2 == authContext.organisation)
.orElse(po.headOption)
.fold(throw InternalError(s"")) {
case (profile, organisationName) =>
val avatar = atMostOneOf[Vertex](attachment).map(_.as[Attachment].attachmentId)
RichUser(user.as[User], avatar, profile.name, profile.permissions, organisationName)
}
}

def richUserWithCustomRenderer[A](organisation: String, entityRenderer: UserSteps => TraversalLike[_, A])(
implicit authContext: AuthContext
): Traversal[(RichUser, A), (RichUser, A)] =
Expand Down

0 comments on commit fc9262d

Please sign in to comment.