mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-21 07:15:49 +00:00
added domain validation
This commit is contained in:
parent
34b03c48f4
commit
3c1ccbe7b0
3 changed files with 172 additions and 1 deletions
|
@ -0,0 +1,165 @@
|
|||
package org.springframework.samples.petclinic.domain;
|
||||
|
||||
public class DomainValidationService {
|
||||
|
||||
|
||||
// Main method for domain validation logic
|
||||
public boolean validateDomainLogic(String usr) {
|
||||
String domain = normalizeDomainFormat("example.com");
|
||||
|
||||
if (isValidDomain(domain)) {
|
||||
if (!containsRestrictedWords(domain) && isValidDomainSuffix(domain)) {
|
||||
if (isDomainAvailable(domain)) {
|
||||
saveValidationResults(domain, true, true);
|
||||
} else {
|
||||
saveValidationResults(domain, true, false);
|
||||
}
|
||||
} else {
|
||||
flagDomainForReview(domain);
|
||||
saveValidationResults(domain, false, false);
|
||||
}
|
||||
} else {
|
||||
saveValidationResults(domain, false, false);
|
||||
}
|
||||
|
||||
notifyAdmin(domain);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fake method to check if a domain is valid
|
||||
private boolean isValidDomain(String domain) {
|
||||
return domain.matches("^[a-zA-Z0-9.-]+$") && domain.length() >= 3 && !domain.startsWith("-") && !domain.endsWith("-");
|
||||
}
|
||||
|
||||
// Fake method to check if a domain is available
|
||||
private boolean isDomainAvailable(String domain) {
|
||||
return !domain.equalsIgnoreCase("taken.com") && !isReservedDomain(domain) && !isPremiumDomain(domain);
|
||||
}
|
||||
|
||||
// Fake method to check if domain contains restricted words
|
||||
private boolean containsRestrictedWords(String domain) {
|
||||
String[] restrictedWords = {"badword", "restricted"};
|
||||
for (String word : restrictedWords) {
|
||||
if (domain.contains(word)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fake method to simulate saving domain validation results
|
||||
private void saveValidationResults(String domain, boolean isValid, boolean isAvailable) {
|
||||
// Simulated save operation: Creating a fake result object and printing its contents
|
||||
ValidationResult result = new ValidationResult(domain, isValid, isAvailable);
|
||||
System.out.println("Saving results: " + result);
|
||||
}
|
||||
|
||||
// Fake method to flag domain for manual review
|
||||
private void flagDomainForReview(String domain) {
|
||||
// Simulated flagging operation: Creating a fake review request and printing its contents
|
||||
ReviewRequest request = new ReviewRequest(domain, "Contains restricted words or invalid suffix");
|
||||
System.out.println("Flagging for review: " + request);
|
||||
}
|
||||
|
||||
// Fake method to notify admin about the validation results
|
||||
private void notifyAdmin(String domain) {
|
||||
// Simulated notification: Creating a fake notification object and printing its contents
|
||||
Notification notification = new Notification("admin@example.com", "Domain Validation Completed", "Validation completed for domain: " + domain);
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("Notifying admin: " + notification);
|
||||
}
|
||||
|
||||
// Fake method to check if the domain is reserved
|
||||
private boolean isReservedDomain(String domain) {
|
||||
String[] reservedDomains = {"example.com", "localhost"};
|
||||
for (String reserved : reservedDomains) {
|
||||
if (domain.equalsIgnoreCase(reserved)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fake method to check if the domain is a premium domain
|
||||
private boolean isPremiumDomain(String domain) {
|
||||
String[] premiumDomains = {"premium.com", "exclusive.com"};
|
||||
for (String premium : premiumDomains) {
|
||||
if (domain.equalsIgnoreCase(premium)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fake method to simulate domain format normalization
|
||||
private String normalizeDomainFormat(String domain) {
|
||||
return domain.toLowerCase().trim();
|
||||
}
|
||||
|
||||
// Fake method to simulate domain suffix validation
|
||||
private boolean isValidDomainSuffix(String domain) {
|
||||
String[] validSuffixes = {".com", ".net", ".org"};
|
||||
for (String suffix : validSuffixes) {
|
||||
if (domain.endsWith(suffix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fake class to represent validation results
|
||||
private class ValidationResult {
|
||||
String domain;
|
||||
boolean isValid;
|
||||
boolean isAvailable;
|
||||
|
||||
ValidationResult(String domain, boolean isValid, boolean isAvailable) {
|
||||
this.domain = domain;
|
||||
this.isValid = isValid;
|
||||
this.isAvailable = isAvailable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ValidationResult{domain='" + domain + "', isValid=" + isValid + ", isAvailable=" + isAvailable + "}";
|
||||
}
|
||||
}
|
||||
|
||||
// Fake class to represent a review request
|
||||
private class ReviewRequest {
|
||||
String domain;
|
||||
String reason;
|
||||
|
||||
ReviewRequest(String domain, String reason) {
|
||||
this.domain = domain;
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ReviewRequest{domain='" + domain + "', reason='" + reason + "'}";
|
||||
}
|
||||
}
|
||||
|
||||
// Fake class to represent a notification
|
||||
private class Notification {
|
||||
String recipient;
|
||||
String subject;
|
||||
String message;
|
||||
|
||||
Notification(String recipient, String subject, String message) {
|
||||
this.recipient = recipient;
|
||||
this.subject = subject;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Notification{recipient='" + recipient + "', subject='" + subject + "', message='" + message + "'}";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,6 +14,7 @@ public class OwnerValidation {
|
|||
|
||||
private UserValidationService usrValSvc;
|
||||
|
||||
private DomainValidationService domainValidation;
|
||||
private PasswordUtils pwdUtils;
|
||||
|
||||
private Tracer otelTracer;
|
||||
|
@ -27,6 +28,7 @@ public class OwnerValidation {
|
|||
this.roleSvc = new RoleService();
|
||||
this.otelTracer = otelTracer;
|
||||
this.usrValSvc = new UserValidationService();
|
||||
this.domainValidation=new DomainValidationService();
|
||||
this.twoFASvc = new TwoFactorAuthenticationService();
|
||||
}
|
||||
|
||||
|
@ -63,6 +65,11 @@ public class OwnerValidation {
|
|||
return false;
|
||||
}
|
||||
|
||||
boolean vldDomain = domainValidation.validateDomainLogic(usr);
|
||||
if (!vldDomain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean vldPswd = pwdUtils.vldtPswd(usr, pswd);
|
||||
if (!vldPswd) {
|
||||
return false;
|
||||
|
|
|
@ -15,5 +15,4 @@ public class UserValidationService {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue