11package com.saveourtool.save.backend.controllers
22
33import com.saveourtool.save.backend.configs.ConfigProperties
4+ import com.saveourtool.save.backend.security.ProjectPermissionEvaluator
45import com.saveourtool.save.backend.service.LnkProjectGithubService
56import com.saveourtool.save.backend.service.ProjectService
67import com.saveourtool.save.configs.RequiresAuthorizationSourceHeader
78import com.saveourtool.save.demo.DemoDto
89import com.saveourtool.save.demo.DemoInfo
910import com.saveourtool.save.demo.DemoStatus
11+ import com.saveourtool.save.permission.Permission
1012import com.saveourtool.save.spring.utils.applyAll
11- import com.saveourtool.save.utils.EmptyResponse
12- import com.saveourtool.save.utils.blockingToMono
13+ import com.saveourtool.save.utils.*
1314import com.saveourtool.save.utils.switchIfEmptyToNotFound
14- import com.saveourtool.save.utils.switchIfEmptyToResponseException
1515import com.saveourtool.save.v1
1616
1717import io.swagger.v3.oas.annotations.Operation
@@ -39,6 +39,7 @@ import reactor.core.publisher.Mono
3939class DemoManagerController (
4040 private val projectService : ProjectService ,
4141 private val lnkProjectGithubService : LnkProjectGithubService ,
42+ private val projectPermissionEvaluator : ProjectPermissionEvaluator ,
4243 configProperties : ConfigProperties ,
4344 customizers : List <WebClientCustomizer >,
4445) {
@@ -60,6 +61,9 @@ class DemoManagerController(
6061 description = " Add demo for a tool." ,
6162 )
6263 @ApiResponse(responseCode = " 200" , description = " Successfully added demo." )
64+ @ApiResponse(responseCode = " 403" , description = " Not enough permission for accessing given project." )
65+ @ApiResponse(responseCode = " 404" , description = " Could not find project in organization." )
66+ @ApiResponse(responseCode = " 409" , description = " Please provide github repository" )
6367 fun addDemo (
6468 @PathVariable organizationName : String ,
6569 @PathVariable projectName : String ,
@@ -74,6 +78,9 @@ class DemoManagerController(
7478 .switchIfEmptyToNotFound {
7579 " Could not find project $projectName in organization $organizationName ."
7680 }
81+ .requireOrSwitchToResponseException({ projectPermissionEvaluator.hasPermission(authentication, this , Permission .DELETE ) }, HttpStatus .FORBIDDEN ) {
82+ " Not enough permission for accessing given project."
83+ }
7784 .flatMap { project ->
7885 blockingToMono {
7986 demoDto.githubProjectCoordinates?.let { githubCoordinates ->
@@ -105,13 +112,16 @@ class DemoManagerController(
105112 Parameter (name = " organizationName" , `in ` = ParameterIn .PATH , description = " name of saveourtool organization" , required = true ),
106113 Parameter (name = " projectName" , `in ` = ParameterIn .PATH , description = " name of saveourtool project" , required = true ),
107114 Parameter (name = " version" , `in ` = ParameterIn .QUERY , description = " version to attach the file to" , required = true ),
115+ Parameter (name = " file" , `in ` = ParameterIn .DEFAULT , description = " a file to upload" , required = true ),
108116 )
109117 @Operation(
110118 method = " POST" ,
111119 summary = " Attach file to demo." ,
112120 description = " Attach file to demo." ,
113121 )
114122 @ApiResponse(responseCode = " 200" , description = " Successfully added demo." )
123+ @ApiResponse(responseCode = " 403" , description = " Not enough permission for accessing given project." )
124+ @ApiResponse(responseCode = " 404" , description = " Could not find project in organization." )
115125 fun uploadFile (
116126 @PathVariable organizationName : String ,
117127 @PathVariable projectName : String ,
@@ -127,6 +137,9 @@ class DemoManagerController(
127137 .switchIfEmptyToNotFound {
128138 " Could not find project $projectName in organization $organizationName ."
129139 }
140+ .requireOrSwitchToResponseException({ projectPermissionEvaluator.hasPermission(authentication, this , Permission .DELETE ) }, HttpStatus .FORBIDDEN ) {
141+ " Not enough permission for accessing given project."
142+ }
130143 .flatMap {
131144 webClientDemo.post()
132145 .uri(" /demo/internal/$organizationName /$projectName /upload-file?version=$version " )
@@ -148,23 +161,36 @@ class DemoManagerController(
148161 description = " Get demo status." ,
149162 )
150163 @ApiResponse(responseCode = " 200" , description = " Successfully fetched demo status." )
164+ @ApiResponse(responseCode = " 403" , description = " Not enough permission for accessing given project." )
165+ @ApiResponse(responseCode = " 404" , description = " Could not find project in organization or demo for it." )
151166 fun getDemoStatus (
152167 @PathVariable organizationName : String ,
153168 @PathVariable projectName : String ,
154169 authentication : Authentication ,
155- ): Mono <DemoStatus > = webClientDemo.get()
156- .uri(" /demo/internal/$organizationName /$projectName /status" )
157- .retrieve()
158- .onStatus({ ! it.is2xxSuccessful }) {
159- Mono .error(
160- ResponseStatusException (
161- HttpStatus .NOT_FOUND ,
162- " Demo for $organizationName /$projectName is not found." ,
163- )
164- )
170+ ): Mono <DemoStatus > = blockingToMono {
171+ projectService.findByNameAndOrganizationNameAndCreatedStatus(projectName, organizationName)
172+ }
173+ .switchIfEmptyToNotFound {
174+ " Could not find project $projectName in organization $organizationName ."
175+ }
176+ .requireOrSwitchToResponseException({ projectPermissionEvaluator.hasPermission(authentication, this , Permission .READ ) }, HttpStatus .FORBIDDEN ) {
177+ " Not enough permission for accessing given project."
178+ }
179+ .flatMap {
180+ webClientDemo.get()
181+ .uri(" /demo/internal/$organizationName /$projectName /status" )
182+ .retrieve()
183+ .onStatus({ ! it.is2xxSuccessful }) {
184+ Mono .error(
185+ ResponseStatusException (
186+ HttpStatus .NOT_FOUND ,
187+ " Demo for $organizationName /$projectName is not found." ,
188+ )
189+ )
190+ }
191+ .bodyToMono<DemoStatus >()
192+ .defaultIfEmpty(DemoStatus .NOT_CREATED )
165193 }
166- .bodyToMono<DemoStatus >()
167- .defaultIfEmpty(DemoStatus .NOT_CREATED )
168194
169195 @GetMapping(" /{organizationName}/{projectName}" )
170196 @RequiresAuthorizationSourceHeader
@@ -179,26 +205,39 @@ class DemoManagerController(
179205 description = " Get demo info." ,
180206 )
181207 @ApiResponse(responseCode = " 200" , description = " Successfully fetched demo status." )
208+ @ApiResponse(responseCode = " 403" , description = " Not enough permission for accessing given project." )
209+ @ApiResponse(responseCode = " 404" , description = " Could not find project in organization or demo for it." )
182210 fun getDemoInfo (
183211 @PathVariable organizationName : String ,
184212 @PathVariable projectName : String ,
185213 authentication : Authentication ,
186- ): Mono <DemoInfo > = webClientDemo.get()
187- .uri(" /demo/internal/$organizationName /$projectName " )
188- .retrieve()
189- .onStatus({ ! it.is2xxSuccessful }) {
190- Mono .error(
191- ResponseStatusException (
192- HttpStatus .NOT_FOUND ,
193- " Demo for $organizationName /$projectName is not found." ,
214+ ): Mono <DemoInfo > = blockingToMono {
215+ projectService.findByNameAndOrganizationNameAndCreatedStatus(projectName, organizationName)
216+ }
217+ .switchIfEmptyToNotFound {
218+ " Could not find project $projectName in organization $organizationName ."
219+ }
220+ .requireOrSwitchToResponseException({ projectPermissionEvaluator.hasPermission(authentication, this , Permission .WRITE ) }, HttpStatus .FORBIDDEN ) {
221+ " Not enough permission for accessing given project."
222+ }
223+ .flatMap {
224+ webClientDemo.get()
225+ .uri(" /demo/internal/$organizationName /$projectName " )
226+ .retrieve()
227+ .onStatus({ ! it.is2xxSuccessful }) {
228+ Mono .error(
229+ ResponseStatusException (
230+ HttpStatus .NOT_FOUND ,
231+ " Demo for $organizationName /$projectName is not found." ,
232+ )
233+ )
234+ }
235+ .bodyToMono<DemoInfo >()
236+ .defaultIfEmpty(
237+ DemoInfo (
238+ DemoDto .emptyForProject(organizationName, projectName),
239+ DemoStatus .NOT_CREATED ,
240+ )
194241 )
195- )
196242 }
197- .bodyToMono<DemoInfo >()
198- .defaultIfEmpty(
199- DemoInfo (
200- DemoDto .emptyForProject(organizationName, projectName),
201- DemoStatus .NOT_CREATED ,
202- )
203- )
204243}
0 commit comments