|
1
|
|
|
package unicon.matthews.admin.service; |
|
2
|
|
|
|
|
3
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
4
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
5
|
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
6
|
|
|
import org.springframework.security.core.AuthenticationException; |
|
7
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
8
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
|
9
|
|
|
import org.springframework.stereotype.Service; |
|
10
|
|
|
import unicon.matthews.admin.AdminUser; |
|
11
|
|
|
import unicon.matthews.admin.AdminUserConfig; |
|
12
|
|
|
import unicon.matthews.admin.endpoint.input.UserDTO; |
|
13
|
|
|
import unicon.matthews.admin.service.repository.AdminUserRepository; |
|
14
|
|
|
|
|
15
|
|
|
import java.util.Optional; |
|
16
|
|
|
|
|
17
|
|
|
@Service |
|
18
|
|
|
@ConfigurationProperties(prefix = "matthews.users") |
|
19
|
|
|
public class AdminUserService { |
|
20
|
|
|
|
|
21
|
|
|
@Autowired |
|
22
|
|
|
private AdminUserRepository adminUserRepository; |
|
23
|
|
|
|
|
24
|
|
|
@Autowired |
|
25
|
|
|
private BCryptPasswordEncoder passwordEncoder; |
|
26
|
|
|
|
|
27
|
|
|
@Autowired |
|
28
|
|
|
private AdminUserConfig adminUserConfig; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a new super admin user. |
|
32
|
|
|
* |
|
33
|
|
|
* @param superAdminUser |
|
34
|
|
|
* @return |
|
35
|
|
|
* @ |
|
36
|
|
|
*/ |
|
37
|
|
|
public AdminUser createUser(final AdminUser superAdminUser) { |
|
38
|
|
|
final String encodedPassword = (adminUserConfig.isEncrypted())? |
|
39
|
|
|
passwordEncoder.encode(superAdminUser.getPassword()) : superAdminUser.getPassword(); |
|
40
|
|
|
superAdminUser.setPassword(encodedPassword); |
|
41
|
|
|
adminUserRepository.save(superAdminUser); |
|
42
|
|
|
return superAdminUser; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new admin user for given tenant and organization |
|
47
|
|
|
* |
|
48
|
|
|
* @param user |
|
49
|
|
|
* @return |
|
50
|
|
|
*/ |
|
51
|
|
|
public AdminUser createAdminUser(final UserDTO user) { |
|
52
|
|
|
final String encodedPassword = (adminUserConfig.isEncrypted())? |
|
53
|
|
|
passwordEncoder.encode(user.getPassword()) : user.getPassword(); |
|
54
|
|
|
AdminUser adminUser = new AdminUser.Builder() |
|
55
|
|
|
.withUserName(user.getUsername()) |
|
56
|
|
|
.withPassword(encodedPassword) |
|
57
|
|
|
.withEmailAddress(user.getEmailAddress()) |
|
58
|
|
|
.withOrgId(user.getOrgId()) |
|
59
|
|
|
.withTenantId(user.getTenantId()) |
|
60
|
|
|
.withSuperAdmin(Boolean.FALSE) |
|
61
|
|
|
.build(); |
|
62
|
|
|
adminUserRepository.save(adminUser); |
|
63
|
|
|
return adminUser; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Authenticate any admin user by username and password |
|
68
|
|
|
* |
|
69
|
|
|
* @param userName |
|
70
|
|
|
* @param password |
|
71
|
|
|
* @return |
|
72
|
|
|
*/ |
|
73
|
|
|
public AdminUser authenticateUser(final String userName, final String password) throws AuthenticationException { |
|
74
|
|
|
AdminUser adminUser = adminUserRepository.findByUsername(userName) |
|
75
|
|
|
.orElseThrow(() -> new UsernameNotFoundException(String.format("User with username=%s was not found", userName))); |
|
76
|
|
|
if (adminUser != null && (adminUserConfig.isEncrypted()) ? passwordEncoder.matches(password, adminUser.getPassword()) : password.equals(adminUser.getPassword())) { |
|
|
|
|
|
|
77
|
|
|
return adminUser; |
|
78
|
|
|
} else { |
|
79
|
|
|
throw new BadCredentialsException(String.format("User with the supplied credentials cannot be authenticated", userName)); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/*** |
|
85
|
|
|
* Find admin user by user name |
|
86
|
|
|
* @param userName |
|
87
|
|
|
* @return |
|
88
|
|
|
*/ |
|
89
|
|
|
public Optional<AdminUser> findByUserName(final String userName) { |
|
90
|
|
|
return adminUserRepository.findByUsername(userName); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|