Blame view

src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java 2.74 KB
fd5e74ca   jcartign   First student ver...
1
2
  package fr.plil.sio.persistence.jpa;
  
bb4bc541   rvangrev   TP terminé
3
  import fr.plil.sio.persistence.api.Group;
fd5e74ca   jcartign   First student ver...
4
5
6
  import fr.plil.sio.persistence.api.Right;
  import fr.plil.sio.persistence.api.User;
  import fr.plil.sio.persistence.api.UserService;
bb4bc541   rvangrev   TP terminé
7
8
  import java.util.List;
  import org.springframework.beans.factory.annotation.Autowired;
fd5e74ca   jcartign   First student ver...
9
10
11
12
  import org.springframework.stereotype.Service;
  
  @Service
  public class UserServiceJpa implements UserService {
bb4bc541   rvangrev   TP terminé
13
14
15
16
17
18
19
20
21
22
      
      @Autowired
      private UserRepository userRepository;
      
      @Autowired
      private GroupRepository groupRepository;
      
      @Autowired
      private RightRepository rightRepository;
      
fd5e74ca   jcartign   First student ver...
23
24
      @Override
      public User create(String name, String groupName) {
bb4bc541   rvangrev   TP terminé
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
          
          if(name==null || groupName==null){
              throw new IllegalArgumentException("name cannot be null");
          }
          
          Group group = new Group();
          group = groupRepository.findByName(groupName);
          User user = userRepository.findByName(name);
          
          if(group==null){
              throw new IllegalArgumentException("group trouve avec le find by name null");
          }
          
          if(user!=null){
              throw new IllegalStateException("a user with the same name already exists");
          }
          
          user = new User();
          user.setName(name);
          
          if(group!=null)
              user.setGroup(group);
          
          group.getUsers().add(user);
          
          userRepository.save(user);
          
          return user;
fd5e74ca   jcartign   First student ver...
53
54
55
56
      }
  
      @Override
      public boolean delete(String name) {
bb4bc541   rvangrev   TP terminé
57
58
59
60
61
62
63
64
65
          if(name == null){
              throw new IllegalArgumentException("name cannot be null");
          }
          User user = findByName(name);
          if(user==null){
              return false;
          }
          userRepository.delete(user.getId());
          return true;
fd5e74ca   jcartign   First student ver...
66
67
68
69
      }
  
      @Override
      public User findByName(String name) {
bb4bc541   rvangrev   TP terminé
70
71
72
73
          if(name==null){
              throw new IllegalArgumentException("name cannot be null");
          }
          return userRepository.findByName(name);
fd5e74ca   jcartign   First student ver...
74
75
76
77
      }
  
      @Override
      public boolean isUserHasRight(String userName, Right right) {
bb4bc541   rvangrev   TP terminé
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
          if(userName==null){
              throw new IllegalArgumentException("userName cannot be null");
          }
          if(right==null){
              throw new IllegalArgumentException("right cannot be null");
          }
          
          if(rightRepository.findByName(right.getName()).size() == 0){
              throw new IllegalArgumentException("right does not exists");
          }
          
          User user = userRepository.findByName(userName);
          Group group = user.getGroup();
          List<Right> list = group.getRights();
          
          if(list.contains(right) || list.contains(right.getParent())){
              return true;
          }
          
          
fd5e74ca   jcartign   First student ver...
98
99
100
          return false;
      }
  }