Blame view

src/main/java/fr/plil/sio/web/mvc/UserServiceImpl.java 1.26 KB
2afad654   Julien Iguchi-Cartigny   deal with userForm
1
2
  package fr.plil.sio.web.mvc;
  
322c9502   Julien Cartigny   Ad Spring securit...
3
  import org.springframework.security.crypto.password.PasswordEncoder;
2afad654   Julien Iguchi-Cartigny   deal with userForm
4
5
6
7
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import javax.annotation.Resource;
322c9502   Julien Cartigny   Ad Spring securit...
8
  import java.util.HashSet;
d0481dcb   Julien Cartigny   Support for user ...
9
  import java.util.List;
322c9502   Julien Cartigny   Ad Spring securit...
10
  import java.util.Set;
2afad654   Julien Iguchi-Cartigny   deal with userForm
11
12
13
14
15
16
17
  
  @Service("userService")
  public class UserServiceImpl implements UserService {
  
      @Resource
      private UserRepository userRepository;
  
322c9502   Julien Cartigny   Ad Spring securit...
18
19
20
21
22
23
      @Resource
      private RoleRepository roleRepository;
  
      @Resource
      private PasswordEncoder passwordEncoder;
  
2afad654   Julien Iguchi-Cartigny   deal with userForm
24
25
26
27
28
      @Override
      @Transactional
      public User createUser(String username, String password) {
          User user = new User();
          user.setUsername(username);
322c9502   Julien Cartigny   Ad Spring securit...
29
30
31
32
          user.setPassword(passwordEncoder.encode(password));
          Set<Role> roles = new HashSet<>();
          roles.add(roleRepository.findByName("ROLE_USER"));
          user.setRoles(roles);
2afad654   Julien Iguchi-Cartigny   deal with userForm
33
34
35
          userRepository.save(user);
          return user;
      }
322c9502   Julien Cartigny   Ad Spring securit...
36
37
  
      @Override
4abe2e3d   Julien Cartigny   Add @Transactionn...
38
      @Transactional(readOnly = true)
322c9502   Julien Cartigny   Ad Spring securit...
39
40
41
      public User findByUsername(String username) {
          return userRepository.findByUsername(username);
      }
d0481dcb   Julien Cartigny   Support for user ...
42
43
  
      @Override
4abe2e3d   Julien Cartigny   Add @Transactionn...
44
      @Transactional(readOnly = true)
d0481dcb   Julien Cartigny   Support for user ...
45
46
47
      public List<User> findAll() {
          return userRepository.findAll();
      }
2afad654   Julien Iguchi-Cartigny   deal with userForm
48
  }