Skip to content

Commit 171ca14

Browse files
committed
Controllers to kotlin
1 parent 087b77f commit 171ca14

File tree

4 files changed

+413
-462
lines changed

4 files changed

+413
-462
lines changed

src/main/java/org/radarbase/appserver/controller/RadarProjectController.java

Lines changed: 0 additions & 197 deletions
This file was deleted.
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2018 King's College London
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * http://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* * *
18+
* *
19+
*
20+
*/
21+
package org.radarbase.appserver.controller
22+
23+
import jakarta.servlet.http.HttpServletRequest
24+
import jakarta.validation.Valid
25+
import jakarta.websocket.server.PathParam
26+
import lombok.extern.slf4j.Slf4j
27+
import org.radarbase.appserver.config.AuthConfig.AuthEntities
28+
import org.radarbase.appserver.config.AuthConfig.AuthPermissions
29+
import org.radarbase.appserver.dto.ProjectDto
30+
import org.radarbase.appserver.dto.ProjectDtos
31+
import org.radarbase.appserver.service.ProjectService
32+
import org.radarbase.auth.token.RadarToken
33+
import org.springframework.http.MediaType
34+
import org.springframework.http.ResponseEntity
35+
import org.springframework.web.bind.annotation.*
36+
import radar.spring.auth.common.AuthAspect
37+
import radar.spring.auth.common.Authorization
38+
import radar.spring.auth.common.Authorized
39+
import radar.spring.auth.common.PermissionOn
40+
import radar.spring.auth.exception.AuthorizationFailedException
41+
import java.io.IOException
42+
import java.net.URI
43+
import java.net.URISyntaxException
44+
import java.util.*
45+
import java.util.stream.Collectors
46+
47+
/**
48+
* Resource Endpoint for getting and adding projects. Each user [ ] needs to be associated to a project. A project may represent
49+
* a Management Portal project.
50+
*
51+
* @see [Management Portal](https://github.com/RADAR-base/ManagementPortal)
52+
*
53+
* @author yatharthranjan
54+
*/
55+
@CrossOrigin
56+
@RestController
57+
class RadarProjectController(
58+
private val projectService: ProjectService,
59+
private val authorization: Authorization<RadarToken>?
60+
) {
61+
/**
62+
* Method for updating a project.
63+
*
64+
* @param projectDto The project info to update
65+
* @return The updated Project DTO. Throws [ ] if project was not found.
66+
*/
67+
@Authorized(permission = AuthPermissions.READ, entity = AuthEntities.SUBJECT)
68+
@PostMapping(
69+
value = ["/${PathsUtil.PROJECT_PATH}"],
70+
consumes = [MediaType.APPLICATION_JSON_VALUE]
71+
)
72+
fun addProject(
73+
request: HttpServletRequest,
74+
@Valid @RequestBody projectDto: ProjectDto
75+
): ResponseEntity<ProjectDto> {
76+
authorization?.let {
77+
val token = request.getAttribute(AuthAspect.TOKEN_KEY) as RadarToken
78+
if (it.hasPermission(
79+
token,
80+
"READ",
81+
"SUBJECT",
82+
PermissionOn.PROJECT,
83+
projectDto.projectId,
84+
null,
85+
null
86+
)
87+
) {
88+
val projectDtoNew = projectService.addProject(projectDto)
89+
return ResponseEntity.created(URI("/projects/project?id=${projectDtoNew.id}"))
90+
.body(projectDtoNew)
91+
} else {
92+
throw AuthorizationFailedException("The token does not have permission for the project ${projectDto.projectId}")
93+
}
94+
} ?: run {
95+
val projectDtoNew = projectService.addProject(projectDto)
96+
return ResponseEntity.created(URI("/projects/project?id=${projectDtoNew.id}"))
97+
.body(projectDtoNew)
98+
}
99+
}
100+
101+
/**
102+
* Method for updating a project.
103+
*
104+
* @param projectDto The project info to update
105+
* @return The updated Project DTO. Throws [ ] if project was not found.
106+
*/
107+
@Authorized(
108+
permission = AuthPermissions.UPDATE,
109+
entity = AuthEntities.SUBJECT,
110+
permissionOn = PermissionOn.PROJECT
111+
)
112+
@PutMapping(
113+
value = ["/" + PathsUtil.PROJECT_PATH + "/" + PathsUtil.PROJECT_ID_CONSTANT],
114+
consumes = [MediaType.APPLICATION_JSON_VALUE]
115+
)
116+
fun updateProject(
117+
@PathVariable("projectId") projectId: String,
118+
@Valid @RequestBody projectDto: ProjectDto
119+
): ResponseEntity<ProjectDto> {
120+
val updatedProject = projectService.updateProject(projectDto)
121+
return ResponseEntity.ok(updatedProject)
122+
}
123+
124+
@Authorized(permission = AuthPermissions.READ, entity = AuthEntities.PROJECT)
125+
@GetMapping("/" + PathsUtil.PROJECT_PATH)
126+
fun getAllProjects(request: HttpServletRequest): ResponseEntity<ProjectDtos> {
127+
val allProjects = projectService.getAllProjects()
128+
return authorization?.let {
129+
val filteredProjects = allProjects.projects.filter { project ->
130+
it.hasPermission(
131+
request.getAttribute(AuthAspect.TOKEN_KEY) as RadarToken,
132+
AuthPermissions.READ,
133+
AuthEntities.PROJECT,
134+
PermissionOn.PROJECT,
135+
project.projectId,
136+
null,
137+
null
138+
)
139+
}
140+
ResponseEntity.ok(ProjectDtos().withProjects(filteredProjects))
141+
} ?: ResponseEntity.ok(allProjects)
142+
}
143+
144+
145+
// TODO think about plain authorized
146+
@Authorized(permission = AuthPermissions.READ, entity = AuthEntities.PROJECT)
147+
@GetMapping("/" + PathsUtil.PROJECT_PATH + "/project")
148+
fun getProjectsUsingId(
149+
request: HttpServletRequest,
150+
@RequestParam("id") id: Long
151+
): ResponseEntity<ProjectDto> {
152+
val projectDto = projectService.getProjectById(id)
153+
return authorization?.let {
154+
val token = request.getAttribute(AuthAspect.TOKEN_KEY) as RadarToken
155+
if (it.hasPermission(
156+
token,
157+
AuthPermissions.READ,
158+
AuthEntities.PROJECT,
159+
PermissionOn.PROJECT,
160+
projectDto.projectId,
161+
null,
162+
null
163+
)
164+
) {
165+
ResponseEntity.ok(projectDto)
166+
} else {
167+
throw AuthorizationFailedException("The token does not have permission for the project ${projectDto.projectId}")
168+
}
169+
} ?: ResponseEntity.ok(projectDto)
170+
}
171+
172+
173+
@Authorized(permission = AuthPermissions.READ, entity = AuthEntities.SUBJECT, permissionOn = PermissionOn.PROJECT)
174+
@GetMapping("/" + PathsUtil.PROJECT_PATH + "/" + PathsUtil.PROJECT_ID_CONSTANT)
175+
fun getProjectsUsingProjectId(@PathVariable projectId: String): ResponseEntity<ProjectDto> {
176+
return ResponseEntity.ok(projectService.getProjectByProjectId(projectId))
177+
}
178+
179+
}

0 commit comments

Comments
 (0)