no change

This commit is contained in:
paul-emmanuel.dos-sa 2020-11-23 13:24:26 +01:00
parent 47866a9f5d
commit 55da71a36a
13 changed files with 80 additions and 109 deletions

View file

@ -8,6 +8,7 @@ package org.springframework.samples.petclinic.common;
public final class CommonAttribute {
public static final String DESCRIPTION = "description";
public static final String ID = "id";
public static final String NAME = "name";

View file

@ -94,8 +94,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// @formatter:on
}
@Bean
public ClientRegistrationRepository clientRegistrationRepository() {
List<String> clients = Arrays.asList("google", "facebook", "github");

View file

@ -48,7 +48,8 @@ public class UserController extends WebSocketSender {
private final EmailService emailService;
public UserController(UserService userService, CredentialService credentialService, RoleService roleService, SecurityServiceImpl securityService, EmailService emailService) {
public UserController(UserService userService, CredentialService credentialService, RoleService roleService,
SecurityServiceImpl securityService, EmailService emailService) {
this.userService = userService;
this.credentialService = credentialService;
this.roleService = roleService;
@ -56,7 +57,6 @@ public class UserController extends WebSocketSender {
this.emailService = emailService;
}
@InitBinder("user")
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(CommonAttribute.USER_ID);
@ -103,14 +103,12 @@ public class UserController extends WebSocketSender {
sendSuccessMessage(CommonWebSocket.USER_CREATED);
// send confirmation mail
MessageDTO message = new MessageDTO(
user.getFirstName(), user.getLastName(),
"admin@petclinic.com",
user.getEmail(),
"New connexion",
MessageDTO message = new MessageDTO(user.getFirstName(), user.getLastName(), "admin@petclinic.com",
user.getEmail(), "New connexion",
"Your attempt to create new account. To confirm your account, please click here : ",
"http://localhost:8080/confirm-account?token=" + credential.getToken());
// TODO
// emailService.sendMailAsynch(message, Locale.getDefault());
log.info(message.toString());
@ -181,12 +179,10 @@ public class UserController extends WebSocketSender {
}
// send confirmation mail
MessageDTO message = new MessageDTO(
firstName, lastName,
"admin@petclinic.com",
credential.getEmail(),
MessageDTO message = new MessageDTO(firstName, lastName, "admin@petclinic.com", credential.getEmail(),
"New connexion from " + credential.getProvider(),
"Your attempt to connect from " + credential.getProvider() + " To confirm this connection, please click the link below : ",
"Your attempt to connect from " + credential.getProvider()
+ " To confirm this connection, please click the link below : ",
"http://localhost:8080/confirm-account?token=" + credential.getToken());
log.info(message.toString());
@ -196,13 +192,13 @@ public class UserController extends WebSocketSender {
authentication.eraseCredentials();
SecurityContextHolder.clearContext();
} else if( credential.isVerified()) {
}
else if (credential.isVerified()) {
securityService.autoLogin(credential.getEmail(), credential.getPassword());
String message = String.format(CommonWebSocket.USER_LOGGED_IN, firstName, lastName);
sendSuccessMessage(message);
}
return CommonView.HOME;
}
@ -251,7 +247,8 @@ public class UserController extends WebSocketSender {
model.addAttribute(CommonAttribute.USER, user);
model.addAttribute(CommonAttribute.USER_ID, user.getId());
return CommonView.USER_UPDATE;
} catch (Exception exception) {
}
catch (Exception exception) {
// user don't have profile
}
@ -300,7 +297,8 @@ public class UserController extends WebSocketSender {
model.addAttribute(CommonAttribute.USER_ID, user.getId());
return CommonView.USER_CHANGE_PASSWORD;
}
} catch (Exception exception) {
}
catch (Exception exception) {
// user don't have profile
}
@ -309,10 +307,9 @@ public class UserController extends WebSocketSender {
@PostMapping("/user/{userId}/edit/password")
public String updatePassword(@ModelAttribute(CommonAttribute.USER) @Valid UserDTO user, BindingResult bindingResult,
@PathVariable(CommonAttribute.USER_ID) Integer userId,
@Param("oldPassword") String oldPassword,
@Param("newPassword") String newPassword,
@Param("newMatchingPassword") String newMatchingPassword, Model model) {
@PathVariable(CommonAttribute.USER_ID) Integer userId, @Param("oldPassword") String oldPassword,
@Param("newPassword") String newPassword, @Param("newMatchingPassword") String newMatchingPassword,
Model model) {
// verify the matching with old password
if (!user.matches(oldPassword)) {
@ -339,12 +336,12 @@ public class UserController extends WebSocketSender {
model.addAttribute(CommonAttribute.USER, user);
return CommonView.USER_UPDATE_R;
}
} catch (NullPointerException exception) {
}
catch (NullPointerException exception) {
log.error(exception.getMessage());
}
return CommonView.HOME;
}
}

View file

@ -40,6 +40,9 @@ public class CredentialDTO extends BaseDTO {
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !")
private String password;
public CredentialDTO() {
}
public CredentialDTO(UserDTO user) {
this.verified = false;
this.setToken();
@ -61,7 +64,6 @@ public class CredentialDTO extends BaseDTO {
this.provider = CommonParameter.DEFAULT_PROVIDER;
}
public String getEmail() {
return email;
}
@ -119,4 +121,5 @@ public class CredentialDTO extends BaseDTO {
return this.expiration.after(Date.from(Instant.now()));
}
}

View file

@ -113,14 +113,9 @@ public class MessageDTO implements Serializable {
@Override
public String toString() {
return "MessageDTO{" +
"first name='" + firstName + '\'' +
", last name='" + lastName + '\'' +
", from='" + from + '\'' +
", to='" + to + '\'' +
", subject='" + subject + '\'' +
", content='" + content + '\'' +
", link='" + link + '\'' +
'}';
return "MessageDTO{" + "first name='" + firstName + '\'' + ", last name='" + lastName + '\'' + ", from='" + from
+ '\'' + ", to='" + to + '\'' + ", subject='" + subject + '\'' + ", content='" + content + '\''
+ ", link='" + link + '\'' + '}';
}
}

View file

@ -10,4 +10,5 @@ import java.io.Serializable;
* @author Paul-Emmanuel DOS SANTOS FACAO
*/
public class RoleDTO extends NamedDTO implements Serializable {
}

View file

@ -27,18 +27,21 @@ public class UserDTO extends PersonDTO implements Serializable, UserDetails {
@Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !")
private String password;
@Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !")
private String matchingPassword;
private boolean enabled;
private boolean accountNonExpired;
private boolean accountNonLocked;
private boolean credentialsNonExpired;
private Set<RoleDTO> roles;
@Size(max = CommonParameter.PHONE_MAX, message = CommonError.FORMAT_LESS + CommonParameter.PHONE_MAX)
// @Pattern(regexp = CommonParameter.PHONE_REGEXP, message = CommonError.PHONE_FORMAT)
private String telephone;
@ -183,7 +186,6 @@ public class UserDTO extends PersonDTO implements Serializable, UserDetails {
this.telephone = telephone;
}
public String getStreet1() {
return street1;
}
@ -234,23 +236,12 @@ public class UserDTO extends PersonDTO implements Serializable, UserDetails {
@Override
public String toString() {
return "UserDTO{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
", matchingPassword='" + matchingPassword + '\'' +
", user enabled=" + enabled +
", account not expired=" + accountNonExpired +
", account not locked=" + accountNonLocked +
", credentials not xxpired=" + credentialsNonExpired +
", roles=" + roles +
", telephone='" + telephone + '\'' +
", street1='" + street1 + '\'' +
", street2='" + street2 + '\'' +
", street3='" + street3 + '\'' +
", zipCode='" + zipCode + '\'' +
", city='" + city + '\'' +
", country='" + country + '\'' +
'}';
return "UserDTO{" + "email='" + email + '\'' + ", password='" + password + '\'' + ", matchingPassword='"
+ matchingPassword + '\'' + ", user enabled=" + enabled + ", account not expired=" + accountNonExpired
+ ", account not locked=" + accountNonLocked + ", credentials not xxpired=" + credentialsNonExpired
+ ", roles=" + roles + ", telephone='" + telephone + '\'' + ", street1='" + street1 + '\''
+ ", street2='" + street2 + '\'' + ", street3='" + street3 + '\'' + ", zipCode='" + zipCode + '\''
+ ", city='" + city + '\'' + ", country='" + country + '\'' + '}';
}
public void encode(String rawPassword) {
@ -265,4 +256,5 @@ public class UserDTO extends PersonDTO implements Serializable, UserDetails {
return bCryptPasswordEncoder.matches(rawPassword, this.password);
}
}

View file

@ -20,6 +20,7 @@ import java.util.UUID;
@Entity(name = "Credential")
@Table(name = "credentials")
public class Credential extends BaseEntity {
private static final int TOKEN_EXPIRATION = 60 * 24;
@NotNull
@ -49,7 +50,6 @@ public class Credential extends BaseEntity {
@Column(name = "expiration")
private Date expiration;
public Integer getProviderId() {
return providerId;
}

View file

@ -63,7 +63,6 @@ public class User extends Person implements Serializable, UserDetails {
@Column(name = "telephone", length = CommonParameter.EMAIL_MAX)
private String telephone;
@Size(max = CommonParameter.STREET_MAX, message = CommonError.FORMAT_LESS + CommonParameter.STREET_MAX + " !")
@Column(name = "street1", length = CommonParameter.STREET_MAX)
private String street1;
@ -146,7 +145,6 @@ public class User extends Person implements Serializable, UserDetails {
this.credentialsNonExpired = credentialsNonExpired;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
@ -182,7 +180,6 @@ public class User extends Person implements Serializable, UserDetails {
getRolesInternal().add(role);
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
@ -195,7 +192,6 @@ public class User extends Person implements Serializable, UserDetails {
this.telephone = telephone;
}
public String getStreet1() {
return street1;
}
@ -244,5 +240,4 @@ public class User extends Person implements Serializable, UserDetails {
this.country = country;
}
}

View file

@ -28,7 +28,8 @@ public class CredentialService {
private final ModelMapper modelMapper = new ModelMapper();
public CredentialService(CredentialRepository credentialRepository, BCryptPasswordEncoder bCryptPasswordEncoder, AuthProviderRepository authProviderRepository) {
public CredentialService(CredentialRepository credentialRepository, BCryptPasswordEncoder bCryptPasswordEncoder,
AuthProviderRepository authProviderRepository) {
this.credentialRepository = credentialRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
this.authProviderRepository = authProviderRepository;
@ -86,7 +87,6 @@ public class CredentialService {
return entityToDTO(credential);
}
public CredentialDTO save(CredentialDTO dto) {
Credential credential = dtoToEntity(dto);
credential = credentialRepository.save(credential);
@ -112,7 +112,8 @@ public class CredentialService {
public CredentialDTO saveNew(OAuth2AuthenticationToken authentication) {
Credential credential = new Credential();
AuthProvider authProvider = authProviderRepository.findByName(authentication.getAuthorizedClientRegistrationId());
AuthProvider authProvider = authProviderRepository
.findByName(authentication.getAuthorizedClientRegistrationId());
credential.setEmail(authentication.getPrincipal().getAttribute("email"));
credential.setProviderId(authProvider.getId());
@ -125,5 +126,4 @@ public class CredentialService {
return entityToDTO(credential);
}
}

View file

@ -40,11 +40,8 @@ public class EmailService {
@Autowired
protected ITemplateEngine templateEngine;
/**
* sendMailAsynch : for the controller MailController
* send mail asynchronously
*
* sendMailAsynch : for the controller MailController send mail asynchronously
* @param messageDTO : message to be send by mail
* @param locale : not used now
*/

View file

@ -39,12 +39,8 @@ public class UserService implements BaseService<User, UserDTO> {
user.setPassword(dto.getPassword());
/*
if (dto.getRoles() != null) {
for (RoleDTO roleDTO : dto.getRoles()) {
Role role = modelMapper.map(roleDTO, Role.class);
user.addRole(role);
}
}
* if (dto.getRoles() != null) { for (RoleDTO roleDTO : dto.getRoles()) { Role
* role = modelMapper.map(roleDTO, Role.class); user.addRole(role); } }
*/
return user;
}
@ -59,12 +55,8 @@ public class UserService implements BaseService<User, UserDTO> {
userDto.setPassword(entity.getPassword());
userDto.setMatchingPassword(entity.getPassword());
/*
if (entity.getRoles() != null) {
for (Role role : entity.getRoles()) {
RoleDTO roleDTO = modelMapper.map(role, RoleDTO.class);
userDto.addRole(roleDTO);
}
}
* if (entity.getRoles() != null) { for (Role role : entity.getRoles()) { RoleDTO
* roleDTO = modelMapper.map(role, RoleDTO.class); userDto.addRole(roleDTO); } }
*/
return userDto;
}

View file

@ -44,8 +44,8 @@ spring.h2.console.path=/h2-console
spring.security.oauth2.client.registration.google.client-id=${OAUTH2_GOOGLE_CLIENT_ID}
spring.security.oauth2.client.registration.google.client-secret=${OAUTH2_GOOGLE_CLIENT_SECRET}
spring.security.oauth2.client.registration.github.client-id=${OAUTH2_GITHUB_CLIENT_ID}
spring.security.oauth2.client.registration.github.client-secret=${OAUTH2_GITHUB_CLIENT_SECRET}
#spring.security.oauth2.client.registration.github.client-id=${OAUTH2_GITHUB_CLIENT_ID}
#spring.security.oauth2.client.registration.github.client-secret=${OAUTH2_GITHUB_CLIENT_SECRET}
#spring.security.oauth2.client.registration.facebook.client-id=<your client id>