Skip to content

Commit 6351bb8

Browse files
committed
Change URL of the activation link to use request parameter insted of URL variable.
/account/activate/key/{key} -> /account/activate?key={key} Addressed to #514 No functional changes.
1 parent 2cef2e9 commit 6351bb8

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

src/main/java/ru/mystamps/web/Url.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ public final class Url {
4545
public static final String AUTHENTICATION_PAGE = "/account/auth";
4646
public static final String LOGIN_PAGE = "/account/login";
4747
public static final String LOGOUT_PAGE = "/account/logout";
48-
4948
public static final String ACTIVATE_ACCOUNT_PAGE = "/account/activate";
50-
public static final String ACTIVATE_ACCOUNT_PAGE_WITH_KEY = "/account/activate/key/{key}";
5149

5250
// CheckStyle: ignore LineLength for next 3 lines
5351
public static final String ADD_SERIES_PAGE = "/series/add";
@@ -76,9 +74,10 @@ public final class Url {
7674
public static final String INTERNAL_ERROR_PAGE = "/error/500";
7775

7876
// For backward compatibility
79-
public static final String INFO_CATEGORY_BY_ID_PAGE = "/category/{id}/{slug}";
80-
public static final String INFO_COUNTRY_BY_ID_PAGE = "/country/{id}/{slug}";
81-
public static final String INFO_COLLECTION_BY_ID_PAGE = "/collection/{id}/{slug}";
77+
public static final String ACTIVATE_ACCOUNT_PAGE_WITH_KEY = "/account/activate/key/{key}";
78+
public static final String INFO_CATEGORY_BY_ID_PAGE = "/category/{id}/{slug}";
79+
public static final String INFO_COUNTRY_BY_ID_PAGE = "/country/{id}/{slug}";
80+
public static final String INFO_COLLECTION_BY_ID_PAGE = "/collection/{id}/{slug}";
8281

8382
// MUST be updated when any of our resources were modified
8483
public static final String RESOURCES_VERSION = "v0.3.0";

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@
2121

2222
import javax.validation.Valid;
2323

24+
import org.apache.commons.lang3.StringUtils;
25+
2426
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
27+
import org.springframework.http.HttpStatus;
2528
import org.springframework.stereotype.Controller;
26-
import org.springframework.ui.Model;
2729
import org.springframework.validation.BindingResult;
2830
import org.springframework.validation.annotation.Validated;
2931
import org.springframework.web.bind.WebDataBinder;
3032
import org.springframework.web.bind.annotation.GetMapping;
3133
import org.springframework.web.bind.annotation.InitBinder;
3234
import org.springframework.web.bind.annotation.PathVariable;
3335
import org.springframework.web.bind.annotation.PostMapping;
36+
import org.springframework.web.bind.annotation.RequestParam;
37+
import org.springframework.web.servlet.View;
3438
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
39+
import org.springframework.web.servlet.view.RedirectView;
3540

3641
import lombok.RequiredArgsConstructor;
3742

@@ -89,20 +94,29 @@ public String processRegistrationForm(
8994
}
9095

9196
@GetMapping(Url.ACTIVATE_ACCOUNT_PAGE)
92-
public ActivateAccountForm showActivationForm() {
93-
return new ActivateAccountForm();
97+
public ActivateAccountForm showActivationForm(
98+
@RequestParam(name = "key", required = false) String activationKey) {
99+
100+
ActivateAccountForm form = new ActivateAccountForm();
101+
if (StringUtils.isNotEmpty(activationKey)) {
102+
form.setActivationKey(activationKey);
103+
}
104+
105+
return form;
94106
}
95107

96108
@GetMapping(Url.ACTIVATE_ACCOUNT_PAGE_WITH_KEY)
97-
public String showActivationFormWithKey(
109+
public View showActivationFormWithKey(
98110
@PathVariable("key") String activationKey,
99-
Model model) {
111+
RedirectAttributes redirectAttributes) {
100112

101-
ActivateAccountForm form = new ActivateAccountForm();
102-
form.setActivationKey(activationKey);
103-
model.addAttribute("activateAccountForm", form);
113+
redirectAttributes.addAttribute("key", activationKey);
114+
115+
RedirectView view = new RedirectView();
116+
view.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
117+
view.setUrl(Url.ACTIVATE_ACCOUNT_PAGE);
104118

105-
return "account/activate";
119+
return view;
106120
}
107121

108122
@PostMapping(Url.ACTIVATE_ACCOUNT_PAGE)

src/main/java/ru/mystamps/web/service/MailServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private String getTextOfActivationMail(SendUsersActivationDto activation) {
151151
String template = messageSource.getMessage("activation.text", null, activation.getLocale());
152152

153153
String activationUrl =
154-
Url.ACTIVATE_ACCOUNT_PAGE_WITH_KEY.replace("{key}", activation.getActivationKey());
154+
String.format("%s?key=%s", Url.ACTIVATE_ACCOUNT_PAGE, activation.getActivationKey());
155155

156156
Map<String, String> ctx = new HashMap<>();
157157
ctx.put("site_url", testMode ? Url.SITE : Url.PUBLIC_URL);

src/test/java/ru/mystamps/web/tests/cases/WhenAnonymousUserActivateAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void shouldHaveStandardStructure() {
7070
@Test(groups = "misc", dependsOnGroups = "std")
7171
public void activationKeyShouldBeAutoFilledFromURL() {
7272
String key = "7777744444";
73-
String url = Url.ACTIVATE_ACCOUNT_PAGE_WITH_KEY.replace("{key}", key);
73+
String url = Url.ACTIVATE_ACCOUNT_PAGE + "?key=" + key;
7474

7575
page.open(url);
7676
assertThat(page).field("activationKey").hasValue(key);

src/test/java/ru/mystamps/web/tests/cases/WhenAnonymousUserRegisterAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class WhenAnonymousUserRegisterAccount
5252
extends WhenAnyUserAtAnyPageWithForm<RegisterAccountPage> {
5353

5454
private static final Pattern ACTIVATION_LINK_REGEXP =
55-
Pattern.compile(".*/account/activate/key/[0-9a-z]{10}.*", Pattern.DOTALL);
55+
Pattern.compile(".*/account/activate\\?key=[0-9a-z]{10}.*", Pattern.DOTALL);
5656

5757
private static final int MAX_TIME_TO_WAIT_EMAIL_IN_SECONDS = 5;
5858

0 commit comments

Comments
 (0)