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 | 1 | package com.PFE.ServerManager; |
2 | 2 | |
3 | +import org.springframework.beans.factory.annotation.Autowired; | |
4 | + | |
3 | 5 | import javax.persistence.*; |
4 | 6 | import java.util.Set; |
5 | 7 | |
... | ... | @@ -8,7 +10,12 @@ import java.util.Set; |
8 | 10 | public class Customer{ |
9 | 11 | |
10 | 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 | 19 | private Integer customer_id; |
13 | 20 | |
14 | 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 | 4 | import org.springframework.stereotype.Controller; |
5 | 5 | import org.springframework.web.bind.annotation.*; |
6 | 6 | import org.springframework.web.servlet.ModelAndView; |
7 | + | |
8 | +import java.util.Arrays; | |
7 | 9 | import java.util.HashSet; |
8 | 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
9 | 11 | import org.springframework.security.core.context.SecurityContextHolder; |
10 | 12 | import org.springframework.security.core.Authentication; |
11 | 13 | |
14 | +import javax.persistence.SequenceGenerator; | |
15 | + | |
12 | 16 | @Controller |
13 | 17 | public class MainController { |
14 | 18 | |
... | ... | @@ -53,16 +57,11 @@ public class MainController { |
53 | 57 | Customer n = new Customer(); |
54 | 58 | n.setPseudo(pseudo); |
55 | 59 | n.setPassword(bCryptPasswordEncoder.encode(password)); |
60 | + n.setId((int)(customerRepository.count()+1)); | |
56 | 61 | n.setActive(1); |
57 | 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 | 66 | if(temp != null) { |
68 | 67 | modelAndView.addObject("ok", "l'utilisateur existe déjà"); | ... | ... |
PFE06/src/main/java/com/PFE/ServerManager/Role.java
1 | 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 | 5 | @Entity |
11 | 6 | @Table(name = "role") |
12 | 7 | public class Role { |
13 | 8 | @Id |
9 | + @Column(name = "role_id",columnDefinition = "serial") | |
14 | 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 | 14 | private Integer role_id; |
17 | 15 | |
18 | 16 | @Column(name = "role") | ... | ... |
PFE06/src/main/resources/application.properties
... | ... | @@ -7,15 +7,11 @@ spring.jpa.hibernate.ddl-auto=create |
7 | 7 | #"create" recrée la base de données à chaque lancement |
8 | 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 | 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 | 16 | # montre les communications JPA avec la BDD |
21 | 17 | spring.jpa.show-sql = true | ... | ... |
PFE06/src/main/resources/data.sql
1 | 1 | /* ce fichier doit être placé dans les ressources afin d'être utilisé */ |
2 | 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 | 4 | INSERT INTO "customer_role" VALUES (1,1); |
7 | -INSERT INTO "customer_role" VALUES (2,1); | |
8 | -*/ | |
9 | 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 | 8 | \ No newline at end of file | ... | ... |