use of net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser in project OsmAnd-tools by osmandapp.
the class UserdataController method userRegister.
@PostMapping(value = "/user-register")
@ResponseBody
public ResponseEntity<String> userRegister(@RequestParam(name = "email", required = true) String email, @RequestParam(name = "deviceid", required = false) String deviceId, @RequestParam(name = "orderid", required = false) String orderid, @RequestParam(name = "login", required = false) boolean login) throws IOException {
// allow to register only with small case
email = email.toLowerCase().trim();
PremiumUser pu = usersRepository.findByEmail(email);
if (!email.contains("@")) {
return error(ERROR_CODE_EMAIL_IS_INVALID, "email is not valid to be registered");
}
if (pu != null) {
if (!login) {
return error(ERROR_CODE_USER_IS_ALREADY_REGISTERED, "user was already registered with such email");
}
// don't check order id validity for login
} else {
String error = checkOrderIdPremium(orderid);
if (error != null) {
return error(ERROR_CODE_NO_VALID_SUBSCRIPTION, error);
}
PremiumUser otherUser = usersRepository.findByOrderid(orderid);
if (otherUser != null) {
String hideEmail = hideEmail(otherUser.email);
return error(ERROR_CODE_SUBSCRIPTION_WAS_USED_FOR_ANOTHER_ACCOUNT, "user was already signed up as " + hideEmail);
}
pu = new PremiumUsersRepository.PremiumUser();
pu.email = email;
pu.regTime = new Date();
pu.orderid = orderid;
}
// keep old order id
pu.tokendevice = deviceId;
pu.token = (new Random().nextInt(8999) + 1000) + "";
pu.tokenTime = new Date();
usersRepository.saveAndFlush(pu);
emailSender.sendOsmAndCloudRegistrationEmail(pu.email, pu.token, true);
return ok();
}
use of net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser in project OsmAnd-tools by osmandapp.
the class UserdataController method check.
@GetMapping(value = "/user-validate-sub")
@ResponseBody
public ResponseEntity<String> check(@RequestParam(name = "deviceid", required = true) int deviceId, @RequestParam(name = "accessToken", required = true) String accessToken) throws IOException {
PremiumUserDevice dev = checkToken(deviceId, accessToken);
if (dev == null) {
return tokenNotValid();
}
PremiumUser pu = usersRepository.findById(dev.userid);
if (pu == null) {
return error(ERROR_CODE_EMAIL_IS_INVALID, "email is not registered");
}
String errorMsg = checkOrderIdPremium(pu.orderid);
if (errorMsg != null) {
return error(ERROR_CODE_NO_VALID_SUBSCRIPTION, errorMsg);
}
return ResponseEntity.ok(gson.toJson(pu));
}
use of net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser in project OsmAnd-tools by osmandapp.
the class UserdataController method registerNewDevice.
private ResponseEntity<String> registerNewDevice(String email, String token, String deviceId, String accessToken) {
PremiumUser pu = usersRepository.findByEmail(email);
if (pu == null) {
return error(ERROR_CODE_USER_IS_NOT_REGISTERED, "user with that email is not registered");
}
if (pu.token == null || !pu.token.equals(token) || pu.tokenTime == null || System.currentTimeMillis() - pu.tokenTime.getTime() > TimeUnit.MILLISECONDS.convert(24, TimeUnit.HOURS)) {
return error(ERROR_CODE_TOKEN_IS_NOT_VALID_OR_EXPIRED, "token is not valid or expired (24h)");
}
pu.token = null;
pu.tokenTime = null;
PremiumUserDevice device = new PremiumUserDevice();
PremiumUserDevice sameDevice;
while ((sameDevice = devicesRepository.findTopByUseridAndDeviceidOrderByUdpatetimeDesc(pu.id, deviceId)) != null) {
devicesRepository.delete(sameDevice);
}
device.userid = pu.id;
device.deviceid = deviceId;
device.udpatetime = new Date();
device.accesstoken = accessToken;
usersRepository.saveAndFlush(pu);
devicesRepository.saveAndFlush(device);
return ResponseEntity.ok(gson.toJson(device));
}
use of net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser in project OsmAnd-tools by osmandapp.
the class UserdataController method upload.
@PostMapping(value = "/upload-file", consumes = MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public ResponseEntity<String> upload(@RequestPart(name = "file") @Valid @NotNull @NotEmpty MultipartFile file, @RequestParam(name = "name", required = true) String name, @RequestParam(name = "type", required = true) String type, @RequestParam(name = "deviceid", required = true) int deviceId, @RequestParam(name = "accessToken", required = true) String accessToken, @RequestParam(name = "clienttime", required = false) Long clienttime) throws IOException {
// This could be slow series of checks (token, user, subscription, amount of space):
// probably it's better to support multiple file upload without these checks
PremiumUserDevice dev = checkToken(deviceId, accessToken);
if (dev == null) {
return tokenNotValid();
}
PremiumUser pu = usersRepository.findById(dev.userid);
if (pu == null) {
return error(ERROR_CODE_USER_IS_NOT_REGISTERED, "Unexpected error: user is not registered.");
}
String errorMsg = checkOrderIdPremium(pu.orderid);
if (errorMsg != null) {
return error(ERROR_CODE_SUBSCRIPTION_WAS_EXPIRED_OR_NOT_PRESENT, "Subscription is not valid any more: " + errorMsg);
}
UserFilesResults res = generateFiles(dev.userid, null, null, false, false);
if (res.totalZipSize > MAXIMUM_ACCOUNT_SIZE) {
return error(ERROR_CODE_SIZE_OF_SUPPORTED_BOX_IS_EXCEEDED, "Maximum size of OsmAnd Cloud exceeded " + (MAXIMUM_ACCOUNT_SIZE / MB) + " MB. Please contact support in order to investigate possible solutions.");
}
UserFile usf = new PremiumUserFilesRepository.UserFile();
long cnt, sum;
long zipsize = file.getSize();
try {
GZIPInputStream gzis = new GZIPInputStream(file.getInputStream());
byte[] buf = new byte[1024];
sum = 0;
while ((cnt = gzis.read(buf)) >= 0) {
sum += cnt;
}
} catch (IOException e) {
return error(ERROR_CODE_GZIP_ONLY_SUPPORTED_UPLOAD, "File is submitted not in gzip format");
}
usf.name = name;
usf.type = type;
usf.updatetime = new Date();
usf.userid = dev.userid;
usf.deviceid = deviceId;
usf.filesize = sum;
usf.zipfilesize = zipsize;
if (clienttime != null) {
usf.clienttime = new Date(clienttime.longValue());
}
// Session session = entityManager.unwrap(Session.class);
// Blob blob = session.getLobHelper().createBlob(file.getInputStream(), file.getSize());
// usf.data = blob;
usf.storage = storageService.save(userFolder(usf), storageFileName(usf), file);
if (storageService.storeLocally()) {
usf.data = file.getBytes();
}
filesRepository.saveAndFlush(usf);
return ok();
}
use of net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser in project OsmAnd-tools by osmandapp.
the class UserdataController method userUpdateOrderid.
@PostMapping(value = "/user-update-orderid")
@ResponseBody
public ResponseEntity<String> userUpdateOrderid(@RequestParam(name = "email", required = true) String email, @RequestParam(name = "deviceid", required = false) String deviceId, @RequestParam(name = "orderid", required = false) String orderid) throws IOException {
PremiumUser pu = usersRepository.findByEmail(email);
if (pu == null) {
return error(ERROR_CODE_EMAIL_IS_INVALID, "email is not registered");
}
String errorMsg = checkOrderIdPremium(orderid);
if (errorMsg != null) {
return error(ERROR_CODE_NO_VALID_SUBSCRIPTION, errorMsg);
}
PremiumUser otherUser = usersRepository.findByOrderid(orderid);
if (otherUser != null && !Algorithms.objectEquals(pu.orderid, orderid)) {
String hideEmail = hideEmail(otherUser.email);
return error(ERROR_CODE_SUBSCRIPTION_WAS_USED_FOR_ANOTHER_ACCOUNT, "user was already signed up as " + hideEmail);
}
pu.orderid = orderid;
usersRepository.saveAndFlush(pu);
return ok();
}
Aggregations