public static User convertToEntity(UserWithDeviceTypeVO dc) { User vo = null; if (dc != null) { vo = new User(); vo.setData(dc.getData()); vo.setId(dc.getId()); vo.setLastLogin(dc.getLastLogin()); vo.setLogin(dc.getLogin()); vo.setLoginAttempts(dc.getLoginAttempts()); vo.setPasswordHash(dc.getPasswordHash()); vo.setPasswordSalt(dc.getPasswordSalt()); vo.setRole(dc.getRole()); vo.setStatus(dc.getStatus()); vo.setIntroReviewed(dc.getIntroReviewed()); vo.setAllDeviceTypesAvailable(dc.getAllDeviceTypesAvailable()); vo.setDeviceTypes(new HashSet<>()); for (DeviceTypeVO deviceTypeVO : dc.getDeviceTypes()) { DeviceType deviceType = DeviceType.convert(deviceTypeVO); vo.getDeviceTypes().add(deviceType); } } return vo; }
public static UserWithDeviceTypeVO fromUserVO(UserVO dc) { UserWithDeviceTypeVO vo = null; if (dc != null) { vo = new UserWithDeviceTypeVO(); vo.setData(dc.getData()); vo.setId(dc.getId()); vo.setData(dc.getData()); vo.setLastLogin(dc.getLastLogin()); vo.setLogin(dc.getLogin()); vo.setLoginAttempts(dc.getLoginAttempts()); vo.setDeviceTypes(new HashSet<>()); vo.setPasswordHash(dc.getPasswordHash()); vo.setPasswordSalt(dc.getPasswordSalt()); vo.setRole(dc.getRole()); vo.setStatus(dc.getStatus()); vo.setIntroReviewed(dc.getIntroReviewed()); vo.setAllDeviceTypesAvailable(dc.getAllDeviceTypesAvailable()); } return vo; }
@Override public UserWithDeviceTypeVO getWithDeviceTypeById(long id) { User user = createNamedQuery(User.class, "User.getWithDeviceTypesById", of(CacheConfig.get())) .setParameter("id", id) .getResultList() .stream().findFirst().orElse(null); if (user == null) { return null; } UserVO vo = User.convertToVo(user); UserWithDeviceTypeVO userWithDeviceTypeVO = UserWithDeviceTypeVO.fromUserVO(vo); //TODO [rafa] change here to bulk fetch data if (user.getDeviceTypes() != null) { for (DeviceType deviceType : user.getDeviceTypes()) { DeviceTypeVO deviceTypeVO = DeviceType.convertDeviceType(deviceType); userWithDeviceTypeVO.getDeviceTypes().add(deviceTypeVO); } } return userWithDeviceTypeVO; }
/** * {@inheritDoc} */ @Override public void getDeviceTypes(long id, @Suspended final AsyncResponse asyncResponse) { UserWithDeviceTypeVO existingUser = userService.findUserWithDeviceType(id); if (existingUser == null) { logger.error("Can't get device types for user with id {}: user not found", id); ErrorResponse errorResponseEntity = new ErrorResponse(NOT_FOUND.getStatusCode(), String.format(Messages.USER_NOT_FOUND, id)); asyncResponse.resume(ResponseFactory.response(NOT_FOUND, errorResponseEntity)); } else { if (existingUser.getAllDeviceTypesAvailable()) { deviceTypeService.listAll().thenApply(deviceTypeVOS -> { logger.debug("User list request proceed successfully"); return ResponseFactory.response(OK, deviceTypeVOS, JsonPolicyDef.Policy.DEVICE_TYPES_LISTED); }).thenAccept(asyncResponse::resume); } else if (!existingUser.getAllDeviceTypesAvailable() && (existingUser.getDeviceTypes() == null || existingUser.getDeviceTypes().isEmpty())) { logger.warn("Unable to get list for empty device types"); asyncResponse.resume(ResponseFactory.response(OK, Collections.<DeviceTypeVO>emptyList(), DEVICE_TYPES_LISTED)); } else { asyncResponse.resume(ResponseFactory.response(OK, existingUser.getDeviceTypes(), JsonPolicyDef.Policy.DEVICE_TYPES_LISTED)); } } }
@HiveWebsocketAuth @PreAuthorize("isAuthenticated() and hasPermission(null, 'GET_DEVICE_TYPE')") public void processUserGetDeviceType(JsonObject request, WebSocketSession session) { Long userId = gson.fromJson(request.get(USER_ID), Long.class); if (userId == null) { logger.error(Messages.USER_ID_REQUIRED); throw new HiveException(Messages.USER_ID_REQUIRED, BAD_REQUEST.getStatusCode()); } Long deviceTypeId = gson.fromJson(request.get(DEVICE_TYPE_ID), Long.class); if (deviceTypeId == null) { logger.error(Messages.DEVICE_TYPE_ID_REQUIRED); throw new HiveException(Messages.DEVICE_TYPE_ID_REQUIRED, BAD_REQUEST.getStatusCode()); } UserWithDeviceTypeVO existingUser = userService.findUserWithDeviceType(userId); if (existingUser == null) { logger.error("Can't get device type with id {}: user {} not found", deviceTypeId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } for (DeviceTypeVO deviceType : existingUser.getDeviceTypes()) { if (deviceTypeId.equals(deviceType.getId())) { WebSocketResponse response = new WebSocketResponse(); response.addValue(DEVICE_TYPE, UserDeviceTypeResponse.fromDeviceType(deviceType), DEVICE_TYPES_LISTED); clientHandler.sendMessage(request, response, session); return; } } clientHandler.sendErrorResponse(request, NOT_FOUND.getStatusCode(), String.format(Messages.USER_DEVICE_TYPE_NOT_FOUND, deviceTypeId, userId), session); }
@HiveWebsocketAuth @PreAuthorize("isAuthenticated() and hasPermission(null, 'MANAGE_DEVICE_TYPE')") public void processUserGetDeviceTypes(JsonObject request, WebSocketSession session) { Long userId = gson.fromJson(request.get(USER_ID), Long.class); if (userId == null) { logger.error(Messages.USER_ID_REQUIRED); throw new HiveException(Messages.USER_ID_REQUIRED, BAD_REQUEST.getStatusCode()); } final UserWithDeviceTypeVO existingUser = userService.findUserWithDeviceType(userId); if (existingUser == null) { logger.error("Can't get device types for user with id {}: user not found", userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } final WebSocketResponse response = new WebSocketResponse(); if (existingUser.getAllDeviceTypesAvailable()) { deviceTypeService.listAll().thenAccept(dt -> { logger.debug("Device types list request proceed successfully"); response.addValue(DEVICE_TYPES, dt, DEVICE_TYPES_LISTED); clientHandler.sendMessage(request, response, session); }); } else { if (!existingUser.getAllDeviceTypesAvailable() && (existingUser.getDeviceTypes() == null || existingUser.getDeviceTypes().isEmpty())) { logger.warn("Unable to get list for empty device types"); response.addValue(DEVICE_TYPES, Collections.emptyList(), DEVICE_TYPES_LISTED); } else { response.addValue(DEVICE_TYPES, existingUser.getDeviceTypes(), DEVICE_TYPES_LISTED); } clientHandler.sendMessage(request, response, session); } }
/** * {@inheritDoc} */ @Override public Response getDeviceType(long id, long deviceTypeId) { UserWithDeviceTypeVO existingUser = userService.findUserWithDeviceType(id); if (existingUser == null) { logger.error("Can't get device type with id {}: user {} not found", deviceTypeId, id); ErrorResponse errorResponseEntity = new ErrorResponse(NOT_FOUND.getStatusCode(), String.format(Messages.USER_NOT_FOUND, id)); return ResponseFactory.response(NOT_FOUND, errorResponseEntity); } if (existingUser.getAllDeviceTypesAvailable()) { DeviceTypeVO deviceTypeVO = deviceTypeService.getWithDevices(deviceTypeId); if (deviceTypeVO != null) { return ResponseFactory.response(OK, UserDeviceTypeResponse.fromDeviceType(deviceTypeVO), JsonPolicyDef.Policy.DEVICE_TYPES_LISTED); } } for (DeviceTypeVO deviceType : existingUser.getDeviceTypes()) { if (deviceType.getId() == deviceTypeId) { return ResponseFactory.response(OK, UserDeviceTypeResponse.fromDeviceType(deviceType), JsonPolicyDef.Policy.DEVICE_TYPES_LISTED); } } ErrorResponse errorResponseEntity = new ErrorResponse(NOT_FOUND.getStatusCode(), String.format(Messages.USER_DEVICE_TYPE_NOT_FOUND, deviceTypeId, id)); return ResponseFactory.response(NOT_FOUND, errorResponseEntity); }
}); if (userWithDeviceType.getAllDeviceTypesAvailable()) { deviceTypeIds.add("*"); } else { Set<DeviceTypeVO> deviceTypes = userWithDeviceType.getDeviceTypes(); if (!deviceTypes.isEmpty()) { deviceTypes.forEach(deviceType -> {
if (!userWithDeviceTypeVO.getAllDeviceTypesAvailable()) { principal.setAllDeviceTypesAvailable(false); principal.setDeviceTypeIds(userWithDeviceTypeVO.getDeviceTypes().stream() .map(DeviceTypeVO::getId).collect(Collectors.toSet()));