|
1
|
|
|
package unicon.matthews.oneroster.service; |
|
2
|
|
|
|
|
3
|
|
|
import java.util.*; |
|
4
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
|
|
|
7
|
|
|
import com.mongodb.WriteResult; |
|
8
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
9
|
|
|
import org.json.JSONArray; |
|
|
|
|
|
|
10
|
|
|
import org.json.JSONObject; |
|
11
|
|
|
import org.slf4j.Logger; |
|
12
|
|
|
import org.slf4j.LoggerFactory; |
|
13
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
14
|
|
|
import org.springframework.data.mongodb.core.MongoOperations; |
|
15
|
|
|
import org.springframework.data.mongodb.core.query.Criteria; |
|
|
|
|
|
|
16
|
|
|
import org.springframework.data.mongodb.core.query.Query; |
|
17
|
|
|
import org.springframework.data.mongodb.core.query.Update; |
|
18
|
|
|
import org.springframework.stereotype.Service; |
|
19
|
|
|
|
|
20
|
|
|
import unicon.matthews.Vocabulary; |
|
21
|
|
|
import unicon.matthews.oneroster.User; |
|
22
|
|
|
import unicon.matthews.oneroster.exception.UserNotFoundException; |
|
23
|
|
|
import unicon.matthews.oneroster.service.repository.MongoUser; |
|
24
|
|
|
import unicon.matthews.oneroster.service.repository.MongoUserRepository; |
|
25
|
|
|
|
|
26
|
|
|
import static org.springframework.data.mongodb.core.query.Criteria.where; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @author ggilbert |
|
30
|
|
|
* @author xchopin <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
@Service |
|
33
|
|
|
public class UserService { |
|
34
|
|
|
private static Logger logger = LoggerFactory.getLogger(UserService.class); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
private MongoUserRepository mongoUserRepository; |
|
37
|
|
|
private final MongoOperations mongoOps; |
|
38
|
|
|
|
|
39
|
|
|
@Autowired |
|
40
|
|
|
public UserService(MongoUserRepository mongoUserRepository, MongoOperations mongoOperations) { |
|
41
|
|
|
this.mongoUserRepository = mongoUserRepository; |
|
42
|
|
|
this.mongoOps = mongoOperations; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public User findBySourcedId(final String tenantId, final String orgId, final String userSourcedId) throws UserNotFoundException { |
|
46
|
|
|
|
|
47
|
|
|
MongoUser mongoUser = mongoUserRepository.findByTenantIdAndOrgIdAndUserSourcedIdIgnoreCase(tenantId, orgId, userSourcedId); |
|
48
|
|
|
if (mongoUser == null) { |
|
49
|
|
|
throw new UserNotFoundException("User not found"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return mongoUser.getUser(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Finds and returns all the users that belong to a tenant and an organization given |
|
57
|
|
|
* |
|
58
|
|
|
* @param tenantId an id of a tenant |
|
59
|
|
|
* @param orgId an id of an organization |
|
60
|
|
|
* @return the users |
|
61
|
|
|
*/ |
|
62
|
|
|
public Collection<MongoUser> findAll(final String tenantId, final String orgId) { |
|
63
|
|
|
if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(orgId)) |
|
64
|
|
|
throw new IllegalArgumentException(); |
|
65
|
|
|
|
|
66
|
|
|
return mongoUserRepository.findByTenantIdAndOrgId(tenantId, orgId); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Deletes a user for its id given (combined with tenant and organization) |
|
71
|
|
|
* @param tenantId tenant id |
|
72
|
|
|
* @param orgId organization id |
|
73
|
|
|
* @param userId its Id |
|
74
|
|
|
* @return boolean (if user has been deleted) |
|
75
|
|
|
*/ |
|
76
|
|
|
public boolean delete(final String tenantId, final String orgId, final String userId) { |
|
77
|
|
|
if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(orgId) || StringUtils.isBlank(userId)) |
|
78
|
|
|
throw new IllegalArgumentException(); |
|
79
|
|
|
|
|
80
|
|
|
return mongoUserRepository.deleteByTenantIdAndOrgIdAndUserSourcedIdIgnoreCase(tenantId, orgId, userId) > 0; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Update a user for its id given |
|
85
|
|
|
* @param tenantId tenant id |
|
86
|
|
|
* @param orgId organization id |
|
87
|
|
|
* @param userId its Id |
|
88
|
|
|
* @param object stringified JSON that has the fields and values to edit/add to the user |
|
89
|
|
|
* @return boolean |
|
90
|
|
|
*/ |
|
91
|
|
|
public boolean update(final String tenantId, final String orgId, final String userId, final String object) { |
|
92
|
|
|
if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(orgId) || StringUtils.isBlank(userId) || StringUtils.isBlank(object)) |
|
93
|
|
|
throw new IllegalArgumentException(); |
|
94
|
|
|
|
|
95
|
|
|
final JSONObject obj = new JSONObject(object); |
|
96
|
|
|
|
|
97
|
|
|
if (obj.has("sourcedId")) |
|
98
|
|
|
throw new IllegalArgumentException("sourcedId attribute cannot be edited."); |
|
99
|
|
|
|
|
100
|
|
|
Iterator<String> keys = obj.keys(); |
|
101
|
|
|
Query query = new Query(); |
|
102
|
|
|
query.addCriteria(where("user.sourcedId").is(userId).and("orgId").is(orgId).and("tenantId").is(tenantId)); |
|
103
|
|
|
|
|
104
|
|
|
while( keys.hasNext() ) { |
|
105
|
|
|
String key = keys.next(); |
|
106
|
|
|
Object value = obj.get(key); |
|
107
|
|
|
Update update = null; |
|
108
|
|
|
|
|
109
|
|
|
if (value instanceof JSONObject && !key.equals("metadata")) |
|
110
|
|
|
continue; |
|
111
|
|
|
|
|
112
|
|
|
if (!(value instanceof JSONObject) && key.equals("metadata")) |
|
113
|
|
|
throw new IllegalArgumentException("metadata attribute has to be an object <String : String>"); |
|
114
|
|
|
|
|
115
|
|
|
if (key.equals("metadata")) { |
|
116
|
|
|
JSONObject jsonMetadata = (JSONObject)obj.get(key); |
|
117
|
|
|
Map<String, Object> metadata = new HashMap<>(); |
|
118
|
|
|
Iterator<?> iterator = jsonMetadata.keys(); |
|
119
|
|
|
while (iterator.hasNext()) { |
|
120
|
|
|
String subKey = (String)iterator.next(); |
|
121
|
|
|
Object subValue = jsonMetadata.get(subKey); |
|
122
|
|
|
metadata.put(subKey, subValue); |
|
123
|
|
|
} |
|
124
|
|
|
update = Update.update("user.metadata", metadata); |
|
125
|
|
|
}else{ |
|
126
|
|
|
update = Update.update("user." + key, obj.get(key)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
WriteResult result = mongoOps.updateFirst(query, update, MongoUser.class); |
|
130
|
|
|
if (!result.isUpdateOfExisting()) |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public User save(final String tenantId, final String orgId, User user, boolean check) { |
|
138
|
|
|
if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(orgId) || user == null) |
|
139
|
|
|
throw new IllegalArgumentException(); |
|
140
|
|
|
|
|
141
|
|
|
MongoUser mongoUserToSave, existingMongoUser = null; |
|
142
|
|
|
|
|
143
|
|
|
if (check) |
|
144
|
|
|
existingMongoUser = mongoUserRepository.findByTenantIdAndOrgIdAndUserSourcedIdIgnoreCase(tenantId, orgId, user.getSourcedId()); |
|
145
|
|
|
|
|
146
|
|
|
if (existingMongoUser == null) { |
|
147
|
|
|
mongoUserToSave = new MongoUser.Builder() |
|
148
|
|
|
.withTenantId(tenantId) |
|
149
|
|
|
.withOrgId(orgId) |
|
150
|
|
|
.withUser(fromUser(user, tenantId)) |
|
151
|
|
|
.build(); |
|
152
|
|
|
} else { |
|
153
|
|
|
mongoUserToSave = new MongoUser.Builder() |
|
154
|
|
|
.withId(existingMongoUser.getId()) |
|
155
|
|
|
.withTenantId(tenantId) |
|
156
|
|
|
.withOrgId(orgId) |
|
157
|
|
|
.withUser(fromUser(user, tenantId)) |
|
158
|
|
|
.build(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
MongoUser saved = mongoUserRepository.save(mongoUserToSave); |
|
162
|
|
|
|
|
163
|
|
|
return saved.getUser(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
private User fromUser(User from, final String tenantId) { |
|
167
|
|
|
|
|
168
|
|
|
User user = null; |
|
169
|
|
|
|
|
170
|
|
|
if (from != null && StringUtils.isNotBlank(tenantId)) { |
|
171
|
|
|
Map<String, String> extensions = new HashMap<>(); |
|
172
|
|
|
extensions.put(Vocabulary.TENANT, tenantId); |
|
173
|
|
|
|
|
174
|
|
|
Map<String, String> metadata = from.getMetadata(); |
|
175
|
|
|
|
|
176
|
|
|
if (metadata != null && !metadata.isEmpty()) |
|
177
|
|
|
extensions.putAll(metadata); |
|
178
|
|
|
|
|
179
|
|
|
String sourcedId = from.getSourcedId(); |
|
180
|
|
|
|
|
181
|
|
|
if (StringUtils.isBlank(sourcedId)) |
|
182
|
|
|
sourcedId = UUID.randomUUID().toString(); |
|
183
|
|
|
|
|
184
|
|
|
user = new User.Builder() |
|
185
|
|
|
.withEmail(from.getEmail()) |
|
186
|
|
|
.withFamilyName(from.getFamilyName()) |
|
187
|
|
|
.withGivenName(from.getGivenName()) |
|
188
|
|
|
.withIdentifier(from.getIdentifier()) |
|
189
|
|
|
.withMetadata(metadata) |
|
190
|
|
|
.withPhone(from.getPhone()) |
|
191
|
|
|
.withRole(from.getRole()) |
|
192
|
|
|
.withSms(from.getSms()) |
|
193
|
|
|
.withSourcedId(sourcedId) |
|
194
|
|
|
.withStatus(from.getStatus()) |
|
195
|
|
|
.withUserId(from.getUserId()) |
|
196
|
|
|
.withUsername(from.getUsername()) |
|
197
|
|
|
.build(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return user; |
|
201
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|