|
1
|
|
|
package unicon.matthews.oneroster.endpoint; |
|
2
|
|
|
|
|
3
|
|
|
import java.time.LocalDateTime; |
|
4
|
|
|
import java.time.ZoneId; |
|
5
|
|
|
import java.util.Collection; |
|
6
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
7
|
|
|
import org.springframework.http.HttpHeaders; |
|
8
|
|
|
import org.springframework.http.HttpStatus; |
|
9
|
|
|
import org.springframework.http.ResponseEntity; |
|
10
|
|
|
import org.springframework.web.bind.annotation.*; |
|
11
|
|
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
|
12
|
|
|
|
|
13
|
|
|
import unicon.matthews.caliper.Event; |
|
14
|
|
|
import unicon.matthews.caliper.exception.EventNotFoundException; |
|
15
|
|
|
import unicon.matthews.caliper.service.EventService; |
|
16
|
|
|
import unicon.matthews.common.exception.BadRequestException; |
|
17
|
|
|
import unicon.matthews.entity.MongoUserMappingRepository; |
|
18
|
|
|
import unicon.matthews.entity.UserMapping; |
|
19
|
|
|
import unicon.matthews.oneroster.Enrollment; |
|
20
|
|
|
import unicon.matthews.oneroster.Result; |
|
21
|
|
|
import unicon.matthews.oneroster.User; |
|
22
|
|
|
import unicon.matthews.oneroster.exception.EnrollmentNotFoundException; |
|
23
|
|
|
import unicon.matthews.oneroster.exception.ExceptionResponse; |
|
|
|
|
|
|
24
|
|
|
import unicon.matthews.oneroster.exception.ResultNotFoundException; |
|
25
|
|
|
import unicon.matthews.oneroster.exception.UserNotFoundException; |
|
26
|
|
|
import unicon.matthews.oneroster.service.EnrollmentService; |
|
27
|
|
|
import unicon.matthews.oneroster.service.ResultService; |
|
28
|
|
|
import unicon.matthews.oneroster.service.UserService; |
|
29
|
|
|
import unicon.matthews.oneroster.service.repository.MongoUser; |
|
30
|
|
|
import unicon.matthews.security.auth.JwtAuthenticationToken; |
|
31
|
|
|
import unicon.matthews.security.model.UserContext; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @author ggilbert |
|
35
|
|
|
* @author xchopin <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
@RestController |
|
38
|
|
|
@RequestMapping("/api/users") |
|
39
|
|
|
public class UserController { |
|
40
|
|
|
|
|
41
|
|
|
private UserService userService; |
|
42
|
|
|
private EnrollmentService enrollmentService; |
|
43
|
|
|
private MongoUserMappingRepository mongoUserMappingRepository; |
|
44
|
|
|
private ResultService resultService; |
|
45
|
|
|
private EventService eventService; |
|
46
|
|
|
|
|
47
|
|
|
@Autowired |
|
48
|
|
|
public UserController(UserService userService, EnrollmentService enrollmentService, MongoUserMappingRepository mongoUserMappingRepository, ResultService resultService, EventService eventService) { |
|
49
|
|
|
this.userService = userService; |
|
50
|
|
|
this.enrollmentService = enrollmentService; |
|
51
|
|
|
this.mongoUserMappingRepository = mongoUserMappingRepository; |
|
52
|
|
|
this.resultService = resultService; |
|
53
|
|
|
this.eventService = eventService; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* POST /api/users |
|
58
|
|
|
* |
|
59
|
|
|
* Inserts a user into the DBMS (MongoDB). |
|
60
|
|
|
* @param token JWT |
|
61
|
|
|
* @param user user to insert |
|
62
|
|
|
* @param check boolean to know if it has to check duplicates (takes more time) |
|
63
|
|
|
* @return HTTP Response |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
@RequestMapping(method = RequestMethod.POST) |
|
|
|
|
|
|
66
|
|
|
public ResponseEntity<?> post(JwtAuthenticationToken token, @RequestBody User user, @RequestParam(value="check", required=false) Boolean check) { |
|
|
|
|
|
|
67
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
68
|
|
|
User savedUser = this.userService.save(userContext.getTenantId(), userContext.getOrgId(), user, (check == null) ? true : check); |
|
69
|
|
|
HttpHeaders httpHeaders = new HttpHeaders(); |
|
70
|
|
|
httpHeaders.setLocation(ServletUriComponentsBuilder |
|
71
|
|
|
.fromCurrentRequest().path("/{id}") |
|
72
|
|
|
.buildAndExpand(savedUser.getSourcedId()).toUri()); |
|
73
|
|
|
|
|
74
|
|
|
return new ResponseEntity<>(savedUser, httpHeaders, HttpStatus.CREATED); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* GET /api/users |
|
79
|
|
|
* |
|
80
|
|
|
* Returns all the users for a tenant id and an organization id given. |
|
81
|
|
|
* @param token a JWT to get authenticated |
|
82
|
|
|
* @return the users |
|
83
|
|
|
* @throws UserNotFoundException |
|
84
|
|
|
*/ |
|
85
|
|
|
@RequestMapping(method = RequestMethod.GET) |
|
86
|
|
|
public Collection<MongoUser> getUsers(JwtAuthenticationToken token) throws UserNotFoundException { |
|
87
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
88
|
|
|
return userService.findAll(userContext.getTenantId(), userContext.getOrgId()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
@RequestMapping(value = "/{userId:.+}", method = RequestMethod.GET) |
|
92
|
|
|
public User getUser(JwtAuthenticationToken token, @PathVariable("userId") final String userId) throws UserNotFoundException { |
|
93
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
94
|
|
|
return userService.findBySourcedId(userContext.getTenantId(), userContext.getOrgId(), userId); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* DELETE /api/users/:id |
|
99
|
|
|
* |
|
100
|
|
|
* Deletes a user for its id given. |
|
101
|
|
|
* @param token a JWT to get authenticated |
|
102
|
|
|
* @param userId id of the aimed user |
|
103
|
|
|
* @return HTTP Response (200 or 404) |
|
104
|
|
|
* @throws UserNotFoundException |
|
105
|
|
|
*/ |
|
106
|
|
|
@RequestMapping(value = "/{userId:.+}", method = RequestMethod.DELETE) |
|
107
|
|
|
public ResponseEntity deleteUser(JwtAuthenticationToken token, @PathVariable("userId") final String userId) throws UserNotFoundException { |
|
108
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
109
|
|
|
return userService.delete(userContext.getTenantId(), userContext.getOrgId(), userId) ? |
|
110
|
|
|
new ResponseEntity<>(HttpStatus.NO_CONTENT) : new ResponseEntity<>(HttpStatus.NOT_FOUND); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* PATCH /api/users/:id |
|
116
|
|
|
* |
|
117
|
|
|
* Updates a user for its id given. |
|
118
|
|
|
* @param token a JWT to get authenticated |
|
119
|
|
|
* @param userId id of the aimed user |
|
120
|
|
|
* @return HTTP Response (200 or 404) |
|
121
|
|
|
* @throws IllegalArgumentException |
|
122
|
|
|
*/ |
|
123
|
|
|
@RequestMapping(value = "/{userId:.+}", method = RequestMethod.PATCH) |
|
124
|
|
|
public ResponseEntity updateUser(JwtAuthenticationToken token, @PathVariable("userId") final String userId, @RequestBody String data) throws IllegalArgumentException { |
|
125
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
126
|
|
|
try { |
|
127
|
|
|
boolean isUpdated = userService.update(userContext.getTenantId(), userContext.getOrgId(), userId, data); |
|
128
|
|
|
return isUpdated ? new ResponseEntity<>(HttpStatus.NO_CONTENT) : ResponseEntity.status(HttpStatus.ACCEPTED).body("Invalid userId"); |
|
129
|
|
|
} catch (IllegalArgumentException e) { |
|
130
|
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
@RequestMapping(value = "/{userId:.+}/enrollments", method = RequestMethod.GET) |
|
136
|
|
|
public Collection<Enrollment> getEnrollmentsForUser(JwtAuthenticationToken token, @PathVariable("userId") final String userId) throws EnrollmentNotFoundException { |
|
137
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
138
|
|
|
return enrollmentService.findEnrollmentsForUser(userContext.getTenantId(), userContext.getOrgId(), userId); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
@RequestMapping(value = "/mapping/{externalUserId:.+}", method = RequestMethod.GET) |
|
142
|
|
|
public UserMapping getUserMapping(JwtAuthenticationToken token, @PathVariable("externalUserId") final String externalUserId) { |
|
143
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
144
|
|
|
return mongoUserMappingRepository.findByTenantIdAndOrganizationIdAndUserExternalIdIgnoreCase(userContext.getTenantId(), userContext.getOrgId(), externalUserId); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
@RequestMapping(value= "/mapping", method = RequestMethod.POST) |
|
148
|
|
|
public ResponseEntity<?> postUserMapping(JwtAuthenticationToken token, @RequestBody UserMapping um) { |
|
|
|
|
|
|
149
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
150
|
|
|
|
|
151
|
|
|
UserMapping existingUserMapping = mongoUserMappingRepository |
|
152
|
|
|
.findByTenantIdAndOrganizationIdAndUserExternalIdIgnoreCase(userContext.getTenantId(), userContext.getOrgId(), um.getUserExternalId()); |
|
153
|
|
|
|
|
154
|
|
|
if (existingUserMapping == null) { |
|
155
|
|
|
UserMapping userMapping |
|
156
|
|
|
= new UserMapping.Builder() |
|
157
|
|
|
.withUserExternalId(um.getUserExternalId()) |
|
158
|
|
|
.withUserSourcedId(um.getUserSourcedId()) |
|
159
|
|
|
.withDateLastModified(LocalDateTime.now(ZoneId.of("UTC"))) |
|
160
|
|
|
.withOrganizationId(userContext.getOrgId()) |
|
161
|
|
|
.withTenantId(userContext.getTenantId()) |
|
162
|
|
|
.build(); |
|
163
|
|
|
|
|
164
|
|
|
UserMapping saved = mongoUserMappingRepository.save(userMapping); |
|
165
|
|
|
|
|
166
|
|
|
return new ResponseEntity<>(saved, null, HttpStatus.CREATED); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return new ResponseEntity<>(existingUserMapping, null, HttpStatus.NOT_MODIFIED); |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** Returns the Result for user |
|
174
|
|
|
* @param token |
|
175
|
|
|
* @param userId |
|
176
|
|
|
* @return Result |
|
177
|
|
|
* @throws ResultNotFoundException |
|
178
|
|
|
*/ |
|
179
|
|
|
@RequestMapping(value = "/{userId:.+}/results", method = RequestMethod.GET) |
|
180
|
|
|
public Result getResultsForUser(JwtAuthenticationToken token, @PathVariable final String userId) throws EventNotFoundException, RuntimeException { |
|
|
|
|
|
|
181
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
182
|
|
|
return resultService.getResultsForUser(userContext.getTenantId(), userContext.getOrgId(), userId); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
@RequestMapping(value = "/{userId:.+}/events", method = RequestMethod.GET) |
|
186
|
|
|
public Collection<Event> getEventsForUser( |
|
187
|
|
|
JwtAuthenticationToken token, |
|
188
|
|
|
@PathVariable final String userId, |
|
189
|
|
|
@RequestParam(value="from", required=false, defaultValue = "") String from, |
|
190
|
|
|
@RequestParam(value="to", required=false, defaultValue = "") String to |
|
191
|
|
|
) throws IllegalArgumentException, EventNotFoundException, BadRequestException { |
|
192
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
193
|
|
|
try { |
|
194
|
|
|
return eventService.getEventsForUser(userContext.getTenantId(), userContext.getOrgId(), userId, from, to); |
|
195
|
|
|
} catch (EventNotFoundException e) { |
|
196
|
|
|
throw new EventNotFoundException(e.getMessage()); |
|
197
|
|
|
} catch (BadRequestException e) { |
|
198
|
|
|
throw new BadRequestException(e.getMessage()); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |