Better email validation for user and partial update for /auth/update

This commit is contained in:
Claudio Maggioni 2020-02-27 09:33:03 +01:00
parent 48441dfe07
commit 40785d0cf1
3 changed files with 13 additions and 13 deletions

View file

@ -49,12 +49,15 @@ public class AuthenticationController {
return user; return user;
} }
@PutMapping("/update") @PatchMapping("/update")
public User update(@Valid @RequestBody final UserUpdateRequest u, final Principal principal) { public User update(@Valid @RequestBody final UserUpdateRequest u, final Principal principal) {
final User oldUser = userRepository.findByUsername(principal.getName()); final User oldUser = userRepository.findByUsername(principal.getName());
oldUser.setName(u.getName()); if (u.getName() != null) oldUser.setName(u.getName());
if (u.getEmail() != null) {
oldUser.setEmail(u.getEmail()); oldUser.setEmail(u.getEmail());
oldUser.setPassword(encoder.encode(u.getPassword())); // TODO: handle email verification
}
if (u.getPassword() != null) oldUser.setPassword(encoder.encode(u.getPassword()));
return userRepository.save(oldUser); return userRepository.save(oldUser);
} }

View file

@ -1,7 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern; import javax.validation.constraints.Pattern;
public class UserUpdateRequest { public class UserUpdateRequest {
@ -10,7 +10,6 @@ public class UserUpdateRequest {
private String name; private String name;
/** A non-salted password */ /** A non-salted password */
@NotNull
@NotEmpty(message = "Please provide a password") @NotEmpty(message = "Please provide a password")
private String password; private String password;
@ -18,11 +17,9 @@ public class UserUpdateRequest {
* The user's email (validated according to criteria used in <code>&gt;input type="email"&lt;> * The user's email (validated according to criteria used in <code>&gt;input type="email"&lt;>
* </code>, technically not RFC 5322 compliant * </code>, technically not RFC 5322 compliant
*/ */
@NotNull
@NotEmpty(message = "Please provide an email") @NotEmpty(message = "Please provide an email")
@Pattern( @Email(message = "Please provide a valid email address")
message = "Please provide a valid email", @Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address")
regexp = "/^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$/")
private String email; private String email;
public String getName() { public String getName() {

View file

@ -2,6 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern; import javax.validation.constraints.Pattern;
@ -40,9 +41,8 @@ public class User {
@Column(nullable = false) @Column(nullable = false)
@NotNull @NotNull
@NotEmpty(message = "Please provide an email") @NotEmpty(message = "Please provide an email")
@Pattern( @Email(message = "Please provide a valid email address")
message = "Please provide a valid email", @Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address")
regexp = "^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$")
private String email; private String email;
/** All rooms in the user's house */ /** All rooms in the user's house */