25
25
import org .springframework .http .HttpStatus ;
26
26
import org .springframework .http .MediaType ;
27
27
import org .springframework .http .ResponseEntity ;
28
+ import org .springframework .security .core .Authentication ;
28
29
import org .springframework .security .core .annotation .AuthenticationPrincipal ;
30
+ import org .springframework .security .core .annotation .CurrentSecurityContext ;
29
31
import org .springframework .stereotype .Controller ;
30
32
import org .springframework .ui .Model ;
31
33
import org .springframework .validation .BindingResult ;
@@ -203,6 +205,7 @@ public String showInfo(
203
205
@ PathVariable ("id" ) Integer seriesId ,
204
206
Model model ,
205
207
@ AuthenticationPrincipal CustomUserDetails currentUser ,
208
+ @ CurrentSecurityContext (expression = "authentication" ) Authentication authentication ,
206
209
Locale userLocale ,
207
210
HttpServletResponse response )
208
211
throws IOException {
@@ -214,6 +217,7 @@ public String showInfo(
214
217
215
218
String lang = LocaleUtils .getLanguageOrNull (userLocale );
216
219
boolean userCanSeeHiddenImages = SecurityContextUtils .hasAuthority (
220
+ authentication ,
217
221
Authority .VIEW_HIDDEN_IMAGES
218
222
);
219
223
Integer currentUserId = currentUser == null ? null : currentUser .getUserId ();
@@ -228,10 +232,11 @@ public String showInfo(
228
232
return null ;
229
233
}
230
234
231
- Map <String , ?> commonAttrs = prepareCommonAttrsForSeriesInfo (series , currentUserId , lang );
235
+ Map <String , ?> commonAttrs =
236
+ prepareCommonAttrsForSeriesInfo (series , currentUserId , authentication , lang );
232
237
model .addAllAttributes (commonAttrs );
233
238
234
- addSeriesSalesFormToModel (model );
239
+ addSeriesSalesFormToModel (authentication , model );
235
240
addImageFormToModel (model );
236
241
addStampsToCollectionForm (model , series );
237
242
@@ -304,6 +309,7 @@ public String replaceImageWithImageUrl(
304
309
@ PathVariable ("id" ) Integer seriesId ,
305
310
Model model ,
306
311
@ AuthenticationPrincipal CustomUserDetails currentUser ,
312
+ @ CurrentSecurityContext (expression = "authentication" ) Authentication authentication ,
307
313
Locale userLocale ,
308
314
HttpServletRequest request ,
309
315
HttpServletResponse response )
@@ -315,6 +321,7 @@ public String replaceImageWithImageUrl(
315
321
seriesId ,
316
322
model ,
317
323
currentUser ,
324
+ authentication ,
318
325
userLocale ,
319
326
request ,
320
327
response
@@ -332,6 +339,7 @@ public String processImageWithImageUrl(
332
339
@ PathVariable ("id" ) Integer seriesId ,
333
340
Model model ,
334
341
@ AuthenticationPrincipal CustomUserDetails currentUser ,
342
+ @ CurrentSecurityContext (expression = "authentication" ) Authentication authentication ,
335
343
Locale userLocale ,
336
344
HttpServletRequest request ,
337
345
HttpServletResponse response )
@@ -343,6 +351,7 @@ public String processImageWithImageUrl(
343
351
seriesId ,
344
352
model ,
345
353
currentUser ,
354
+ authentication ,
346
355
userLocale ,
347
356
request ,
348
357
response
@@ -364,6 +373,7 @@ public String processImage(
364
373
@ PathVariable ("id" ) Integer seriesId ,
365
374
Model model ,
366
375
@ AuthenticationPrincipal CustomUserDetails currentUser ,
376
+ @ CurrentSecurityContext (expression = "authentication" ) Authentication authentication ,
367
377
Locale userLocale ,
368
378
HttpServletRequest request ,
369
379
HttpServletResponse response )
@@ -376,6 +386,7 @@ public String processImage(
376
386
377
387
String lang = LocaleUtils .getLanguageOrNull (userLocale );
378
388
boolean userCanSeeHiddenImages = SecurityContextUtils .hasAuthority (
389
+ authentication ,
379
390
Authority .VIEW_HIDDEN_IMAGES
380
391
);
381
392
Integer currentUserId = currentUser .getUserId ();
@@ -392,15 +403,16 @@ public String processImage(
392
403
393
404
loadErrorsFromDownloadInterceptor (form , result , request );
394
405
395
- boolean maxQuantityOfImagesExceeded = !isAdmin () && !isAllowedToAddingImages (series );
406
+ boolean maxQuantityOfImagesExceeded = !isAdmin (authentication )
407
+ && !isAllowedToAddingImages (series );
396
408
model .addAttribute ("maxQuantityOfImagesExceeded" , maxQuantityOfImagesExceeded );
397
409
398
410
if (result .hasErrors () || maxQuantityOfImagesExceeded ) {
399
411
Map <String , ?> commonAttrs =
400
- prepareCommonAttrsForSeriesInfo (series , currentUserId , lang );
412
+ prepareCommonAttrsForSeriesInfo (series , currentUserId , authentication , lang );
401
413
model .addAllAttributes (commonAttrs );
402
414
403
- addSeriesSalesFormToModel (model );
415
+ addSeriesSalesFormToModel (authentication , model );
404
416
addStampsToCollectionForm (model , series );
405
417
406
418
// don't try to re-display file upload field
@@ -427,6 +439,7 @@ public String addToCollection(
427
439
BindingResult result ,
428
440
@ PathVariable ("id" ) Integer seriesId ,
429
441
@ AuthenticationPrincipal CustomUserDetails currentUserDetails ,
442
+ @ CurrentSecurityContext (expression = "authentication" ) Authentication authentication ,
430
443
Locale userLocale ,
431
444
RedirectAttributes redirectAttributes ,
432
445
HttpServletResponse response ,
@@ -456,6 +469,7 @@ public String addToCollection(
456
469
if (result .hasErrors ()) {
457
470
String lang = LocaleUtils .getLanguageOrNull (userLocale );
458
471
boolean userCanSeeHiddenImages = SecurityContextUtils .hasAuthority (
472
+ authentication ,
459
473
Authority .VIEW_HIDDEN_IMAGES
460
474
);
461
475
SeriesDto series = seriesService .findFullInfoById (
@@ -470,10 +484,10 @@ public String addToCollection(
470
484
}
471
485
472
486
// CheckStyle: ignore LineLength for next 1 line
473
- Map <String , ?> commonAttrs = prepareCommonAttrsForSeriesInfo (series , currentUserId , lang );
487
+ Map <String , ?> commonAttrs = prepareCommonAttrsForSeriesInfo (series , currentUserId , authentication , lang );
474
488
model .addAllAttributes (commonAttrs );
475
489
476
- addSeriesSalesFormToModel (model );
490
+ addSeriesSalesFormToModel (authentication , model );
477
491
addImageFormToModel (model );
478
492
addStampsToCollectionForm (model , series );
479
493
@@ -523,14 +537,16 @@ public String removeFromCollection(
523
537
String collectionSlug = currentUserDetails .getUserCollectionSlug ();
524
538
return redirectTo (CollectionUrl .INFO_COLLECTION_PAGE , collectionSlug );
525
539
}
526
-
540
+
541
+ @ SuppressWarnings ("checkstyle:parameternumber" )
527
542
@ PostMapping (SeriesUrl .ADD_SERIES_ASK_PAGE )
528
543
public String processAskForm (
529
544
@ Validated ({ Default .class , AddSeriesSalesForm .UrlChecks .class }) AddSeriesSalesForm form ,
530
545
BindingResult result ,
531
546
@ PathVariable ("id" ) Integer seriesId ,
532
547
Model model ,
533
548
@ AuthenticationPrincipal CustomUserDetails currentUser ,
549
+ @ CurrentSecurityContext (expression = "authentication" ) Authentication authentication ,
534
550
Locale userLocale ,
535
551
HttpServletResponse response )
536
552
throws IOException {
@@ -542,6 +558,7 @@ public String processAskForm(
542
558
543
559
String lang = LocaleUtils .getLanguageOrNull (userLocale );
544
560
boolean userCanSeeHiddenImages = SecurityContextUtils .hasAuthority (
561
+ authentication ,
545
562
Authority .VIEW_HIDDEN_IMAGES
546
563
);
547
564
Integer currentUserId = currentUser .getUserId ();
@@ -556,15 +573,16 @@ public String processAskForm(
556
573
return null ;
557
574
}
558
575
559
- boolean maxQuantityOfImagesExceeded = !isAdmin () && !isAllowedToAddingImages (series );
576
+ boolean maxQuantityOfImagesExceeded = !isAdmin (authentication )
577
+ && !isAllowedToAddingImages (series );
560
578
model .addAttribute ("maxQuantityOfImagesExceeded" , maxQuantityOfImagesExceeded );
561
579
562
580
if (result .hasErrors () || maxQuantityOfImagesExceeded ) {
563
581
Map <String , ?> commonAttrs =
564
- prepareCommonAttrsForSeriesInfo (series , currentUserId , lang );
582
+ prepareCommonAttrsForSeriesInfo (series , currentUserId , authentication , lang );
565
583
model .addAllAttributes (commonAttrs );
566
584
567
- addSeriesSalesFormToModel (model );
585
+ addSeriesSalesFormToModel (authentication , model );
568
586
addImageFormToModel (model );
569
587
addStampsToCollectionForm (model , series );
570
588
@@ -729,6 +747,7 @@ public static void loadErrorsFromDownloadInterceptor(
729
747
private Map <String , ?> prepareCommonAttrsForSeriesInfo (
730
748
SeriesDto series ,
731
749
Integer currentUserId ,
750
+ Authentication authentication ,
732
751
String lang ) {
733
752
734
753
Map <String , Object > model = new HashMap <>();
@@ -752,38 +771,39 @@ public static void loadErrorsFromDownloadInterceptor(
752
771
model .put ("solovyovNumbers" , solovyovNumbers );
753
772
model .put ("zagorskiNumbers" , zagorskiNumbers );
754
773
755
- boolean userCanAddImagesToSeries = isUserCanAddImagesToSeries (currentUserId , series );
774
+ boolean userCanAddImagesToSeries =
775
+ isUserCanAddImagesToSeries (authentication , currentUserId , series );
756
776
model .put ("allowAddingImages" , userCanAddImagesToSeries );
757
777
758
778
// we require DOWNLOAD_IMAGE and ADD_IMAGES_TO_SERIES in order to reduce
759
779
// a number of the possible cases to maintain
760
780
boolean userCanReplaceImages =
761
- SecurityContextUtils .hasAuthority (Authority .REPLACE_IMAGE )
762
- && SecurityContextUtils .hasAuthority (Authority .DOWNLOAD_IMAGE )
763
- && SecurityContextUtils .hasAuthority (Authority .ADD_IMAGES_TO_SERIES );
781
+ SecurityContextUtils .hasAuthority (authentication , Authority .REPLACE_IMAGE )
782
+ && SecurityContextUtils .hasAuthority (authentication , Authority .DOWNLOAD_IMAGE )
783
+ && SecurityContextUtils .hasAuthority (authentication , Authority .ADD_IMAGES_TO_SERIES );
764
784
model .put ("allowReplacingImages" , userCanReplaceImages );
765
785
766
- if (SecurityContextUtils .hasAuthority (Authority .UPDATE_COLLECTION )) {
786
+ if (SecurityContextUtils .hasAuthority (authentication , Authority .UPDATE_COLLECTION )) {
767
787
Map <Integer , Integer > seriesInstances =
768
788
collectionService .findSeriesInstances (currentUserId , seriesId );
769
789
model .put ("seriesInstances" , seriesInstances );
770
790
}
771
791
772
- if (SecurityContextUtils .hasAuthority (Authority .VIEW_SERIES_SALES )) {
792
+ if (SecurityContextUtils .hasAuthority (authentication , Authority .VIEW_SERIES_SALES )) {
773
793
List <SeriesSaleDto > seriesSales = seriesSalesService .findSales (seriesId );
774
794
model .put ("seriesSales" , seriesSales );
775
795
}
776
796
777
- if (SecurityContextUtils .hasAuthority (Authority .IMPORT_SERIES )) {
797
+ if (SecurityContextUtils .hasAuthority (authentication , Authority .IMPORT_SERIES )) {
778
798
ImportRequestInfo importInfo = seriesImportService .findRequestInfo (seriesId );
779
799
model .put ("importInfo" , importInfo );
780
800
}
781
801
782
802
return model ;
783
803
}
784
804
785
- private void addSeriesSalesFormToModel (Model model ) {
786
- if (!SecurityContextUtils .hasAuthority (Authority .ADD_SERIES_SALES )) {
805
+ private void addSeriesSalesFormToModel (Authentication authentication , Model model ) {
806
+ if (!SecurityContextUtils .hasAuthority (authentication , Authority .ADD_SERIES_SALES )) {
787
807
return ;
788
808
}
789
809
@@ -820,13 +840,17 @@ private static boolean isAllowedToAddingImages(SeriesDto series) {
820
840
821
841
// I like these parentheses and also ErrorProne suggests to have an explicit order
822
842
@ SuppressWarnings ("PMD.UselessParentheses" )
823
- private static boolean isUserCanAddImagesToSeries (Integer userId , SeriesDto series ) {
824
- return isAdmin ()
843
+ private static boolean isUserCanAddImagesToSeries (
844
+ Authentication authentication ,
845
+ Integer userId ,
846
+ SeriesDto series
847
+ ) {
848
+ return isAdmin (authentication )
825
849
|| (isOwner (userId , series ) && isAllowedToAddingImages (series ));
826
850
}
827
851
828
- private static boolean isAdmin () {
829
- return SecurityContextUtils .hasAuthority (Authority .ADD_IMAGES_TO_SERIES );
852
+ private static boolean isAdmin (Authentication authentication ) {
853
+ return SecurityContextUtils .hasAuthority (authentication , Authority .ADD_IMAGES_TO_SERIES );
830
854
}
831
855
832
856
@ SuppressWarnings ("PMD.UnusedNullCheckInEquals" )
0 commit comments