diff --git a/src/main/java/org/springframework/samples/petclinic/configuration/SocketSecurityConfig.java b/src/main/java/org/springframework/samples/petclinic/configuration/SocketSecurityConfig.java index 64146539a..d793513e8 100644 --- a/src/main/java/org/springframework/samples/petclinic/configuration/SocketSecurityConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/configuration/SocketSecurityConfig.java @@ -9,13 +9,8 @@ public class SocketSecurityConfig extends AbstractSecurityWebSocketMessageBroker @Override protected void configureInbound(MessageSecurityMetadataSourceRegistry message) { - // @formatter:off - - // message types other than MESSAGE and SUBSCRIBE - message.simpDestMatchers("/app/**").permitAll() - .simpSubscribeDestMatchers("/topic/**").permitAll() - // catch all - .anyMessage().denyAll(); + message.simpDestMatchers("/app/**").permitAll().simpSubscribeDestMatchers("/topic/**").permitAll().anyMessage() + .permitAll(); // @formatter:on } diff --git a/src/main/java/org/springframework/samples/petclinic/configuration/WebSecurityConfig.java b/src/main/java/org/springframework/samples/petclinic/configuration/WebSecurityConfig.java index 8bc422f6e..cd3ef47a2 100644 --- a/src/main/java/org/springframework/samples/petclinic/configuration/WebSecurityConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/configuration/WebSecurityConfig.java @@ -20,8 +20,6 @@ import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService; import org.springframework.security.oauth2.client.registration.ClientRegistration; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository; -import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.cors.CorsConfigurationSource; import javax.annotation.Resource; import java.util.Arrays; @@ -29,7 +27,7 @@ import java.util.List; import java.util.stream.Collectors; @Configuration -@EnableWebSecurity(debug = true) +@EnableWebSecurity @PropertySource("classpath:oauth2.properties") public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @@ -64,7 +62,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/login", "/logout", "/register","/confirm-account").permitAll() - .antMatchers("/websocket/**", "/topic/**", "/app/**").permitAll() + .antMatchers("/webjars/**","/websocket/**", "/topic/**", "/app/**").permitAll() .antMatchers("/resources/**").permitAll() .antMatchers("/h2-console/**").permitAll() .antMatchers("/edit/**").authenticated() diff --git a/src/main/java/org/springframework/samples/petclinic/configuration/WebSocketConfig.java b/src/main/java/org/springframework/samples/petclinic/configuration/WebSocketConfig.java index 6d4f04c0f..3c63e3b70 100644 --- a/src/main/java/org/springframework/samples/petclinic/configuration/WebSocketConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/configuration/WebSocketConfig.java @@ -3,7 +3,6 @@ package org.springframework.samples.petclinic.configuration; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; -import org.springframework.security.config.annotation.web.messaging.MessageSecurityMetadataSourceRegistry; import org.springframework.security.config.annotation.web.socket.AbstractSecurityWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; @@ -17,17 +16,6 @@ import org.springframework.web.socket.config.annotation.StompEndpointRegistry; @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer { - /* - * @Override public void registerStompEndpoints(StompEndpointRegistry - * stompEndpointRegistry) { - * stompEndpointRegistry.addEndpoint("/websocket").withSockJS(); } - * - * @Override public void configureMessageBroker(MessageBrokerRegistry registry) { - * registry.enableSimpleBroker("/topic"); - * registry.setApplicationDestinationPrefixes("/app"); } - * - */ - @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); diff --git a/src/main/java/org/springframework/samples/petclinic/controller/business/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/controller/business/OwnerController.java index d8fa50824..75677a8bd 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/business/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/business/OwnerController.java @@ -80,7 +80,7 @@ class OwnerController extends WebSocketSender { @GetMapping(CommonEndPoint.OWNERS_FIND) public String initFindForm(Map model) { model.put(CommonAttribute.OWNER, new OwnerDTO()); - sendSuccessMessage("TEST WEBSOCKET"); + return CommonView.OWNER_FIND_OWNERS; } @@ -105,13 +105,13 @@ class OwnerController extends WebSocketSender { else if (results.size() == 1) { // 1 owner found owner = results.iterator().next(); - sendSuccessMessage("TEST WEBSOCKET"); + return CommonView.OWNER_OWNERS_R + owner.getId(); } else { // multiple owners found model.put(CommonAttribute.SELECTIONS, results); - sendSuccessMessage("TEST WEBSOCKET"); + return CommonView.OWNER_OWNERS_LIST; } } @@ -120,7 +120,7 @@ class OwnerController extends WebSocketSender { public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { OwnerDTO ownerDTO = this.ownerService.findById(ownerId); model.addAttribute(CommonAttribute.OWNER, ownerDTO); - sendSuccessMessage("TEST WEBSOCKET"); + return CommonView.OWNER_CREATE_OR_UPDATE; } @@ -155,7 +155,7 @@ class OwnerController extends WebSocketSender { } modelAndView.addObject(CommonAttribute.OWNER, owner); - sendSuccessMessage("TEST WEBSOCKET"); + return modelAndView; } diff --git a/src/main/java/org/springframework/samples/petclinic/controller/business/PetController.java b/src/main/java/org/springframework/samples/petclinic/controller/business/PetController.java index 86c9d2bfe..042ba766d 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/business/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/business/PetController.java @@ -42,7 +42,7 @@ import java.util.Collection; * @author Paul-Emmanuel DOS SANTOS FACAO */ @Controller -@RequestMapping("/owners/{ownerId}") +@RequestMapping(CommonEndPoint.OWNERS_ID) // "/owners/{ownerId}") class PetController extends WebSocketSender { private final OwnerService ownerService; @@ -62,7 +62,7 @@ class PetController extends WebSocketSender { @ModelAttribute("owner") public OwnerDTO findOwner(@PathVariable("ownerId") int ownerId) { - return ownerService.findById(ownerId); + return this.ownerService.findById(ownerId); } @InitBinder("owner") @@ -110,14 +110,16 @@ class PetController extends WebSocketSender { } } - @GetMapping("/pets/{petId}/edit") + // @GetMapping("/pets/{petId}/edit") + @GetMapping(CommonEndPoint.PETS_ID_EDIT) public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) { PetDTO pet = this.petService.findById(petId); model.put(CommonAttribute.PET, pet); return CommonView.PET_CREATE_OR_UPDATE; } - @PostMapping("/pets/{petId}/edit") + // @PostMapping("/pets/{petId}/edit") + @PostMapping(CommonEndPoint.PETS_ID_EDIT) public String processUpdateForm(@ModelAttribute("pet") @Valid PetDTO pet, BindingResult result, @ModelAttribute("owner") OwnerDTO owner, ModelMap model) { if (result.hasErrors()) { diff --git a/src/main/java/org/springframework/samples/petclinic/dto/business/OwnerDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/business/OwnerDTO.java index 19c973b3a..cdb11a6ff 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/business/OwnerDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/business/OwnerDTO.java @@ -130,22 +130,4 @@ public class OwnerDTO extends PersonDTO { .append(CommonAttribute.OWNER_PHONE, this.telephone).toString(); } - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (!(o instanceof OwnerDTO)) - return false; - - OwnerDTO ownerDTO = (OwnerDTO) o; - - if (!getAddress().equals(ownerDTO.getAddress())) - return false; - if (!getCity().equals(ownerDTO.getCity())) - return false; - if (!getTelephone().equals(ownerDTO.getTelephone())) - return false; - return getPets() != null ? getPets().equals(ownerDTO.getPets()) : ownerDTO.getPets() == null; - } - } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/business/PetDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/business/PetDTO.java index 070c09091..f9f76be70 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/business/PetDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/business/PetDTO.java @@ -85,12 +85,6 @@ public class PetDTO extends NamedDTO { visit.setPetId(this.getId()); } - @Override - public String toString() { - return "PetDTO{" + "birthDate=" + birthDate + ", type=" + type + ", owner=" + owner + ", visits=" + visits - + '}'; - } - @Override public boolean equals(Object o) { if (this == o) @@ -100,15 +94,15 @@ public class PetDTO extends NamedDTO { if (!super.equals(o)) return false; - PetDTO petDTO = (PetDTO) o; + PetDTO pet = (PetDTO) o; - if (!getBirthDate().equals(petDTO.getBirthDate())) + if (!getBirthDate().equals(pet.getBirthDate())) return false; - if (!getType().equals(petDTO.getType())) + if (!getType().equals(pet.getType())) return false; - if (getOwner() != null ? !getOwner().getId().equals(petDTO.getOwner().getId()) : petDTO.getOwner() != null) + if (getOwner() != null ? !getOwner().equals(pet.getOwner()) : pet.getOwner() != null) return false; - return getVisits() != null ? getVisits().equals(petDTO.getVisits()) : petDTO.getVisits() == null; + return getVisits() != null ? getVisits().equals(pet.getVisits()) : pet.getVisits() == null; } } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/business/VisitDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/business/VisitDTO.java index e2cb486a2..35a669177 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/business/VisitDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/business/VisitDTO.java @@ -17,6 +17,7 @@ package org.springframework.samples.petclinic.dto.business; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.samples.petclinic.dto.common.BaseDTO; +import org.springframework.samples.petclinic.model.business.Visit; import javax.validation.constraints.NotEmpty; import java.time.LocalDate; @@ -74,14 +75,13 @@ public class VisitDTO extends BaseDTO { if (!(o instanceof VisitDTO)) return false; - VisitDTO visitDTO = (VisitDTO) o; + VisitDTO visit = (VisitDTO) o; - if (!getDate().equals(visitDTO.getDate())) + if (!getDate().equals(visit.getDate())) return false; - if (getDescription() != null ? !getDescription().equals(visitDTO.getDescription()) - : visitDTO.getDescription() != null) + if (!getDescription().equals(visit.getDescription())) return false; - return getPetId().equals(visitDTO.getPetId()); + return getPetId().equals(visit.getPetId()); } } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/common/BaseDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/common/BaseDTO.java index 64c3ba019..7ea6032b9 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/common/BaseDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/common/BaseDTO.java @@ -39,9 +39,14 @@ public class BaseDTO implements Serializable { return this.id == null; } - @Override - public String toString() { - return "BaseDTO{" + "id=" + id + '}'; - } + /* + * @Override public boolean equals(Object o) { if (this == o) return true; + * + * if (!(o instanceof BaseDTO)) return false; + * + * BaseDTO baseDTO = (BaseDTO) o; + * + * return new EqualsBuilder().append(getId(), baseDTO.getId()).isEquals(); } + */ } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/common/CredentialDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/common/CredentialDTO.java index 3247bfdfa..9638f4342 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/common/CredentialDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/common/CredentialDTO.java @@ -1,5 +1,9 @@ package org.springframework.samples.petclinic.dto.common; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; import org.springframework.samples.petclinic.common.CommonError; import org.springframework.samples.petclinic.common.CommonParameter; @@ -17,6 +21,10 @@ import java.util.UUID; * * @author Paul-Emmanuel DOS SANTOS FACAO */ +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor public class CredentialDTO extends BaseDTO { @NotNull @@ -40,9 +48,6 @@ 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(); @@ -52,58 +57,14 @@ public class CredentialDTO extends BaseDTO { this.password = user.getId().toString(); } - public String getProvider() { - return provider; - } - - public void setProvider(String provider) { - this.provider = provider; - } - public void setDefaultProvider() { this.provider = CommonParameter.DEFAULT_PROVIDER; } - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - public Boolean isVerified() { return verified; } - public void setVerified(Boolean verified) { - this.verified = verified; - } - - public String getToken() { - return token; - } - - public void setToken(String token) { - this.token = token; - } - - public Date getExpiration() { - return expiration; - } - - public void setExpiration(Date expiration) { - this.expiration = expiration; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - public void setExpiration() { Calendar cal = Calendar.getInstance(); cal.setTime(new Timestamp(cal.getTime().getTime())); diff --git a/src/main/java/org/springframework/samples/petclinic/dto/common/NamedDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/common/NamedDTO.java index f827a1d2c..fadf79082 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/common/NamedDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/common/NamedDTO.java @@ -33,11 +33,6 @@ public class NamedDTO extends BaseDTO { this.name = name; } - @Override - public String toString() { - return this.getName(); - } - @Override public boolean equals(Object o) { if (this == o) @@ -45,9 +40,14 @@ public class NamedDTO extends BaseDTO { if (!(o instanceof NamedDTO)) return false; - NamedDTO namedDTO = (NamedDTO) o; + NamedDTO that = (NamedDTO) o; - return getName().equals(namedDTO.getName()); + return getName().equals(that.getName()); + } + + @Override + public String toString() { + return this.getName(); } } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/common/PersonDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/common/PersonDTO.java index 7392b791c..3282bb495 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/common/PersonDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/common/PersonDTO.java @@ -15,6 +15,9 @@ */ package org.springframework.samples.petclinic.dto.common; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; + import javax.validation.constraints.NotEmpty; /** @@ -53,11 +56,11 @@ public class PersonDTO extends BaseDTO { if (!(o instanceof PersonDTO)) return false; - PersonDTO personDTO = (PersonDTO) o; + PersonDTO person = (PersonDTO) o; - if (!getFirstName().equals(personDTO.getFirstName())) + if (!getFirstName().equals(person.getFirstName())) return false; - return getLastName().equals(personDTO.getLastName()); + return getLastName().equals(person.getLastName()); } } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/common/PrivilegeDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/common/PrivilegeDTO.java index 54f0ff273..d3f80bc93 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/common/PrivilegeDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/common/PrivilegeDTO.java @@ -1,8 +1,11 @@ package org.springframework.samples.petclinic.dto.common; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import java.io.Serializable; import java.util.Collection; @@ -15,6 +18,7 @@ import java.util.Collection; @Getter @Setter @NoArgsConstructor +@AllArgsConstructor public class PrivilegeDTO implements Serializable { private Integer id; @@ -23,4 +27,15 @@ public class PrivilegeDTO implements Serializable { private Collection roles; + /* + * @Override public boolean equals(Object o) { if (this == o) return true; + * + * if (!(o instanceof PrivilegeDTO)) return false; + * + * PrivilegeDTO that = (PrivilegeDTO) o; + * + * return new EqualsBuilder().append(getId(), that.getId()).append(getName(), + * that.getName()) .append(getRoles(), that.getRoles()).isEquals(); } + */ + } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/common/RoleDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/common/RoleDTO.java index 7dee5829a..2de06a040 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/common/RoleDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/common/RoleDTO.java @@ -1,8 +1,11 @@ package org.springframework.samples.petclinic.dto.common; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.springframework.samples.petclinic.common.CommonParameter; import javax.validation.constraints.NotEmpty; @@ -19,6 +22,7 @@ import java.util.Collection; @Getter @Setter @NoArgsConstructor +@AllArgsConstructor public class RoleDTO implements Serializable { private Integer id; @@ -32,4 +36,16 @@ public class RoleDTO implements Serializable { private Collection privileges; + /* + * @Override public boolean equals(Object o) { if (this == o) return true; + * + * if (!(o instanceof RoleDTO)) return false; + * + * RoleDTO roleDTO = (RoleDTO) o; + * + * return new EqualsBuilder().append(getId(), roleDTO.getId()).append(getName(), + * roleDTO.getName()) .append(getUsers(), roleDTO.getUsers()).append(getPrivileges(), + * roleDTO.getPrivileges()).isEquals(); } + */ + } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/common/UserDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/common/UserDTO.java index 935d5549a..0d21a17dd 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/common/UserDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/common/UserDTO.java @@ -2,6 +2,8 @@ package org.springframework.samples.petclinic.dto.common; import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.springframework.samples.petclinic.common.CommonError; import org.springframework.samples.petclinic.common.CommonParameter; import org.springframework.security.core.GrantedAuthority; @@ -154,11 +156,33 @@ public class UserDTO extends PersonDTO implements Serializable, UserDetails { return getGrantedAuthorities(getPrivileges(this.roles)); } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + + if (!(o instanceof UserDTO)) + return false; + + UserDTO userDTO = (UserDTO) o; + + return new EqualsBuilder().appendSuper(super.equals(o)).append(isEnabled(), userDTO.isEnabled()) + .append(isAccountNonExpired(), userDTO.isAccountNonExpired()) + .append(isAccountNonLocked(), userDTO.isAccountNonLocked()) + .append(isCredentialsNonExpired(), userDTO.isCredentialsNonExpired()) + .append(getEmail(), userDTO.getEmail()).append(getPassword(), userDTO.getPassword()) + .append(getMatchingPassword(), userDTO.getMatchingPassword()).append(getRoles(), userDTO.getRoles()) + .append(getTelephone(), userDTO.getTelephone()).append(getStreet1(), userDTO.getStreet1()) + .append(getStreet2(), userDTO.getStreet2()).append(getStreet3(), userDTO.getStreet3()) + .append(getZipCode(), userDTO.getZipCode()).append(getCity(), userDTO.getCity()) + .append(getCountry(), userDTO.getCountry()).isEquals(); + } + @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 + + matchingPassword + '\'' + ", enabled=" + enabled + ", accountNonExpired=" + accountNonExpired + + ", accountNonLocked=" + accountNonLocked + ", credentialsNonExpired=" + credentialsNonExpired + ", roles=" + roles + ", telephone='" + telephone + '\'' + ", street1='" + street1 + '\'' + ", street2='" + street2 + '\'' + ", street3='" + street3 + '\'' + ", zipCode='" + zipCode + '\'' + ", city='" + city + '\'' + ", country='" + country + '\'' + '}'; diff --git a/src/main/java/org/springframework/samples/petclinic/model/business/Owner.java b/src/main/java/org/springframework/samples/petclinic/model/business/Owner.java index 734dca752..8a2b344f9 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/business/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/model/business/Owner.java @@ -141,7 +141,7 @@ public class Owner extends Person { @Override public String toString() { - return new ToStringCreator(this).append(CommonAttribute.ID, this.getId()) + return new ToStringCreator(this).append(CommonAttribute.OWNER_ID, this.getId()) .append(CommonAttribute.NEW, this.isNew()).append(CommonAttribute.OWNER_LAST_NAME, this.getLastName()) .append(CommonAttribute.OWNER_FIRST_NAME, this.getFirstName()) .append(CommonAttribute.OWNER_ADDRESS, this.address).append(CommonAttribute.OWNER_CITY, this.city) diff --git a/src/main/java/org/springframework/samples/petclinic/model/common/Credential.java b/src/main/java/org/springframework/samples/petclinic/model/common/Credential.java index 7d53bb20a..28e061fa3 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/common/Credential.java +++ b/src/main/java/org/springframework/samples/petclinic/model/common/Credential.java @@ -1,5 +1,7 @@ package org.springframework.samples.petclinic.model.common; +import lombok.Getter; +import lombok.Setter; import org.springframework.samples.petclinic.common.CommonError; import org.springframework.samples.petclinic.common.CommonParameter; @@ -19,6 +21,8 @@ import java.util.UUID; */ @Entity(name = "Credential") @Table(name = "credentials") +@Getter +@Setter public class Credential extends BaseEntity { private static final int TOKEN_EXPIRATION = 60 * 24; @@ -50,54 +54,10 @@ public class Credential extends BaseEntity { @Column(name = "expiration") private Date expiration; - public Integer getProviderId() { - return providerId; - } - - public void setProviderId(Integer providerId) { - this.providerId = providerId; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - public Boolean isVerified() { return verified; } - public void setVerified(Boolean verified) { - this.verified = verified; - } - - public String getToken() { - return token; - } - - public void setToken(String token) { - this.token = token; - } - - public Date getExpiration() { - return expiration; - } - - public void setExpiration(Date expirationDate) { - this.expiration = expirationDate; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - public void setExpiration() { Calendar cal = Calendar.getInstance(); cal.setTime(new Timestamp(cal.getTime().getTime())); diff --git a/src/main/java/org/springframework/samples/petclinic/model/common/Privilege.java b/src/main/java/org/springframework/samples/petclinic/model/common/Privilege.java index 70ab218b5..06fa0515a 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/common/Privilege.java +++ b/src/main/java/org/springframework/samples/petclinic/model/common/Privilege.java @@ -1,8 +1,6 @@ package org.springframework.samples.petclinic.model.common; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import org.springframework.samples.petclinic.common.CommonParameter; import javax.persistence.*; @@ -17,6 +15,7 @@ import java.util.Collection; @Getter @Setter @NoArgsConstructor +@AllArgsConstructor public class Privilege implements Serializable { @Id @@ -32,4 +31,20 @@ public class Privilege implements Serializable { @ManyToMany(mappedBy = "privileges", fetch = FetchType.EAGER) private Collection roles; + /* + * @Override public boolean equals(Object o) { if (this == o) return true; if (!(o + * instanceof Role)) return false; + * + * Privilege privilege = (Privilege) o; + * + * if (!getId().equals(privilege.getId())) return false; if + * (!getName().equals(privilege.getName())) return false; + * + * return getRoles() != null ? getRoles().equals(privilege.getRoles()) : + * privilege.getRoles() == null; } + * + * @Override public String toString() { return "Privilege{" + "id=" + id + ", name='" + * + name + '\'' + ", roles=" + roles + '}'; } + */ + } diff --git a/src/main/java/org/springframework/samples/petclinic/model/common/Role.java b/src/main/java/org/springframework/samples/petclinic/model/common/Role.java index d9e893696..587ba4851 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/common/Role.java +++ b/src/main/java/org/springframework/samples/petclinic/model/common/Role.java @@ -1,8 +1,6 @@ package org.springframework.samples.petclinic.model.common; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; import org.springframework.samples.petclinic.common.CommonParameter; @@ -24,6 +22,7 @@ import java.util.Collection; @Getter @Setter @NoArgsConstructor +@AllArgsConstructor public class Role implements Serializable { @Id @@ -46,4 +45,17 @@ public class Role implements Serializable { inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id")) private Collection privileges; + /* + * @Override public boolean equals(Object o) { if (this == o) return true; if (!(o + * instanceof Role)) return false; + * + * Role role = (Role) o; + * + * if (!getId().equals(role.getId())) return false; if + * (!getName().equals(role.getName())) return false; if (getUsers() == null && + * role.getUsers() != null) return false; if (getUsers() != null && role.getUsers() == + * null) return false; return getPrivileges() != null ? + * getPrivileges().equals(role.getPrivileges()) : role.getPrivileges() == null; } + */ + } diff --git a/src/main/java/org/springframework/samples/petclinic/model/common/User.java b/src/main/java/org/springframework/samples/petclinic/model/common/User.java index 3bbf40f89..e7e394194 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/common/User.java +++ b/src/main/java/org/springframework/samples/petclinic/model/common/User.java @@ -2,6 +2,8 @@ package org.springframework.samples.petclinic.model.common; import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.springframework.samples.petclinic.common.CommonError; import org.springframework.samples.petclinic.common.CommonParameter; import org.springframework.security.core.GrantedAuthority; @@ -154,5 +156,32 @@ public class User extends Person implements Serializable, UserDetails { return getGrantedAuthorities(getPrivileges(this.roles)); } + /* + * @Override public boolean equals(Object o) { if (this == o) return true; + * + * if (!(o instanceof User)) return false; + * + * User user = (User) o; + * + * return new EqualsBuilder().appendSuper(super.equals(o)).append(isEnabled(), + * user.isEnabled()) .append(isAccountNonExpired(), user.isAccountNonExpired()) + * .append(isAccountNonLocked(), user.isAccountNonLocked()) + * .append(isCredentialsNonExpired(), + * user.isCredentialsNonExpired()).append(getEmail(), user.getEmail()) + * .append(getPassword(), user.getPassword()).append(getRoles(), user.getRoles()) + * .append(getTelephone(), user.getTelephone()).append(getStreet1(), + * user.getStreet1()) .append(getStreet2(), user.getStreet2()).append(getStreet3(), + * user.getStreet3()) .append(getZipCode(), user.getZipCode()).append(getCity(), + * user.getCity()) .append(getCountry(), user.getCountry()).isEquals(); } + * + * @Override public int hashCode() { return new HashCodeBuilder(17, + * 37).append(getEmail()).append(getPassword()).append(isEnabled()) + * .append(isAccountNonExpired()).append(isAccountNonLocked()).append( + * isCredentialsNonExpired()) + * .append(getRoles()).append(getTelephone()).append(getStreet1()).append(getStreet2() + * ) + * .append(getStreet3()).append(getZipCode()).append(getCity()).append(getCountry()). + * toHashCode(); } + */ } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PrivilegeRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PrivilegeRepository.java new file mode 100644 index 000000000..9b6c5a6bc --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/PrivilegeRepository.java @@ -0,0 +1,51 @@ +package org.springframework.samples.petclinic.repository; + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; +import org.springframework.samples.petclinic.model.common.Privilege; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Collection; + +/** + * Repository class for Privilege domain objects All method names are + * compliant with Spring Data naming conventions so this interface can easily be extended + * for Spring + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ +public interface PrivilegeRepository extends Repository { + + /** + * Retrieve a {@link Privilege} from the data store by id. + * @param id the id to search for + * @return the {@link Privilege} if found + */ + @Query("SELECT privilege FROM Privilege privilege join fetch privilege.roles WHERE privilege.id =:id") + @Transactional(readOnly = true) + Privilege findById(@Param("id") Integer id); + + /** + * Retrieve a {@link Privilege} from the data store by id. + * @param name the name to search for + * @return the {@link Privilege} if found + */ + @Query("SELECT privilege FROM Privilege privilege left join fetch privilege.roles WHERE privilege.name =:name") + @Transactional(readOnly = true) + Privilege findByName(@Param("name") String name); + + /** + * Retrieve all {@link Privilege}s from the data store + * @return a Collection of {@link Privilege}s (or an empty Collection if none + */ + Collection findAll(); + + /** + * Save a {@link Privilege} to the data store, either inserting or updating it. + * @param privilege the {@link Privilege} to save + * @return the {@link Privilege} saved + */ + Privilege save(Privilege privilege); + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/RoleRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/RoleRepository.java index 9ef1cba52..4efd67247 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/RoleRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/RoleRepository.java @@ -43,7 +43,15 @@ public interface RoleRepository extends Repository { /** * Save a {@link Role} to the data store, either inserting or updating it. * @param role the {@link Role} to save + * @return the {@link Role} saved */ Role save(Role role); + /** + * Delete a {@link Role} to the data store. + * @param role the {@link Role} to delete + * @return the {@link Role} deleted + */ + Role delete(Role role); + } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/UserRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/UserRepository.java index 0ad63ba61..38af918c9 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/UserRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/UserRepository.java @@ -3,12 +3,10 @@ package org.springframework.samples.petclinic.repository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import org.springframework.data.repository.query.Param; -import org.springframework.samples.petclinic.model.business.Owner; import org.springframework.samples.petclinic.model.common.User; import org.springframework.transaction.annotation.Transactional; import java.util.Collection; -import java.util.List; /** * Repository class for User domain objects All method names are compliant diff --git a/src/main/java/org/springframework/samples/petclinic/service/common/PrivilegeService.java b/src/main/java/org/springframework/samples/petclinic/service/common/PrivilegeService.java new file mode 100644 index 000000000..02fed4c52 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/service/common/PrivilegeService.java @@ -0,0 +1,95 @@ +package org.springframework.samples.petclinic.service.common; + +import org.modelmapper.ModelMapper; +import org.springframework.samples.petclinic.dto.common.PrivilegeDTO; +import org.springframework.samples.petclinic.model.common.Privilege; +import org.springframework.samples.petclinic.repository.PrivilegeRepository; +import org.springframework.samples.petclinic.service.business.BaseService; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Simple Service between Privilege entity and PrivilegeDTO Data Transfert Object. + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ +@Service("PrivilegeService") +public class PrivilegeService implements BaseService { + + private final PrivilegeRepository privilegeRepository; + + private final ModelMapper modelMapper = new ModelMapper(); + + public PrivilegeService(PrivilegeRepository privilegeRepository) { + this.privilegeRepository = privilegeRepository; + } + + @Override + public Privilege dtoToEntity(PrivilegeDTO dto) { + if (dto != null) { + return modelMapper.map(dto, Privilege.class); + } + + return new Privilege(); + } + + @Override + public PrivilegeDTO entityToDTO(Privilege entity) { + if (entity != null) { + return modelMapper.map(entity, PrivilegeDTO.class); + } + + return new PrivilegeDTO(); + } + + @Override + public List entitiesToDTOS(List entities) { + List dtos = new ArrayList<>(); + + entities.forEach(entity -> dtos.add(entityToDTO(entity))); + + return dtos; + } + + @Override + public List dtosToEntities(List dtos) { + List entities = new ArrayList<>(); + + dtos.forEach(dto -> entities.add(dtoToEntity(dto))); + + return entities; + } + + @Override + public PrivilegeDTO findById(int id) { + return entityToDTO(privilegeRepository.findById(id)); + } + + @Override + public List findAll() { + Collection privileges = privilegeRepository.findAll(); + List privilegeDTOS = new ArrayList<>(); + + privileges.forEach(privilege -> { + privilegeDTOS.add(entityToDTO(privilege)); + }); + + return privilegeDTOS; + } + + @Override + public PrivilegeDTO save(PrivilegeDTO dto) { + Privilege privilege = dtoToEntity(dto); + privilege = privilegeRepository.save(privilege); + + return entityToDTO(privilege); + } + + public PrivilegeDTO findByName(String name) { + return entityToDTO(privilegeRepository.findByName(name)); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/service/common/RoleService.java b/src/main/java/org/springframework/samples/petclinic/service/common/RoleService.java index 4dff3254f..6f7880599 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/common/RoleService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/common/RoleService.java @@ -12,7 +12,7 @@ import java.util.Collection; import java.util.List; /** - * Simple Service between Specialty entity and SpecialtyDTO Data Transfert Object. + * Simple Service between Role entity and RoleDTO Data Transfert Object. * * @author Paul-Emmanuel DOS SANTOS FACAO */ @@ -39,7 +39,9 @@ public class RoleService implements BaseService { @Override public RoleDTO entityToDTO(Role entity) { if (entity != null) { - return modelMapper.map(entity, RoleDTO.class); + RoleDTO roleDTO = modelMapper.map(entity, RoleDTO.class); + roleDTO.getUsers().forEach(userDTO -> userDTO.setMatchingPassword(userDTO.getPassword())); + return roleDTO; } return new RoleDTO(); @@ -63,11 +65,6 @@ public class RoleService implements BaseService { return entities; } - @Override - public RoleDTO findById(int id) { - return entityToDTO(roleRepository.findById(id)); - } - @Override public List findAll() { Collection roles = roleRepository.findAll(); @@ -80,6 +77,11 @@ public class RoleService implements BaseService { return roleDTOS; } + @Override + public RoleDTO findById(int id) { + return entityToDTO(roleRepository.findById(id)); + } + @Override public RoleDTO save(RoleDTO dto) { Role role = dtoToEntity(dto); @@ -88,6 +90,13 @@ public class RoleService implements BaseService { return entityToDTO(role); } + public RoleDTO delete(RoleDTO dto) { + Role role = dtoToEntity(dto); + role = roleRepository.delete(role); + + return entityToDTO(role); + } + public RoleDTO findByName(String name) { return entityToDTO(roleRepository.findByName(name)); } diff --git a/src/main/java/org/springframework/samples/petclinic/service/common/UserService.java b/src/main/java/org/springframework/samples/petclinic/service/common/UserService.java index ac906a0cd..03d56f9cd 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/common/UserService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/common/UserService.java @@ -38,7 +38,6 @@ public class UserService implements BaseService { } User user = modelMapper.map(dto, User.class); - user.setPassword(dto.getPassword()); dto.getRoles().forEach(role -> user.addRole(modelMapper.map(role, Role.class))); return user; @@ -51,7 +50,6 @@ public class UserService implements BaseService { } UserDTO userDto = modelMapper.map(entity, UserDTO.class); - userDto.setPassword(entity.getPassword()); userDto.setMatchingPassword(entity.getPassword()); return userDto; diff --git a/src/main/resources/static/resources/css/style.css b/src/main/resources/static/resources/css/style.css index d02509799..4201dd841 100644 --- a/src/main/resources/static/resources/css/style.css +++ b/src/main/resources/static/resources/css/style.css @@ -43,23 +43,9 @@ input:hover, } /* add appropriate colors to fb, twitter and google buttons */ -.fb { - background-color: #3B5998; - color: white; -} - -.twitter { - background-color: #55ACEE; - color: white; -} - -.google { - background-color: #dd4b39; - color: white; -} - -.github { - background-color: #dd4b39; +.btn-social { + width: 25%; + margin: 10px; color: white; } diff --git a/src/main/resources/templates/fragments/layout.html b/src/main/resources/templates/fragments/layout.html index 72de108c6..7ded0bd3e 100755 --- a/src/main/resources/templates/fragments/layout.html +++ b/src/main/resources/templates/fragments/layout.html @@ -106,7 +106,7 @@ -