Blame view

src/main/java/fr/plil/sio/web/mvc/UserServiceImpl.java 1.07 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
9
  import java.util.HashSet;
  import java.util.Set;
2afad654   Julien Iguchi-Cartigny   deal with userForm
10
11
12
13
14
15
16
  
  @Service("userService")
  public class UserServiceImpl implements UserService {
  
      @Resource
      private UserRepository userRepository;
  
322c9502   Julien Cartigny   Ad Spring securit...
17
18
19
20
21
22
      @Resource
      private RoleRepository roleRepository;
  
      @Resource
      private PasswordEncoder passwordEncoder;
  
2afad654   Julien Iguchi-Cartigny   deal with userForm
23
24
25
26
27
      @Override
      @Transactional
      public User createUser(String username, String password) {
          User user = new User();
          user.setUsername(username);
322c9502   Julien Cartigny   Ad Spring securit...
28
29
30
31
          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
32
33
34
          userRepository.save(user);
          return user;
      }
322c9502   Julien Cartigny   Ad Spring securit...
35
36
37
38
39
  
      @Override
      public User findByUsername(String username) {
          return userRepository.findByUsername(username);
      }
2afad654   Julien Iguchi-Cartigny   deal with userForm
40
  }