Skip to content

Commit

Permalink
feat: user management enhancement to chk mandatory feilds
Browse files Browse the repository at this point in the history
  • Loading branch information
pujavs committed Apr 13, 2022
1 parent e6e2781 commit 3ac4b19
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public Response getUsers(@DefaultValue(DEFAULT_LIST_SIZE) @QueryParam(value = Ap
@QueryParam(value = ApiConstants.SORT_ORDER) String sortOrder)
throws IllegalAccessException, InvocationTargetException {
if (logger.isDebugEnabled()) {
logger.error("User search param - limit:{}, pattern:{}, startIndex:{}, sortBy:{}, sortOrder:{}",
logger.debug("User search param - limit:{}, pattern:{}, startIndex:{}, sortBy:{}, sortOrder:{}",
escapeLog(limit), escapeLog(pattern), escapeLog(startIndex), escapeLog(sortBy),
escapeLog(sortOrder));
}
SearchRequest searchReq = createSearchRequest(userSrv.getPeopleBaseDn(), pattern, sortBy, sortOrder, startIndex,
limit, null, userSrv.getUserExclusionAttributesAsString());

List<User> users = this.doSearch(searchReq);
logger.error("User search result:{}", users);
logger.debug("User search result:{}", users);

return Response.ok(users).build();
}
Expand All @@ -74,11 +74,11 @@ public Response getUsers(@DefaultValue(DEFAULT_LIST_SIZE) @QueryParam(value = Ap
public Response getUserByInum(@PathParam(ApiConstants.INUM) @NotNull String inum)
throws IllegalAccessException, InvocationTargetException {
if (logger.isDebugEnabled()) {
logger.error("User search by inum:{}", escapeLog(inum));
logger.debug("User search by inum:{}", escapeLog(inum));
}
User user = userSrv.getUserBasedOnInum(inum);
checkResourceNotNull(user, USER);
logger.error("user:{}", user);
logger.debug("user:{}", user);

// excludedAttributes
user = excludeUserAttributes(user);
Expand All @@ -91,14 +91,14 @@ public Response getUserByInum(@PathParam(ApiConstants.INUM) @NotNull String inum
public Response createUser(@Valid User user)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (logger.isDebugEnabled()) {
logger.error("User details to be added - user:{}", escapeLog(user));
logger.debug("User details to be added - user:{}", escapeLog(user));
}

// checking mandatory attributes
checkMissingAttributes(user);

user = userSrv.addUser(user, true);
logger.error("User created {}", user);
logger.debug("User created {}", user);

// excludedAttributes
user = excludeUserAttributes(user);
Expand All @@ -111,14 +111,14 @@ public Response createUser(@Valid User user)
public Response updateUser(@Valid User user)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (logger.isDebugEnabled()) {
logger.error("User details to be updated - user:{}", escapeLog(user));
logger.debug("User details to be updated - user:{}", escapeLog(user));
}

// checking mandatory attributes
checkMissingAttributes(user);

user = userSrv.updateUser((user));
logger.error("Updated user:{}", user);
logger.debug("Updated user:{}", user);

// excludedAttributes
user = excludeUserAttributes(user);
Expand All @@ -133,15 +133,15 @@ public Response patchUser(@PathParam(ApiConstants.INUM) @NotNull String inum,
@NotNull UserPatchRequest userPatchRequest)
throws IllegalAccessException, InvocationTargetException, JsonPatchException, IOException {
if (logger.isDebugEnabled()) {
logger.error("User:{} to be patched with :{} ", escapeLog(inum), escapeLog(userPatchRequest));
logger.debug("User:{} to be patched with :{} ", escapeLog(inum), escapeLog(userPatchRequest));
}
// check if user exists
User existingUser = userSrv.getUserBasedOnInum(inum);
checkResourceNotNull(existingUser, USER);

// patch user
existingUser = userSrv.patchUser(inum, userPatchRequest);
logger.error("Patched user:{}", existingUser);
logger.debug("Patched user:{}", existingUser);

// excludedAttributes
existingUser = excludeUserAttributes(existingUser);
Expand All @@ -154,7 +154,7 @@ public Response patchUser(@PathParam(ApiConstants.INUM) @NotNull String inum,
@ProtectedApi(scopes = { ApiAccessConstants.USER_DELETE_ACCESS })
public Response deleteUser(@PathParam(ApiConstants.INUM) @NotNull String inum) {
if (logger.isDebugEnabled()) {
logger.error("User to be deleted - inum:{} ", escapeLog(inum));
logger.debug("User to be deleted - inum:{} ", escapeLog(inum));
}
User user = userSrv.getUserBasedOnInum(inum);
checkResourceNotNull(user, USER);
Expand All @@ -164,21 +164,21 @@ public Response deleteUser(@PathParam(ApiConstants.INUM) @NotNull String inum) {

private List<User> doSearch(SearchRequest searchReq) throws IllegalAccessException, InvocationTargetException {
if (logger.isDebugEnabled()) {
logger.error("User search params - searchReq:{} ", escapeLog(searchReq));
logger.debug("User search params - searchReq:{} ", escapeLog(searchReq));
}

PagedResult<User> pagedResult = userSrv.searchUsers(searchReq);
if (logger.isTraceEnabled()) {
logger.error("PagedResult - pagedResult:{}", pagedResult);
logger.debug("PagedResult - pagedResult:{}", pagedResult);
}

List<User> users = new ArrayList<>();
if (pagedResult != null) {
logger.error("Users fetched - pagedResult.getEntries():{}", pagedResult.getEntries());
logger.debug("Users fetched - pagedResult.getEntries():{}", pagedResult.getEntries());
users = pagedResult.getEntries();
}
if (logger.isDebugEnabled()) {
logger.error("Users fetched - users:{}", users);
logger.debug("Users fetched - users:{}", users);
}

// excludedAttributes
Expand All @@ -194,7 +194,7 @@ private User excludeUserAttributes(User user) throws IllegalAccessException, Inv
private void checkMissingAttributes(User user)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
String missingAttributes = userSrv.checkMandatoryFields(user);
logger.error("missingAttributes:{}", missingAttributes);
logger.debug("missingAttributes:{}", missingAttributes);

if (StringHelper.isEmpty(missingAttributes)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,24 @@ public User patchUser(String inum, UserPatchRequest userPatchRequest) throws Jso
return null;
}

logger.error("User to be patched- user:{}", user);
logger.debug("User to be patched- user:{}", user);
// apply direct patch for basic attributes
if (StringUtils.isNotEmpty(userPatchRequest.getJsonPatchString())) {
logger.error("Patch basic attributes");
logger.debug("Patch basic attributes");
user = Jackson.applyPatch(userPatchRequest.getJsonPatchString(), user);
logger.error("User after patching basic attributes - user:{}", user);
logger.debug("User after patching basic attributes - user:{}", user);
}

// patch for customAttributes
if (userPatchRequest.getCustomAttributes() != null && !userPatchRequest.getCustomAttributes().isEmpty()) {
updateCustomAttributes(user, userPatchRequest.getCustomAttributes());
}

logger.error("User before patch user:{}", user);
logger.debug("User before patch user:{}", user);

// persist user
user = updateUser(user);
logger.error("User after patch user:{}", user);
logger.debug("User after patch user:{}", user);
return user;

}
Expand All @@ -136,18 +136,18 @@ public User getUserBasedOnInum(String inum) {
}

private User updateCustomAttributes(User user, List<CustomObjectAttribute> customAttributes) {
logger.error("Custom Attributes to update for - user:{}, customAttributes:{} ", user, customAttributes);
logger.debug("Custom Attributes to update for - user:{}, customAttributes:{} ", user, customAttributes);

if (customAttributes != null && !customAttributes.isEmpty()) {
for (CustomObjectAttribute attribute : customAttributes) {
CustomObjectAttribute existingAttribute = getCustomAttribute(user, attribute.getName());
logger.error("Existing CustomAttributes with existingAttribute:{} ", existingAttribute);
logger.debug("Existing CustomAttributes with existingAttribute:{} ", existingAttribute);

// add
if (existingAttribute == null) {
boolean result = addUserAttribute(user, attribute.getName(), attribute.getValues(),
attribute.isMultiValued());
logger.error("Result of adding CustomAttributes attribute:{} , result:{} ", attribute, result);
logger.debug("Result of adding CustomAttributes attribute:{} , result:{} ", attribute, result);
}
// remove attribute
else if (attribute.getValue() == null || attribute.getValues() == null) {
Expand All @@ -160,7 +160,7 @@ else if (attribute.getValue() == null || attribute.getValues() == null) {
existingAttribute.setValues(attribute.getValues());
}
// Final attribute
logger.error("Finally user CustomAttributes user.getCustomAttributes:{} ", user.getCustomAttributes());
logger.debug("Finally user CustomAttributes user.getCustomAttributes:{} ", user.getCustomAttributes());

}
}
Expand All @@ -170,43 +170,43 @@ else if (attribute.getValue() == null || attribute.getValues() == null) {

public List<User> excludeAttributes(List<User> users, String commaSeparatedString)
throws IllegalAccessException, InvocationTargetException {
logger.error("Attributes:{} to be excluded from users:{} ", commaSeparatedString, users);
logger.debug("Attributes:{} to be excluded from users:{} ", commaSeparatedString, users);
for (User user : users) {
excludeAttributes(user, commaSeparatedString);
}
logger.error("Users:{} after excluding attribute:{} ", users, commaSeparatedString);
logger.debug("Users:{} after excluding attribute:{} ", users, commaSeparatedString);

return users;
}

public User excludeAttributes(User user, String commaSeparatedString)
throws IllegalAccessException, InvocationTargetException {
logger.error("Attributes:{} to be excluded from user:{} ", commaSeparatedString, user);
logger.debug("Attributes:{} to be excluded from user:{} ", commaSeparatedString, user);
if (user == null || StringUtils.isEmpty(commaSeparatedString)) {
return user;
}
List<String> excludedAttributes = Arrays.asList(commaSeparatedString.split(","));
logger.error("Attributes List:{} to be excluded ", excludedAttributes);
logger.debug("Attributes List:{} to be excluded ", excludedAttributes);

List<Field> allFields = authUtil.getAllFields(user.getClass());
logger.error("All user fields :{} ", allFields);
logger.debug("All user fields :{} ", allFields);

HashMap<String, String> map = new HashMap<>();
for (String attribute : excludedAttributes) {
logger.error("User class allFields:{} conatins attribute:{} ? :{} ", allFields, attribute,
logger.debug("User class allFields:{} conatins attribute:{} ? :{} ", allFields, attribute,
authUtil.containsField(allFields, attribute));
if (authUtil.containsField(allFields, attribute)) {
logger.error("User class contains attribute:{} ! ", attribute);
logger.debug("User class contains attribute:{} ! ", attribute);
map.put(attribute, null);
} else {
logger.error("Removing custom attribute:{} from user:{} ", attribute, user);
logger.debug("Removing custom attribute:{} from user:{} ", attribute, user);
user.removeAttribute(attribute);
}
}

logger.error("Attributes map:{} to be excluded ", map);
logger.debug("Attributes map:{} to be excluded ", map);
if (!map.isEmpty()) {
logger.error("Removing simple attributes:{} from user object ", map);
logger.debug("Removing simple attributes:{} from user object ", map);
BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
BeanUtils.populate(user, map);
}
Expand All @@ -221,7 +221,7 @@ public String getUserExclusionAttributesAsString() {
public String checkMandatoryFields(User user)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
List<String> mandatoryAttributes = authUtil.getUserMandatoryAttributes();
logger.error("mandatoryAttributess :{} ", mandatoryAttributes);
logger.debug("mandatoryAttributess :{} ", mandatoryAttributes);

StringBuilder missingAttributes = new StringBuilder();

Expand All @@ -230,30 +230,30 @@ public String checkMandatoryFields(User user)
}

List<Field> allFields = authUtil.getAllFields(user.getClass());
logger.error("All user fields :{} ", allFields);
logger.debug("All user fields :{} ", allFields);

Object attributeValue = null;
for (String attribute : mandatoryAttributes) {
logger.error("User class allFields:{} conatins attribute:{} ? :{} ", allFields, attribute,
logger.debug("User class allFields:{} conatins attribute:{} ? :{} ", allFields, attribute,
authUtil.containsField(allFields, attribute));
if (authUtil.containsField(allFields, attribute)) {
logger.error("Checking if attribute:{} is simple attribute", attribute);
logger.debug("Checking if attribute:{} is simple attribute", attribute);
attributeValue = BeanUtils.getProperty(user, attribute);
logger.error("User basic attribute:{} - attributeValue:{} ", attribute, attributeValue);
logger.debug("User basic attribute:{} - attributeValue:{} ", attribute, attributeValue);
} else {
logger.error("Checking if attribute:{} is custom attribute", attribute);
logger.debug("Checking if attribute:{} is custom attribute", attribute);
attributeValue = user.getAttribute(attribute);
logger.error("User custom attribute:{} - attributeValue:{} ", attribute, attributeValue);
logger.debug("User custom attribute:{} - attributeValue:{} ", attribute, attributeValue);
}

if (attributeValue == null) {
missingAttributes.append(attribute).append(",");
}
}
logger.error("Checking mandatory missingAttributes:{} ", missingAttributes);
logger.debug("Checking mandatory missingAttributes:{} ", missingAttributes);
missingAttributes.replace(missingAttributes.lastIndexOf(","), missingAttributes.length(), "");

logger.error("Returning missingAttributes:{} ", missingAttributes);
logger.debug("Returning missingAttributes:{} ", missingAttributes);
return missingAttributes.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
"corsRequestDecorate": true,
"corsEnabled": true
}
],
"userExclusionAttributes": [
"userPassword"
],
"userMandatoryAttributes": [
"mail",
"displayName",
"jansStatus",
"userPassword",
"givenName"
]
}

Expand Down

0 comments on commit 3ac4b19

Please sign in to comment.