
To enable adding new users or groups of users to a workspace, you can implement search for users and groups with the help of the User Management Engine (UME) APIs contained in the com.sap.security.api package.
The UME factories, which serve as entry points to the various UME objects, are accessed as follows:
IGroupFactory groupFactory = UMFactory.getGroupFactory(); IRoleFactory roleFactory = UMFactory.getRoleFactory(); IUserFactory userFactory = UMFactory.getUserFactory();
The following samples illustrate how to search for groups according to various configuration options, defined in the Search for Groups configuration under .
private void searchForGroups( String searchFor )
{
IConfiguration configurationService = (IConfiguration) RuntimeFactory.getWorkspacesRuntime().getService(IConfiguration.class);
// Check if search for groups is enabled
String groupSearchEnabled = configurationService.getValue(ConfigurationKeys.GROUP_SEARCH_ENABLE);
// Get the root object in the user management data source
// from which to start searching for groups
String groupSearchRoot = configurationService.getValue(ConfigurationKeys.GROUP_SEARCH_ROOT);
// Get the type (‘group’, ‘role’ or ‘folder’) of the root object
// from which to start searching for groups
String groupSearchRootType = configurationService.getValue(ConfigurationKeys.GROUP_SEARCH_ROOT_TYPE);
List groups = new ArrayList();
if("true".equals( groupSearchEnabled )){
if("".equals( groupSearchRoot ))
{ // No specific root object is configured, search for all groups
groups = basicSearch(searchFor, groups);
}
else if("Group".equals(groupSearchRootType))
{ // Search for groups under the root group
groups = rootGroupSearch(searchFor, groups, groupSearchRoot);
}
else if("Role".equals(groupSearchRootType))
{ // Search for groups under the root role
groups = rootRoleSearch(searchFor, groups, groupSearchRoot);
}
else if("Folder".equals(groupSearchRootType))
{ // Search for groups under the root folder
String folderRegex = "*" + groupSearchRoot;
rootFolderSearch(searchFor, groups, folderRegex);
}
}
}
// Search for all groups
private void basicSearch(String searchFor, List groups)
throws UMException{
IGroupSearchFilter groupSearch = UMFactory.getGroupFactory().getGroupSearchFilter();
groupSearch.setDisplayName(searchFor,ISearchAttribute.LIKE_OPERATOR, false);
ISearchResult result = groupFactory.searchGroups(groupSearch);
if(result != null){
filterResults(result, groups, searchFor);
}
}
// Search for groups starting from the root group
private void rootGroupSearch(String searchFor, List groups, String rootGroupId) throws UMException{
IGroup rootGroup = UMFactory.getGroupFactory().getGroup(rootGroupId);
Iterator result = rootGroup.getGroupMembers(true);
if(result != null){
filterResults(result, groups, searchFor);
}
}
// Search for groups starting from the root role
private void rootRoleSearch(String searchFor, List groups, String rootRoleId) throws UMException
{
IRole rootRole = UMFactory.getRoleFactory().getRole(rootRoleId);
Iterator result = rootRole.getGroupMembers(true);
if(result != null){
filterResults(result, groups, searchFor);
}
}
// Search for groups starting from the root folder
private void rootFolderSearch(String regexSearchFor, List groups, String rootFolderId) throws UMException{
IGroupSearchFilter groupSearchFilter = UMFactory.getGroupFactory().getGroupSearchFilter();
groupSearchFilter.setDisplayName("*", ISearchAttribute.LIKE_OPERATOR, false);
ISearchResult result = UMFactory.getGroupFactory().searchGroups(groupSearchFilter);
//Filter groups by the configured root folder ID
if(result != null){
while (result.hasNext()) {
String groupID = (String) result.next();
if(groupID.matches(rootFolderId)){
groups.add(groupID);
}
}
addMatches(groups.iterator(), groups, regexSearchFor);
}
}
// Filter group search results by the specified search string
private void filterResults(Iterator result, List groups, String regexSearchFor)
{
IGroupFactory groupFactory = UMFactory.getGroupFactory();
while (result.hasNext())
{
String groupUniqueID = (String)result.next();
IGroup group = null;
try {
group = groupFactory.getGroup(groupUniqueID);
if(group != null){
String displayName = group.getDisplayName();
if(regexSearchFor == null || "*".equals(regexSearchFor) || displayName.matches(regexSearchFor))
{
groups.add(group);
}
}
} catch (UMException e) {}
}
}The following sample illustrates how to search for users.
// Get the Workspace Access role that enables access to enterprise workspaces
String ws_access_portal_role = "pcd:portal_content/every_user/workspaces/workspaces_access_role";
// Get the IRole object from UME
IRole WKSP_ACCESS_ROLE = roleFactory.getRole(ws_access_portal_role);
// Set the properties of user search
IUserSearchFilter userSearch = userFactory.getUserSearchFilter();
userSearch.setUniqueName(searchFor,
ISearchAttribute.LIKE_OPERATOR,false);
userSearch.setFirstName(searchFor,
ISearchAttribute.LIKE_OPERATOR,false);
userSearch.setLastName(searchFor,
ISearchAttribute.LIKE_OPERATOR,false);
userSearch.setSearchMethod(IPrincipalSearchFilter.SEARCHMETHOD_OR);
// Retrieve the search results using UME
ISearchResult result = userFactory.searchUsers(userSearch);
List<IUser> users = new ArrayList<IUser>();
while (result.hasNext()) {
String userUniqueID = (String)result.next();
// Check if the user assigned to the Workspace Access role)
if (WKSP_ACCESS_ROLE.isUserMember(userUniqueID, true)){
// Get the IUser object from UME
IUser user = userFactory.getUser(userUniqueID);
users.add(user);
}
}