1818import org .springframework .http .MediaType ;
1919import org .springframework .http .ResponseEntity ;
2020import org .springframework .security .access .prepost .PreAuthorize ;
21+ import org .springframework .security .core .Authentication ;
2122import org .springframework .validation .annotation .Validated ;
2223import org .springframework .web .bind .annotation .*;
2324import org .springframework .web .multipart .MultipartFile ;
@@ -43,17 +44,18 @@ public class PropertyController {
4344 @ ApiResponse (responseCode = "413" , description = "Превышен максимальный размер запроса" ),
4445 @ ApiResponse (responseCode = "500" , description = "Внутренняя ошибка сервера" )
4546 })
46- @ PreAuthorize ("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
47+ @ PreAuthorize ("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
4748 @ SecurityRequirement (name = "JWT" )
4849 public ResponseEntity <List <ImageDTO >> uploadMultipleImages (
4950 @ PathVariable @ Parameter (description = "id объявления" , example = "10" ) long id ,
5051 @ Parameter (
5152 description = "Массив файлов фотографий" ,
5253 required = true ,
5354 content = @ Content (mediaType = MediaType .MULTIPART_FORM_DATA_VALUE , schema = @ Schema (type = "string" , format = "binary" ))
54- ) @ RequestPart ("files" ) MultipartFile [] files ) {
55-
55+ ) @ RequestPart ("files" ) MultipartFile [] files ,
56+ Authentication authentication ) {
5657
58+ propertyService .ownsPropertyOrAdmin (id , authentication .getName ());
5759 List <ImageDTO > savedImages = propertyService .uploadImages (files , id );
5860
5961 return ResponseEntity .ok (savedImages );
@@ -104,10 +106,11 @@ public ResponseEntity<PropertyDTO> findById(@Valid @PathVariable @Parameter(desc
104106 @ ApiResponse (responseCode = "422" , description = "Ошибка валидации" , content = @ Content ),
105107 @ ApiResponse (responseCode = "500" , description = "Непредвиденная ошибка со стороны сервера" , content = @ Content )
106108 })
107- @ PreAuthorize ("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
109+ @ PreAuthorize ("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
108110 @ SecurityRequirement (name = "JWT" )
109111 @ PostMapping (path = "/" , consumes = MediaType .MULTIPART_FORM_DATA_VALUE )
110- public ResponseEntity <PropertyDTO > createPropertyWithImage (@ Valid @ ModelAttribute PropertyCreateEditDTO propertyCreateEditDTO ) {
112+ public ResponseEntity <PropertyDTO > createPropertyWithImage (@ Valid @ ModelAttribute PropertyCreateEditDTO propertyCreateEditDTO , Authentication authentication ) {
113+ propertyService .allowedToCreatePropertyOrAdmin (propertyCreateEditDTO , authentication .getName ());
111114 return new ResponseEntity <>(propertyService .createWithImages (propertyCreateEditDTO ), HttpStatus .CREATED );
112115 }
113116
@@ -122,11 +125,13 @@ public ResponseEntity<PropertyDTO> createPropertyWithImage(@Valid @ModelAttribut
122125 @ ApiResponse (responseCode = "422" , description = "Ошибка валидации" , content = @ Content ),
123126 @ ApiResponse (responseCode = "500" , description = "Непредвиденная ошибка со стороны сервера" , content = @ Content )
124127 })
125- @ PreAuthorize ("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
128+ @ PreAuthorize ("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
126129 @ SecurityRequirement (name = "JWT" )
127130 @ PatchMapping (path = "/{id}" , consumes = MediaType .MULTIPART_FORM_DATA_VALUE )
128131 public ResponseEntity <PropertyDTO > updateProperty (@ Valid @ PathVariable @ Parameter (description = "id объявления" , example = "1" ) long id ,
129- @ Valid @ ModelAttribute PropertyCreateEditDTO propertyCreateEditDTO ) {
132+ @ Valid @ ModelAttribute PropertyCreateEditDTO propertyCreateEditDTO ,
133+ Authentication authentication ) {
134+ propertyService .ownsPropertyOrAdmin (id , authentication .getName ());
130135 return ResponseEntity .ok (propertyService .update (id , propertyCreateEditDTO ));
131136 }
132137
@@ -141,11 +146,13 @@ public ResponseEntity<PropertyDTO> updateProperty(@Valid @PathVariable @Paramete
141146 @ ApiResponse (responseCode = "422" , description = "Ошибка валидации" , content = @ Content ),
142147 @ ApiResponse (responseCode = "500" , description = "Непредвиденная ошибка со стороны сервера" , content = @ Content )
143148 })
144- @ PreAuthorize ("hasAuthority('ROLE_ADMIN')" )
149+ @ PreAuthorize ("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
145150 @ SecurityRequirement (name = "JWT" )
146151 @ DeleteMapping ("/{id}" )
147- public ResponseEntity <Void > deleteProperty (@ Valid @ PathVariable @ Parameter (description = "id объявления" , example = "10" ) long id ) {
148- propertyService .deleteById (id );
152+ public ResponseEntity <Void > deleteProperty (@ Valid @ PathVariable @ Parameter (description = "id объявления" , example = "10" ) long id , Authentication authentication ) {
153+ if (propertyService .ownsPropertyOrAdmin (id , authentication .getName ())) {
154+ propertyService .deleteById (id );
155+ }
149156 return ResponseEntity .noContent ().build ();
150157 }
151158}
0 commit comments