1
|
package vldc.aswi.service;
|
2
|
|
3
|
import lombok.extern.slf4j.Slf4j;
|
4
|
import org.springframework.beans.factory.annotation.Autowired;
|
5
|
import org.springframework.context.event.ContextRefreshedEvent;
|
6
|
import org.springframework.context.event.EventListener;
|
7
|
import org.springframework.core.annotation.Order;
|
8
|
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
9
|
import org.springframework.security.core.userdetails.UserDetails;
|
10
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
11
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
12
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
13
|
import org.springframework.stereotype.Service;
|
14
|
import vldc.aswi.dao.RoleRepository;
|
15
|
import vldc.aswi.dao.UserRepository;
|
16
|
import vldc.aswi.domain.Role;
|
17
|
import vldc.aswi.domain.User;
|
18
|
import vldc.aswi.model.login.WebCredentials;
|
19
|
|
20
|
import javax.transaction.Transactional;
|
21
|
import java.util.LinkedList;
|
22
|
import java.util.List;
|
23
|
|
24
|
/**
|
25
|
* Manager for User.
|
26
|
*/
|
27
|
@Service
|
28
|
@Slf4j
|
29
|
public class UserManagerImpl implements UserManager, UserDetailsService {
|
30
|
|
31
|
@Autowired
|
32
|
private PasswordEncoder encoder;
|
33
|
|
34
|
/** Autowired user repository. */
|
35
|
@Autowired
|
36
|
private UserRepository userRepository;
|
37
|
|
38
|
/** Autowired role repository. */
|
39
|
@Autowired
|
40
|
private RoleRepository roleRepository;
|
41
|
|
42
|
/**
|
43
|
* Initialization setup for user manager.
|
44
|
* Check if table "Uzivatel" exists.
|
45
|
*/
|
46
|
@EventListener(classes = {
|
47
|
ContextRefreshedEvent.class,
|
48
|
})
|
49
|
@Order(2)
|
50
|
@Transactional
|
51
|
public void setup() {
|
52
|
try {
|
53
|
if (this.userRepository.count() == 0) {
|
54
|
// Just checking if table exists.
|
55
|
}
|
56
|
}
|
57
|
catch (InvalidDataAccessResourceUsageException e) {
|
58
|
log.error("Table \"Uzivatel\" did not exists in database!");
|
59
|
System.exit(1);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Converts role name to Spring role name
|
65
|
* @param role role which name should be converted
|
66
|
* @return string with Spring role
|
67
|
*/
|
68
|
private String toSpringRole(Role role) {
|
69
|
return role.getName();
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Load user details by his username.
|
74
|
* @param username Username of user.
|
75
|
* @return UserDetails of user.
|
76
|
* @throws UsernameNotFoundException Exception.
|
77
|
*/
|
78
|
@Override
|
79
|
@Transactional
|
80
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
81
|
User user = this.userRepository.findByUsername(username);
|
82
|
if (user == null) {
|
83
|
throw new UsernameNotFoundException("Invalid username!");
|
84
|
}
|
85
|
|
86
|
WebCredentials credentials = new WebCredentials(user.getUsername(), user.getPassword());
|
87
|
|
88
|
credentials.addRole(toSpringRole(user.getRole()));
|
89
|
|
90
|
return credentials;
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Get all users from database.
|
95
|
* @return List of users.
|
96
|
*/
|
97
|
@Override
|
98
|
public List<User> getUsers() {
|
99
|
List<User> retVal = new LinkedList<>();
|
100
|
this.userRepository.findAll().forEach(retVal::add);
|
101
|
return retVal;
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Add new user into database.
|
106
|
* @param username - login username for user.
|
107
|
* @param password - password for user.
|
108
|
* @return ID of newly created user.
|
109
|
*/
|
110
|
@Override
|
111
|
public Long addUser(String username, String password, Long roleId) {
|
112
|
String hashed = this.encoder.encode(password);
|
113
|
User user = new User(username, hashed);
|
114
|
user.setRole(this.roleRepository.getById(roleId));
|
115
|
this.userRepository.save(user);
|
116
|
return user.getId();
|
117
|
}
|
118
|
}
|