From 955b03568774b0830ceda0a6c8cf1b415dbacb5c Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Thu, 19 Sep 2024 08:22:57 -0400 Subject: [PATCH] cloud env check --- .../cc/pulseapp/api/common/EnvironmentUtils.java | 6 ++++++ .../pulseapp/api/service/OrganizationService.java | 14 ++++++++++++-- .../cc/pulseapp/api/service/StatusPageService.java | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/cc/pulseapp/api/common/EnvironmentUtils.java b/src/main/java/cc/pulseapp/api/common/EnvironmentUtils.java index 5c4e061..ca18f30 100644 --- a/src/main/java/cc/pulseapp/api/common/EnvironmentUtils.java +++ b/src/main/java/cc/pulseapp/api/common/EnvironmentUtils.java @@ -13,8 +13,14 @@ public final class EnvironmentUtils { */ @Getter private static final boolean production; + /** + * Is the app running in a "cloud" environment? + */ + @Getter private static final boolean cloud; static { String appEnv = System.getenv("APP_ENV"); + String cloudEnv = System.getenv("APP_CLOUD"); production = appEnv != null && (appEnv.equals("production")); + cloud = cloudEnv != null && (cloudEnv.equals("true")); } } \ No newline at end of file diff --git a/src/main/java/cc/pulseapp/api/service/OrganizationService.java b/src/main/java/cc/pulseapp/api/service/OrganizationService.java index e6ae833..87e0788 100644 --- a/src/main/java/cc/pulseapp/api/service/OrganizationService.java +++ b/src/main/java/cc/pulseapp/api/service/OrganizationService.java @@ -1,5 +1,6 @@ package cc.pulseapp.api.service; +import cc.pulseapp.api.common.EnvironmentUtils; import cc.pulseapp.api.exception.impl.BadRequestException; import cc.pulseapp.api.model.Feature; import cc.pulseapp.api.model.IGenericResponse; @@ -65,10 +66,18 @@ public final class OrganizationService { if (!Feature.ORG_CREATION_ENABLED.isEnabled()) { throw new BadRequestException(Error.ORG_CREATION_DISABLED); } + List ownedOrgs = orgRepository.findByOwnerSnowflake(owner.getSnowflake()); + // Ensure the org name isn't taken - if (orgRepository.findByNameIgnoreCase(name) != null) { + if (ownedOrgs.stream().anyMatch(org -> org.getName().equalsIgnoreCase(name))) { throw new BadRequestException(Error.ORG_NAME_TAKEN); } + // Handle cloud environment checks + if (EnvironmentUtils.isCloud()) { + if (ownedOrgs.size() >= owner.getTier().getMaxOrganizations()) { + throw new BadRequestException(Error.MAX_ORGS_REACHED); + } + } // Create the org and return it slug = slug.trim().replaceAll("-+$", ""); // Trim slug trailing dashes return orgRepository.save(new Organization(snowflakeService.generateSnowflake(), name, slug, null, owner.getSnowflake())); @@ -95,6 +104,7 @@ public final class OrganizationService { */ private enum Error implements IGenericResponse { ORG_CREATION_DISABLED, - ORG_NAME_TAKEN + ORG_NAME_TAKEN, + MAX_ORGS_REACHED } } \ No newline at end of file diff --git a/src/main/java/cc/pulseapp/api/service/StatusPageService.java b/src/main/java/cc/pulseapp/api/service/StatusPageService.java index e040d75..c986f8e 100644 --- a/src/main/java/cc/pulseapp/api/service/StatusPageService.java +++ b/src/main/java/cc/pulseapp/api/service/StatusPageService.java @@ -1,5 +1,6 @@ package cc.pulseapp.api.service; +import cc.pulseapp.api.common.EnvironmentUtils; import cc.pulseapp.api.exception.impl.BadRequestException; import cc.pulseapp.api.model.Feature; import cc.pulseapp.api.model.IGenericResponse; @@ -53,6 +54,10 @@ public final class StatusPageService { if (pageRepository.findByNameIgnoreCase(name) != null) { throw new BadRequestException(Error.STATUS_PAGE_NAME_TAKEN); } + // Handle cloud environment checks + if (EnvironmentUtils.isCloud()) { + // TODO: do UserTier#maxStatusPages check + } // Create the status page and return it String slug = name.replace(" ", "-") + "-" + ThreadLocalRandom.current().nextInt(10000, 99999);