From 03bc99f9da8a07b55d010d277b5d124ddd83ebf3 Mon Sep 17 00:00:00 2001 From: Sreerag K S <58926794+sreeragksgh@users.noreply.github.com> Date: Fri, 12 Jul 2024 10:50:31 +0530 Subject: [PATCH] New API to Fetch WhiteListed Domains for email - KB-5850 (#641) * New API for fetching whitelisted domains * Updated changes * Review changes * Response structure changes * review changes --- .../org/sunbird/common/util/Constants.java | 2 + .../UserRegistrationController.java | 6 +++ .../service/UserRegistrationService.java | 2 + .../service/UserRegistrationServiceImpl.java | 54 +++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 430842e85..eb7d1aa1f 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -1064,6 +1064,8 @@ public class Constants { public static final String PROFILE_DESIGNATION_STATUS = "profileDesignationStatus"; public static final String NOT_MY_USER = "NOT-MY-USER"; public static final String SPACE = " "; + public static final String API_APPROVED_DOMAINS = "api.approved.domains"; + public static final String DOMAINS = "domains"; private Constants() { throw new IllegalStateException("Utility class"); diff --git a/src/main/java/org/sunbird/user/registration/controller/UserRegistrationController.java b/src/main/java/org/sunbird/user/registration/controller/UserRegistrationController.java index 5a9d6bfae..5c1d6d2c5 100644 --- a/src/main/java/org/sunbird/user/registration/controller/UserRegistrationController.java +++ b/src/main/java/org/sunbird/user/registration/controller/UserRegistrationController.java @@ -44,4 +44,10 @@ public ResponseEntity generateOTP(@RequestBody Map(response, response.getResponseCode()); } + + @GetMapping("/user/email/approved/domains") + public ResponseEntity getApprovedDomain() throws Exception { + SBApiResponse response = userRegService.getApprovedDomains(); + return new ResponseEntity<>(response, response.getResponseCode()); + } } diff --git a/src/main/java/org/sunbird/user/registration/service/UserRegistrationService.java b/src/main/java/org/sunbird/user/registration/service/UserRegistrationService.java index ea46c1868..27ae9b78c 100644 --- a/src/main/java/org/sunbird/user/registration/service/UserRegistrationService.java +++ b/src/main/java/org/sunbird/user/registration/service/UserRegistrationService.java @@ -48,4 +48,6 @@ public interface UserRegistrationService { public SBApiResponse generateOTP(Map otpRequests); + public SBApiResponse getApprovedDomains(); + } diff --git a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java index c6a69643d..d091f88d9 100644 --- a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java +++ b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java @@ -5,6 +5,7 @@ import java.io.InputStreamReader; import java.util.*; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.RandomStringUtils; @@ -305,6 +306,34 @@ public void initiateCreateUserFlow(String registrationCode) { } } + public SBApiResponse getApprovedDomains() { + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_APPROVED_DOMAINS); + String errMsg = ""; + try { + Set approvedDomains = getApprovedDomainsFromDB(); + if (CollectionUtils.isNotEmpty(approvedDomains)) { + Map result = new HashMap<>(); + result.put(Constants.DOMAINS, approvedDomains); + LOGGER.info("Fetched pre-approved and approved domains successfully"); + response.setResult(result); + response.getResult().put(Constants.RESPONSE, Constants.SUCCESS.toUpperCase()); + } else { + errMsg = "Failed to Fetch Approved Domains"; + } + } catch (Exception e) { + LOGGER.error(String.format("Exception in %s : %s", "getApprovedDomains", e.getMessage()), e); + errMsg = "Failed to process message. Exception: " + e.getMessage(); + } + + if (StringUtils.isNotBlank(errMsg)) { + LOGGER.error("Failed to Fetch Approved Domains, error message : ", errMsg); + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg(errMsg); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + } + return response; + } + private SBApiResponse createDefaultResponse(String api) { SBApiResponse response = new SBApiResponse(); response.setId(api); @@ -588,4 +617,29 @@ private Boolean isApprovedDomains(String emailDomain, String domainType) { Constants.KEYSPACE_SUNBIRD, Constants.TABLE_MASTER_DATA, propertyMap, Arrays.asList(Constants.CONTEXT_TYPE, Constants.CONTEXT_NAME)); return CollectionUtils.isNotEmpty(listOfDomains); } + + private Set getApprovedDomainsFromDB() { + Set domains = new HashSet<>(); + + Set preApprovedDomains = fetchDomainsByContextType(Constants.USER_REGISTRATION_PRE_APPROVED_DOMAIN); + domains.addAll(preApprovedDomains); + + Set approvedDomains = fetchDomainsByContextType(Constants.USER_REGISTRATION_DOMAIN); + domains.addAll(approvedDomains); + + return domains; + } + + private Set fetchDomainsByContextType(String contextType) { + Map propertyMap = new HashMap<>(); + propertyMap.put(Constants.CONTEXT_TYPE, contextType); + + List> records = cassandraOperation.getRecordsByPropertiesWithoutFiltering( + Constants.KEYSPACE_SUNBIRD, Constants.TABLE_MASTER_DATA, propertyMap, Arrays.asList(Constants.CONTEXT_NAME)); + + return records.stream() + .map(map -> (String) map.get(Constants.CONTEXT_NAME)) + .collect(Collectors.toSet()); + } + }