Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
<artifactId>spring-session-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-mail</artifactId>-->
<!--</dependency>-->

<!-- JAXB -->
<dependency>
Expand Down
47 changes: 0 additions & 47 deletions src/main/java/com/example/sweater/config/MailConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ public String registration() {
@PostMapping("/registration")
public String addUser(
@RequestParam("password2") String passwordConfirm,
@RequestParam("g-recaptcha-response") String captchaResponce,
// @RequestParam("g-recaptcha-response") String captchaResponce,
@Valid User user,
BindingResult bindingResult,
Model model
) {
String url = String.format(CAPTCHA_URL, secret, captchaResponce);
CaptchaResponseDto response = restTemplate.postForObject(url, Collections.emptyList(), CaptchaResponseDto.class);
// String url = String.format(CAPTCHA_URL, secret, captchaResponce);
// CaptchaResponseDto response = restTemplate.postForObject(url, Collections.emptyList(), CaptchaResponseDto.class);

if (!response.isSuccess()) {
model.addAttribute("captchaError", "Fill captcha");
}
// if (!response.isSuccess()) {
// model.addAttribute("captchaError", "Fill captcha");
// }

boolean isConfirmEmpty = StringUtils.isEmpty(passwordConfirm);

Expand All @@ -60,9 +60,10 @@ public String addUser(

if (user.getPassword() != null && !user.getPassword().equals(passwordConfirm)) {
model.addAttribute("passwordError", "Passwords are different!");
return "registration";
}

if (isConfirmEmpty || bindingResult.hasErrors() || !response.isSuccess()) {
if (isConfirmEmpty || bindingResult.hasErrors()) {
Map<String, String> errors = ControllerUtils.getErrors(bindingResult);

model.mergeAttributes(errors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,16 @@ public String userSave(
@GetMapping("profile")
public String getProfile(Model model, @AuthenticationPrincipal User user) {
model.addAttribute("username", user.getUsername());
model.addAttribute("email", user.getEmail());

return "profile";
}

@PostMapping("profile")
public String updateProfile(
@AuthenticationPrincipal User user,
@RequestParam String password,
@RequestParam String email
@RequestParam String password
) {
userSevice.updateProfile(user, password, email);
userSevice.updateProfile(user, password);

return "redirect:/user/profile";
}
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/example/sweater/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -23,9 +22,6 @@ public class User implements UserDetails {
private String password;
private boolean active;

@Email(message = "Email is not correct")
@NotBlank(message = "Email cannot be empty")
private String email;
private String activationCode;

@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
Expand Down Expand Up @@ -135,14 +131,6 @@ public void setRoles(Set<Role> roles) {
this.roles = roles;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getActivationCode() {
return activationCode;
}
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/com/example/sweater/service/MailSender.java

This file was deleted.

36 changes: 1 addition & 35 deletions src/main/java/com/example/sweater/service/UserSevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public class UserSevice implements UserDetailsService {
@Autowired
private UserRepo userRepo;

@Autowired
private MailSender mailSender;

@Autowired
private PasswordEncoder passwordEncoder;

Expand Down Expand Up @@ -54,25 +51,9 @@ public boolean addUser(User user) {

userRepo.save(user);

sendMessage(user);

return true;
}

private void sendMessage(User user) {
if (!StringUtils.isEmpty(user.getEmail())) {
String message = String.format(
"Hello, %s! \n" +
"Welcome to Sweater. Please, visit next link: http://%s/activate/%s",
user.getUsername(),
hostname,
user.getActivationCode()
);

mailSender.send(user.getEmail(), "Activation code", message);
}
}

public boolean activateUser(String code) {
User user = userRepo.findByActivationCode(code);

Expand Down Expand Up @@ -109,29 +90,14 @@ public void saveUser(User user, String username, Map<String, String> form) {
userRepo.save(user);
}

public void updateProfile(User user, String password, String email) {
String userEmail = user.getEmail();

boolean isEmailChanged = (email != null && !email.equals(userEmail)) ||
(userEmail != null && !userEmail.equals(email));

if (isEmailChanged) {
user.setEmail(email);

if (!StringUtils.isEmpty(email)) {
user.setActivationCode(UUID.randomUUID().toString());
}
}
public void updateProfile(User user, String password) {

if (!StringUtils.isEmpty(password)) {
user.setPassword(password);
}

userRepo.save(user);

if (isEmailChanged) {
sendMessage(user);
}
}

public void subscribe(User currentUser, User user) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring.datasource.url=jdbc:postgresql://localhost/sweater
spring.datasource.username=postgres
spring.datasource.password=123
spring.datasource.password=postgres
spring.jpa.generate-ddl=false
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=validate
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring.datasource.url=jdbc:postgresql://localhost/sweater
spring.datasource.username=postgres
spring.datasource.password=123
spring.datasource.password=postgres
spring.jpa.generate-ddl=false
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=validate
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/db/migration/V6__Drop_email_column.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE usr
DROP COLUMN email;
2 changes: 1 addition & 1 deletion src/main/resources/templates/parts/common.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous" />
<script src='https://www.google.com/recaptcha/api.js'></script>
<#--<script src='https://www.google.com/recaptcha/api.js'></script>-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/turbolinks/5.2.0/turbolinks.js"></script>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
Expand Down
29 changes: 8 additions & 21 deletions src/main/resources/templates/parts/login.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,14 @@
</#if>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Email:</label>
<div class="col-sm-6">
<input type="email" name="email" value="<#if user??>${user.email}</#if>"
class="form-control ${(emailError??)?string('is-invalid', '')}"
placeholder="some@some.com" />
<#if emailError??>
<div class="invalid-feedback">
${emailError}
</div>
</#if>
</div>
</div>
<div class="col-sm-6">
<div class="g-recaptcha" data-sitekey="6LduQVoUAAAAAD8hypySNroht_6UnzhoQRV3QIWc"></div>
<#if captchaError??>
<div class="alert alert-danger" role="alert">
${captchaError}
</div>
</#if>
</div>
<#--<div class="col-sm-6">-->
<#--<div class="g-recaptcha" data-sitekey="6LduQVoUAAAAAD8hypySNroht_6UnzhoQRV3QIWc"></div>-->
<#--<#if captchaError??>-->
<#--<div class="alert alert-danger" role="alert">-->
<#--${captchaError}-->
<#--</div>-->
<#--</#if>-->
<#--</div>-->
</#if>
<input type="hidden" name="_csrf" value="${_csrf.token}" />
<#if !isRegisterForm><a href="/registration">Add new user</a></#if>
Expand Down
6 changes: 0 additions & 6 deletions src/main/resources/templates/profile.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ ${message?ifExists}
<input type="password" name="password" class="form-control" placeholder="Password" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Email:</label>
<div class="col-sm-6">
<input type="email" name="email" class="form-control" placeholder="some@some.com" value="${email!''}" />
</div>
</div>
<input type="hidden" name="_csrf" value="${_csrf.token}" />
<button class="btn btn-primary" type="submit">Save</button>
</form>
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/com/example/sweater/LoginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
//import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Expand Down Expand Up @@ -42,13 +42,13 @@ public void accessDeniedTest() throws Exception {
.andExpect(redirectedUrl("http://localhost/login"));
}

@Test
@Sql(value = {"/create-user-before.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public void correctLoginTest() throws Exception {
this.mockMvc.perform(formLogin().user("dru").password("1"))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/"));
}
// @Test
// @Sql(value = {"/create-user-before.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
// public void correctLoginTest() throws Exception {
// this.mockMvc.perform(formLogin().user("dru").password("1"))
// .andExpect(status().is3xxRedirection())
// .andExpect(redirectedUrl("/"));
// }

@Test
public void badCredentials() throws Exception {
Expand Down
Loading