From cb678cccb7254e81dfe47cc2ed482825613e2f29 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 15 Dec 2018 16:24:21 +0100 Subject: [PATCH] Changement pour la configuration du réseau --- PFE06/app.conf | 15 +-------------- PFE06/src/main/java/com/PFE/ServerManager/AppConfig.java | 35 ++++++++++++++++------------------- PFE06/src/main/java/com/PFE/ServerManager/ConfYAML.java | 10 ---------- PFE06/src/main/java/com/PFE/ServerManager/ConfigNodes.java | 16 ++++++++++++++++ PFE06/src/main/java/com/PFE/ServerManager/MainController.java | 33 ++++++++++++++++++++++++++++++++- PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java | 1 + PFE06/src/main/resources/application.properties | 2 +- PFE06/src/main/resources/data.sql | 5 +---- PFE06/src/main/resources/static/js/session.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ PFE06/src/main/resources/templates/home.html | 1 - PFE06/src/main/resources/templates/registration.html | 2 +- PFE06/src/main/resources/templates/session.html | 31 +++++++++++++++++++++++++++++++ 12 files changed, 144 insertions(+), 51 deletions(-) create mode 100644 PFE06/src/main/java/com/PFE/ServerManager/ConfigNodes.java diff --git a/PFE06/app.conf b/PFE06/app.conf index 7a237a1..4d8a0db 100644 --- a/PFE06/app.conf +++ b/PFE06/app.conf @@ -1,15 +1,2 @@ adminEmail: pfe@pfe.fr -adminPassword: pfepfe -nodes: - - name: AT-001 - ip: '85.10.201.246' - arch: ARM - - name: AT-002 - ip: '85.10.201.247' - arch: ARM - - name: AT-003 - ip: '85.10.201.248' - arch: ARM - - name: ATTTT-004 - ip: '85.10.201.249' - arch: ARM \ No newline at end of file +adminPassword: pfepfe \ No newline at end of file diff --git a/PFE06/src/main/java/com/PFE/ServerManager/AppConfig.java b/PFE06/src/main/java/com/PFE/ServerManager/AppConfig.java index 9adac08..8881d07 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/AppConfig.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/AppConfig.java @@ -3,7 +3,6 @@ package com.PFE.ServerManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Component; -import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; @@ -22,18 +21,28 @@ public class AppConfig { RoleRepository roleRepository; @Autowired - MajRepository majRepository; - - @Autowired BCryptPasswordEncoder bCryptPasswordEncoder; - @Autowired - NodeRepository nodeRepository; - @PostConstruct public void init() { System.out.println("============= Configuration de l'application ============="); + /* Ajout des rôles si non présent */ + + if(roleRepository.count() != 2) { + roleRepository.deleteAll(); + Role roleAdmin = new Role(); + roleAdmin.setRole_id(1); + roleAdmin.setRole("ADMIN"); + roleRepository.save(roleAdmin); + Role roleUser = new Role(); + roleUser.setRole_id(2); + roleUser.setRole("USER"); + roleRepository.save(roleUser); + } + + /* Ajout d'un utilisateur ADMIN avec les informations du fichier de conf*/ + Yaml yaml = new Yaml(new Constructor(ConfYAML.class)); InputStream inputStream = null; try { @@ -56,17 +65,5 @@ public class AppConfig { if(temp == null) { customerRepository.save(n); } - - Iterator iter = conf.getNodes().iterator(); - while (iter.hasNext()) { - NodeYAML element = iter.next(); - Node node = new Node(); - node.setName(element.getName()); - node.setArch(element.getArch()); - node.setIp(element.getIp()); - node.setNode_id((int)(nodeRepository.count() + 1)); - nodeRepository.save(node); - } - } } \ No newline at end of file diff --git a/PFE06/src/main/java/com/PFE/ServerManager/ConfYAML.java b/PFE06/src/main/java/com/PFE/ServerManager/ConfYAML.java index 1dfe4fb..3fea5e2 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/ConfYAML.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/ConfYAML.java @@ -1,12 +1,9 @@ package com.PFE.ServerManager; -import java.util.List; - public class ConfYAML { private String adminEmail; private String adminPassword; - private List nodes; public void setAdminEmail(String adminEmail) { this.adminEmail = adminEmail; @@ -16,10 +13,6 @@ public class ConfYAML { this.adminPassword = adminPassword; } - public void setNodes(List nodes) { - this.nodes = nodes; - } - public String getAdminEmail() { return adminEmail; } @@ -28,8 +21,5 @@ public class ConfYAML { return adminPassword; } - public List getNodes() { - return nodes; - } } diff --git a/PFE06/src/main/java/com/PFE/ServerManager/ConfigNodes.java b/PFE06/src/main/java/com/PFE/ServerManager/ConfigNodes.java new file mode 100644 index 0000000..69be161 --- /dev/null +++ b/PFE06/src/main/java/com/PFE/ServerManager/ConfigNodes.java @@ -0,0 +1,16 @@ +package com.PFE.ServerManager; + +import java.util.List; + +public class ConfigNodes { + + private List nodes; + + public List getNodes() { + return nodes; + } + + public void setNodes(List nodes) { + this.nodes = nodes; + } +} diff --git a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java index a390723..0e27bcd 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java @@ -14,6 +14,8 @@ import java.util.*; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.Authentication; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; import javax.servlet.annotation.MultipartConfig; @@ -39,7 +41,7 @@ public class MainController { @GetMapping(value="/") public String uploadRedirection(){ - return "redirect:upload"; + return "redirect:home"; } @GetMapping(value="/home") @@ -186,6 +188,35 @@ public class MainController { } } + @RequestMapping(value = "/config", method = RequestMethod.POST) + @ResponseStatus(value = HttpStatus.OK) + public void submitConfig(@RequestParam MultipartFile file) { + + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + Customer customer = customerRepository.findByEmail(auth.getName()); + InputStream inputStream = null; + try { + inputStream = file.getInputStream(); + } + catch (IOException e) { + e.printStackTrace(); + } + + nodeRepository.deleteAll(); + Yaml yaml = new Yaml(new Constructor(ConfigNodes.class)); + ConfigNodes conf = yaml.load(inputStream); + + Iterator iter = conf.getNodes().iterator(); + while (iter.hasNext()) { + NodeYAML element = iter.next(); + Node node = new Node(); + node.setName(element.getName()); + node.setArch(element.getArch()); + node.setIp(element.getIp()); + node.setNode_id((int)(nodeRepository.count() + 1)); + nodeRepository.save(node); + } + } @GetMapping(path="/login") public ModelAndView login() { diff --git a/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java b/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java index 757d04c..95669a9 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java @@ -35,6 +35,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .authorizeRequests() .antMatchers("/home").hasAnyAuthority("USER","ADMIN") .antMatchers("/registration").hasAuthority("ADMIN") + .antMatchers("/config").hasAuthority("ADMIN") .antMatchers("/all").hasAuthority("ADMIN") .antMatchers("/login").permitAll() .antMatchers("/denied").permitAll() diff --git a/PFE06/src/main/resources/application.properties b/PFE06/src/main/resources/application.properties index 2a4e67e..538d2e3 100644 --- a/PFE06/src/main/resources/application.properties +++ b/PFE06/src/main/resources/application.properties @@ -2,7 +2,7 @@ #-----------------------------------------------# #---------- Spring Database management ---------# #-----------------------------------------------# -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=update #update #"create" recrée la base de données à chaque lancement #"update" met à jour la base données diff --git a/PFE06/src/main/resources/data.sql b/PFE06/src/main/resources/data.sql index 74b54d1..d83d73d 100644 --- a/PFE06/src/main/resources/data.sql +++ b/PFE06/src/main/resources/data.sql @@ -1,4 +1 @@ -/* ce fichier doit être placé dans les ressources afin d'être utilisé */ - -INSERT INTO "role" VALUES (1, 'ADMIN'); -INSERT INTO "role" VALUES (2, 'USER'); \ No newline at end of file +/* ce fichier doit être placé dans les ressources afin d'être utilisé */ \ No newline at end of file diff --git a/PFE06/src/main/resources/static/js/session.js b/PFE06/src/main/resources/static/js/session.js index 9bb4180..f07b57a 100644 --- a/PFE06/src/main/resources/static/js/session.js +++ b/PFE06/src/main/resources/static/js/session.js @@ -1,5 +1,49 @@ $(document).ready(function() { + /********** Configuration file ***********/ + + var configName = document.getElementById("config_name"); + var configSend = document.getElementById("config_send"); + var file = document.getElementById("configInputFile"); + var readyToSend = false; + var formData = new FormData(); + + file.addEventListener('change', function() { + delete formData; + formData = new FormData(); + configName.innerHTML = this.files[0].name; + formData.append("file", this.files[0]); + readyToSend = true; + }, false) + + configSend.addEventListener('click', function() { + var modalContent = document.getElementById("modal-content"); + var modalTitle = document.getElementById("modal-title"); + if(readyToSend) { + var request = new XMLHttpRequest(); + request.open("POST", "/config"); + + request.onreadystatechange = function() { + if(this.readyState === XMLHttpRequest.DONE && this.status === 200) { + var modalButton = document.getElementById("modal-button"); + modalButton.addEventListener('click', function(e) { + location.reload(); + }) + modalContent.innerHTML = "Upload terminé !"; + modalTitle.innerHTML = "Félicitations"; + $("#warningFilesNumber").modal(); + } + } + + request.send(formData); + } + else { + modalContent.innerHTML = "Veuillez sélectionner un fichier !"; + $("#warningFilesNumber").modal() + } + + }) + /********** Tableau **********/ var tableNodes = $('#nodes-table').DataTable( { diff --git a/PFE06/src/main/resources/templates/home.html b/PFE06/src/main/resources/templates/home.html index b0c82ae..a3b406c 100644 --- a/PFE06/src/main/resources/templates/home.html +++ b/PFE06/src/main/resources/templates/home.html @@ -100,6 +100,5 @@ - diff --git a/PFE06/src/main/resources/templates/registration.html b/PFE06/src/main/resources/templates/registration.html index 718ae60..84ef78d 100644 --- a/PFE06/src/main/resources/templates/registration.html +++ b/PFE06/src/main/resources/templates/registration.html @@ -59,7 +59,7 @@
- +
diff --git a/PFE06/src/main/resources/templates/session.html b/PFE06/src/main/resources/templates/session.html index a7587cd..1cc81c5 100644 --- a/PFE06/src/main/resources/templates/session.html +++ b/PFE06/src/main/resources/templates/session.html @@ -44,6 +44,36 @@
+ +
+

Changer la configuration réseau

+
+
+ +
+
+ + +
+
+ +
+ + +
+
+

Relancer une mise à jour