AppConfig.java
2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.PFE.ServerManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import javax.annotation.PostConstruct;
import java.io.*;
@Component
public class AppConfig {
@Autowired
CustomerRepository customerRepository;
@Autowired
RoleRepository roleRepository;
@Autowired
TeamRepository teamRepository;
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;
@PostConstruct
public void init() {
System.out.println("============= Configuration de l'application =============");
/* Ajout des rôles si non présent */
if(roleRepository.count() != 2) {
roleRepository.deleteAll();
Role roleAdmin = new Role();
roleAdmin.setRoleId(1);
roleAdmin.setRole("ADMIN");
roleRepository.save(roleAdmin);
Role roleUser = new Role();
roleUser.setRoleId(2);
roleUser.setRole("USER");
roleRepository.save(roleUser);
}
/* Ajout d'un utilisateur ADMIN avec les informations du fichier de conf*/
Yaml yaml = new Yaml(new Constructor(ConfYAML.class));
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("app.conf"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ConfYAML conf = yaml.load(inputStream);
Customer n = new Customer();
n.setEmail(conf.getAdminEmail());
n.setPassword(bCryptPasswordEncoder.encode(conf.getAdminPassword()));
n.setCustomerId((int)(customerRepository.count() + 1));
n.setActive(1);
Customer temp = customerRepository.findByEmail(conf.getAdminEmail());
Role userRole = roleRepository.findByRole("ADMIN");
n.addRole(userRole);
if(temp == null) {
customerRepository.save(n);
}
/* Partie sur les groupes */
Team g = new Team();
g.setTeamId((int)(teamRepository.count() +1));
g.setTeam("Labo1");
g.addCustomer(n);
teamRepository.save(g);
}
}