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

Trying to get back my features

parent 54ae1ff0
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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,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();
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment