Last change
on this file since 1ad8e64 was 1ad8e64, checked in by Ema <ema_spirova@…>, 3 years ago |
spring security
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | package finki.diplomska.tripplanner.service.impl;
|
---|
2 |
|
---|
3 | import finki.diplomska.tripplanner.models.User;
|
---|
4 | import finki.diplomska.tripplanner.models.dto.UserDto;
|
---|
5 | import finki.diplomska.tripplanner.models.exceptions.UsernameAlreadyExistsException;
|
---|
6 | import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository;
|
---|
7 | import finki.diplomska.tripplanner.service.UserService;
|
---|
8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
9 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class UserServiceImpl implements UserService {
|
---|
15 |
|
---|
16 | @Autowired
|
---|
17 | private JpaUserRepository userRepository;
|
---|
18 |
|
---|
19 | @Autowired
|
---|
20 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
21 |
|
---|
22 | public User saveUser (User newUser){
|
---|
23 | try{
|
---|
24 | newUser.setPassword(bCryptPasswordEncoder.encode(newUser.getPassword()));
|
---|
25 | //Username has to be unique (exception)
|
---|
26 | newUser.setUsername(newUser.getUsername());
|
---|
27 | // Make sure that password and confirmPassword match
|
---|
28 | // We don't persist or show the confirmPassword
|
---|
29 | newUser.setConfirmPassword("");
|
---|
30 | return this.userRepository.save(newUser);
|
---|
31 | }catch(Exception e){
|
---|
32 | throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists");
|
---|
33 | }
|
---|
34 |
|
---|
35 | }
|
---|
36 |
|
---|
37 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.