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;
}
@PutMapping("/update")
@PatchMapping("/update")
public User update(@Valid @RequestBody final UserUpdateRequest u, final Principal principal) {
final User oldUser = userRepository.findByUsername(principal.getName());
oldUser.setName(u.getName());
oldUser.setEmail(u.getEmail());
oldUser.setPassword(encoder.encode(u.getPassword()));
if (u.getName() != null) oldUser.setName(u.getName());
if (u.getEmail() != null) {
oldUser.setEmail(u.getEmail());
// TODO: handle email verification
}
if (u.getPassword() != null) oldUser.setPassword(encoder.encode(u.getPassword()));
return userRepository.save(oldUser);
}

View file

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

View file

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