Skip to content

Commit

Permalink
feat(jans-config-api): user custom attributes at root level - 1348
Browse files Browse the repository at this point in the history
  • Loading branch information
pujavs committed May 17, 2022
1 parent 3c15e44 commit 5b3f0a1
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 17 deletions.
35 changes: 35 additions & 0 deletions agama/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Eclipse
.project
.classpath
.settings/
bin/

# IntelliJ
.idea
*.ipr
*.iml
*.iws

# NetBeans
nb-configuration.xml

# Visual Studio Code
.vscode

# OSX
.DS_Store

# Vim
*.swp
*.swo

# patch
*.orig
*.rej

# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
release.properties
6 changes: 6 additions & 0 deletions jans-config-api/docs/jans-config-api-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6697,6 +6697,12 @@ components:
dn:
type: string
description: Domain name.
baseDN:
type: string
description: Base DN for the User entity
status:
type: string
description: User status
userId:
description: A domain issued and managed identifier for the user.
type: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.jans.configapi.plugin.mgt.model.user;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import io.jans.orm.model.base.CustomObjectAttribute;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class User extends io.jans.as.common.model.common.User {

private static final long serialVersionUID = 1L;

private String mail;
private String displayName;
private String jansStatus;
private String givenName;
private String userPassword;

public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getJansStatus() {
return jansStatus;
}
public void setJansStatus(String jansStatus) {
this.jansStatus = jansStatus;
}
public String getGivenName() {
return givenName;
}
public void setGivenName(String givenName) {
this.givenName = givenName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
@Override
public String toString() {
return "User [mail=" + mail + ", displayName=" + displayName + ", jansStatus=" + jansStatus + ", givenName="
+ givenName + ", userPassword= XXXXX ]";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import com.github.fge.jsonpatch.JsonPatchException;

import static io.jans.as.model.util.Util.escapeLog;
import io.jans.as.common.model.common.User;
import io.jans.configapi.plugin.mgt.model.user.User;
import io.jans.as.common.service.common.EncryptionService;
import io.jans.configapi.core.rest.BaseResource;
import io.jans.configapi.core.rest.ProtectedApi;
import io.jans.configapi.plugin.mgt.model.config.UserMgtConfigSource;
import io.jans.configapi.plugin.mgt.model.user.UserPatchRequest;
import io.jans.configapi.plugin.mgt.service.UserService;
import io.jans.configapi.plugin.mgt.util.Constants;
Expand Down Expand Up @@ -94,7 +93,7 @@ public Response getUserByInum(@PathParam(ApiConstants.INUM) @NotNull String inum

return Response.ok(decryptUserPassword(user)).build();
}

/*
@POST
@ProtectedApi(scopes = { ApiAccessConstants.USER_WRITE_ACCESS })
public Response createUser(@Valid User user)
Expand Down Expand Up @@ -171,6 +170,8 @@ public Response deleteUser(@PathParam(ApiConstants.INUM) @NotNull String inum) {
return Response.noContent().build();
}
*/

private List<User> doSearch(SearchRequest searchReq) throws IllegalAccessException, InvocationTargetException {
if (logger.isDebugEnabled()) {
logger.debug("User search params - searchReq:{} ", escapeLog(searchReq));
Expand Down Expand Up @@ -236,6 +237,6 @@ private User decryptUserPassword(User user) throws EncryptionException {
}
return user;
}


}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.jans.configapi.plugin.mgt.service;

import com.github.fge.jsonpatch.JsonPatchException;
import io.jans.as.common.model.common.User;
import io.jans.configapi.plugin.mgt.model.user.User;
import io.jans.as.common.util.AttributeConstants;
import io.jans.as.model.config.StaticConfiguration;
import io.jans.as.model.configuration.AppConfiguration;
import io.jans.configapi.core.util.Jackson;
import io.jans.configapi.plugin.mgt.model.user.UserPatchRequest;
import io.jans.configapi.core.model.SearchRequest;
import io.jans.configapi.core.util.DataUtil;
import io.jans.configapi.util.AuthUtil;
import io.jans.orm.model.PagedResult;
import io.jans.orm.model.SortOrder;
import io.jans.orm.model.base.CustomObjectAttribute;
import io.jans.orm.reflect.property.Setter;
import io.jans.orm.search.filter.Filter;
import io.jans.util.StringHelper;

Expand Down Expand Up @@ -78,12 +80,27 @@ public PagedResult<User> searchUsers(SearchRequest searchRequest) {
searchRequest.getStartIndex() - 1, searchRequest.getCount(), searchRequest.getMaxCount());

}

public User getUserBasedOnInum(String inum) {
User result = null;
try {
io.jans.as.common.model.common.User user = getUserByInum(inum);
result = getConfigUser(user);
} catch (Exception ex) {
logger.error("Failed to load user entry", ex);
}
return result;
}

private User getConfigUser(io.jans.as.common.model.common.User user) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
return populateConfigUser(user);
}

public void removeUser(User user) {
persistenceEntryManager.removeRecursively(user.getDn(), User.class);
}

public User patchUser(String inum, UserPatchRequest userPatchRequest) throws JsonPatchException, IOException {
public io.jans.as.common.model.common.User patchUser(String inum, UserPatchRequest userPatchRequest) throws JsonPatchException, IOException {
if (logger.isDebugEnabled()) {
logger.debug("Details to patch user inum:{}, UserPatchRequest:{} ", escapeLog(inum),
escapeLog(userPatchRequest));
Expand All @@ -92,7 +109,7 @@ public User patchUser(String inum, UserPatchRequest userPatchRequest) throws Jso
return null;
}

User user = getUserByInum(inum);
io.jans.as.common.model.common.User user = getUserByInum(inum);
if (user == null) {
return null;
}
Expand All @@ -119,17 +136,9 @@ public User patchUser(String inum, UserPatchRequest userPatchRequest) throws Jso

}

public User getUserBasedOnInum(String inum) {
User result = null;
try {
result = getUserByInum(inum);
} catch (Exception ex) {
logger.error("Failed to load user entry", ex);
}
return result;
}


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

if (customAttributes == null || customAttributes.isEmpty()) {
Expand Down Expand Up @@ -221,6 +230,7 @@ public String getUserExclusionAttributesAsString() {
return authUtil.getUserExclusionAttributesAsString();
}


public String checkMandatoryFields(User user)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
List<String> mandatoryAttributes = authUtil.getUserMandatoryAttributes();
Expand Down Expand Up @@ -262,4 +272,43 @@ public String checkMandatoryFields(User user)
return missingAttributes.toString();
}

private User populateConfigUser(io.jans.as.common.model.common.User user) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
{
List<String> mandatoryAttributes = authUtil.getUserMandatoryAttributes();
logger.debug("mandatoryAttributess :{} ", mandatoryAttributes);

User userConfig = (User) user;
StringBuilder missingAttributes = new StringBuilder();
if (mandatoryAttributes == null || mandatoryAttributes.isEmpty()) {
return userConfig;
}

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

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

//set attribute
Setter setterMethod = DataUtil.getSetterMethod(userConfig.getClass(), attribute);
Object propertyValue = setterMethod.getMethod().invoke(userConfig, attributeValue);
logger.error("After setterMethod invoked attribute:{}, propertyValue:{} ", attribute, propertyValue);
}//for
logger.debug("Checking mandatory userConfig:{} ", userConfig);

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

return userConfig;
}
}
Loading

0 comments on commit 5b3f0a1

Please sign in to comment.