diff --git a/src/main/java/org/hbp/mip/MIPApplication.java b/src/main/java/org/hbp/mip/MIPApplication.java index 56034f9b899ee83a50140a9e0d6b2ae4eaddff5b..d5f62247e97375b5b6eada30c3b1c99193516242 100644 --- a/src/main/java/org/hbp/mip/MIPApplication.java +++ b/src/main/java/org/hbp/mip/MIPApplication.java @@ -23,28 +23,28 @@ package org.hbp.mip; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiParam; import org.hbp.mip.model.User; import org.hbp.mip.utils.CORSFilter; -import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties; -import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices; import org.springframework.boot.context.embedded.FilterRegistrationBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.client.OAuth2ClientContext; -import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter; import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter; import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; @@ -53,13 +53,14 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.E import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.web.access.channel.ChannelProcessingFilter; import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; -import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; import org.springframework.security.web.csrf.CsrfFilter; import org.springframework.security.web.csrf.CsrfToken; import org.springframework.security.web.csrf.CsrfTokenRepository; import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.WebUtils; @@ -114,21 +115,31 @@ public static void main(String[] args) { return userAuthentication.getDetails().toString(); } - public User getUser(Principal principal) { - Session session = sessionFactoryBean.getCurrentSession(); - session.beginTransaction(); - Query query = session.createQuery("from User where username= :username"); - query.setString("username", principal.getName()); - User user = (User) query.uniqueResult(); - session.getTransaction().commit(); - if (user == null) { - session = sessionFactoryBean.getCurrentSession(); - session.beginTransaction(); + /** + * returns the user for the current session. + * + * the "synchronized" keyword is there to avoid a bug that the transaction is supposed to protect me from. + * To test if your solution to removing it works, do the following: + * - clean DB from scratch + * - restart DB and backend (no session or anything like that) + * - log in using the front end + * - check you have no 500 error in the network logs. + * @param principal + * @return + */ + public synchronized User getUser(Principal principal) { + Session session = sessionFactoryBean.getCurrentSession(); + session.beginTransaction(); + User user = (User) session + .createQuery("from User where username= :username") + .setString("username", principal.getName()) + .uniqueResult(); + if (user == null) { user = new User(getUserInfos()); user.setTeam("CHUV"); session.save(user); - session.getTransaction().commit(); } + session.getTransaction().commit(); return user; } @@ -156,7 +167,7 @@ public static void main(String[] args) { .build(); } - @RequestMapping("/user") + @RequestMapping(path = "/user", method = RequestMethod.GET) public Principal user(Principal principal, HttpServletResponse response) { ObjectMapper mapper = new ObjectMapper(); @@ -164,7 +175,6 @@ public static void main(String[] args) { String userJSON = mapper.writeValueAsString(getUser(principal)); Cookie cookie = new Cookie("user", URLEncoder.encode(userJSON, "UTF-8")); cookie.setPath("/"); - cookie.setMaxAge(2592000); response.addCookie(cookie); } catch (JsonProcessingException e) { e.printStackTrace(); @@ -174,6 +184,25 @@ public static void main(String[] args) { return principal; } + @RequestMapping(path = "/user", method = RequestMethod.POST) + public ResponseEntity<Void> postUser(Principal principal, HttpServletResponse response, + @ApiParam(value = "Has the user agreed on the NDA") @RequestParam(value = "agreeNDA", required = true) Boolean agreeNDA) { + ObjectMapper mapper = new ObjectMapper(); + Session session = sessionFactoryBean.getCurrentSession(); + session.beginTransaction(); + User user = (User) session + .createQuery("from User where username= :username") + .setString("username", principal.getName()) + .uniqueResult(); + if (user != null) { + user.setAgreeNDA(agreeNDA); + session.update(user); + } + session.getTransaction().commit(); + + return new ResponseEntity<Void>(HttpStatus.OK); + } + @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off diff --git a/src/main/java/org/hbp/mip/model/Model.java b/src/main/java/org/hbp/mip/model/Model.java index 6d0257a03a500fbae73a9f344d1141c04beb1b49..13b1a61c34afc9a50767c26fe0c3729556c0f50b 100644 --- a/src/main/java/org/hbp/mip/model/Model.java +++ b/src/main/java/org/hbp/mip/model/Model.java @@ -41,6 +41,7 @@ public class Model { private User createdBy = null; @ManyToOne private User updatedBy = null; + private String textQuery = null; public Model() { } @@ -188,6 +189,19 @@ public class Model { this.updatedBy = updatedBy; } + /** + * Text query + **/ + @ApiModelProperty(value = "Text query") + @JsonProperty("textQuery") + public String getTextQuery() { + return textQuery; + } + + public void setTextQuery(String textQuery) { + this.textQuery = textQuery; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); @@ -204,6 +218,7 @@ public class Model { sb.append(" updatedAt: ").append(updatedAt).append("\n"); sb.append(" createdBy: ").append(createdBy).append("\n"); sb.append(" updatedBy: ").append(updatedBy).append("\n"); + sb.append(" textQuery: ").append(textQuery).append("\n"); sb.append("}\n"); return sb.toString(); } diff --git a/src/main/java/org/hbp/mip/model/User.java b/src/main/java/org/hbp/mip/model/User.java index dd8757d02fd138c2350441a7ab4eeeb11219c16f..4334afea9fd5e159c23711211078cd1acfd60750 100644 --- a/src/main/java/org/hbp/mip/model/User.java +++ b/src/main/java/org/hbp/mip/model/User.java @@ -41,6 +41,7 @@ public class User { private List<String> languages = new LinkedList<String>(); @ElementCollection(fetch = FetchType.EAGER) private List<String> roles = new LinkedList<String>(); + private Boolean agreeNDA = null; public User() { } @@ -329,6 +330,20 @@ public class User { this.roles = roles; } + /** + * Agree NDA + **/ + @ApiModelProperty(value = "Agree NDA") + @JsonProperty("agreeNDA") + public Boolean getAgreeNDA() { + return agreeNDA; + } + + public void setAgreeNDA(Boolean agreeNDA) { + this.agreeNDA = agreeNDA; + } + + @Override public String toString() { StringBuilder sb = new StringBuilder(); @@ -352,6 +367,7 @@ public class User { sb.append(" isActive: ").append(isActive).append("\n"); sb.append(" languages: ").append(languages).append("\n"); sb.append(" roles: ").append(roles).append("\n"); + sb.append(" agreeNDA: ").append(agreeNDA).append("\n"); sb.append("}\n"); return sb.toString(); }