-
Notifications
You must be signed in to change notification settings - Fork 2
/
UserServiceImpl.java
115 lines (98 loc) · 4.06 KB
/
UserServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package org.folio.consortia.service.impl;
import static org.folio.consortia.utils.TenantContextUtils.prepareContextForTenant;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import org.folio.consortia.client.UsersClient;
import org.folio.consortia.domain.dto.Personal;
import org.folio.consortia.domain.dto.User;
import org.folio.consortia.domain.dto.UserType;
import org.folio.consortia.exception.ConsortiumClientException;
import org.folio.consortia.exception.ResourceNotFoundException;
import org.folio.consortia.service.UserService;
import org.folio.consortia.utils.HelperUtils;
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.FolioModuleMetadata;
import org.folio.spring.scope.FolioExecutionContextSetter;
import org.springframework.stereotype.Service;
import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
@Service
@Log4j2
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
private static final String USER_ID = "userId";
private final UsersClient usersClient;
private final FolioExecutionContext folioExecutionContext;
private final FolioModuleMetadata folioModuleMetadata;
private static final Integer RANDOM_STRING_COUNT = 5;
private static final String ORIGINAL_TENANT_ID_REF_ID = "originaltenantid";
@Override
public User createUser(User user) {
log.info("Creating user with id {}.", user.getId());
usersClient.saveUser(user);
return user;
}
public void updateUser(User user) {
log.info("Updating User '{}'.", user.getId());
usersClient.updateUser(user.getId(), user);
}
@Override
public User getById(UUID userId) {
try {
log.info("Getting user by userId {}.", userId);
return usersClient.getUserById(String.valueOf(userId));
} catch (FeignException.NotFound e) {
log.info("User with userId {} does not exist in schema, going to use new one", userId);
return new User();
} catch (FeignException.Forbidden e) {
throw new ConsortiumClientException(e);
} catch (FeignException e) {
throw new IllegalStateException(e);
}
}
@Override
public Optional<User> getByUsername(String username) {
return usersClient.getUsersByQuery("username==" + username)
.getUsers()
.stream()
.findFirst();
}
@Override
public List<User> getUsersByQuery(String query, int offset, int limit) {
return usersClient.getUserCollection(query, offset, limit).getUsers();
}
@Override
public void deleteById(String userId) {
usersClient.deleteUser(userId);
}
public User prepareShadowUser(UUID userId, String tenantId) {
try (var ignored = new FolioExecutionContextSetter(prepareContextForTenant(tenantId, folioModuleMetadata, folioExecutionContext))) {
log.info("prepareShadowUser:: Try to get user of tenant={} ", folioExecutionContext.getTenantId());
var realUser = usersClient.getUserById(userId.toString());
if (Objects.isNull(realUser.getId())) {
log.warn("Could not find real user with id: {} in his home tenant: {}", userId.toString(), tenantId);
throw new ResourceNotFoundException(USER_ID, userId.toString());
}
var shadowUser = new User();
shadowUser.setId(userId.toString());
shadowUser.setUsername(String.format("%s_%s", realUser.getUsername(), HelperUtils.randomString(RANDOM_STRING_COUNT)));
shadowUser.setType(UserType.SHADOW.getName());
shadowUser.setActive(true);
if (Objects.nonNull(realUser.getPersonal())) {
// these firstname, lastname fields needed to correctly build UI metadata objects
shadowUser.setPersonal(new Personal()
.firstName(realUser.getPersonal().getFirstName())
.lastName(realUser.getPersonal().getLastName())
.email(realUser.getPersonal().getEmail())
.preferredContactTypeId(realUser.getPersonal().getPreferredContactTypeId())
);
}
shadowUser.setCustomFields(Map.of(ORIGINAL_TENANT_ID_REF_ID, tenantId));
return shadowUser;
}
}
}