From 258c7e20c21f4a1cbf7b5e95b911eb30c920c1b5 Mon Sep 17 00:00:00 2001 From: benhoumine Date: Sat, 14 Mar 2020 20:48:33 +0100 Subject: [PATCH] version 2 --- pom.xml | 14 ++++ .../java/isima/F2/DAO/ICompteConnexion.java | 8 +++ .../java/isima/F2/config/SecurityConfig.java | 54 ++++++++++++++ .../F2/controllers/ActionController.java | 7 +- .../F2/controllers/ConnexionController.java | 41 +++++++++++ .../F2/controllers/EmployeController.java | 23 ++++++ .../F2/controllers/PoubelleController.java | 9 ++- .../F2/model/CompteConnexionEmploye.java | 71 +++++++++++++++++++ src/main/java/isima/F2/model/Employe.java | 8 +++ .../java/isima/F2/model/enums/RolesEnum.java | 32 +++++++++ .../java/isima/F2/services/ImpEmploye.java | 11 ++- .../java/isima/F2/services/ImpPoubelle.java | 2 +- .../isima/F2/services/MeteoInformation.java | 59 +++++++++++++++ 13 files changed, 329 insertions(+), 10 deletions(-) create mode 100644 src/main/java/isima/F2/DAO/ICompteConnexion.java create mode 100644 src/main/java/isima/F2/config/SecurityConfig.java create mode 100644 src/main/java/isima/F2/controllers/ConnexionController.java create mode 100644 src/main/java/isima/F2/controllers/EmployeController.java create mode 100644 src/main/java/isima/F2/model/CompteConnexionEmploye.java create mode 100644 src/main/java/isima/F2/model/enums/RolesEnum.java create mode 100644 src/main/java/isima/F2/services/MeteoInformation.java diff --git a/pom.xml b/pom.xml index e3d5409..a7ededa 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,20 @@ + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.security + spring-security-test + test + + + org.json + json + 20190722 + diff --git a/src/main/java/isima/F2/DAO/ICompteConnexion.java b/src/main/java/isima/F2/DAO/ICompteConnexion.java new file mode 100644 index 0000000..e7df353 --- /dev/null +++ b/src/main/java/isima/F2/DAO/ICompteConnexion.java @@ -0,0 +1,8 @@ +package isima.f2.dao; + +import org.springframework.data.jpa.repository.JpaRepository; + +import isima.f2.model.CompteConnexionEmploye; + +public interface ICompteConnexion extends JpaRepository{ +} diff --git a/src/main/java/isima/F2/config/SecurityConfig.java b/src/main/java/isima/F2/config/SecurityConfig.java new file mode 100644 index 0000000..eb7b7e2 --- /dev/null +++ b/src/main/java/isima/F2/config/SecurityConfig.java @@ -0,0 +1,54 @@ +package isima.f2.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + public static final String[] PUBLIC_MATCHES = { + "**/**" + }; + + @Override + public void configure(HttpSecurity http) throws Exception{ + http.authorizeRequests() + .antMatchers(PUBLIC_MATCHES).permitAll(); + } + /* + @Override + protected void configure(HttpSecurity http) throws Exception { + + http + //HTTP Basic authentication + .httpBasic() + .and() + .authorizeRequests() + .antMatchers(HttpMethod.GET, "/actions/**").hasRole("USER") + .antMatchers(HttpMethod.POST, "/actions").hasRole("ADMIN") + .antMatchers(HttpMethod.PUT, "/actions/**").hasRole("ADMIN") + .antMatchers(HttpMethod.PATCH, "/actions/**").hasRole("ADMIN") + .antMatchers(HttpMethod.DELETE, "/actions/**").hasRole("ADMIN") + .and() + .csrf().disable() + .formLogin().disable(); + } + */ + + + // 2 utilisateur pour le demos + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication() + .withUser("user").password("{noop}password").roles("USER") + .and() + .withUser("admin").password("{noop}password").roles("USER", "ADMIN"); + + } + + +} diff --git a/src/main/java/isima/F2/controllers/ActionController.java b/src/main/java/isima/F2/controllers/ActionController.java index 6ff4ec7..081fa71 100644 --- a/src/main/java/isima/F2/controllers/ActionController.java +++ b/src/main/java/isima/F2/controllers/ActionController.java @@ -4,7 +4,6 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -22,9 +21,5 @@ public class ActionController { public List getActions() { return actions.getActions(); } - - @GetMapping("/{id}") - public Action getAction(@PathVariable Long id) { - return actions.getAction(id).orElse(null); - } + } diff --git a/src/main/java/isima/F2/controllers/ConnexionController.java b/src/main/java/isima/F2/controllers/ConnexionController.java new file mode 100644 index 0000000..01339a4 --- /dev/null +++ b/src/main/java/isima/F2/controllers/ConnexionController.java @@ -0,0 +1,41 @@ +package isima.f2.controllers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.request.WebRequest; + +import isima.f2.dao.ICompteConnexion; +import isima.f2.dao.IEmployeDAO; +import isima.f2.model.Employe; + +@RestController +@RequestMapping("connexion") +public class ConnexionController { + + @Autowired + private ICompteConnexion connexion ; + @Autowired + private IEmployeDAO empploye ; + + @GetMapping("/") + public ResponseEntity customHandleNotFound(Exception ex, WebRequest request) { + return new ResponseEntity<>("errors", HttpStatus.NOT_FOUND); + + } + + @GetMapping("/login") + @ResponseBody + public Employe login(@RequestParam String id) { + if(empploye.findById(Long.parseLong(id)).isPresent()) { + return empploye.findById(Long.parseLong(id)).get(); + }else{ + return null ; + } + } +} diff --git a/src/main/java/isima/F2/controllers/EmployeController.java b/src/main/java/isima/F2/controllers/EmployeController.java new file mode 100644 index 0000000..1d42314 --- /dev/null +++ b/src/main/java/isima/F2/controllers/EmployeController.java @@ -0,0 +1,23 @@ +package isima.f2.controllers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import isima.f2.model.Employe; +import isima.f2.services.ImpEmploye; + +@RestController +@RequestMapping("employes") +public class EmployeController { + + @Autowired + private ImpEmploye employeService; + + @PostMapping(value="/saveemploye", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public Employe savePoubelle(@RequestBody Employe employe) { + return employeService.ajouterEmploye(employe); + } +} diff --git a/src/main/java/isima/F2/controllers/PoubelleController.java b/src/main/java/isima/F2/controllers/PoubelleController.java index f54e450..fedcfdc 100644 --- a/src/main/java/isima/F2/controllers/PoubelleController.java +++ b/src/main/java/isima/F2/controllers/PoubelleController.java @@ -3,7 +3,10 @@ package isima.f2.controllers; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import isima.f2.model.Poubelle; @@ -16,9 +19,13 @@ public class PoubelleController { @Autowired private ImpPoubelle poubelles; - @GetMapping("/") + @GetMapping(value="/", produces = MediaType.APPLICATION_JSON_VALUE) public List getPoubelles() { return poubelles.getPoubelles(); } + @PostMapping(value="/savepoubelle", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public Poubelle savePoubelle(@RequestBody Poubelle poubelle) { + return poubelles.ajouterPoubelle(poubelle); + } } diff --git a/src/main/java/isima/F2/model/CompteConnexionEmploye.java b/src/main/java/isima/F2/model/CompteConnexionEmploye.java new file mode 100644 index 0000000..42a4030 --- /dev/null +++ b/src/main/java/isima/F2/model/CompteConnexionEmploye.java @@ -0,0 +1,71 @@ +package isima.f2.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class CompteConnexionEmploye implements Serializable{ + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy= GenerationType.AUTO) + private Long id ; + private String codeConnexion ; + private String password ; + + @OneToOne + private Employe employe ; + + + public CompteConnexionEmploye(Long id, String codeConnexion, String password, Employe employe) { + super(); + this.id = id; + this.codeConnexion = codeConnexion; + this.password = password; + this.employe = employe; + } + + public CompteConnexionEmploye() { + super(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getCodeConnexion() { + return codeConnexion; + } + + public void setCodeConnexion(String codeConnexion) { + this.codeConnexion = codeConnexion; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Employe getEmploye() { + return employe; + } + + public void setEmploye(Employe employe) { + this.employe = employe; + } + + +} diff --git a/src/main/java/isima/F2/model/Employe.java b/src/main/java/isima/F2/model/Employe.java index af4e9ac..a394087 100644 --- a/src/main/java/isima/F2/model/Employe.java +++ b/src/main/java/isima/F2/model/Employe.java @@ -2,9 +2,12 @@ package isima.f2.model; import java.io.Serializable; import java.util.Date; +import java.util.List; + import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.OneToMany; @Entity @@ -19,6 +22,11 @@ public class Employe implements Serializable{ private Date premierJour ; private String poste ; + //Chaque employe peut avoir des avertissement + @OneToMany + private List avertissements; + + public Employe() { super(); } diff --git a/src/main/java/isima/F2/model/enums/RolesEnum.java b/src/main/java/isima/F2/model/enums/RolesEnum.java new file mode 100644 index 0000000..58219fe --- /dev/null +++ b/src/main/java/isima/F2/model/enums/RolesEnum.java @@ -0,0 +1,32 @@ +package isima.f2.model.enums; + + +/**** + * + * Cette enumération est pour définir les role de chaque utilisateur après l'authentification + * @author Abdelkhalek BENHOUMINE + * + */ +public enum RolesEnum { + + BASIC(1,"ROLE_BASIC"), + PRO(2,"ROLE_PRO"), + ADMIN(3,"ROLE_ADMIN"); + + + private final int id ; + private final String roleName; + + RolesEnum(int id , String roleName) { + this.id = id; + this.roleName = roleName; + } + + public int getId() { + return id ; + } + + public String getRoleName() { + return roleName ; + } +} diff --git a/src/main/java/isima/F2/services/ImpEmploye.java b/src/main/java/isima/F2/services/ImpEmploye.java index f9601be..d89baaf 100644 --- a/src/main/java/isima/F2/services/ImpEmploye.java +++ b/src/main/java/isima/F2/services/ImpEmploye.java @@ -3,15 +3,22 @@ package isima.f2.services; import java.util.List; import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + import isima.f2.contrats.IEmploye; +import isima.f2.dao.IEmployeDAO; import isima.f2.model.Employe; +@Service public class ImpEmploye implements IEmploye { + @Autowired + private IEmployeDAO employeService; + @Override public Employe ajouterEmploye(Employe employe) { - // TODO Auto-generated method stub - return null; + return employeService.save(employe); } @Override diff --git a/src/main/java/isima/F2/services/ImpPoubelle.java b/src/main/java/isima/F2/services/ImpPoubelle.java index e7caedf..b081823 100644 --- a/src/main/java/isima/F2/services/ImpPoubelle.java +++ b/src/main/java/isima/F2/services/ImpPoubelle.java @@ -18,7 +18,7 @@ public class ImpPoubelle implements IPoubelle{ @Override public Poubelle ajouterPoubelle(Poubelle poubelle) { - return null; + return poubelleDAO.save(poubelle); } @Override diff --git a/src/main/java/isima/F2/services/MeteoInformation.java b/src/main/java/isima/F2/services/MeteoInformation.java new file mode 100644 index 0000000..44bd8d2 --- /dev/null +++ b/src/main/java/isima/F2/services/MeteoInformation.java @@ -0,0 +1,59 @@ +package isima.f2.services; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.Iterator; + +import org.json.JSONArray; +import org.json.JSONObject; + + +/** Cette classe ets pour récuperer les informations de la méteo + * @author Abdelkhalek BENHOUMINE + * + */ +public class MeteoInformation { + + + private MeteoInformation() { + //Rien juste pour assurer la qualite de code + } + private static final String URL = "https://samples.openweathermap.org/data/2.5/weather?zip=63000,fr&appid=b6907d289e10d714a6e88b30761fae22"; + + + private static String readAll(Reader rd) throws IOException { + StringBuilder sb = new StringBuilder(); + int cp; + while ((cp = rd.read()) != -1) { + sb.append((char) cp); + } + return sb.toString(); + } + + public static JSONObject readJsonFromUrl(String url) throws IOException{ + InputStream is = new URL(url).openStream(); + try( BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));) { + String jsonText = readAll(rd); + return new JSONObject(jsonText); + } finally { + is.close(); + } + } + public static String getInformation() throws IOException { + JSONObject json = readJsonFromUrl(URL); + JSONArray weather = (JSONArray)json.get("weather"); + Iterator iterator = weather.iterator(); + while (iterator.hasNext()) { + JSONObject weatherJson = (JSONObject)iterator.next(); + return weatherJson.get("main").toString() ; + + } + return null ; + } + +} -- GitLab