Search in sources :

Example 11 with SCIMGroupHandler

use of org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMUserManager method getGroupNamesFromDB.

/**
 * Return group names when search using meta data; list of groups.
 *
 * @param attributeName   Attribute name which is used to search.
 * @param filterOperation Operator value.
 * @param attributeValue  Search value.
 * @param domainName      Domain to be filtered.
 * @return list of groups
 * @throws org.wso2.carbon.user.core.UserStoreException
 * @throws IdentitySCIMException
 */
private List<String> getGroupNamesFromDB(String attributeName, String filterOperation, String attributeValue, String domainName) throws org.wso2.carbon.user.core.UserStoreException, IdentitySCIMException {
    String searchAttribute = getSearchAttribute(attributeName, filterOperation, attributeValue, SQL_FILTERING_DELIMITER);
    SCIMGroupHandler groupHandler = new SCIMGroupHandler(carbonUM.getTenantId());
    if (log.isDebugEnabled()) {
        log.debug(String.format("Filtering roleNames from DB from search attribute: %s", searchAttribute));
    }
    return Arrays.asList(groupHandler.getGroupListFromAttributeName(attributeName, searchAttribute, domainName));
}
Also used : SCIMGroupHandler(org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler)

Example 12 with SCIMGroupHandler

use of org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMUserOperationListener method postAddRole.

private boolean postAddRole(String roleName, UserStoreManager userStoreManager) throws UserStoreException {
    try {
        SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(userStoreManager.getTenantId());
        String domainName = UserCoreUtil.getDomainName(userStoreManager.getRealmConfiguration());
        if (domainName == null) {
            domainName = UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME;
        }
        String roleNameWithDomain = UserCoreUtil.addDomainToName(roleName, domainName);
        // UserCore Util functionality does not append primary.
        roleNameWithDomain = SCIMCommonUtils.getGroupNameWithDomain(roleNameWithDomain);
        // Query role name from identity table.
        try {
            if (!scimGroupHandler.isGroupExisting(roleNameWithDomain)) {
                // If no attributes - i.e: group added via mgt console, not via SCIM endpoint.
                // Add META.
                scimGroupHandler.addMandatoryAttributes(roleNameWithDomain);
            }
        } catch (IdentitySCIMException e) {
            throw new UserStoreException("Error retrieving group information from SCIM Tables.", e);
        }
        return true;
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        throw new UserStoreException(e);
    }
}
Also used : UserStoreException(org.wso2.carbon.user.core.UserStoreException) SCIMGroupHandler(org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler) IdentitySCIMException(org.wso2.carbon.identity.scim2.common.exceptions.IdentitySCIMException)

Example 13 with SCIMGroupHandler

use of org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class AdminAttributeUtil method updateAdminGroup.

/**
 * Update admin group for given tenant.
 *
 * @param tenantId
 */
public static void updateAdminGroup(int tenantId) {
    try {
        UserStoreManager userStoreManager = (UserStoreManager) SCIMCommonComponentHolder.getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
        if (log.isDebugEnabled()) {
            log.debug("SCIM enable in Userstore level : " + userStoreManager.isSCIMEnabled() + ", for " + "Tenant ID : " + tenantId);
        }
        // User store level property to enable/disable SCIM
        if (userStoreManager.isSCIMEnabled()) {
            SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(userStoreManager.getTenantId());
            String domainName = UserCoreUtil.getDomainName(userStoreManager.getRealmConfiguration());
            if (domainName == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Domain name is null and setting default domain as " + IdentityUtil.getPrimaryDomainName());
                }
                domainName = IdentityUtil.getPrimaryDomainName();
            }
            String adminRoleName = userStoreManager.getRealmConfiguration().getAdminRoleName();
            String roleNameWithDomain = UserCoreUtil.addDomainToName(adminRoleName, domainName);
            // UserCore Util functionality does not append primary domain.
            roleNameWithDomain = SCIMCommonUtils.getGroupNameWithDomain(roleNameWithDomain);
            try {
                // Validate the SCIM IS is avaialble for Groups.
                if (!scimGroupHandler.isGroupExisting(roleNameWithDomain)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Group does not exist, setting scim attribute group value: " + roleNameWithDomain);
                    }
                    scimGroupHandler.addMandatoryAttributes(roleNameWithDomain);
                }
                // Adding the SCIM attributes for admin group
                if (((AbstractUserStoreManager) userStoreManager).isRoleAndGroupSeparationEnabled()) {
                    String groupNameWithDomain = getAdminGroupName(adminRoleName, domainName);
                    // Validate the SCIM ID is available for groups.
                    if (userStoreManager.isExistingRole(groupNameWithDomain) && !scimGroupHandler.isGroupExisting(groupNameWithDomain)) {
                        if (log.isDebugEnabled()) {
                            log.debug("Group does not exist, setting scim attributes for group: " + groupNameWithDomain);
                        }
                        scimGroupHandler.addMandatoryAttributes(groupNameWithDomain);
                    }
                }
            } catch (IdentitySCIMException e) {
                throw new UserStoreException("Error retrieving group information from SCIM Tables for tenant ID: " + userStoreManager.getTenantId(), e);
            }
        }
    } catch (Exception e) {
        log.error("Error occurred while updating the admin groups's attributes in Tenant ID : " + tenantId + ", " + "Error : " + e.getMessage(), e);
    }
}
Also used : UserStoreException(org.wso2.carbon.user.core.UserStoreException) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) SCIMGroupHandler(org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler) IdentitySCIMException(org.wso2.carbon.identity.scim2.common.exceptions.IdentitySCIMException) IdentitySCIMException(org.wso2.carbon.identity.scim2.common.exceptions.IdentitySCIMException) UserStoreException(org.wso2.carbon.user.core.UserStoreException)

Example 14 with SCIMGroupHandler

use of org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMGroupHandlerTest method testCreateSCIMAttributes.

@Test
public void testCreateSCIMAttributes() throws Exception {
    ResultSet resultSet = mock(ResultSet.class);
    mockStatic(IdentityDatabaseUtil.class);
    mockStatic(SCIMCommonUtils.class);
    Group group = new Group();
    Date date = new Date();
    group.setCreatedDate(date);
    group.setLastModified(date);
    group.setLocation("LOCATION_URI");
    when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
    when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
    when(mockedPreparedStatement.executeQuery()).thenReturn(resultSet);
    when(resultSet.next()).thenReturn(false);
    when(mockedGroupDAO.isExistingGroup(SCIMCommonUtils.getGroupNameWithDomain("NON_EXISTANT_GROUP_NAME"), 1)).thenReturn(false);
    SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
    scimGroupHandler.createSCIMAttributes(group);
    assertNotNull(group);
}
Also used : Group(org.wso2.charon3.core.objects.Group) ResultSet(java.sql.ResultSet) Date(java.util.Date) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with SCIMGroupHandler

use of org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMGroupHandlerTest method testGetGroupWithAttributesSecondScenario.

@Test
public void testGetGroupWithAttributesSecondScenario() throws Exception {
    Group group = new Group();
    ResultSet resultSet = mock(ResultSet.class);
    mockStatic(IdentityDatabaseUtil.class);
    mockStatic(IdentityTenantUtil.class);
    mockStatic(SCIMCommonUtils.class);
    Date today = Calendar.getInstance().getTime();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put("urn:ietf:params:scim:schemas:core:2.0:id", "100");
    attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.created", formatter.format(today));
    attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.lastModified", formatter.format(today));
    attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.location", "https://localhost:9443/t/TENANT_DOMAIN/Groups/100");
    when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
    when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
    when(resultSet.next()).thenReturn(true);
    when(mockedPreparedStatement.executeQuery()).thenReturn(resultSet);
    when(mockedGroupDAO.isExistingGroup("EXISTING_GROUP_NAME", 1)).thenReturn(true);
    whenNew(GroupDAO.class).withNoArguments().thenReturn(mockedGroupDAO);
    when(mockedGroupDAO.getSCIMGroupAttributes(anyInt(), anyString())).thenReturn(attributes);
    when(IdentityTenantUtil.getTenantDomain(1)).thenReturn("TENANT_DOMAIN");
    when(SCIMCommonUtils.getSCIMGroupURL()).thenReturn("https://localhost:9443/t/TENANT_DOMAIN/Groups");
    assertEquals(new SCIMGroupHandler(1).getGroupWithAttributes(group, "EXISTING_GROUP_NAME"), group);
}
Also used : Group(org.wso2.charon3.core.objects.Group) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) Matchers.anyString(org.mockito.Matchers.anyString) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

SCIMGroupHandler (org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler)10 IdentitySCIMException (org.wso2.carbon.identity.scim2.common.exceptions.IdentitySCIMException)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 Group (org.wso2.charon3.core.objects.Group)5 ResultSet (java.sql.ResultSet)4 Date (java.util.Date)4 Matchers.anyString (org.mockito.Matchers.anyString)4 UserStoreException (org.wso2.carbon.user.core.UserStoreException)4 CharonException (org.wso2.charon3.core.exceptions.CharonException)3 HashMap (java.util.HashMap)2 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)2 SCIMUserStoreException (org.wso2.carbon.identity.scim2.common.extenstion.SCIMUserStoreException)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 AbstractUserStoreManager (org.wso2.carbon.user.core.common.AbstractUserStoreManager)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)1