diff --git a/api.raml b/api.raml index f43c3a1..868ef88 100644 --- a/api.raml +++ b/api.raml @@ -6,6 +6,9 @@ version: v1 securitySchemes: - oauth_2_0: settings: + authorizationUri: /oauth2/authorize + accessTokenUri: /oauth2/token + authorizationGrants: [ code, token ] description: Il faut un token OAuth 2.0 pour utiliser cette API type: OAuth 2.0 describedBy: @@ -128,7 +131,6 @@ securitySchemes: type: date example: "14-Feb-1997" required: false - example: 1997-02-14 login: displayName: Login Polytech type: string @@ -179,7 +181,6 @@ securitySchemes: type: string required: false example: etudiant - default: etudiant responses: 201: description: Personne ajoutée avec succès diff --git a/src/main/java/etunicorn/Application.java b/src/main/java/etunicorn/Application.java index 404c19b..3d883f7 100644 --- a/src/main/java/etunicorn/Application.java +++ b/src/main/java/etunicorn/Application.java @@ -1,7 +1,9 @@ package etunicorn; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; /** * Created by geoffrey on 28/01/17. @@ -11,4 +13,14 @@ public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } + + @Bean + public CommandLineRunner demo(PermissionRepository permissionRepository) { + return (args) -> { + permissionRepository.save(new Permission("ROLE_ADMIN")); + permissionRepository.save(new Permission("CONSO_ADMIN")); + permissionRepository.save(new Permission("EVNMT_ADMIN")); + // ... + }; + } } diff --git a/src/main/java/etunicorn/Permission.java b/src/main/java/etunicorn/Permission.java index 3c327d0..bde81c2 100644 --- a/src/main/java/etunicorn/Permission.java +++ b/src/main/java/etunicorn/Permission.java @@ -1,11 +1,23 @@ package etunicorn; +import javax.persistence.Entity; +import javax.persistence.Id; + /** * Created by geoffrey on 28/01/17. */ +@Entity public class Permission { + + @Id private String nom; + public Permission() { + } + + public Permission(String nom) { + this.nom = nom; + } public String getNom() { return nom; } diff --git a/src/main/java/etunicorn/PermissionRepository.java b/src/main/java/etunicorn/PermissionRepository.java new file mode 100644 index 0000000..0ef0087 --- /dev/null +++ b/src/main/java/etunicorn/PermissionRepository.java @@ -0,0 +1,10 @@ +package etunicorn; + +import org.springframework.data.repository.CrudRepository; + +/** + * Created by geoffrey on 29/01/17. + */ +public interface PermissionRepository extends CrudRepository { + Permission findByNom(String nom); +} diff --git a/src/main/java/etunicorn/Personne.java b/src/main/java/etunicorn/Personne.java index ada8cd2..ec4557c 100644 --- a/src/main/java/etunicorn/Personne.java +++ b/src/main/java/etunicorn/Personne.java @@ -1,9 +1,6 @@ package etunicorn; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; import java.util.Date; /** @@ -16,10 +13,17 @@ public class Personne { @GeneratedValue(strategy = GenerationType.AUTO) private int id; + @Column(unique = true) private String carte; + private Date naissance; + + @Column(unique = true) private String login; - // private Role role; + + @ManyToOne + private Role role; + public Personne() { } @@ -56,11 +60,11 @@ public class Personne { this.login = login; } - // public Role getRole() { - // return role; - // } + public Role getRole() { + return role; + } - // public void setRole(Role role) { - // this.role = role; - // } + public void setRole(Role role) { + this.role = role; + } } diff --git a/src/main/java/etunicorn/PersonneController.java b/src/main/java/etunicorn/PersonneController.java index f4f41ae..3f8c3af 100644 --- a/src/main/java/etunicorn/PersonneController.java +++ b/src/main/java/etunicorn/PersonneController.java @@ -1,20 +1,16 @@ package etunicorn; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; -import java.util.Locale; /** * Created by geoffrey on 28/01/17. @@ -25,14 +21,15 @@ public class PersonneController implements etunicorn.generated.PersonneControlle @Autowired private PersonneRepository personneRepository; + @Autowired + private RoleRepository roleRepository; + @Override public ResponseEntity getPersonne() { return new ResponseEntity((List) this.personneRepository.findAll(), HttpStatus.OK); } - @Override - public ResponseEntity updatePersonne(@RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false, defaultValue = "etudiant") String role) { - Personne personne = new Personne(); + private ResponseEntity mergePersonne(Personne personne, String carte, Date naissance, String login, String role) { if (carte != null) { personne.setCarte(carte); } @@ -42,11 +39,28 @@ public class PersonneController implements etunicorn.generated.PersonneControlle if (login != null) { personne.setLogin(login); } - this.personneRepository.save(personne); + if (role != null) { + Role roleObj = roleRepository.findByNom(role); + personne.setRole(roleObj); + if (roleObj == null) { + return new ResponseEntity("Rôle inconnu", HttpStatus.NOT_FOUND); + } + } + try { + this.personneRepository.save(personne); + } catch (DataIntegrityViolationException e) { + return new ResponseEntity(HttpStatus.CONFLICT); + } return new ResponseEntity(personne, HttpStatus.CREATED); } @Override + public ResponseEntity updatePersonne(@RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) { + Personne personne = new Personne(); + return mergePersonne(personne, carte, naissance, login, role); + } + + @Override public ResponseEntity getPersonneById(@PathVariable BigDecimal idPersonne) { Personne personne = personneRepository.findById(idPersonne.intValueExact()); if (personne == null) { @@ -61,17 +75,7 @@ public class PersonneController implements etunicorn.generated.PersonneControlle if (personne == null) { return new ResponseEntity(HttpStatus.NOT_FOUND); } - if (carte != null) { - personne.setCarte(carte); - } - if (naissance != null) { - personne.setNaissance(naissance); - } - if (login != null) { - personne.setLogin(login); - } - this.personneRepository.save(personne); - return new ResponseEntity(personne, HttpStatus.CREATED); + return mergePersonne(personne, carte, naissance, login, role); } @Override diff --git a/src/main/java/etunicorn/PersonneRepository.java b/src/main/java/etunicorn/PersonneRepository.java index 8e55d61..76d6281 100644 --- a/src/main/java/etunicorn/PersonneRepository.java +++ b/src/main/java/etunicorn/PersonneRepository.java @@ -2,13 +2,12 @@ package etunicorn; import org.springframework.data.repository.CrudRepository; -import java.math.BigDecimal; -import java.util.List; - /** * Created by geoffrey on 29/01/17. */ public interface PersonneRepository extends CrudRepository { Personne findByLogin(String login); + + Personne findByCarte(String carte); Personne findById(Integer id); } diff --git a/src/main/java/etunicorn/Role.java b/src/main/java/etunicorn/Role.java index ad8ffc4..c37e429 100644 --- a/src/main/java/etunicorn/Role.java +++ b/src/main/java/etunicorn/Role.java @@ -1,11 +1,27 @@ package etunicorn; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import java.util.Collection; +import java.util.List; + /** * Created by geoffrey on 28/01/17. */ +@Entity public class Role { + @Id + @Column(unique = true) private String nom = "etudiant"; + @OneToMany + private List permissions; + + public Role() { + } + public String getNom() { return nom; } @@ -13,4 +29,20 @@ public class Role { public void setNom(String nom) { this.nom = nom; } + + public Collection getPermissions() { + return permissions; + } + + public void setPermissions(List permissions) { + this.permissions = permissions; + } + + public void addPermission(Permission permission) { + this.permissions.add(permission); + } + + public void delPermission(Permission permission) { + this.permissions.remove(permission); + } } diff --git a/src/main/java/etunicorn/RoleController.java b/src/main/java/etunicorn/RoleController.java new file mode 100644 index 0000000..d36d21c --- /dev/null +++ b/src/main/java/etunicorn/RoleController.java @@ -0,0 +1,94 @@ +package etunicorn; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * Created by geoffrey on 29/01/17. + */ +@RestController +public class RoleController implements etunicorn.generated.RoleController { + @Autowired + private RoleRepository roleRepository; + + @Autowired + private PermissionRepository permissionRepository; + + @Override + public ResponseEntity getRole() { + return new ResponseEntity((List) roleRepository.findAll(), HttpStatus.OK); + } + + @Override + public ResponseEntity updateRole(@RequestParam String nom) { + Role oldRole = roleRepository.findByNom(nom); + if (oldRole != null) { + return new ResponseEntity(HttpStatus.CONFLICT); + } + Role role = new Role(); + role.setNom(nom); + try { + roleRepository.save(role); + } catch (DataIntegrityViolationException e) { + return new ResponseEntity(HttpStatus.CONFLICT); + } + return new ResponseEntity(role, HttpStatus.CREATED); + } + + @Override + public ResponseEntity deleteRoleById(@PathVariable String nomRole) { + Role role = roleRepository.findByNom(nomRole); + if (role == null) { + return new ResponseEntity("Rôle inconnu", HttpStatus.NOT_FOUND); + } + roleRepository.delete(role); + return new ResponseEntity(HttpStatus.NO_CONTENT); + } + + @Override + public ResponseEntity updateRoleById(@PathVariable String nomRole, @RequestParam String nom) { + Role role = roleRepository.findByNom(nomRole); + if (role == null) { + return new ResponseEntity("Rôle inconnu", HttpStatus.NOT_FOUND); + } + Permission permission = permissionRepository.findByNom(nom); + if (permission == null) { + return new ResponseEntity("Permission inconnue", HttpStatus.NOT_FOUND); + } + role.addPermission(permission); + try { + roleRepository.save(role); + } catch (DataIntegrityViolationException e) { + // Si la permission était déjà là, on fait rien + } + return new ResponseEntity(role, HttpStatus.ACCEPTED); + } + + @Override + public ResponseEntity deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable String nomRole) { + Role role = roleRepository.findByNom(nomRole); + if (role == null) { + return new ResponseEntity("Rôle inconnu", HttpStatus.NOT_FOUND); + } + Permission permission = permissionRepository.findByNom(nomPermission); + if (permission == null) { + return new ResponseEntity("Permission inconnue", HttpStatus.NOT_FOUND); + } + role.delPermission(permission); + roleRepository.save(role); + return new ResponseEntity(role, HttpStatus.ACCEPTED); + } + + @Override + public ResponseEntity getPermission() { + return new ResponseEntity((List) permissionRepository.findAll(), HttpStatus.OK); + } +} diff --git a/src/main/java/etunicorn/RoleRepository.java b/src/main/java/etunicorn/RoleRepository.java new file mode 100644 index 0000000..40c3381 --- /dev/null +++ b/src/main/java/etunicorn/RoleRepository.java @@ -0,0 +1,10 @@ +package etunicorn; + +import org.springframework.data.repository.CrudRepository; + +/** + * Created by geoffrey on 29/01/17. + */ +public interface RoleRepository extends CrudRepository { + Role findByNom(String nom); +} diff --git a/src/main/java/etunicorn/UserDetailsService.java b/src/main/java/etunicorn/UserDetailsService.java deleted file mode 100644 index 36242f7..0000000 --- a/src/main/java/etunicorn/UserDetailsService.java +++ /dev/null @@ -1,11 +0,0 @@ -package etunicorn; - -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UsernameNotFoundException; - -/** - * Created by geoffrey on 29/01/17. - */ -public interface UserDetailsService { - UserDetails loadByUsername(String s) throws UsernameNotFoundException; -} diff --git a/src/main/java/etunicorn/generated/PersonneController.java b/src/main/java/etunicorn/generated/PersonneController.java index 3061a1e..a466c90 100644 --- a/src/main/java/etunicorn/generated/PersonneController.java +++ b/src/main/java/etunicorn/generated/PersonneController.java @@ -1,14 +1,11 @@ package etunicorn.generated; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + import java.math.BigDecimal; import java.util.Date; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PathVariable; -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; /** @@ -26,56 +23,56 @@ public interface PersonneController { * */ @RequestMapping(value = "", method = RequestMethod.GET) - public ResponseEntity getPersonne(); + ResponseEntity getPersonne(); /** * Ajoute une nouvelle personne * */ @RequestMapping(value = "", method = RequestMethod.POST) - public ResponseEntity updatePersonne( - @RequestParam(required = false) - String carte, - @RequestParam(required = false) - Date naissance, - @RequestParam(required = false) - String login, - @RequestParam(required = false, defaultValue = "etudiant") - String role); + ResponseEntity updatePersonne( + @RequestParam(required = false) + String carte, + @RequestParam(required = false) + Date naissance, + @RequestParam(required = false) + String login, + @RequestParam(required = false) + String role); /** * Obtenir les infos sur une personne. Nécessite COMPTE_ADMIN * */ @RequestMapping(value = "/{idPersonne}", method = RequestMethod.GET) - public ResponseEntity getPersonneById( - @PathVariable - BigDecimal idPersonne); + ResponseEntity getPersonneById( + @PathVariable + BigDecimal idPersonne); /** * Modifer les infos d'une personne. Nécessite COMPTE_ADMIN * */ @RequestMapping(value = "/{idPersonne}", method = RequestMethod.PUT) - public ResponseEntity updatePersonneById( - @PathVariable - BigDecimal idPersonne, - @RequestParam(required = false) - String carte, - @RequestParam(required = false) - Date naissance, - @RequestParam(required = false) - String login, - @RequestParam(required = false) - String role); + ResponseEntity updatePersonneById( + @PathVariable + BigDecimal idPersonne, + @RequestParam(required = false) + String carte, + @RequestParam(required = false) + Date naissance, + @RequestParam(required = false) + String login, + @RequestParam(required = false) + String role); /** * Obtenir les infos sur une personne. Nécessite COMPTE_ADMIN * */ @RequestMapping(value = "/{idPersonne}", method = RequestMethod.DELETE) - public ResponseEntity deletePersonneById( - @PathVariable - BigDecimal idPersonne); + ResponseEntity deletePersonneById( + @PathVariable + BigDecimal idPersonne); } diff --git a/src/main/main.iml b/src/main/main.iml index e9ea5f1..bf4edbb 100644 --- a/src/main/main.iml +++ b/src/main/main.iml @@ -25,5 +25,6 @@ + - + \ No newline at end of file -- libgit2 0.21.2