Search in sources :

Example 1 with PremiumUser

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();
}
Also used : Random(java.util.Random) PremiumUsersRepository(net.osmand.server.api.repo.PremiumUsersRepository) PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with PremiumUser

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));
}
Also used : PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with PremiumUser

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));
}
Also used : PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) Date(java.util.Date)

Example 4 with PremiumUser

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();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) IOException(java.io.IOException) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with PremiumUser

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();
}
Also used : PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

PremiumUser (net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser)8 Date (java.util.Date)5 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 PremiumUserDevice (net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice)3 Random (java.util.Random)2 SupporterDeviceSubscription (net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription)2 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 UserFile (net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile)1 PremiumUsersRepository (net.osmand.server.api.repo.PremiumUsersRepository)1 UserFilesResults (net.osmand.server.controllers.pub.UserdataController.UserFilesResults)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1