Skip to content

Commit ff2baf6

Browse files
committed
Replace User by Integer in controllers' arguments.
Addressed to #120 No functional changes.
1 parent b1a5f79 commit ff2baf6

File tree

8 files changed

+64
-45
lines changed

8 files changed

+64
-45
lines changed

src/main/java/ru/mystamps/web/config/MvcConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
import ru.mystamps.web.Url;
4545
import ru.mystamps.web.controller.converter.LinkEntityDtoGenericConverter;
46-
import ru.mystamps.web.support.spring.security.UserArgumentResolver;
46+
import ru.mystamps.web.support.spring.security.CurrentUserArgumentResolver;
4747

4848
@Configuration
4949
@EnableScheduling
@@ -93,7 +93,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
9393

9494
@Override
9595
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
96-
argumentResolvers.add(new UserArgumentResolver());
96+
argumentResolvers.add(new CurrentUserArgumentResolver());
9797
}
9898

9999
@Override

src/main/java/ru/mystamps/web/controller/CategoryController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
import ru.mystamps.web.Url;
4141
import ru.mystamps.web.controller.converter.annotation.Category;
42-
import ru.mystamps.web.entity.User;
42+
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
4343
import ru.mystamps.web.model.AddCategoryForm;
4444
import ru.mystamps.web.service.CategoryService;
4545
import ru.mystamps.web.service.SeriesService;
@@ -70,14 +70,14 @@ public AddCategoryForm showForm() {
7070
public String processInput(
7171
@Valid AddCategoryForm form,
7272
BindingResult result,
73-
User currentUser,
73+
@CurrentUser Integer currentUserId,
7474
RedirectAttributes redirectAttributes) {
7575

7676
if (result.hasErrors()) {
7777
return null;
7878
}
7979

80-
UrlEntityDto categoryUrl = categoryService.add(form, currentUser.getId());
80+
UrlEntityDto categoryUrl = categoryService.add(form, currentUserId);
8181

8282
String dstUrl = UriComponentsBuilder.fromUriString(Url.INFO_CATEGORY_PAGE)
8383
.buildAndExpand(categoryUrl.getId(), categoryUrl.getSlug())

src/main/java/ru/mystamps/web/controller/CountryController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
import ru.mystamps.web.Url;
4141
import ru.mystamps.web.controller.converter.annotation.Country;
42-
import ru.mystamps.web.entity.User;
42+
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
4343
import ru.mystamps.web.model.AddCountryForm;
4444
import ru.mystamps.web.service.CountryService;
4545
import ru.mystamps.web.service.SeriesService;
@@ -70,14 +70,14 @@ public AddCountryForm showForm() {
7070
public String processInput(
7171
@Valid AddCountryForm form,
7272
BindingResult result,
73-
User currentUser,
73+
@CurrentUser Integer currentUserId,
7474
RedirectAttributes redirectAttributes) {
7575

7676
if (result.hasErrors()) {
7777
return null;
7878
}
7979

80-
UrlEntityDto countryUrl = countryService.add(form, currentUser.getId());
80+
UrlEntityDto countryUrl = countryService.add(form, currentUserId);
8181

8282
String dstUrl = UriComponentsBuilder.fromUriString(Url.INFO_COUNTRY_PAGE)
8383
.buildAndExpand(countryUrl.getId(), countryUrl.getSlug())

src/main/java/ru/mystamps/web/controller/ErrorController.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import lombok.RequiredArgsConstructor;
3030

3131
import ru.mystamps.web.Url;
32-
import ru.mystamps.web.entity.User;
32+
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
3333
import ru.mystamps.web.service.SiteService;
3434

3535
@Controller
@@ -42,7 +42,7 @@ public class ErrorController {
4242
@RequestMapping(Url.NOT_FOUND_PAGE)
4343
public void notFound(
4444
HttpServletRequest request,
45-
User currentUser,
45+
@CurrentUser Integer currentUserId,
4646
@RequestHeader(value = "referer", required = false) String referer,
4747
@RequestHeader(value = "user-agent", required = false) String agent) {
4848

@@ -51,12 +51,7 @@ public void notFound(
5151
String ip = request.getRemoteAddr();
5252
String method = request.getMethod();
5353

54-
Integer userId = null;
55-
if (currentUser != null) {
56-
userId = currentUser.getId();
57-
}
58-
59-
siteService.logAboutAbsentPage(page, method, userId, ip, referer, agent);
54+
siteService.logAboutAbsentPage(page, method, currentUserId, ip, referer, agent);
6055
}
6156

6257
@RequestMapping(Url.INTERNAL_ERROR_PAGE)

src/main/java/ru/mystamps/web/controller/SeriesController.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import ru.mystamps.web.Url;
5353
import ru.mystamps.web.controller.converter.annotation.Category;
5454
import ru.mystamps.web.controller.converter.annotation.Country;
55-
import ru.mystamps.web.entity.User;
55+
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
5656
import ru.mystamps.web.model.AddImageForm;
5757
import ru.mystamps.web.model.AddSeriesForm;
5858
import ru.mystamps.web.service.CategoryService;
@@ -162,7 +162,7 @@ public String processInput(
162162
AddSeriesForm.ImageChecks.class }) AddSeriesForm form,
163163
BindingResult result,
164164
HttpServletRequest request,
165-
User currentUser) {
165+
@CurrentUser Integer currentUserId) {
166166

167167
if (result.hasErrors()) {
168168
// don't try to re-display file upload field
@@ -172,7 +172,7 @@ public String processInput(
172172

173173
boolean userCanAddComments =
174174
SecurityContextUtils.hasAuthority(request, "ADD_COMMENTS_TO_SERIES");
175-
Integer seriesId = seriesService.add(form, currentUser.getId(), userCanAddComments);
175+
Integer seriesId = seriesService.add(form, currentUserId, userCanAddComments);
176176

177177
return redirectTo(Url.INFO_SERIES_PAGE, seriesId);
178178
}
@@ -181,7 +181,7 @@ public String processInput(
181181
public String showInfo(
182182
@PathVariable("id") Integer seriesId,
183183
Model model,
184-
User currentUser,
184+
@CurrentUser Integer currentUserId,
185185
Locale userLocale,
186186
HttpServletResponse response)
187187
throws IOException {
@@ -209,9 +209,9 @@ public String showInfo(
209209

210210
model.addAttribute(
211211
"isSeriesInCollection",
212-
currentUser == null
212+
currentUserId == null
213213
? false
214-
: collectionService.isSeriesInCollection(currentUser.getId(), series.getId())
214+
: collectionService.isSeriesInCollection(currentUserId, series.getId())
215215
);
216216

217217
model.addAttribute(
@@ -230,7 +230,7 @@ public String processImage(
230230
BindingResult result,
231231
@PathVariable("id") Integer seriesId,
232232
Model model,
233-
User currentUser,
233+
@CurrentUser Integer currentUserId,
234234
Locale userLocale,
235235
HttpServletResponse response)
236236
throws IOException {
@@ -257,7 +257,7 @@ public String processImage(
257257

258258
model.addAttribute(
259259
"isSeriesInCollection",
260-
collectionService.isSeriesInCollection(currentUser.getId(), series.getId())
260+
collectionService.isSeriesInCollection(currentUserId, series.getId())
261261
);
262262

263263
model.addAttribute(
@@ -274,7 +274,7 @@ public String processImage(
274274
return "series/info";
275275
}
276276

277-
seriesService.addImageToSeries(form, series.getId(), currentUser.getId());
277+
seriesService.addImageToSeries(form, series.getId(), currentUserId);
278278

279279
return redirectTo(Url.INFO_SERIES_PAGE, series.getId());
280280
}
@@ -286,7 +286,7 @@ public String processImage(
286286
)
287287
public String addToCollection(
288288
@PathVariable("id") Integer seriesId,
289-
User currentUser,
289+
@CurrentUser Integer currentUserId,
290290
RedirectAttributes redirectAttributes,
291291
HttpServletResponse response)
292292
throws IOException {
@@ -302,7 +302,7 @@ public String addToCollection(
302302
return null;
303303
}
304304

305-
UrlEntityDto collection = collectionService.addToCollection(currentUser.getId(), seriesId);
305+
UrlEntityDto collection = collectionService.addToCollection(currentUserId, seriesId);
306306

307307
redirectAttributes.addFlashAttribute("justAddedSeries", true);
308308
redirectAttributes.addFlashAttribute("justAddedSeriesId", seriesId);
@@ -317,7 +317,7 @@ public String addToCollection(
317317
)
318318
public String removeFromCollection(
319319
@PathVariable("id") Integer seriesId,
320-
User currentUser,
320+
@CurrentUser Integer currentUserId,
321321
RedirectAttributes redirectAttributes,
322322
HttpServletResponse response)
323323
throws IOException {
@@ -333,8 +333,7 @@ public String removeFromCollection(
333333
return null;
334334
}
335335

336-
UrlEntityDto collection =
337-
collectionService.removeFromCollection(currentUser.getId(), seriesId);
336+
UrlEntityDto collection = collectionService.removeFromCollection(currentUserId, seriesId);
338337

339338
redirectAttributes.addFlashAttribute("justRemovedSeries", true);
340339

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.controller.converter.annotation;
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
@Target(ElementType.PARAMETER)
27+
@Retention(RetentionPolicy.RUNTIME)
28+
@Documented
29+
public @interface CurrentUser {
30+
}

src/main/java/ru/mystamps/web/support/spring/security/UserArgumentResolver.java renamed to src/main/java/ru/mystamps/web/support/spring/security/CurrentUserArgumentResolver.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
2424
import org.springframework.web.method.support.ModelAndViewContainer;
2525

26-
import ru.mystamps.web.entity.User;
26+
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
2727

28-
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
28+
public class CurrentUserArgumentResolver implements HandlerMethodArgumentResolver {
2929

3030
@Override
3131
public boolean supportsParameter(MethodParameter parameter) {
32-
return User.class.isAssignableFrom(parameter.getParameterType());
32+
return parameter.getParameterType().isAssignableFrom(Integer.class)
33+
&& parameter.hasParameterAnnotation(CurrentUser.class);
3334
}
3435

3536
@Override
@@ -39,8 +40,7 @@ public Object resolveArgument(
3940
NativeWebRequest webRequest,
4041
WebDataBinderFactory binderFactory) {
4142

42-
return SecurityContextUtils.getUser();
43+
return SecurityContextUtils.getUserId();
4344
}
4445

4546
}
46-

src/main/java/ru/mystamps/web/support/spring/security/SecurityContextUtils.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,17 @@ public static boolean hasAuthority(HttpServletRequest request, String authority)
3636
return new SecurityContextHolderAwareRequestWrapper(request, null).isUserInRole(authority);
3737
}
3838

39-
public static User getUser() {
39+
/**
40+
* @author Sergey Chechenev
41+
* @author Slava Semushin
42+
*/
43+
public static Integer getUserId() {
4044
return Optional
4145
.ofNullable(SecurityContextHolder.getContext().getAuthentication())
4246
.map(Authentication::getPrincipal)
4347
.filter(CustomUserDetails.class::isInstance)
4448
.map(CustomUserDetails.class::cast)
4549
.map(CustomUserDetails::getUser)
46-
.orElse(null);
47-
}
48-
49-
/**
50-
* @author Sergey Chechenev
51-
* @author Slava Semushin
52-
*/
53-
public static Integer getUserId() {
54-
return Optional.ofNullable(getUser())
5550
.map(User::getId)
5651
.orElse(null);
5752
}

0 commit comments

Comments
 (0)