4141import org .eclipse .sw360 .datahandler .thrift .components .Release ;
4242import org .eclipse .sw360 .datahandler .thrift .components .ReleaseLink ;
4343import org .eclipse .sw360 .datahandler .thrift .projects .Project ;
44+ import org .eclipse .sw360 .datahandler .thrift .users .RequestedAction ;
4445import org .eclipse .sw360 .datahandler .thrift .users .User ;
4546import org .eclipse .sw360 .datahandler .thrift .vendors .Vendor ;
4647import org .eclipse .sw360 .datahandler .thrift .vulnerabilities .VulnerabilityDTO ;
9596import java .util .stream .Collectors ;
9697
9798import static org .eclipse .sw360 .datahandler .common .WrappedException .wrapSW360Exception ;
99+ import static org .eclipse .sw360 .datahandler .permissions .PermissionUtils .makePermission ;
98100import static org .springframework .hateoas .server .mvc .WebMvcLinkBuilder .linkTo ;
99101
100102@ BasePathAwareController
@@ -108,6 +110,8 @@ public class ComponentController implements RepresentationModelProcessor<Reposit
108110 private static final Logger log = LogManager .getLogger (ComponentController .class );
109111 private static final ImmutableMap <String , String > RESPONSE_BODY_FOR_MODERATION_REQUEST = ImmutableMap .<String , String >builder ()
110112 .put ("message" , "Moderation request is created" ).build ();
113+ private static final ImmutableMap <String , String > RESPONSE_BODY_FOR_MODERATION_REQUEST_WITH_COMMIT = ImmutableMap .<String , String >builder ()
114+ .put ("message" , "Unauthorized user or empty commit message passed." ).build ();
111115
112116 @ NonNull
113117 private final Sw360ComponentService componentService ;
@@ -383,12 +387,20 @@ public ResponseEntity<EntityModel<Component>> patchComponent(
383387 @ Parameter (description = "The id of the component to be updated." )
384388 @ PathVariable ("id" ) String id ,
385389 @ Parameter (description = "The component with updated fields." )
386- @ RequestBody ComponentDTO updateComponentDto
390+ @ RequestBody ComponentDTO updateComponentDto ,
391+ @ Parameter (description = "Comment message." )
392+ @ RequestParam (value = "comment" , required = false ) String comment
387393 ) throws TException {
388394 User user = restControllerHelper .getSw360UserFromAuthentication ();
389395 Component sw360Component = componentService .getComponentForUserById (id , user );
396+ user .setCommentMadeDuringModerationRequest (comment );
397+ if (!restControllerHelper .isWriteActionAllowed (sw360Component , user )
398+ && (comment == null || comment .isBlank ())) {
399+ return new ResponseEntity (RESPONSE_BODY_FOR_MODERATION_REQUEST_WITH_COMMIT , HttpStatus .BAD_REQUEST );
400+ }
390401 if (updateComponentDto .getAttachments () != null ) {
391- updateComponentDto .getAttachments ().forEach (attachment -> wrapSW360Exception (() -> this .attachmentService .fillCheckedAttachmentData (attachment , user )));
402+ updateComponentDto .getAttachments ().forEach (attachment -> wrapSW360Exception (
403+ () -> this .attachmentService .fillCheckedAttachmentData (attachment , user )));
392404 }
393405 sw360Component = this .restControllerHelper .updateComponent (sw360Component , updateComponentDto );
394406 RequestStatus updateComponentStatus = componentService .updateComponent (sw360Component , user );
@@ -516,18 +528,27 @@ public ResponseEntity<EntityModel<Attachment>> patchComponentAttachmentInfo(
516528 @ Parameter (description = "The id of the attachment." )
517529 @ PathVariable ("attachmentId" ) String attachmentId ,
518530 @ Parameter (description = "The attachment info to be updated." )
519- @ RequestBody Attachment attachmentData
531+ @ RequestBody Attachment attachmentData ,
532+ @ Parameter (description = "Comment message." )
533+ @ RequestParam (value = "comment" , required = false ) String comment
534+
520535 ) throws TException {
521536 final User sw360User = restControllerHelper .getSw360UserFromAuthentication ();
522537 final Component sw360Component = componentService .getComponentForUserById (id , sw360User );
523538 Set <Attachment > attachments = sw360Component .getAttachments ();
524- Attachment updatedAttachment = attachmentService .updateAttachment (attachments , attachmentData , attachmentId , sw360User );
539+ sw360User .setCommentMadeDuringModerationRequest (comment );
540+ Attachment updatedAttachment = attachmentService .updateAttachment (attachments , attachmentData , attachmentId ,
541+ sw360User );
525542 RequestStatus updateComponentStatus = componentService .updateComponent (sw360Component , sw360User );
526- if (updateComponentStatus == RequestStatus .SENT_TO_MODERATOR ) {
527- return new ResponseEntity (RESPONSE_BODY_FOR_MODERATION_REQUEST , HttpStatus .ACCEPTED );
543+ if (!restControllerHelper .isWriteActionAllowed (sw360Component , sw360User ) && comment == null ) {
544+ return new ResponseEntity (RESPONSE_BODY_FOR_MODERATION_REQUEST_WITH_COMMIT , HttpStatus .BAD_REQUEST );
545+ } else {
546+ if (updateComponentStatus == RequestStatus .SENT_TO_MODERATOR ) {
547+ return new ResponseEntity (RESPONSE_BODY_FOR_MODERATION_REQUEST , HttpStatus .ACCEPTED );
548+ }
549+ EntityModel <Attachment > attachmentResource = EntityModel .of (updatedAttachment );
550+ return new ResponseEntity <>(attachmentResource , HttpStatus .OK );
528551 }
529- EntityModel <Attachment > attachmentResource = EntityModel .of (updatedAttachment );
530- return new ResponseEntity <>(attachmentResource , HttpStatus .OK );
531552 }
532553
533554 @ Operation (
@@ -563,10 +584,16 @@ public ResponseEntity<HalResource> addAttachmentToComponent(
563584 @ Parameter (description = "The file to be uploaded." )
564585 @ RequestPart ("file" ) MultipartFile file ,
565586 @ Parameter (description = "The attachment info to be created." )
566- @ RequestPart ("attachment" ) Attachment newAttachment
587+ @ RequestPart ("attachment" ) Attachment newAttachment ,
588+ @ Parameter (description = "Comment message." )
589+ @ RequestParam (value = "comment" , required = false ) String comment
567590 ) throws TException {
568591 final User sw360User = restControllerHelper .getSw360UserFromAuthentication ();
569592 final Component component = componentService .getComponentForUserById (componentId , sw360User );
593+ sw360User .setCommentMadeDuringModerationRequest (comment );
594+ if (!restControllerHelper .isWriteActionAllowed (component , sw360User ) && comment == null ) {
595+ return new ResponseEntity (RESPONSE_BODY_FOR_MODERATION_REQUEST_WITH_COMMIT , HttpStatus .BAD_REQUEST );
596+ }
570597 Attachment attachment = null ;
571598 try {
572599 attachment = attachmentService .uploadAttachment (file , newAttachment , sw360User );
@@ -641,10 +668,15 @@ public void downloadAttachmentBundleFromComponent(
641668 @ DeleteMapping (COMPONENTS_URL + "/{componentId}/attachments/{attachmentIds}" )
642669 public ResponseEntity <HalResource <Component >> deleteAttachmentsFromComponent (
643670 @ PathVariable ("componentId" ) String componentId ,
644- @ PathVariable ("attachmentIds" ) List <String > attachmentIds ) throws TException {
671+ @ PathVariable ("attachmentIds" ) List <String > attachmentIds ,
672+ @ Parameter (description = "Comment message." )
673+ @ RequestParam (value = "comment" , required = false ) String comment ) throws TException {
645674 User user = restControllerHelper .getSw360UserFromAuthentication ();
646675 Component component = componentService .getComponentForUserById (componentId , user );
647-
676+ user .setCommentMadeDuringModerationRequest (comment );
677+ if (!restControllerHelper .isWriteActionAllowed (component , user ) && comment == null ) {
678+ return new ResponseEntity (RESPONSE_BODY_FOR_MODERATION_REQUEST_WITH_COMMIT , HttpStatus .BAD_REQUEST );
679+ }
648680 Set <Attachment > attachmentsToDelete = attachmentService .filterAttachmentsToRemove (Source .componentId (componentId ),
649681 component .getAttachments (), attachmentIds );
650682 if (attachmentsToDelete .isEmpty ()) {
0 commit comments