Better email validation for user and partial update for /auth/update
This commit is contained in:
parent
48441dfe07
commit
40785d0cf1
3 changed files with 13 additions and 13 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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>>input type="email"<>
|
||||
* </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() {
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in a new issue