Skip to content

Commit

Permalink
feat(jans-config-api): user management mandatory field chk changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pujavs committed Apr 12, 2022
1 parent a134adb commit e242ec6
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class ApiAppConfiguration implements Configuration {
private String externalLoggerConfiguration;
private Boolean disableJdkLogger = true;
private int maxCount;

private List<String> userExclusionAttributes;
private List<String> userMandatoryAttributes;

public boolean isConfigOauthEnabled() {
return configOauthEnabled;
Expand Down Expand Up @@ -203,6 +206,22 @@ public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}

public List<String> getUserExclusionAttributes() {
return userExclusionAttributes;
}

public void setUserExclusionAttributes(List<String> userExclusionAttributes) {
this.userExclusionAttributes = userExclusionAttributes;
}

public List<String> getUserMandatoryAttributes() {
return userMandatoryAttributes;
}

public void setUserMandatoryAttributes(List<String> userMandatoryAttributes) {
this.userMandatoryAttributes = userMandatoryAttributes;
}

@Override
public String toString() {
return "ApiAppConfiguration [" + " apiApprovedIssuer=" + apiApprovedIssuer + ", apiProtectionType="
Expand All @@ -214,7 +233,10 @@ public String toString() {
+ ", corsConfigurationFilters=" + corsConfigurationFilters + ", exclusiveAuthScopes="
+ exclusiveAuthScopes + ", loggingLevel=" + loggingLevel + " , loggingLayout=" + loggingLayout
+ " , externalLoggerConfiguration=" + externalLoggerConfiguration + " , disableJdkLogger="
+ disableJdkLogger + " , maxCount =" + maxCount + "]";
+ disableJdkLogger + " , maxCount =" + maxCount
+ " , userExclusionAttributes="+ userExclusionAttributes
+ " , userMandatoryAttributes="+ userMandatoryAttributes
+ "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ private ApiConstants() {}
public static final String SORT_ORDER = "sortOrder";

//commaSeparatedString of attributes to be excluded in User fetch request
public static final String USER_EXCLUDED_ATTRIBUTES = "userPassword,";
//public static final String USER_EXCLUDED_ATTRIBUTES = "userPassword,";

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import io.jans.configapi.service.auth.UserService;
import io.jans.configapi.util.ApiAccessConstants;
import io.jans.configapi.util.ApiConstants;
import io.jans.configapi.util.AttributeNames;
import io.jans.orm.model.PagedResult;
import io.jans.util.StringHelper;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -44,7 +46,7 @@ public class UserResource extends BaseResource {

@Inject
UserService userSrv;

@GET
@ProtectedApi(scopes = { ApiAccessConstants.USER_READ_ACCESS })
public Response getUsers(@DefaultValue(DEFAULT_LIST_SIZE) @QueryParam(value = ApiConstants.LIMIT) int limit,
Expand All @@ -59,7 +61,7 @@ public Response getUsers(@DefaultValue(DEFAULT_LIST_SIZE) @QueryParam(value = Ap
escapeLog(sortOrder));
}
SearchRequest searchReq = createSearchRequest(userSrv.getPeopleBaseDn(), pattern, sortBy, sortOrder, startIndex,
limit, null, ApiConstants.USER_EXCLUDED_ATTRIBUTES);
limit, null, userSrv.getUserExclusionAttributesAsString());

List<User> users = this.doSearch(searchReq);
logger.debug("User search result:{}", users);
Expand Down Expand Up @@ -94,6 +96,8 @@ public Response createUser(@Valid User user) throws IllegalAccessException, Invo
user = userSrv.addUser(user, true);
logger.debug("User created {}", user);



// excludedAttributes
user = excludeUserAttributes(user);

Expand Down Expand Up @@ -176,7 +180,18 @@ private List<User> doSearch(SearchRequest searchReq) throws IllegalAccessExcepti
}

private User excludeUserAttributes(User user) throws IllegalAccessException, InvocationTargetException {
return userSrv.excludeAttributes(user, ApiConstants.USER_EXCLUDED_ATTRIBUTES);
return userSrv.excludeAttributes(user, userSrv.getUserExclusionAttributesAsString());
}



private void checkMissingAttributes(User user) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
String missingAttributes = userSrv.checkMandatoryFields(user);

if(StringHelper.isEmpty(missingAttributes)) {
return;
}

checkNotNull(missingAttributes, "Mandatory User Attributes");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public User patchUser(String inum, UserPatchRequest userPatchRequest) throws Jso
return user;

}

public User getUserBasedOnInum(String inum) {
User result = null;
try {
Expand Down Expand Up @@ -189,25 +189,23 @@ public User excludeAttributes(User user, String commaSeparatedString)
logger.debug("Attributes List:{} to be excluded ", excludedAttributes);

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


logger.debug("All user fields :{} ", allFields);

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

logger.debug("Attributes map:{} to be excluded ", map);
if(!map.isEmpty()) {
if (!map.isEmpty()) {
logger.debug("Removing simple attributes:{} from user object ", map);
BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);
BeanUtils.populate(user, map);
Expand All @@ -216,5 +214,44 @@ public User excludeAttributes(User user, String commaSeparatedString)
return user;
}


public String getUserExclusionAttributesAsString() {
return authUtil.getUserExclusionAttributesAsString();
}

public String checkMandatoryFields(User user)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
List<String> mandatoryAttributes = authUtil.getUserMandatoryAttributes();
logger.error("mandatoryAttributess :{} ", mandatoryAttributes);

StringBuilder missingAttributes = new StringBuilder();

if (mandatoryAttributes == null || mandatoryAttributes.isEmpty()) {
return missingAttributes.toString();
}

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

Object attributeValue = null;
for (String attribute : mandatoryAttributes) {
logger.error("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);
attributeValue = BeanUtils.getProperty(user, attribute);
logger.error("User basic attribute:{} - attributeValue:{} ", attribute, attributeValue);
} else {
logger.error("Checking if attribute:{} is custom attribute", attribute);
attributeValue = user.getAttribute(attribute);
logger.error("User custom attribute:{} - attributeValue:{} ", attribute, attributeValue);
}

if(attributeValue == null) {
missingAttributes.append(attribute).append(",");
}
}
logger.error("Checking mandatory missingAttributes:{} ", missingAttributes);
return missingAttributes.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ public String getServiceUrl(String url) {
public String getClientId() {
return this.configurationFactory.getApiClientId();
}

public List<String> getUserExclusionAttributes() {
return this.configurationFactory.getApiAppConfiguration().getUserExclusionAttributes();
}

public String getUserExclusionAttributesAsString() {
List<String> excludedAttributes = getUserExclusionAttributes();
return excludedAttributes == null ? null : excludedAttributes.stream().collect(Collectors.joining(","));
}

public List<String> getUserMandatoryAttributes() {
return this.configurationFactory.getApiAppConfiguration().getUserMandatoryAttributes();
}

public String getTokenUrl() {
return this.configurationService.find().getTokenEndpoint();
Expand Down

0 comments on commit e242ec6

Please sign in to comment.