@@ -8,21 +8,23 @@ import org.thp.scalligraph.query.PropertyUpdater
8
8
import org .thp .scalligraph .services ._
9
9
import org .thp .scalligraph .traversal .TraversalOps ._
10
10
import org .thp .scalligraph .traversal .{Converter , Graph , StepLabel , Traversal }
11
- import org .thp .scalligraph .{CreateError , EntityIdOrName , EntityName , RichSeq }
11
+ import org .thp .scalligraph .{BadRequestError , CreateError , EntityIdOrName , EntityName , RichSeq }
12
12
import org .thp .thehive .controllers .v1 .Conversion ._
13
13
import org .thp .thehive .models ._
14
14
import org .thp .thehive .services .CaseTemplateOps ._
15
15
import org .thp .thehive .services .CustomFieldOps ._
16
16
import org .thp .thehive .services .OrganisationOps ._
17
17
import org .thp .thehive .services .TaskOps ._
18
18
import org .thp .thehive .services .UserOps ._
19
+ import play .api .Configuration
19
20
import play .api .libs .json .{JsObject , Json }
20
21
21
22
import java .util .{Map => JMap }
22
23
import javax .inject .{Inject , Named }
23
24
import scala .util .{Failure , Success , Try }
24
25
25
26
class CaseTemplateSrv @ Inject () (
27
+ configuration : Configuration ,
26
28
customFieldSrv : CustomFieldSrv ,
27
29
organisationSrv : OrganisationSrv ,
28
30
tagSrv : TagSrv ,
@@ -57,6 +59,7 @@ class CaseTemplateSrv @Inject() (
57
59
Failure (CreateError (s """ The case template " ${caseTemplate.name}" already exists """ ))
58
60
else
59
61
for {
62
+ _ <- checkCaseTemplateQuota(organisation)
60
63
createdCaseTemplate <- createEntity(caseTemplate)
61
64
_ <- caseTemplateOrganisationSrv.create(CaseTemplateOrganisation (), createdCaseTemplate, organisation)
62
65
createdTasks <- tasks.toTry(createTask(createdCaseTemplate, _))
@@ -66,6 +69,19 @@ class CaseTemplateSrv @Inject() (
66
69
_ <- auditSrv.caseTemplate.create(createdCaseTemplate, richCaseTemplate.toJson)
67
70
} yield richCaseTemplate
68
71
72
+ private def checkCaseTemplateQuota (organisation : Organisation with Entity )(implicit
73
+ graph : Graph ,
74
+ authContext : AuthContext
75
+ ): Try [Unit ] = {
76
+ val caseTemplateQuota = configuration.getOptional[Long ](" quota.organisation.caseTemplate.count" )
77
+ val caseTemplateCount = organisationSrv.get(organisation).caseTemplates.getCount
78
+
79
+ caseTemplateQuota.fold[Try [Unit ]](Success (()))(quota =>
80
+ if (caseTemplateCount < quota) Success (())
81
+ else Failure (BadRequestError (s " Case template quota is reached, this organisation cannot have more case templates " ))
82
+ )
83
+ }
84
+
69
85
def createTask (caseTemplate : CaseTemplate with Entity , task : Task )(implicit graph : Graph , authContext : AuthContext ): Try [RichTask ] =
70
86
for {
71
87
assignee <- task.assignee.map(u => organisationSrv.current.users(Permissions .manageTask).getByName(u).getOrFail(" User" )).flip
0 commit comments