11package org .thp .cortex .controllers
22
3- import scala .concurrent .{ ExecutionContext , Future }
3+ import scala .concurrent .{ExecutionContext , Future }
44
5- import play .api .libs .json .{ JsObject , JsString , Json }
6- import play .api .mvc .{ AbstractController , Action , AnyContent , ControllerComponents }
5+ import play .api .libs .json .{JsObject , JsString , Json }
6+ import play .api .mvc .{AbstractController , Action , AnyContent , ControllerComponents }
77
88import akka .stream .Materializer
9- import javax .inject .{ Inject , Singleton }
10- import org .thp .cortex .models .{ Roles , Worker }
11- import org .thp .cortex .services .{ UserSrv , WorkerSrv }
9+ import javax .inject .{Inject , Singleton }
10+ import org .thp .cortex .models .{Roles , Worker }
11+ import org .thp .cortex .services .{UserSrv , WorkerSrv }
1212
13- import org .elastic4play .controllers .{ Authenticated , Fields , FieldsBodyParser , Renderer }
13+ import org .elastic4play .controllers .{Authenticated , Fields , FieldsBodyParser , Renderer }
1414import org .elastic4play .services .JsonFormat .queryReads
15- import org .elastic4play .services .{ QueryDSL , QueryDef }
15+ import org .elastic4play .services .{QueryDSL , QueryDef }
1616
1717@ Singleton
18- class AnalyzerCtrl @ Inject () (
18+ class AnalyzerCtrl @ Inject ()(
1919 workerSrv : WorkerSrv ,
2020 userSrv : UserSrv ,
2121 authenticated : Authenticated ,
2222 fieldsBodyParser : FieldsBodyParser ,
2323 renderer : Renderer ,
2424 components : ControllerComponents ,
2525 implicit val ec : ExecutionContext ,
26- implicit val mat : Materializer ) extends AbstractController (components) {
26+ implicit val mat : Materializer
27+ ) extends AbstractController (components) {
2728
2829 def find : Action [Fields ] = authenticated(Roles .read).async(fieldsBodyParser) { request ⇒
29- val query = request.body.getValue(" query" ).fold[QueryDef ](QueryDSL .any)(_.as[QueryDef ])
30- val range = request.body.getString(" range" )
31- val sort = request.body.getStrings(" sort" ).getOrElse(Nil )
32- val isAdmin = request.roles.contains(Roles .orgAdmin)
30+ val query = request.body.getValue(" query" ).fold[QueryDef ](QueryDSL .any)(_.as[QueryDef ])
31+ val range = request.body.getString(" range" )
32+ val sort = request.body.getStrings(" sort" ).getOrElse(Nil )
33+ val isAdmin = request.roles.contains(Roles .orgAdmin)
3334 val (analyzers, analyzerTotal) = workerSrv.findAnalyzersForUser(request.userId, query, range, sort)
3435 renderer.toOutput(OK , analyzers.map(analyzerJson(isAdmin)), analyzerTotal)
3536 }
3637
3738 def get (analyzerId : String ): Action [AnyContent ] = authenticated(Roles .read).async { request ⇒
3839 val isAdmin = request.roles.contains(Roles .orgAdmin)
39- workerSrv.getForUser(request.userId, analyzerId)
40+ workerSrv
41+ .getForUser(request.userId, analyzerId)
4042 .map(a ⇒ renderer.toOutput(OK , analyzerJson(isAdmin)(a)))
4143 }
4244
43- private def analyzerJson (isAdmin : Boolean )(analyzer : Worker ): JsObject = {
45+ private def analyzerJson (isAdmin : Boolean )(analyzer : Worker ): JsObject =
4446 if (isAdmin)
4547 analyzer.toJson + (" configuration" → Json .parse(analyzer.configuration())) + (" analyzerDefinitionId" → JsString (analyzer.workerDefinitionId()))
4648 else
4749 analyzer.toJson + (" analyzerDefinitionId" → JsString (analyzer.workerDefinitionId()))
48- }
4950
5051 def listForType (dataType : String ): Action [AnyContent ] = authenticated(Roles .read).async { request ⇒
5152 import org .elastic4play .services .QueryDSL ._
@@ -55,9 +56,9 @@ class AnalyzerCtrl @Inject() (
5556
5657 def create (analyzerDefinitionId : String ): Action [Fields ] = authenticated(Roles .orgAdmin).async(fieldsBodyParser) { implicit request ⇒
5758 for {
58- organizationId ← userSrv.getOrganizationId(request.userId)
59+ organizationId ← userSrv.getOrganizationId(request.userId)
5960 workerDefinition ← Future .fromTry(workerSrv.getDefinition(analyzerDefinitionId))
60- analyzer ← workerSrv.create(organizationId, workerDefinition, request.body)
61+ analyzer ← workerSrv.create(organizationId, workerDefinition, request.body)
6162 } yield renderer.toOutput(CREATED , analyzerJson(isAdmin = false )(analyzer))
6263 }
6364
@@ -74,14 +75,14 @@ class AnalyzerCtrl @Inject() (
7475 def delete (analyzerId : String ): Action [AnyContent ] = authenticated(Roles .orgAdmin, Roles .superAdmin).async { implicit request ⇒
7576 for {
7677 analyzer ← workerSrv.getForUser(request.userId, analyzerId)
77- _ ← workerSrv.delete(analyzer)
78+ _ ← workerSrv.delete(analyzer)
7879 } yield NoContent
7980 }
8081
8182 def update (analyzerId : String ): Action [Fields ] = authenticated(Roles .orgAdmin).async(fieldsBodyParser) { implicit request ⇒
8283 for {
83- analyzer ← workerSrv.getForUser(request.userId, analyzerId)
84+ analyzer ← workerSrv.getForUser(request.userId, analyzerId)
8485 updatedAnalyzer ← workerSrv.update(analyzer, request.body)
8586 } yield renderer.toOutput(OK , analyzerJson(isAdmin = true )(updatedAnalyzer))
8687 }
87- }
88+ }
0 commit comments