|
24 | 24 | import org.cryptomator.hub.entities.User;
|
25 | 25 | import org.cryptomator.hub.entities.Vault;
|
26 | 26 | import org.cryptomator.hub.entities.WotEntry;
|
| 27 | +import org.cryptomator.hub.entities.events.AuditEvent; |
27 | 28 | import org.cryptomator.hub.entities.events.EventLogger;
|
| 29 | +import org.cryptomator.hub.entities.events.VaultKeyRetrievedEvent; |
28 | 30 | import org.eclipse.microprofile.jwt.JsonWebToken;
|
29 | 31 | import org.eclipse.microprofile.openapi.annotations.Operation;
|
30 | 32 | import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
|
@@ -58,6 +60,8 @@ public class UsersResource {
|
58 | 60 | WotEntry.Repository wotRepo;
|
59 | 61 | @Inject
|
60 | 62 | EffectiveWot.Repository effectiveWotRepo;
|
| 63 | + @Inject |
| 64 | + AuditEvent.Repository auditEventRepo; |
61 | 65 |
|
62 | 66 | @Inject
|
63 | 67 | JsonWebToken jwt;
|
@@ -156,10 +160,23 @@ public Response updateMyAccessTokens(@NotNull Map<UUID, String> tokens) {
|
156 | 160 | @Operation(summary = "get the logged-in user")
|
157 | 161 | @APIResponse(responseCode = "200", description = "returns the current user")
|
158 | 162 | @APIResponse(responseCode = "404", description = "no user matching the subject of the JWT passed as Bearer Token")
|
159 |
| - public UserDto getMe(@QueryParam("withDevices") boolean withDevices) { |
| 163 | + public UserDto getMe(@QueryParam("withDevices") boolean withDevices, @QueryParam("withLastAccess") boolean withLastAccess) { |
160 | 164 | User user = userRepo.findById(jwt.getSubject());
|
161 |
| - Function<Device, DeviceResource.DeviceDto> mapDevices = d -> new DeviceResource.DeviceDto(d.getId(), d.getName(), d.getType(), d.getPublickey(), d.getUserPrivateKeys(), d.getOwner().getId(), d.getCreationTime().truncatedTo(ChronoUnit.MILLIS)); |
162 |
| - var devices = withDevices ? user.devices.stream().map(mapDevices).collect(Collectors.toSet()) : Set.<DeviceResource.DeviceDto>of(); |
| 165 | + Set<DeviceResource.DeviceDto> devices; |
| 166 | + if (withLastAccess) { |
| 167 | + var deviceEntities = user.devices.stream().toList(); |
| 168 | + var deviceIds = deviceEntities.stream().map(Device::getId).toList(); |
| 169 | + var events = auditEventRepo.findLastVaultKeyRetrieve(deviceIds).collect(Collectors.toMap(VaultKeyRetrievedEvent::getDeviceId, Function.identity())); |
| 170 | + devices = deviceEntities.stream().map(d -> { |
| 171 | + var event = events.get(d.getId()); |
| 172 | + var lastIpAddress = (event != null) ? event.getIpAddress() : null; |
| 173 | + var lastAccessTime = (event != null) ? event.getTimestamp() : null; |
| 174 | + return new DeviceResource.DeviceDto(d.getId(), d.getName(), d.getType(), d.getPublickey(), d.getUserPrivateKeys(), d.getOwner().getId(), d.getCreationTime().truncatedTo(ChronoUnit.MILLIS), lastIpAddress, lastAccessTime); |
| 175 | + }).collect(Collectors.toSet()); |
| 176 | + } else { |
| 177 | + Function<Device, DeviceResource.DeviceDto> mapDevices = d -> new DeviceResource.DeviceDto(d.getId(), d.getName(), d.getType(), d.getPublickey(), d.getUserPrivateKeys(), d.getOwner().getId(), d.getCreationTime().truncatedTo(ChronoUnit.MILLIS), null, null); |
| 178 | + devices = withDevices ? user.devices.stream().map(mapDevices).collect(Collectors.toSet()) : Set.of(); |
| 179 | + } |
163 | 180 | return new UserDto(user.getId(), user.getName(), user.getPictureUrl(), user.getEmail(), user.getLanguage(), devices, user.getEcdhPublicKey(), user.getEcdsaPublicKey(), user.getPrivateKeys(), user.getSetupCode());
|
164 | 181 | }
|
165 | 182 |
|
|
0 commit comments