Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.compile.nullAnalysis.mode": "automatic"
}
3 changes: 2 additions & 1 deletion src/main/java/UsernameGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class UsernameGenerator {
int MAX_USERNAME_LENGTH = 12;


@SuppressWarnings("null")
public String generateUsername(String firstName, String lastName) throws GeneralException {
firstName = StringUtils.trimToNull(firstName);
lastName = StringUtils.trimToNull(lastName);
Expand Down Expand Up @@ -46,7 +47,7 @@ public String generateUsername(String firstName, String lastName) throws General
String username = null;
String fullName = firstName + "." + lastName;

if(fullName.length() > MAX_USERNAME_LENGTH) {
if(fullName.length() > MAX_USERNAME_LENGTH && fullName!=null) {
int firstNameLength = firstName.length();

if(firstNameLength > (MAX_USERNAME_LENGTH - 2)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule name="GenerateUniqueDN" type="AttributeGenerator">
<Description>Generate a unique email address for Active Directory.</Description>
<Source><![CDATA[

import sailpoint.object.Identity;
import sailpoint.object.Attributes;
import sailpoint.tools.GeneralException;
import sailpoint.object.AttributeDefinition;
import sailpoint.api.SailPointContext;
import org.apache.commons.lang.StringUtils;
import sailpoint.tools.Util;



public String getAccountName(int option, String firstName, String lastName, String middleName) {
String firstInit = StringUtils.substring(firstName, 0, 1);
String lastInit = StringUtils.substring(lastName, 0, 1);
String middleInit = null;
boolean hasMiddleName = false;
if (Util.isNotNullOrEmpty(middleName)) {
middleInit = StringUtils.substring(middleName, 0, 1);
hasMiddleName = true;
}
switch (option) {
case 1:
return firstName + lastName;
case 2:
if (hasMiddleName) {
return firstName + middleInit + lastName;
}
return null;
case 3:
return firstInit + lastName;
case 4:
if (hasMiddleName) {
return firstInit + middleInit + lastName;
}
return null;
case 5:
return firstName + lastInit;
case 6:
if (hasMiddleName) {
return firstName + middleInit + lastInit;
}
return null;
default:
if (accountIteration < firstName.length() && accountIteration <= 50) {
accountIteration = accountIteration + 1;
String addInit = StringUtils.substring(firstName, 0, accountIteration);
return addInit + lastName;
}
accountIteration = accountIteration + 1;
return firstInit + lastName + (accountIteration - firstName.length());
}
}

public String formatString(String value) {
value = StringUtils.trimToNull(value);
if (Util.isNotNullOrEmpty(value)) {
value = value.replaceAll(\"\\\\W\", \"\").replaceAll(\"\\\\'\", \"\").toLowerCase().replaceAll(\"[^a-zA-Z0-9]\", \"\");
}
return value;
}

public boolean isUnique(Identity identity, String username) {
String sourceId = "";
String attributeName = "displayName";
String attributeValue = formatString(username);


return identity.isUniqueAttributeValue(sourceId, attributeName, attributeValue);
}


public String generateUniqueDN(SailPointContext context, Identity identity) throws GeneralException {
String firstName = (String) identity.getAttribute("firstName");
String lastName = (String) identity.getAttribute("lastName");
String middleName = (String) identity.getAttribute("middleName");
String displayName = (String) identity.getAttribute("displayName");
String UserOU = (String) identity.getAttribute("ou");

String uniqueDN = null;

if (Util.isNullOrEmpty(displayName)) {
displayName = getAccountName(1, firstName, lastName, middleName);
} else {
displayName = formatString(displayName);
}
boolean uniqueNameFound = false;
if (isUnique(identity, displayName)) {
uniqueDN = "CN=" + displayName + UserOU;
uniqueNameFound = true;
}
if(!uniqueNameFound){
int option = 1;
while (option <= 6) {
String username = getAccountName(option, firstName, lastName, middleName);
if (username != null && isUnique(identity, username)) {
uniqueDN = "CN=" + username + UserOU;
uniqueNameFound = true;
break;
}
option++;
}
}
if(!uniqueNameFound)
{
int accountIteration = 1;
while (accountIteration <= 50) {
String username = getAccountName(1, firstName, lastName, middleName) + accountIteration;
if (username != null && isUnique(identity, username)) {
uniqueDN = "CN=" + username + UserOU;
uniqueNameFound = true;
break;
}
accountIteration++;
}
}

return uniqueDN;
}

return generateUniqueDN(context, identity);
]]></Source>
</Rule>
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<Rule name="EmailGenerator" type="AttributeGenerator">
<Description>Generate a unique email address for Active Directory.</Description>
<Source><![CDATA[
import sailpoint.tools.GeneralException;
import sailpoint.object.Identity;
import sailpoint.object.Application;
import sailpoint.object.ProvisioningPlan.AccountRequest;
import sailpoint.object.ProvisioningPlan.AccountRequest.Operation;
import sailpoint.object.ProvisioningPlan.AttributeRequest;
import sailpoint.object.ProvisioningPlan;
import sailpoint.object.ProvisioningPlan.Operation;
import java.util.Random;
import org.apache.commons.lang.StringUtils;
import java.util.List;
import java.util.ArrayList;
import sailpoint.tools.Util;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

log.info("EMAIL GENERATOR RULE AD || 1: Entering the RULE - EmailGenerator");

String companyID = identity.getAttribute("companyId");
log.info("EMAIL GENERATOR RULE AD || 3: Retrieved COMPANY_ID is: " + companyID);

// Check for empty company ID, first name, or last name and throw an exception
if (Util.isNullOrEmpty(companyID)) {
String errorMessage = "EMAIL GENERATOR RULE AD || 4: Company ID is empty. Throwing exception.";
log.error(errorMessage);
throw new GeneralException(errorMessage);
}

String firstName = identity.getFirstname();
String lastName = identity.getLastname();
String isAssociate = identity.getAttribute("isAssociate");

// Check if first name or last name is empty and throw an exception
if (Util.isNullOrEmpty(firstName)) {
String errorMessage = "EMAIL GENERATOR RULE AD || 5: First name is empty. Throwing exception.";
log.error(errorMessage);
throw new GeneralException(errorMessage);
}
if (Util.isNullOrEmpty(lastName)) {
String errorMessage = "EMAIL GENERATOR RULE AD || 6: Last name is empty. Throwing exception.";
log.error(errorMessage);
throw new GeneralException(errorMessage);
}

/**
* Method to retrieve the email domain based on the company ID.
*/
public static String getDomainByCompanyID(String companyID) {
log.info("EMAIL GENERATOR RULE AD || 7: Entering getDomainByCompanyID method with companyID: " + companyID);

// Initialize the map with CompanyID and Email Domain pairs
Map companyDomains = new HashMap();
companyDomains.put("98", "jmfamily.com");
companyDomains.put("10", "jmfamily.com");
companyDomains.put("100", "jmfamily.com");
companyDomains.put("101", "jmfamily.com");
companyDomains.put("160", "jmfamilyholdings.com");
companyDomains.put("200", "setoyota.com");
companyDomains.put("250", "setoyota.com");
companyDomains.put("290", "setoyota.com");
companyDomains.put("300", "jmagroup.com");
companyDomains.put("301", "jmagroup.com");
companyDomains.put("303", "jmagroup.com");
companyDomains.put("305", "jmagroup.com");
companyDomains.put("307", "jmagroup.com");
companyDomains.put("400", "setf.com");
companyDomains.put("40011", "wofco.com");
companyDomains.put("40013", "ctrone.com");
companyDomains.put("40014", "yatc.org");
companyDomains.put("40015", "centurywarranty.com");
companyDomains.put("40016", "fiasolutions.com");
companyDomains.put("40017", "dsfs.ca");
companyDomains.put("40018", "mmsa.com");
companyDomains.put("40019", "jmcustomcreations.com");
companyDomains.put("40020", "dsfs.com");
companyDomains.put("421", "onedatascan.com");
companyDomains.put("422", "onedatascan.com");
companyDomains.put("424", "onedatascan.ca");
companyDomains.put("464", "onedatascan.com");
companyDomains.put("470", "onedatascan.com");
companyDomains.put("495", "onedatascan.ca");
companyDomains.put("700", "jmlexus.com");
companyDomains.put("997", "bellsouth.net");

// Return the domain for the given CompanyID, or a message if not found
String domain = companyDomains.getOrDefault(companyID, "Domain Not Found For Specific company ID");
log.info("EMAIL GENERATOR RULE AD || 8: Exiting getDomainByCompanyID method with domain: " + domain);
return domain;
}

log.info("EMAIL GENERATOR RULE AD || 9: Retrieved domain: " + getDomainByCompanyID(companyID));

/**
* Method to check if the generated email is unique.
*/
public boolean isUnique(String email, String identityId, String applicationName, String attributeName) throws GeneralException {
log.info("EMAIL GENERATOR RULE AD || 10: Entering isUnique method with email: " + email);

try {
boolean isUnique = idn.isUniqueLDAPValue(identityId, applicationName, attributeName, email);
log.info("EMAIL GENERATOR RULE AD || 11: Exiting isUnique method with result: " + isUnique);
return isUnique;
} catch (Exception e) {
log.error("EMAIL GENERATOR RULE AD || 12: Error in isUnique method: " + e.getMessage());
throw new GeneralException("Error in isUnique method", e);
}
}

/**
* Method to generate a valid and unique email address.
*/
public String generateEmail() throws GeneralException {

log.info("EMAIL GENERATOR RULE AD || 13: Entering generateEmail method");

String firstName = identity.getFirstname();
String lastName = identity.getLastname();
String companyId = identity.getAttribute("companyId");
String isAssociate = identity.getAttribute("isAssociate");

if (Util.isNullOrEmpty(firstName) || Util.isNullOrEmpty(lastName)) {
String errorMessage = "EMAIL GENERATOR RULE AD || 14: First name or last name is empty.";
log.error(errorMessage);
throw new GeneralException(errorMessage);
}

log.info("EMAIL GENERATOR RULE AD || 15: First name: " + firstName + ", Last name: " + lastName);

try {
String domain = getDomainByCompanyID(companyId);
if(domain.equalsIgnoreCase("Domain Not Found For Specific company ID")){
String errorMessage = "EMAIL GENERATOR RULE AD || 16: Invalid company ID, domain not found.";
log.error(errorMessage);
throw new GeneralException(errorMessage);
}

// Determine the base email structure
String baseEmail = firstName + "." + lastName;
if ("N".equalsIgnoreCase(isAssociate)) {
baseEmail += "_contractor"; // Add _contractor suffix for associates
}

String generatedEmail = baseEmail + "@" + domain;
log.info("EMAIL GENERATOR RULE AD || 17: Generated email: " + generatedEmail);

// Check if the generated email is unique
if (isUnique(generatedEmail, identity.getId(), application.getName(), "mail")) {
log.info("EMAIL GENERATOR RULE AD || 18: Exiting generateEmail method with result: " + generatedEmail.toLowerCase());
return generatedEmail.toLowerCase();
} else {
log.warn("EMAIL GENERATOR RULE AD || 19: Generated email is not unique. Adding iteration.");
}

// Fallback logic with iteration for both isAssociate == "N" and not "N"
int iteration = 1;
while (iteration < 100) {
String emailWithIteration = firstName + "." + lastName + iteration;
if ("N".equalsIgnoreCase(isAssociate)) {
emailWithIteration += "_contractor"; // Add _contractor suffix for associates
}
emailWithIteration += "@" + domain;

if (isUnique(emailWithIteration, identity.getId(), application.getName(), "mail")) {
log.info("EMAIL GENERATOR RULE AD || 20: Exiting generateEmail method with result: " + emailWithIteration.toLowerCase());
return emailWithIteration.toLowerCase();
}
iteration++;
}

String errorMessage = "EMAIL GENERATOR RULE AD || 21: Cannot generate email after 100 attempts";
log.error(errorMessage);
throw new GeneralException(errorMessage);

} catch (Exception e) {
String errorMessage = "EMAIL GENERATOR RULE AD || 22: Error in generateEmail method: " + e.getMessage();
log.error(errorMessage);
throw new GeneralException(errorMessage, e);
}
}

log.info("EMAIL GENERATOR RULE AD || 23: Exiting EmailGenerator rule");

return generateEmail();

]]></Source>
</Rule>
Loading