Skip to content
Snippets Groups Projects
Commit 1d0cabea authored by Mirco Nasuti's avatar Mirco Nasuti
Browse files

new grouping + text query + add agreeNDA field to user

parent ce2813da
No related branches found
No related tags found
No related merge requests found
......@@ -23,10 +23,10 @@ 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.hbp.mip.utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
......@@ -36,6 +36,8 @@ import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoT
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
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;
......@@ -57,8 +59,7 @@ 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.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.WebUtils;
import springfox.documentation.builders.ApiInfoBuilder;
......@@ -100,21 +101,31 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
return userAuthentication.getDetails().toString();
}
public static User getUser(Principal principal) {
/**
* 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 static synchronized User getUser(Principal principal) {
Session session = HibernateUtil.getSessionFactory().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();
User user = (User) session
.createQuery("from User where username= :username")
.setString("username", principal.getName())
.uniqueResult();
if (user == null) {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
user = new User(getUserInfos());
user.setTeam("CHUV");
session.save(user);
session.getTransaction().commit();
}
session.getTransaction().commit();
return user;
}
......@@ -142,7 +153,7 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
.build();
}
@RequestMapping("/user")
@RequestMapping(path = "/user", method = RequestMethod.GET)
public Principal user(Principal principal, HttpServletResponse response) {
ObjectMapper mapper = new ObjectMapper();
......@@ -150,7 +161,6 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
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();
......@@ -160,6 +170,25 @@ public class MIPApplication extends WebSecurityConfigurerAdapter {
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 = HibernateUtil.getSessionFactory().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
......
......@@ -182,7 +182,8 @@ public class ArticlesApi {
if(session.getTransaction() != null)
{
session.getTransaction().rollback();
} }
}
}
return new ResponseEntity<Void>(HttpStatus.OK);
}
......
......@@ -87,7 +87,8 @@ public class ModelsApi {
if(session.getTransaction() != null)
{
session.getTransaction().rollback();
} }
}
}
return new ResponseEntity<List<Model>>(HttpStatus.OK).ok(models);
}
......
......@@ -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();
}
......
......@@ -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,19 @@ 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 +366,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();
}
......
db @ af28ed02
Subproject commit f14bdeddf4e4c3a7af9271e8637ee064282da6a0
Subproject commit af28ed02d855345f286e171c758758283c742dff
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment