|
19 | 19 | package org.dependencytrack.resources.v1;
|
20 | 20 |
|
21 | 21 | import alpine.common.logging.Logger;
|
| 22 | +import alpine.model.Team; |
22 | 23 | import alpine.persistence.PaginatedResult;
|
23 | 24 | import alpine.server.auth.PermissionRequired;
|
24 | 25 | import alpine.server.resources.AlpineResource;
|
|
35 | 36 | import org.dependencytrack.model.NotificationRule;
|
36 | 37 | import org.dependencytrack.model.Project;
|
37 | 38 | import org.dependencytrack.notification.NotificationScope;
|
| 39 | +import org.dependencytrack.notification.publisher.DefaultNotificationPublishers; |
38 | 40 | import org.dependencytrack.persistence.QueryManager;
|
39 | 41 |
|
40 | 42 | import javax.validation.Validator;
|
@@ -255,4 +257,92 @@ public Response removeProjectFromRule(
|
255 | 257 | return Response.status(Response.Status.NOT_MODIFIED).build();
|
256 | 258 | }
|
257 | 259 | }
|
| 260 | + |
| 261 | + @POST |
| 262 | + @Path("/{ruleUuid}/team/{teamUuid}") |
| 263 | + @Consumes(MediaType.APPLICATION_JSON) |
| 264 | + @Produces(MediaType.APPLICATION_JSON) |
| 265 | + @ApiOperation( |
| 266 | + value = "Adds a team to a notification rule", |
| 267 | + response = NotificationRule.class |
| 268 | + ) |
| 269 | + @ApiResponses(value = { |
| 270 | + @ApiResponse(code = 304, message = "The rule already has the specified team assigned"), |
| 271 | + @ApiResponse(code = 401, message = "Unauthorized"), |
| 272 | + @ApiResponse(code = 404, message = "The notification rule or team could not be found") |
| 273 | + }) |
| 274 | + @PermissionRequired(Permissions.Constants.SYSTEM_CONFIGURATION) |
| 275 | + public Response addTeamToRule( |
| 276 | + @ApiParam(value = "The UUID of the rule to add a team to", required = true) |
| 277 | + @PathParam("ruleUuid") String ruleUuid, |
| 278 | + @ApiParam(value = "The UUID of the team to add to the rule", required = true) |
| 279 | + @PathParam("teamUuid") String teamUuid) { |
| 280 | + try (QueryManager qm = new QueryManager()) { |
| 281 | + final NotificationRule rule = qm.getObjectByUuid(NotificationRule.class, ruleUuid); |
| 282 | + if (rule == null) { |
| 283 | + return Response.status(Response.Status.NOT_FOUND).entity("The notification rule could not be found.").build(); |
| 284 | + } |
| 285 | + if (rule.getScope() != NotificationScope.PORTFOLIO) { |
| 286 | + return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Team subscriptions are only possible on notification rules with PORTFOLIO scope.").build(); |
| 287 | + } |
| 288 | + if (!rule.getPublisher().getName().equals(DefaultNotificationPublishers.EMAIL.getPublisherName())) { |
| 289 | + return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Team subscriptions are only possible on notification rules with EMAIL publisher.").build(); |
| 290 | + } |
| 291 | + final Team team = qm.getObjectByUuid(Team.class, teamUuid); |
| 292 | + if (team == null) { |
| 293 | + return Response.status(Response.Status.NOT_FOUND).entity("The team could not be found.").build(); |
| 294 | + } |
| 295 | + final List<Team> teams = rule.getTeams(); |
| 296 | + if (teams != null && !teams.contains(team)) { |
| 297 | + rule.getTeams().add(team); |
| 298 | + qm.persist(rule); |
| 299 | + return Response.ok(rule).build(); |
| 300 | + } |
| 301 | + return Response.status(Response.Status.NOT_MODIFIED).build(); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + @DELETE |
| 306 | + @Path("/{ruleUuid}/team/{teamUuid}") |
| 307 | + @Consumes(MediaType.APPLICATION_JSON) |
| 308 | + @Produces(MediaType.APPLICATION_JSON) |
| 309 | + @ApiOperation( |
| 310 | + value = "Removes a team from a notification rule", |
| 311 | + response = NotificationRule.class |
| 312 | + ) |
| 313 | + @ApiResponses(value = { |
| 314 | + @ApiResponse(code = 304, message = "The rule does not have the specified team assigned"), |
| 315 | + @ApiResponse(code = 401, message = "Unauthorized"), |
| 316 | + @ApiResponse(code = 404, message = "The notification rule or team could not be found") |
| 317 | + }) |
| 318 | + @PermissionRequired(Permissions.Constants.SYSTEM_CONFIGURATION) |
| 319 | + public Response removeTeamFromRule( |
| 320 | + @ApiParam(value = "The UUID of the rule to remove the project from", required = true) |
| 321 | + @PathParam("ruleUuid") String ruleUuid, |
| 322 | + @ApiParam(value = "The UUID of the project to remove from the rule", required = true) |
| 323 | + @PathParam("teamUuid") String teamUuid) { |
| 324 | + try (QueryManager qm = new QueryManager()) { |
| 325 | + final NotificationRule rule = qm.getObjectByUuid(NotificationRule.class, ruleUuid); |
| 326 | + if (rule == null) { |
| 327 | + return Response.status(Response.Status.NOT_FOUND).entity("The notification rule could not be found.").build(); |
| 328 | + } |
| 329 | + if (rule.getScope() != NotificationScope.PORTFOLIO) { |
| 330 | + return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Team subscriptions are only possible on notification rules with PORTFOLIO scope.").build(); |
| 331 | + } |
| 332 | + if (!rule.getPublisher().getName().equals(DefaultNotificationPublishers.EMAIL.getPublisherName())) { |
| 333 | + return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Team subscriptions are only possible on notification rules with EMAIL publisher.").build(); |
| 334 | + } |
| 335 | + final Team team = qm.getObjectByUuid(Team.class, teamUuid); |
| 336 | + if (team == null) { |
| 337 | + return Response.status(Response.Status.NOT_FOUND).entity("The team could not be found.").build(); |
| 338 | + } |
| 339 | + final List<Team> teams = rule.getTeams(); |
| 340 | + if (teams != null && teams.contains(team)) { |
| 341 | + rule.getTeams().remove(team); |
| 342 | + qm.persist(rule); |
| 343 | + return Response.ok(rule).build(); |
| 344 | + } |
| 345 | + return Response.status(Response.Status.NOT_MODIFIED).build(); |
| 346 | + } |
| 347 | + } |
258 | 348 | }
|
0 commit comments