Commit bb4bc541bbd346cbccefb8cd833708758e84d338
1 parent
01d94034
TP terminé
Showing
1 changed file
with
74 additions
and
3 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java
1 | 1 | package fr.plil.sio.persistence.jpa; |
2 | 2 | |
3 | +import fr.plil.sio.persistence.api.Group; | |
3 | 4 | import fr.plil.sio.persistence.api.Right; |
4 | 5 | import fr.plil.sio.persistence.api.User; |
5 | 6 | import fr.plil.sio.persistence.api.UserService; |
7 | +import java.util.List; | |
8 | +import org.springframework.beans.factory.annotation.Autowired; | |
6 | 9 | import org.springframework.stereotype.Service; |
7 | 10 | |
8 | 11 | @Service |
9 | 12 | public class UserServiceJpa implements UserService { |
13 | + | |
14 | + @Autowired | |
15 | + private UserRepository userRepository; | |
16 | + | |
17 | + @Autowired | |
18 | + private GroupRepository groupRepository; | |
19 | + | |
20 | + @Autowired | |
21 | + private RightRepository rightRepository; | |
22 | + | |
10 | 23 | @Override |
11 | 24 | public User create(String name, String groupName) { |
12 | - return null; | |
25 | + | |
26 | + if(name==null || groupName==null){ | |
27 | + throw new IllegalArgumentException("name cannot be null"); | |
28 | + } | |
29 | + | |
30 | + Group group = new Group(); | |
31 | + group = groupRepository.findByName(groupName); | |
32 | + User user = userRepository.findByName(name); | |
33 | + | |
34 | + if(group==null){ | |
35 | + throw new IllegalArgumentException("group trouve avec le find by name null"); | |
36 | + } | |
37 | + | |
38 | + if(user!=null){ | |
39 | + throw new IllegalStateException("a user with the same name already exists"); | |
40 | + } | |
41 | + | |
42 | + user = new User(); | |
43 | + user.setName(name); | |
44 | + | |
45 | + if(group!=null) | |
46 | + user.setGroup(group); | |
47 | + | |
48 | + group.getUsers().add(user); | |
49 | + | |
50 | + userRepository.save(user); | |
51 | + | |
52 | + return user; | |
13 | 53 | } |
14 | 54 | |
15 | 55 | @Override |
16 | 56 | public boolean delete(String name) { |
17 | - return false; | |
57 | + if(name == null){ | |
58 | + throw new IllegalArgumentException("name cannot be null"); | |
59 | + } | |
60 | + User user = findByName(name); | |
61 | + if(user==null){ | |
62 | + return false; | |
63 | + } | |
64 | + userRepository.delete(user.getId()); | |
65 | + return true; | |
18 | 66 | } |
19 | 67 | |
20 | 68 | @Override |
21 | 69 | public User findByName(String name) { |
22 | - return null; | |
70 | + if(name==null){ | |
71 | + throw new IllegalArgumentException("name cannot be null"); | |
72 | + } | |
73 | + return userRepository.findByName(name); | |
23 | 74 | } |
24 | 75 | |
25 | 76 | @Override |
26 | 77 | public boolean isUserHasRight(String userName, Right right) { |
78 | + if(userName==null){ | |
79 | + throw new IllegalArgumentException("userName cannot be null"); | |
80 | + } | |
81 | + if(right==null){ | |
82 | + throw new IllegalArgumentException("right cannot be null"); | |
83 | + } | |
84 | + | |
85 | + if(rightRepository.findByName(right.getName()).size() == 0){ | |
86 | + throw new IllegalArgumentException("right does not exists"); | |
87 | + } | |
88 | + | |
89 | + User user = userRepository.findByName(userName); | |
90 | + Group group = user.getGroup(); | |
91 | + List<Right> list = group.getRights(); | |
92 | + | |
93 | + if(list.contains(right) || list.contains(right.getParent())){ | |
94 | + return true; | |
95 | + } | |
96 | + | |
97 | + | |
27 | 98 | return false; |
28 | 99 | } |
29 | 100 | } | ... | ... |