diff --git a/etunicorn-server.iml b/etunicorn-server.iml
index 9f776d0..fbca5fc 100644
--- a/etunicorn-server.iml
+++ b/etunicorn-server.iml
@@ -10,7 +10,6 @@
-
@@ -50,15 +49,8 @@
-
-
-
-
-
-
-
@@ -85,7 +77,15 @@
-
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 3a3169a..b4984d9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,17 +32,22 @@
- org.xerial
- sqlite-jdbc
- 3.16.1
-
-
-
commons-lang
commons-lang
2.3
+
+ org.hibernate
+ hibernate-entitymanager
+ runtime
+
+
+
+ mysql
+ mysql-connector-java
+ runtime
+
diff --git a/src/main/java/etunicorn/Application.java b/src/main/java/etunicorn/Application.java
index 00c8eec..703ef78 100644
--- a/src/main/java/etunicorn/Application.java
+++ b/src/main/java/etunicorn/Application.java
@@ -1,21 +1,19 @@
package etunicorn;
-import etunicorn.entity.Permission;
-import etunicorn.entity.Personne;
-import etunicorn.entity.Role;
-import etunicorn.entity.Session;
-import etunicorn.repository.PermissionRepository;
-import etunicorn.repository.PersonneRepository;
-import etunicorn.repository.RoleRepository;
-import etunicorn.repository.SessionRepository;
+import etunicorn.domain.Permission;
+import etunicorn.domain.Personne;
+import etunicorn.domain.Role;
+import etunicorn.domain.Session;
+import etunicorn.spring.jpa.repository.PermissionRepository;
+import etunicorn.spring.jpa.repository.PersonneRepository;
+import etunicorn.spring.jpa.repository.RoleRepository;
+import etunicorn.spring.jpa.repository.SessionRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
-import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -59,12 +57,4 @@ public class Application {
};
}
-
- @Bean
- public DataSource dataSource() {
- DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
- dataSourceBuilder.driverClassName("org.sqlite.JDBC");
- dataSourceBuilder.url("jdbc:sqlite:etunicorn.db");
- return dataSourceBuilder.build();
- }
}
diff --git a/src/main/java/etunicorn/SecurityInterceptor.java b/src/main/java/etunicorn/SecurityInterceptor.java
index fda1f6a..7069aa2 100644
--- a/src/main/java/etunicorn/SecurityInterceptor.java
+++ b/src/main/java/etunicorn/SecurityInterceptor.java
@@ -1,10 +1,9 @@
package etunicorn;
-import etunicorn.controller.BaseController;
-import etunicorn.entity.Permission;
-import etunicorn.entity.Session;
-import etunicorn.repository.PermissionRepository;
-import etunicorn.service.SessionService;
+import etunicorn.domain.Permission;
+import etunicorn.domain.Session;
+import etunicorn.domain.service.impl.SessionService;
+import etunicorn.spring.jpa.repository.PermissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -58,21 +57,22 @@ public class SecurityInterceptor extends HandlerInterceptorAdapter {
return false;
}
- if (annotation.authentifie()) {
- BaseController baseController = new BaseController();
- baseController.setRequest(request);
- if (session == null) {
- ResponseEntity responseEntity = baseController.generateError(HttpStatus.UNAUTHORIZED);
- responseEntityToServletResponse(responseEntity, response);
- return false;
- } else {
- if (!session.hasPermission(requiredPermission)) {
- ResponseEntity responseEntity = baseController.generateError(HttpStatus.FORBIDDEN);
- responseEntityToServletResponse(responseEntity, response);
- return false;
- }
- }
- }
+ // TODO Remettre bien
+ // if (annotation.authentifie()) {
+ // BaseController baseController = new BaseController();
+ // baseController.setRequest(request);
+ // if (session == null) {
+ // ResponseEntity responseEntity = baseController.generateError(HttpStatus.UNAUTHORIZED);
+ // responseEntityToServletResponse(responseEntity, response);
+ // return false;
+ // } else {
+ // if (!session.hasPermission(requiredPermission)) {
+ // ResponseEntity responseEntity = baseController.generateError(HttpStatus.FORBIDDEN);
+ // responseEntityToServletResponse(responseEntity, response);
+ // return false;
+ // }
+ // }
+ // }
}
return super.preHandle(request, response, handler);
}
diff --git a/src/main/java/etunicorn/common/PersistenceJPAConfig.java b/src/main/java/etunicorn/common/PersistenceJPAConfig.java
new file mode 100644
index 0000000..990d8fa
--- /dev/null
+++ b/src/main/java/etunicorn/common/PersistenceJPAConfig.java
@@ -0,0 +1,76 @@
+package etunicorn.common;
+
+import org.springframework.boot.autoconfigure.domain.EntityScan;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.orm.jpa.JpaVendorAdapter;
+import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
+import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+import javax.persistence.EntityManagerFactory;
+import javax.sql.DataSource;
+import java.util.Properties;
+
+/**
+ * etunicorn-server
+ * Copyright © 2017 Le Club Info Polytech Lille
+ * Tous droits réservés
+ */
+@Configuration
+@EnableTransactionManagement
+@EnableJpaRepositories("etunicorn.*")
+@ComponentScan(basePackages = {"etunicorn.*"})
+@EntityScan("etunicorn.*")
+
+public class PersistenceJPAConfig {
+
+ @Bean
+ public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
+ LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
+ em.setDataSource(dataSource());
+ em.setPackagesToScan("org.baeldung.persistence.model");
+
+ JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
+ em.setJpaVendorAdapter(vendorAdapter);
+ em.setJpaProperties(additionalProperties());
+
+ return em;
+ }
+
+ @Bean
+ public DataSource dataSource() {
+ DriverManagerDataSource dataSource = new DriverManagerDataSource();
+ dataSource.setDriverClassName("com.mysql.jdbc.Driver");
+ dataSource.setUrl("jdbc:mysql://localhost:3306/etunicorn");
+ dataSource.setUsername("etunicorn");
+ dataSource.setPassword("etunicorn");
+ return dataSource;
+ }
+
+ @Bean
+ public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
+ JpaTransactionManager transactionManager = new JpaTransactionManager();
+ transactionManager.setEntityManagerFactory(emf);
+
+ return transactionManager;
+ }
+
+ @Bean
+ public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
+ return new PersistenceExceptionTranslationPostProcessor();
+ }
+
+ Properties additionalProperties() {
+ Properties properties = new Properties();
+ properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
+ properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
+ return properties;
+ }
+}
diff --git a/src/main/java/etunicorn/controller/BaseController.java b/src/main/java/etunicorn/controller/BaseController.java
deleted file mode 100644
index f7f5d3c..0000000
--- a/src/main/java/etunicorn/controller/BaseController.java
+++ /dev/null
@@ -1,89 +0,0 @@
-package etunicorn.controller;
-
-import etunicorn.entity.Permission;
-import etunicorn.entity.Session;
-import etunicorn.repository.PermissionRepository;
-import etunicorn.service.SessionService;
-import net.minidev.json.JSONObject;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.repository.Repository;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.RestController;
-
-import javax.servlet.http.HttpServletRequest;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.Date;
-import java.util.Map;
-
-/**
- * etunicorn-server
- * Copyright © 2017 Le Club Info Polytech Lille
- * Tous droits réservés
- */
-@RestController
-public class BaseController {
-
- // Utilités pour merger un request dans une entity
- @Autowired
- Map repositories;
- // Permettent la vérification de permissions dans les méthodes de controlleur
- @Autowired
- private HttpServletRequest request;
- @Autowired
- private SessionService sessionService;
- @Autowired
- private PermissionRepository permissionRepository;
-
- public void setRequest(HttpServletRequest request) {
- this.request = request;
- }
-
- protected boolean hasPermission(Permission permission) {
- Session session = sessionService.getSession(request);
- if (session == null || permission == null) {
- return false;
- }
- return session.hasPermission(permission);
- }
-
- protected boolean hasPermission(String nomPermission) {
- Permission permission = permissionRepository.findByNom(nomPermission);
- return hasPermission(permission);
- }
-
- // Utilités pour générer des erreurs
- private JSONObject generateErrorJSON(HttpStatus status, String message) {
- JSONObject json = new JSONObject();
- json.put("timestamp", new Date().getTime());
- json.put("status", status.value());
- json.put("message", message);
- json.put("path", request.getPathInfo());
- return json;
-
- }
-
- public ResponseEntity generateError(HttpStatus status, String message) {
- JSONObject json = generateErrorJSON(status, message);
- return new ResponseEntity(json, status);
- }
-
- public ResponseEntity generateError(HttpStatus status, Exception exception, String message) {
- JSONObject json = generateErrorJSON(status, message);
- json.put("error", exception.getMessage());
- // From http://stackoverflow.com/a/1149721
- StringWriter sw = new StringWriter();
- exception.printStackTrace(new PrintWriter(sw));
- json.put("stacktrace", sw.toString());
- return new ResponseEntity(json, status);
- }
-
- public ResponseEntity generateError(HttpStatus status) {
- return generateError(status, status.getReasonPhrase());
- }
-
- public ResponseEntity generateError(Exception exception) {
- return generateError(HttpStatus.INTERNAL_SERVER_ERROR, exception, exception.getLocalizedMessage());
- }
-}
diff --git a/src/main/java/etunicorn/controller/LoginController.java b/src/main/java/etunicorn/controller/LoginController.java
deleted file mode 100644
index 4912af3..0000000
--- a/src/main/java/etunicorn/controller/LoginController.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package etunicorn.controller;
-
-import etunicorn.RestrictedTo;
-import etunicorn.entity.Personne;
-import etunicorn.entity.Session;
-import etunicorn.generated.model.UpdateLoginRequest;
-import etunicorn.repository.PersonneRepository;
-import etunicorn.service.SessionService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RestController;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.validation.Valid;
-
-/**
- * etunicorn-server
- * Copyright © 2017 Le Club Info Polytech Lille
- * Tous droits réservés
- */
-@RestController
-public class LoginController extends BaseController implements etunicorn.generated.LoginController {
- @Autowired
- HttpServletRequest request;
- @Autowired
- private PersonneRepository personneRepository;
- @Autowired
- private SessionService sessionService;
-
- @Override
- @RestrictedTo(authentifie = false)
- public ResponseEntity> updateLogin(@Valid @RequestBody UpdateLoginRequest updateLoginRequest) {
- Personne personne = personneRepository.findByLogin(updateLoginRequest.getLogin());
- if (personne == null) {
- return generateError(HttpStatus.UNAUTHORIZED);
- }
-
- // TODO Vraie vérification du mot de passe
- if (!updateLoginRequest.getPassword().equals("test")) {
- return generateError(HttpStatus.UNAUTHORIZED);
- }
-
- Session session = sessionService.createSession(personne);
-
- return new ResponseEntity