Commit 57e157a4f59db6a654894b0571b5eb0872f6cc99
1 parent
d7c9beff
optimisation de la communication avec la BDD
il est désormais posssible d'ajouter des utilisateurs dans le fichier data.sql. Il n'y a plus de duplication du role Admin. Le role User à été ajouté également.
Showing
5 changed files
with
26 additions
and
28 deletions
Show diff stats
PFE06/src/main/java/com/PFE/ServerManager/Customer.java
1 | package com.PFE.ServerManager; | 1 | package com.PFE.ServerManager; |
2 | 2 | ||
3 | +import org.springframework.beans.factory.annotation.Autowired; | ||
4 | + | ||
3 | import javax.persistence.*; | 5 | import javax.persistence.*; |
4 | import java.util.Set; | 6 | import java.util.Set; |
5 | 7 | ||
@@ -8,7 +10,12 @@ import java.util.Set; | @@ -8,7 +10,12 @@ import java.util.Set; | ||
8 | public class Customer{ | 10 | public class Customer{ |
9 | 11 | ||
10 | @Id | 12 | @Id |
11 | - @GeneratedValue(strategy=GenerationType.AUTO) | 13 | + //@GeneratedValue(strategy=GenerationType.AUTO) |
14 | + @Column(columnDefinition = "serial") | ||
15 | + /*la méthode ci-dessous oblige à connaitre le nombre de lignes déjà présentes dans la table customer au lancement du programme*/ | ||
16 | + /*@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customer_seq_gen") | ||
17 | + @SequenceGenerator(name = "customer_seq_gen", sequenceName = "customer_id_seq", allocationSize=1, initialValue=3)*/ | ||
18 | + @Basic(optional = false) | ||
12 | private Integer customer_id; | 19 | private Integer customer_id; |
13 | 20 | ||
14 | @Column(name = "pseudo") | 21 | @Column(name = "pseudo") |
PFE06/src/main/java/com/PFE/ServerManager/MainController.java
@@ -4,11 +4,15 @@ import org.springframework.beans.factory.annotation.Autowired; | @@ -4,11 +4,15 @@ import org.springframework.beans.factory.annotation.Autowired; | ||
4 | import org.springframework.stereotype.Controller; | 4 | import org.springframework.stereotype.Controller; |
5 | import org.springframework.web.bind.annotation.*; | 5 | import org.springframework.web.bind.annotation.*; |
6 | import org.springframework.web.servlet.ModelAndView; | 6 | import org.springframework.web.servlet.ModelAndView; |
7 | + | ||
8 | +import java.util.Arrays; | ||
7 | import java.util.HashSet; | 9 | import java.util.HashSet; |
8 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
9 | import org.springframework.security.core.context.SecurityContextHolder; | 11 | import org.springframework.security.core.context.SecurityContextHolder; |
10 | import org.springframework.security.core.Authentication; | 12 | import org.springframework.security.core.Authentication; |
11 | 13 | ||
14 | +import javax.persistence.SequenceGenerator; | ||
15 | + | ||
12 | @Controller | 16 | @Controller |
13 | public class MainController { | 17 | public class MainController { |
14 | 18 | ||
@@ -53,16 +57,11 @@ public class MainController { | @@ -53,16 +57,11 @@ public class MainController { | ||
53 | Customer n = new Customer(); | 57 | Customer n = new Customer(); |
54 | n.setPseudo(pseudo); | 58 | n.setPseudo(pseudo); |
55 | n.setPassword(bCryptPasswordEncoder.encode(password)); | 59 | n.setPassword(bCryptPasswordEncoder.encode(password)); |
60 | + n.setId((int)(customerRepository.count()+1)); | ||
56 | n.setActive(1); | 61 | n.setActive(1); |
57 | Customer temp = customerRepository.findByPseudo(pseudo); | 62 | Customer temp = customerRepository.findByPseudo(pseudo); |
58 | - | ||
59 | - Role role = new Role(); // l'utilisation d'un role au lieu d'un tableau semble valide, ormis la première ligne de la table qui n'est pas utilisé | ||
60 | - role.setRole("ADMIN"); | ||
61 | - | ||
62 | - HashSet<Role> hset = new HashSet<Role>(); | ||
63 | - hset.add(role); | ||
64 | - | ||
65 | - n.setRoles(hset); | 63 | + Role userRole = roleRepository.findByRole("ADMIN"); |
64 | + n.setRoles(new HashSet<Role>(Arrays.asList(userRole))); | ||
66 | 65 | ||
67 | if(temp != null) { | 66 | if(temp != null) { |
68 | modelAndView.addObject("ok", "l'utilisateur existe déjà"); | 67 | modelAndView.addObject("ok", "l'utilisateur existe déjà"); |
PFE06/src/main/java/com/PFE/ServerManager/Role.java
1 | package com.PFE.ServerManager; | 1 | package com.PFE.ServerManager; |
2 | 2 | ||
3 | -import javax.persistence.Column; | ||
4 | -import javax.persistence.Entity; | ||
5 | -import javax.persistence.GeneratedValue; | ||
6 | -import javax.persistence.GenerationType; | ||
7 | -import javax.persistence.Id; | ||
8 | -import javax.persistence.Table; | 3 | +import javax.persistence.*; |
9 | 4 | ||
10 | @Entity | 5 | @Entity |
11 | @Table(name = "role") | 6 | @Table(name = "role") |
12 | public class Role { | 7 | public class Role { |
13 | @Id | 8 | @Id |
9 | + @Column(name = "role_id",columnDefinition = "serial") | ||
14 | @GeneratedValue(strategy = GenerationType.AUTO) | 10 | @GeneratedValue(strategy = GenerationType.AUTO) |
15 | - @Column(name = "role_id") | 11 | + // inutile d'utiliser les lignes ci-dessous à moins que l'utilisateur n'ajoute des roles par une page Web |
12 | + //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "role_seq_gen") | ||
13 | + //@SequenceGenerator(name = "role_seq_gen", sequenceName = "role_id_seq", allocationSize=1, initialValue=2) | ||
16 | private Integer role_id; | 14 | private Integer role_id; |
17 | 15 | ||
18 | @Column(name = "role") | 16 | @Column(name = "role") |
PFE06/src/main/resources/application.properties
@@ -7,15 +7,11 @@ spring.jpa.hibernate.ddl-auto=create | @@ -7,15 +7,11 @@ spring.jpa.hibernate.ddl-auto=create | ||
7 | #"create" recrée la base de données à chaque lancement | 7 | #"create" recrée la base de données à chaque lancement |
8 | #"update" met à jour la base données | 8 | #"update" met à jour la base données |
9 | 9 | ||
10 | -#Simon Postgres config : | ||
11 | -spring.datasource.url=jdbc:postgresql://localhost:3306/sql_only | 10 | +#Postgres config : |
11 | +spring.datasource.url=jdbc:postgresql://localhost:5432/sql_only | ||
12 | spring.datasource.username=postgres | 12 | spring.datasource.username=postgres |
13 | -spring.datasource.password=admin | 13 | +spring.datasource.password=idalurf123 |
14 | 14 | ||
15 | -#Antoine Postgres config : | ||
16 | -#spring.datasource.url=jdbc:postgresql://localhost:3302/sql_only | ||
17 | -#spring.datasource.username=postgres | ||
18 | -#spring.datasource.password=admin | ||
19 | 15 | ||
20 | # montre les communications JPA avec la BDD | 16 | # montre les communications JPA avec la BDD |
21 | spring.jpa.show-sql = true | 17 | spring.jpa.show-sql = true |
PFE06/src/main/resources/data.sql
1 | /* ce fichier doit être placé dans les ressources afin d'être utilisé */ | 1 | /* ce fichier doit être placé dans les ressources afin d'être utilisé */ |
2 | INSERT INTO "role" VALUES (1,'ADMIN'); | 2 | INSERT INTO "role" VALUES (1,'ADMIN'); |
3 | -/* | ||
4 | -INSERT INTO "customer" VALUES (1,1,'Feutrier','Simon'); | ||
5 | -INSERT INTO "customer" VALUES (2,1,'Duquenoy','Antoine'); | 3 | +INSERT INTO "customer" VALUES (1,1,'$2a$10$GflhaD2IYuErynuOlxS2W.Gp1kXksVdiSviYN/lDYCsuH.lVm6Ph2','admin'); |
6 | INSERT INTO "customer_role" VALUES (1,1); | 4 | INSERT INTO "customer_role" VALUES (1,1); |
7 | -INSERT INTO "customer_role" VALUES (2,1); | ||
8 | -*/ | ||
9 | \ No newline at end of file | 5 | \ No newline at end of file |
6 | +INSERT INTO "customer" VALUES (2,1,'$2a$10$GflhaD2IYuErynuOlxS2W.Gp1kXksVdiSviYN/lDYCsuH.lVm6Ph2','root'); | ||
7 | +INSERT INTO "customer_role" VALUES (2,1); | ||
10 | \ No newline at end of file | 8 | \ No newline at end of file |