Commit 8f05ee77b40a3cff7a9eb75f4659666136f3c9b1

Authored by Geoffrey PREUD'HOMME
1 parent dcfbf6e6

API /role et amélioration /personne

api.raml
... ... @@ -6,6 +6,9 @@ version: v1
6 6 securitySchemes:
7 7 - oauth_2_0:
8 8 settings:
  9 + authorizationUri: /oauth2/authorize
  10 + accessTokenUri: /oauth2/token
  11 + authorizationGrants: [ code, token ]
9 12 description: Il faut un token OAuth 2.0 pour utiliser cette API
10 13 type: OAuth 2.0
11 14 describedBy:
... ... @@ -128,7 +131,6 @@ securitySchemes:
128 131 type: date
129 132 example: "14-Feb-1997"
130 133 required: false
131   - example: 1997-02-14
132 134 login:
133 135 displayName: Login Polytech
134 136 type: string
... ... @@ -179,7 +181,6 @@ securitySchemes:
179 181 type: string
180 182 required: false
181 183 example: etudiant
182   - default: etudiant
183 184 responses:
184 185 201:
185 186 description: Personne ajoutée avec succès
... ...
src/main/java/etunicorn/Application.java
1 1 package etunicorn;
2 2  
  3 +import org.springframework.boot.CommandLineRunner;
3 4 import org.springframework.boot.SpringApplication;
4 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
  6 +import org.springframework.context.annotation.Bean;
5 7  
6 8 /**
7 9 * Created by geoffrey on 28/01/17.
... ... @@ -11,4 +13,14 @@ public class Application {
11 13 public static void main(String[] args) {
12 14 SpringApplication.run(Application.class, args);
13 15 }
  16 +
  17 + @Bean
  18 + public CommandLineRunner demo(PermissionRepository permissionRepository) {
  19 + return (args) -> {
  20 + permissionRepository.save(new Permission("ROLE_ADMIN"));
  21 + permissionRepository.save(new Permission("CONSO_ADMIN"));
  22 + permissionRepository.save(new Permission("EVNMT_ADMIN"));
  23 + // ...
  24 + };
  25 + }
14 26 }
... ...
src/main/java/etunicorn/Permission.java
1 1 package etunicorn;
2 2  
  3 +import javax.persistence.Entity;
  4 +import javax.persistence.Id;
  5 +
3 6 /**
4 7 * Created by geoffrey on 28/01/17.
5 8 */
  9 +@Entity
6 10 public class Permission {
  11 +
  12 + @Id
7 13 private String nom;
8 14  
  15 + public Permission() {
  16 + }
  17 +
  18 + public Permission(String nom) {
  19 + this.nom = nom;
  20 + }
9 21 public String getNom() {
10 22 return nom;
11 23 }
... ...
src/main/java/etunicorn/UserDetailsService.java renamed to src/main/java/etunicorn/PermissionRepository.java
1 1 package etunicorn;
2 2  
3   -import org.springframework.security.core.userdetails.UserDetails;
4   -import org.springframework.security.core.userdetails.UsernameNotFoundException;
  3 +import org.springframework.data.repository.CrudRepository;
5 4  
6 5 /**
7 6 * Created by geoffrey on 29/01/17.
8 7 */
9   -public interface UserDetailsService {
10   - UserDetails loadByUsername(String s) throws UsernameNotFoundException;
  8 +public interface PermissionRepository extends CrudRepository<Permission, Long> {
  9 + Permission findByNom(String nom);
11 10 }
... ...
src/main/java/etunicorn/Personne.java
1 1 package etunicorn;
2 2  
3   -import javax.persistence.Entity;
4   -import javax.persistence.GeneratedValue;
5   -import javax.persistence.GenerationType;
6   -import javax.persistence.Id;
  3 +import javax.persistence.*;
7 4 import java.util.Date;
8 5  
9 6 /**
... ... @@ -16,10 +13,17 @@ public class Personne {
16 13 @GeneratedValue(strategy = GenerationType.AUTO)
17 14 private int id;
18 15  
  16 + @Column(unique = true)
19 17 private String carte;
  18 +
20 19 private Date naissance;
  20 +
  21 + @Column(unique = true)
21 22 private String login;
22   - // private Role role;
  23 +
  24 + @ManyToOne
  25 + private Role role;
  26 +
23 27  
24 28 public Personne() {
25 29 }
... ... @@ -56,11 +60,11 @@ public class Personne {
56 60 this.login = login;
57 61 }
58 62  
59   - // public Role getRole() {
60   - // return role;
61   - // }
  63 + public Role getRole() {
  64 + return role;
  65 + }
62 66  
63   - // public void setRole(Role role) {
64   - // this.role = role;
65   - // }
  67 + public void setRole(Role role) {
  68 + this.role = role;
  69 + }
66 70 }
... ...
src/main/java/etunicorn/PersonneController.java
1 1 package etunicorn;
2 2  
3 3 import org.springframework.beans.factory.annotation.Autowired;
  4 +import org.springframework.dao.DataIntegrityViolationException;
4 5 import org.springframework.http.HttpStatus;
5 6 import org.springframework.http.ResponseEntity;
6 7 import org.springframework.web.bind.annotation.PathVariable;
7 8 import org.springframework.web.bind.annotation.RequestParam;
8   -import org.springframework.web.bind.annotation.ResponseBody;
9 9 import org.springframework.web.bind.annotation.RestController;
10 10  
11 11 import java.math.BigDecimal;
12   -import java.text.DateFormat;
13   -import java.text.ParseException;
14   -import java.text.SimpleDateFormat;
15 12 import java.util.Date;
16 13 import java.util.List;
17   -import java.util.Locale;
18 14  
19 15 /**
20 16 * Created by geoffrey on 28/01/17.
... ... @@ -25,14 +21,15 @@ public class PersonneController implements etunicorn.generated.PersonneControlle
25 21 @Autowired
26 22 private PersonneRepository personneRepository;
27 23  
  24 + @Autowired
  25 + private RoleRepository roleRepository;
  26 +
28 27 @Override
29 28 public ResponseEntity<?> getPersonne() {
30 29 return new ResponseEntity<List>((List) this.personneRepository.findAll(), HttpStatus.OK);
31 30 }
32 31  
33   - @Override
34   - 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) {
35   - Personne personne = new Personne();
  32 + private ResponseEntity<?> mergePersonne(Personne personne, String carte, Date naissance, String login, String role) {
36 33 if (carte != null) {
37 34 personne.setCarte(carte);
38 35 }
... ... @@ -42,11 +39,28 @@ public class PersonneController implements etunicorn.generated.PersonneControlle
42 39 if (login != null) {
43 40 personne.setLogin(login);
44 41 }
45   - this.personneRepository.save(personne);
  42 + if (role != null) {
  43 + Role roleObj = roleRepository.findByNom(role);
  44 + personne.setRole(roleObj);
  45 + if (roleObj == null) {
  46 + return new ResponseEntity<Object>("Rôle inconnu", HttpStatus.NOT_FOUND);
  47 + }
  48 + }
  49 + try {
  50 + this.personneRepository.save(personne);
  51 + } catch (DataIntegrityViolationException e) {
  52 + return new ResponseEntity<Object>(HttpStatus.CONFLICT);
  53 + }
46 54 return new ResponseEntity<Object>(personne, HttpStatus.CREATED);
47 55 }
48 56  
49 57 @Override
  58 + public ResponseEntity<?> updatePersonne(@RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) {
  59 + Personne personne = new Personne();
  60 + return mergePersonne(personne, carte, naissance, login, role);
  61 + }
  62 +
  63 + @Override
50 64 public ResponseEntity<?> getPersonneById(@PathVariable BigDecimal idPersonne) {
51 65 Personne personne = personneRepository.findById(idPersonne.intValueExact());
52 66 if (personne == null) {
... ... @@ -61,17 +75,7 @@ public class PersonneController implements etunicorn.generated.PersonneControlle
61 75 if (personne == null) {
62 76 return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
63 77 }
64   - if (carte != null) {
65   - personne.setCarte(carte);
66   - }
67   - if (naissance != null) {
68   - personne.setNaissance(naissance);
69   - }
70   - if (login != null) {
71   - personne.setLogin(login);
72   - }
73   - this.personneRepository.save(personne);
74   - return new ResponseEntity<Object>(personne, HttpStatus.CREATED);
  78 + return mergePersonne(personne, carte, naissance, login, role);
75 79 }
76 80  
77 81 @Override
... ...
src/main/java/etunicorn/PersonneRepository.java
... ... @@ -2,13 +2,12 @@ package etunicorn;
2 2  
3 3 import org.springframework.data.repository.CrudRepository;
4 4  
5   -import java.math.BigDecimal;
6   -import java.util.List;
7   -
8 5 /**
9 6 * Created by geoffrey on 29/01/17.
10 7 */
11 8 public interface PersonneRepository extends CrudRepository<Personne, Long> {
12 9 Personne findByLogin(String login);
  10 +
  11 + Personne findByCarte(String carte);
13 12 Personne findById(Integer id);
14 13 }
... ...
src/main/java/etunicorn/Role.java
1 1 package etunicorn;
2 2  
  3 +import javax.persistence.Column;
  4 +import javax.persistence.Entity;
  5 +import javax.persistence.Id;
  6 +import javax.persistence.OneToMany;
  7 +import java.util.Collection;
  8 +import java.util.List;
  9 +
3 10 /**
4 11 * Created by geoffrey on 28/01/17.
5 12 */
  13 +@Entity
6 14 public class Role {
  15 + @Id
  16 + @Column(unique = true)
7 17 private String nom = "etudiant";
8 18  
  19 + @OneToMany
  20 + private List<Permission> permissions;
  21 +
  22 + public Role() {
  23 + }
  24 +
9 25 public String getNom() {
10 26 return nom;
11 27 }
... ... @@ -13,4 +29,20 @@ public class Role {
13 29 public void setNom(String nom) {
14 30 this.nom = nom;
15 31 }
  32 +
  33 + public Collection<Permission> getPermissions() {
  34 + return permissions;
  35 + }
  36 +
  37 + public void setPermissions(List<Permission> permissions) {
  38 + this.permissions = permissions;
  39 + }
  40 +
  41 + public void addPermission(Permission permission) {
  42 + this.permissions.add(permission);
  43 + }
  44 +
  45 + public void delPermission(Permission permission) {
  46 + this.permissions.remove(permission);
  47 + }
16 48 }
... ...
src/main/java/etunicorn/RoleController.java 0 → 100644
... ... @@ -0,0 +1,94 @@
  1 +package etunicorn;
  2 +
  3 +
  4 +import org.springframework.beans.factory.annotation.Autowired;
  5 +import org.springframework.dao.DataIntegrityViolationException;
  6 +import org.springframework.http.HttpStatus;
  7 +import org.springframework.http.ResponseEntity;
  8 +import org.springframework.web.bind.annotation.PathVariable;
  9 +import org.springframework.web.bind.annotation.RequestParam;
  10 +import org.springframework.web.bind.annotation.RestController;
  11 +
  12 +import java.util.List;
  13 +
  14 +/**
  15 + * Created by geoffrey on 29/01/17.
  16 + */
  17 +@RestController
  18 +public class RoleController implements etunicorn.generated.RoleController {
  19 + @Autowired
  20 + private RoleRepository roleRepository;
  21 +
  22 + @Autowired
  23 + private PermissionRepository permissionRepository;
  24 +
  25 + @Override
  26 + public ResponseEntity<?> getRole() {
  27 + return new ResponseEntity<List>((List) roleRepository.findAll(), HttpStatus.OK);
  28 + }
  29 +
  30 + @Override
  31 + public ResponseEntity<?> updateRole(@RequestParam String nom) {
  32 + Role oldRole = roleRepository.findByNom(nom);
  33 + if (oldRole != null) {
  34 + return new ResponseEntity<Object>(HttpStatus.CONFLICT);
  35 + }
  36 + Role role = new Role();
  37 + role.setNom(nom);
  38 + try {
  39 + roleRepository.save(role);
  40 + } catch (DataIntegrityViolationException e) {
  41 + return new ResponseEntity<Object>(HttpStatus.CONFLICT);
  42 + }
  43 + return new ResponseEntity<Object>(role, HttpStatus.CREATED);
  44 + }
  45 +
  46 + @Override
  47 + public ResponseEntity<?> deleteRoleById(@PathVariable String nomRole) {
  48 + Role role = roleRepository.findByNom(nomRole);
  49 + if (role == null) {
  50 + return new ResponseEntity<Object>("Rôle inconnu", HttpStatus.NOT_FOUND);
  51 + }
  52 + roleRepository.delete(role);
  53 + return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
  54 + }
  55 +
  56 + @Override
  57 + public ResponseEntity<?> updateRoleById(@PathVariable String nomRole, @RequestParam String nom) {
  58 + Role role = roleRepository.findByNom(nomRole);
  59 + if (role == null) {
  60 + return new ResponseEntity<Object>("Rôle inconnu", HttpStatus.NOT_FOUND);
  61 + }
  62 + Permission permission = permissionRepository.findByNom(nom);
  63 + if (permission == null) {
  64 + return new ResponseEntity<Object>("Permission inconnue", HttpStatus.NOT_FOUND);
  65 + }
  66 + role.addPermission(permission);
  67 + try {
  68 + roleRepository.save(role);
  69 + } catch (DataIntegrityViolationException e) {
  70 + // Si la permission était déjà là, on fait rien
  71 + }
  72 + return new ResponseEntity<Object>(role, HttpStatus.ACCEPTED);
  73 + }
  74 +
  75 + @Override
  76 + public ResponseEntity<?> deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable String nomRole) {
  77 + Role role = roleRepository.findByNom(nomRole);
  78 + if (role == null) {
  79 + return new ResponseEntity<Object>("Rôle inconnu", HttpStatus.NOT_FOUND);
  80 + }
  81 + Permission permission = permissionRepository.findByNom(nomPermission);
  82 + if (permission == null) {
  83 + return new ResponseEntity<Object>("Permission inconnue", HttpStatus.NOT_FOUND);
  84 + }
  85 + role.delPermission(permission);
  86 + roleRepository.save(role);
  87 + return new ResponseEntity<Object>(role, HttpStatus.ACCEPTED);
  88 + }
  89 +
  90 + @Override
  91 + public ResponseEntity<?> getPermission() {
  92 + return new ResponseEntity<List>((List) permissionRepository.findAll(), HttpStatus.OK);
  93 + }
  94 +}
... ...
src/main/java/etunicorn/RoleRepository.java 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +package etunicorn;
  2 +
  3 +import org.springframework.data.repository.CrudRepository;
  4 +
  5 +/**
  6 + * Created by geoffrey on 29/01/17.
  7 + */
  8 +public interface RoleRepository extends CrudRepository<Role, Long> {
  9 + Role findByNom(String nom);
  10 +}
... ...
src/main/java/etunicorn/generated/PersonneController.java
1 1  
2 2 package etunicorn.generated;
3 3  
  4 +import org.springframework.http.ResponseEntity;
  5 +import org.springframework.web.bind.annotation.*;
  6 +
4 7 import java.math.BigDecimal;
5 8 import java.util.Date;
6   -import org.springframework.http.ResponseEntity;
7   -import org.springframework.web.bind.annotation.PathVariable;
8   -import org.springframework.web.bind.annotation.RequestMapping;
9   -import org.springframework.web.bind.annotation.RequestMethod;
10   -import org.springframework.web.bind.annotation.RequestParam;
11   -import org.springframework.web.bind.annotation.RestController;
12 9  
13 10  
14 11 /**
... ... @@ -26,56 +23,56 @@ public interface PersonneController {
26 23 *
27 24 */
28 25 @RequestMapping(value = "", method = RequestMethod.GET)
29   - public ResponseEntity<?> getPersonne();
  26 + ResponseEntity<?> getPersonne();
30 27  
31 28 /**
32 29 * Ajoute une nouvelle personne
33 30 *
34 31 */
35 32 @RequestMapping(value = "", method = RequestMethod.POST)
36   - public ResponseEntity<?> updatePersonne(
37   - @RequestParam(required = false)
38   - String carte,
39   - @RequestParam(required = false)
40   - Date naissance,
41   - @RequestParam(required = false)
42   - String login,
43   - @RequestParam(required = false, defaultValue = "etudiant")
44   - String role);
  33 + ResponseEntity<?> updatePersonne(
  34 + @RequestParam(required = false)
  35 + String carte,
  36 + @RequestParam(required = false)
  37 + Date naissance,
  38 + @RequestParam(required = false)
  39 + String login,
  40 + @RequestParam(required = false)
  41 + String role);
45 42  
46 43 /**
47 44 * Obtenir les infos sur une personne. Nécessite COMPTE_ADMIN
48 45 *
49 46 */
50 47 @RequestMapping(value = "/{idPersonne}", method = RequestMethod.GET)
51   - public ResponseEntity<?> getPersonneById(
52   - @PathVariable
53   - BigDecimal idPersonne);
  48 + ResponseEntity<?> getPersonneById(
  49 + @PathVariable
  50 + BigDecimal idPersonne);
54 51  
55 52 /**
56 53 * Modifer les infos d'une personne. Nécessite COMPTE_ADMIN
57 54 *
58 55 */
59 56 @RequestMapping(value = "/{idPersonne}", method = RequestMethod.PUT)
60   - public ResponseEntity<?> updatePersonneById(
61   - @PathVariable
62   - BigDecimal idPersonne,
63   - @RequestParam(required = false)
64   - String carte,
65   - @RequestParam(required = false)
66   - Date naissance,
67   - @RequestParam(required = false)
68   - String login,
69   - @RequestParam(required = false)
70   - String role);
  57 + ResponseEntity<?> updatePersonneById(
  58 + @PathVariable
  59 + BigDecimal idPersonne,
  60 + @RequestParam(required = false)
  61 + String carte,
  62 + @RequestParam(required = false)
  63 + Date naissance,
  64 + @RequestParam(required = false)
  65 + String login,
  66 + @RequestParam(required = false)
  67 + String role);
71 68  
72 69 /**
73 70 * Obtenir les infos sur une personne. Nécessite COMPTE_ADMIN
74 71 *
75 72 */
76 73 @RequestMapping(value = "/{idPersonne}", method = RequestMethod.DELETE)
77   - public ResponseEntity<?> deletePersonneById(
78   - @PathVariable
79   - BigDecimal idPersonne);
  74 + ResponseEntity<?> deletePersonneById(
  75 + @PathVariable
  76 + BigDecimal idPersonne);
80 77  
81 78 }
... ...
src/main/main.iml
... ... @@ -25,5 +25,6 @@
25 25 <orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.6.RELEASE" level="project" />
26 26 <orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.1.9" level="project" />
27 27 <orderEntry type="library" name="Maven: org.slf4j:log4j-over-slf4j:1.7.22" level="project" />
  28 + <orderEntry type="library" name="Maven: org.springframework:spring-tx:4.3.6.RELEASE" level="project" />
28 29 </component>
29 30 -</module>
  31 +</module>
30 32 \ No newline at end of file
... ...