package oracle.iam.plugins;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.iam.identity.exception.UserNameGenerationException;
import oracle.iam.identity.usermgmt.api.UserNamePolicy;
import oracle.iam.identity.usermgmt.utils.UserNameGenerationUtil;
import oracle.iam.identity.usermgmt.utils.UserNamePolicyUtil;
import oracle.iam.platform.kernel.ValidationFailedException;
public class ShahbazUserNamePolicy implements UserNamePolicy {
private static final String CLASS_NAME =
ShahbazUserNamePolicy.class.getSimpleName();
private static final String policy =
"ShahbazUserNamePolicy:Validation Failed: ";
private static Logger logger =
Logger.getLogger("COM.SHAHBAZ.ShahbazUserNamePolicy");
public ShahbazUserNamePolicy() {
super();
}
/**
*This is the main method which gets trigger and generate user name based on requested input data e.g. first name and last name.
* @param reqData - Map contains request data e.g. first name and last name
* @return- generated user name
* @throws ValidationFailedException
*/
public String getUserNameFromPolicy(Map<String, String> reqData) throws ValidationFailedException {
String METHOD_NAME = CLASS_NAME + ":" + "getUserNameFromPolicy: ";
logger.log(Level.FINE, METHOD_NAME + ":START");
String userName = null;
String fullName =
(String)reqData.get("First Name") + " " + (String)reqData.get("Last Name");
logger.log(Level.FINE,
METHOD_NAME + ":Generating Network ID for User Full Name:" +
fullName);
userName = generateNetworkId();
userName = UserNameGenerationUtil.trimWhiteSpaces(userName);
try {
if ((UserNamePolicyUtil.isUserExists(userName)) ||
(UserNamePolicyUtil.isUserNameReserved(userName))) {
boolean userNameGenerated = false;
for (int ix = 1; ix < Integer.MAX_VALUE; ix++) {
userName = generateNetworkId();
if (UserNameGenerationUtil.isUserNameExistingOrReserved(userName)) {
continue;
}
userNameGenerated = true;
break;
}
if (!userNameGenerated) {
String error_message =
"This should never happen. OIM is failed to generate unique user name for " +
fullName;
showErrorMessagePopup(error_message);
}
}
} catch (UserNameGenerationException e) {
logger.log(Level.SEVERE,
METHOD_NAME + "Exception: " + e.getMessage(), e);
}
System.out.println(METHOD_NAME +
":Successfully generated unique network ID for " +
fullName + " Network ID: " + userName);
logger.log(Level.FINE,
METHOD_NAME + ":Successfully generated unique network ID for " +
fullName + " Network ID: " + userName);
logger.log(Level.FINE, METHOD_NAME + ":END");
return userName;
}
@Override
public boolean isUserNameValid(String string, Map<String, String> map) {
return false;
}
@Override
public String getDescription(Locale locale) {
return "Generate Unique Network ID n + 5 random numbers + 2 random chars ShahbazUserNamePolicy";
}
/**
*This method is used to generate random netowork id
* @returns random network id
*/
private static String generateNetworkId() {
Random rand = new Random();
String userid = "n";
int pick = rand.nextInt(90000) + 10000;
userid = userid.concat(Integer.toString(pick));
char c1 = (char)(rand.nextInt(26) + 'a');
char c2 = (char)(rand.nextInt(26) + 'a');
userid = userid.concat(Character.toString(c1));
userid = userid.concat(Character.toString(c2));
return userid;
}
/**
*This method is used to show error message popup if there is any validation failure.
* @param errorMessage - Message to be displayed.
* @throws ValidationFailedException
*/
private void showErrorMessagePopup(String errorMessage) throws ValidationFailedException {
String error_message = policy + errorMessage;
ValidationFailedException exception =
new ValidationFailedException(error_message);
exception.setErrorCode("IAM-3050105");
exception.setErrorData(new Object[] { error_message });
throw exception;
}
/* public static void main(String[] args) {
ShahbazUserNamePolicy unamePolicy = new ShahbazUserNamePolicy();
for (int j = 0; j < 10; j++) {
System.out.println(unamePolicy.generateNetworkId());
}
}
*/
}
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.iam.identity.exception.UserNameGenerationException;
import oracle.iam.identity.usermgmt.api.UserNamePolicy;
import oracle.iam.identity.usermgmt.utils.UserNameGenerationUtil;
import oracle.iam.identity.usermgmt.utils.UserNamePolicyUtil;
import oracle.iam.platform.kernel.ValidationFailedException;
public class ShahbazUserNamePolicy implements UserNamePolicy {
private static final String CLASS_NAME =
ShahbazUserNamePolicy.class.getSimpleName();
private static final String policy =
"ShahbazUserNamePolicy:Validation Failed: ";
private static Logger logger =
Logger.getLogger("COM.SHAHBAZ.ShahbazUserNamePolicy");
public ShahbazUserNamePolicy() {
super();
}
/**
*This is the main method which gets trigger and generate user name based on requested input data e.g. first name and last name.
* @param reqData - Map contains request data e.g. first name and last name
* @return- generated user name
* @throws ValidationFailedException
*/
public String getUserNameFromPolicy(Map<String, String> reqData) throws ValidationFailedException {
String METHOD_NAME = CLASS_NAME + ":" + "getUserNameFromPolicy: ";
logger.log(Level.FINE, METHOD_NAME + ":START");
String userName = null;
String fullName =
(String)reqData.get("First Name") + " " + (String)reqData.get("Last Name");
logger.log(Level.FINE,
METHOD_NAME + ":Generating Network ID for User Full Name:" +
fullName);
userName = generateNetworkId();
userName = UserNameGenerationUtil.trimWhiteSpaces(userName);
try {
if ((UserNamePolicyUtil.isUserExists(userName)) ||
(UserNamePolicyUtil.isUserNameReserved(userName))) {
boolean userNameGenerated = false;
for (int ix = 1; ix < Integer.MAX_VALUE; ix++) {
userName = generateNetworkId();
if (UserNameGenerationUtil.isUserNameExistingOrReserved(userName)) {
continue;
}
userNameGenerated = true;
break;
}
if (!userNameGenerated) {
String error_message =
"This should never happen. OIM is failed to generate unique user name for " +
fullName;
showErrorMessagePopup(error_message);
}
}
} catch (UserNameGenerationException e) {
logger.log(Level.SEVERE,
METHOD_NAME + "Exception: " + e.getMessage(), e);
}
System.out.println(METHOD_NAME +
":Successfully generated unique network ID for " +
fullName + " Network ID: " + userName);
logger.log(Level.FINE,
METHOD_NAME + ":Successfully generated unique network ID for " +
fullName + " Network ID: " + userName);
logger.log(Level.FINE, METHOD_NAME + ":END");
return userName;
}
@Override
public boolean isUserNameValid(String string, Map<String, String> map) {
return false;
}
@Override
public String getDescription(Locale locale) {
return "Generate Unique Network ID n + 5 random numbers + 2 random chars ShahbazUserNamePolicy";
}
/**
*This method is used to generate random netowork id
* @returns random network id
*/
private static String generateNetworkId() {
Random rand = new Random();
String userid = "n";
int pick = rand.nextInt(90000) + 10000;
userid = userid.concat(Integer.toString(pick));
char c1 = (char)(rand.nextInt(26) + 'a');
char c2 = (char)(rand.nextInt(26) + 'a');
userid = userid.concat(Character.toString(c1));
userid = userid.concat(Character.toString(c2));
return userid;
}
/**
*This method is used to show error message popup if there is any validation failure.
* @param errorMessage - Message to be displayed.
* @throws ValidationFailedException
*/
private void showErrorMessagePopup(String errorMessage) throws ValidationFailedException {
String error_message = policy + errorMessage;
ValidationFailedException exception =
new ValidationFailedException(error_message);
exception.setErrorCode("IAM-3050105");
exception.setErrorData(new Object[] { error_message });
throw exception;
}
/* public static void main(String[] args) {
ShahbazUserNamePolicy unamePolicy = new ShahbazUserNamePolicy();
for (int j = 0; j < 10; j++) {
System.out.println(unamePolicy.generateNetworkId());
}
}
*/
}
No comments:
Post a Comment