-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from harmlessprince/tenant
tenant implemented with index
- Loading branch information
Showing
33 changed files
with
430 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,6 @@ public class Cart extends BaseEntity { | |
@DBRef | ||
Product product; | ||
|
||
@Indexed | ||
private String tenantId; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/harmlessprince/ecommerceApi/color/ColorDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.harmlessprince.ecommerceApi.color; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
@AllArgsConstructor | ||
public class ColorDTO { | ||
private String id; | ||
private String name; | ||
private String code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/harmlessprince/ecommerceApi/configs/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.harmlessprince.ecommerceApi.configs; | ||
|
||
import com.harmlessprince.ecommerceApi.interceptors.TenantInterceptor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(new TenantInterceptor()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/harmlessprince/ecommerceApi/contexts/TenantContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.harmlessprince.ecommerceApi.contexts; | ||
|
||
import com.harmlessprince.ecommerceApi.tenant.TenantService; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@AllArgsConstructor | ||
@Component | ||
public class TenantContext { | ||
private static final ThreadLocal<String> currentTenant = new ThreadLocal<>(); | ||
public static void setCurrentTenant(String tenantId) { | ||
currentTenant.set(tenantId); | ||
} | ||
|
||
public static String getCurrentTenant() { | ||
return currentTenant.get(); | ||
} | ||
|
||
public static void clear() { | ||
currentTenant.remove(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/harmlessprince/ecommerceApi/custom/AppConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.harmlessprince.ecommerceApi.custom; | ||
|
||
public class AppConstants { | ||
public static final String CURRENT_USER_TENANT_ID = "CURRENT_USER_TENANT_ID"; | ||
public static final String CURRENT_TENANT_ID_FROM_HEADER = "X-Tenant-Id"; | ||
public static final String CURRENT_USER_ROLE_ID = "CURRENT_USER_ROLE_ID"; | ||
public static final String CURRENT_USER_ROLE_NAME = "CURRENT_USER_ROLE_NAME"; | ||
|
||
// ROLES | ||
public static final String SUPER_ADMIN = "super_admin"; | ||
public static final String ADMIN = "admin"; | ||
public static final String CUSTOMER = "customer"; | ||
public static final String STORE_OWNER = "store_owner"; | ||
|
||
|
||
//PREFIX | ||
public static final String TENANT_PREFIX = "tao_commerce_tenant_:"; | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/harmlessprince/ecommerceApi/interceptors/TenantInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.harmlessprince.ecommerceApi.interceptors; | ||
|
||
import com.harmlessprince.ecommerceApi.contexts.TenantContext; | ||
import com.harmlessprince.ecommerceApi.exceptions.CustomBadRequestException; | ||
import com.harmlessprince.ecommerceApi.custom.AppConstants; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Slf4j | ||
public class TenantInterceptor implements HandlerInterceptor { | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
String requestURI = request.getRequestURI(); | ||
if (isPathAllowed(requestURI)) { | ||
return true; | ||
} | ||
String currentTenantFromAttribute = (String) request.getAttribute(AppConstants.CURRENT_USER_TENANT_ID); | ||
String currentUserRole = (String) request.getAttribute(AppConstants.CURRENT_USER_ROLE_NAME); | ||
String currentTenantFromHeader = request.getHeader(AppConstants.CURRENT_TENANT_ID_FROM_HEADER); | ||
log.info("Current tenant id from currentTenantFromAttribute: {}", currentTenantFromAttribute); | ||
log.info("Current tenant id from currentTenantFromHeader: {}", currentTenantFromHeader); | ||
String finalTenantId = StringUtils.hasText(currentTenantFromAttribute) | ||
? currentTenantFromAttribute | ||
: currentTenantFromHeader; | ||
|
||
if (!StringUtils.hasText(finalTenantId) && !Objects.equals(currentUserRole, AppConstants.SUPER_ADMIN)) { | ||
throw new CustomBadRequestException("Tenant ID is missing from headers"); | ||
} | ||
TenantContext.setCurrentTenant(finalTenantId); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { | ||
TenantContext.clear(); | ||
} | ||
|
||
private List<String> allowedPaths(){ | ||
List<String> allowedPaths = new ArrayList<>(); | ||
allowedPaths.add("auth/login"); | ||
allowedPaths.add("auth/logout"); | ||
allowedPaths.add("auth/register"); | ||
allowedPaths.add("countries"); | ||
allowedPaths.add("countries"); | ||
allowedPaths.add("local-governments"); | ||
allowedPaths.add("states"); | ||
allowedPaths.add("payment-methods"); | ||
return allowedPaths; | ||
} | ||
|
||
private boolean isPathAllowed(String url) { | ||
// Remove the version part (e.g., "/v1/") from the URL | ||
String path = url.replaceFirst("^/v\\d+/",""); | ||
|
||
// Get the list of allowed paths | ||
List<String> allowedPaths = allowedPaths(); | ||
|
||
// Check if the remaining path is in the allowed paths | ||
for (String allowedPath : allowedPaths) { | ||
if (path.contains(allowedPath)) { | ||
return true; // Return early when a match is found | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.