Commit 258c7e20 authored by benhoumine's avatar benhoumine

version 2

parent 400977ce
...@@ -54,6 +54,20 @@ ...@@ -54,6 +54,20 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package isima.f2.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import isima.f2.model.CompteConnexionEmploye;
public interface ICompteConnexion extends JpaRepository<CompteConnexionEmploye, Long>{
}
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");
}
}
...@@ -4,7 +4,6 @@ import java.util.List; ...@@ -4,7 +4,6 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -22,9 +21,5 @@ public class ActionController { ...@@ -22,9 +21,5 @@ public class ActionController {
public List<Action> getActions() { public List<Action> getActions() {
return actions.getActions(); return actions.getActions();
} }
@GetMapping("/{id}")
public Action getAction(@PathVariable Long id) {
return actions.getAction(id).orElse(null);
}
} }
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 ;
}
}
}
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);
}
}
...@@ -3,7 +3,10 @@ package isima.f2.controllers; ...@@ -3,7 +3,10 @@ package isima.f2.controllers;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; 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.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import isima.f2.model.Poubelle; import isima.f2.model.Poubelle;
...@@ -16,9 +19,13 @@ public class PoubelleController { ...@@ -16,9 +19,13 @@ public class PoubelleController {
@Autowired @Autowired
private ImpPoubelle poubelles; private ImpPoubelle poubelles;
@GetMapping("/") @GetMapping(value="/", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Poubelle> getPoubelles() { public List<Poubelle> getPoubelles() {
return poubelles.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);
}
} }
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;
}
}
...@@ -2,9 +2,12 @@ package isima.f2.model; ...@@ -2,9 +2,12 @@ package isima.f2.model;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity @Entity
...@@ -19,6 +22,11 @@ public class Employe implements Serializable{ ...@@ -19,6 +22,11 @@ public class Employe implements Serializable{
private Date premierJour ; private Date premierJour ;
private String poste ; private String poste ;
//Chaque employe peut avoir des avertissement
@OneToMany
private List<Avertissement> avertissements;
public Employe() { public Employe() {
super(); super();
} }
......
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 ;
}
}
...@@ -3,15 +3,22 @@ package isima.f2.services; ...@@ -3,15 +3,22 @@ package isima.f2.services;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import isima.f2.contrats.IEmploye; import isima.f2.contrats.IEmploye;
import isima.f2.dao.IEmployeDAO;
import isima.f2.model.Employe; import isima.f2.model.Employe;
@Service
public class ImpEmploye implements IEmploye { public class ImpEmploye implements IEmploye {
@Autowired
private IEmployeDAO employeService;
@Override @Override
public Employe ajouterEmploye(Employe employe) { public Employe ajouterEmploye(Employe employe) {
// TODO Auto-generated method stub return employeService.save(employe);
return null;
} }
@Override @Override
......
...@@ -18,7 +18,7 @@ public class ImpPoubelle implements IPoubelle{ ...@@ -18,7 +18,7 @@ public class ImpPoubelle implements IPoubelle{
@Override @Override
public Poubelle ajouterPoubelle(Poubelle poubelle) { public Poubelle ajouterPoubelle(Poubelle poubelle) {
return null; return poubelleDAO.save(poubelle);
} }
@Override @Override
......
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<Object> iterator = weather.iterator();
while (iterator.hasNext()) {
JSONObject weatherJson = (JSONObject)iterator.next();
return weatherJson.get("main").toString() ;
}
return null ;
}
}
Markdown is supported
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