Search in sources :

Example 1 with GroupBasicInfo

use of org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo in project carbon-identity-framework by wso2.

the class RoleDAOImpl method getGroupListOfRole.

@Override
public List<GroupBasicInfo> getGroupListOfRole(String roleID, String tenantDomain) throws IdentityRoleManagementException {
    if (!isExistingRoleID(roleID, tenantDomain)) {
        throw new IdentityRoleManagementClientException(ROLE_NOT_FOUND.getCode(), "Role id: " + roleID + " does not exist in the system.");
    }
    String roleName = getRoleNameByID(roleID, tenantDomain);
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    List<GroupBasicInfo> groupList = new ArrayList<>();
    List<String> groupNames = new ArrayList<>();
    List<String> disabledDomainName = getDisabledDomainNames();
    String primaryDomainName = IdentityUtil.getPrimaryDomainName();
    if (primaryDomainName != null) {
        primaryDomainName = primaryDomainName.toUpperCase(Locale.ENGLISH);
    }
    try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, GET_GROUP_LIST_OF_ROLE_SQL, RoleTableColumns.UM_ID)) {
            statement.setString(RoleTableColumns.UM_ROLE_NAME, roleName);
            statement.setInt(RoleTableColumns.UM_TENANT_ID, tenantId);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    String name = resultSet.getString(1);
                    String domain = resultSet.getString(2);
                    if (!disabledDomainName.contains(domain)) {
                        if (!StringUtils.equals(primaryDomainName, domain)) {
                            name = UserCoreUtil.addDomainToName(name, domain);
                        } else {
                            name = primaryDomainName + UserCoreConstants.DOMAIN_SEPARATOR + name;
                        }
                        groupNames.add(name);
                    }
                }
            }
        }
    } catch (SQLException e) {
        String errorMessage = "Error while while getting the group list of role for role name: %s in the " + "tenantDomain: %s";
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
    }
    Map<String, String> groupNamesToIDs = getGroupIDsByNames(groupNames, tenantDomain);
    groupNamesToIDs.forEach((groupName, groupID) -> groupList.add(new GroupBasicInfo(groupID, groupName)));
    return groupList;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) GroupBasicInfo(org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) ResultSet(java.sql.ResultSet) IdentityRoleManagementClientException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)

Example 2 with GroupBasicInfo

use of org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo in project carbon-identity-framework by wso2.

the class RoleManagementServiceImpl method getGroupListOfRole.

@Override
public List<GroupBasicInfo> getGroupListOfRole(String roleID, String tenantDomain) throws IdentityRoleManagementException {
    RoleManagementEventPublisherProxy roleManagementEventPublisherProxy = RoleManagementEventPublisherProxy.getInstance();
    roleManagementEventPublisherProxy.publishPreGetUserListOfRole(roleID, tenantDomain);
    List<GroupBasicInfo> groupBasicInfoList = roleDAO.getGroupListOfRole(roleID, tenantDomain);
    roleManagementEventPublisherProxy.publishPostGetUserListOfRole(roleID, tenantDomain);
    if (log.isDebugEnabled()) {
        log.debug(String.format("%s get list of groups of role of id : %s successfully.", getUser(tenantDomain), roleID));
    }
    return groupBasicInfoList;
}
Also used : GroupBasicInfo(org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo) RoleManagementEventPublisherProxy(org.wso2.carbon.identity.role.mgt.core.RoleManagementEventPublisherProxy)

Example 3 with GroupBasicInfo

use of org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMRoleManagerTest method getDummyIdentityRole.

private org.wso2.carbon.identity.role.mgt.core.Role getDummyIdentityRole(String roleId, String roleName, String domain, String tenantDomain) {
    org.wso2.carbon.identity.role.mgt.core.Role role = new org.wso2.carbon.identity.role.mgt.core.Role();
    role.setId(roleId);
    role.setPermissions(Arrays.asList("permission", "usermgt"));
    role.setName(roleName);
    role.setDomain(domain);
    role.setTenantDomain(tenantDomain);
    role.setUsers(Arrays.asList(new UserBasicInfo("7646b885-4207-4ca0-bc65-5df82272b6d1", "username1"), new UserBasicInfo("7646b885-4207-4ca0-bc65-5df82272b6d2", "username2")));
    GroupBasicInfo groupBasicInfo1 = new GroupBasicInfo();
    groupBasicInfo1.setName("groupName1");
    groupBasicInfo1.setId("26d3a726-9c00-4f4c-8a4e-f5e310138081");
    GroupBasicInfo groupBasicInfo2 = new GroupBasicInfo();
    groupBasicInfo2.setName("groupName2");
    groupBasicInfo2.setId("26d3a726-9c00-4f4c-8a4e-f5e310138082");
    role.setGroups(Arrays.asList(groupBasicInfo1, groupBasicInfo2));
    return role;
}
Also used : Role(org.wso2.charon3.core.objects.Role) UserBasicInfo(org.wso2.carbon.identity.role.mgt.core.UserBasicInfo) GroupBasicInfo(org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo)

Example 4 with GroupBasicInfo

use of org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMRoleManager method getRole.

@Override
public Role getRole(String roleID, Map<String, Boolean> requiredAttributes) throws BadRequestException, CharonException, NotFoundException {
    try {
        org.wso2.carbon.identity.role.mgt.core.Role role = roleManagementService.getRole(roleID, tenantDomain);
        Role scimRole = new Role();
        scimRole.setId(role.getId());
        scimRole.setDisplayName(role.getName());
        String locationURI = SCIMCommonUtils.getSCIMRoleURL(role.getId());
        scimRole.setLocation(locationURI);
        scimRole.setPermissions(role.getPermissions());
        scimRole.setSchemas();
        if (systemRoles.contains(role.getName())) {
            scimRole.setSystemRole(true);
        }
        if (CollectionUtils.isNotEmpty(role.getUsers())) {
            for (UserBasicInfo userInfo : role.getUsers()) {
                String userLocationURI = SCIMCommonUtils.getSCIMUserURL(userInfo.getId());
                User user = new User();
                user.setUserName(userInfo.getName());
                user.setId(userInfo.getId());
                user.setLocation(userLocationURI);
                scimRole.setUser(user);
            }
        }
        if (CollectionUtils.isNotEmpty(role.getGroups())) {
            for (GroupBasicInfo groupInfo : role.getGroups()) {
                String groupLocationURI = SCIMCommonUtils.getSCIMGroupURL(groupInfo.getId());
                Group group = new Group();
                group.setDisplayName(groupInfo.getName());
                group.setId(groupInfo.getId());
                group.setLocation(groupLocationURI);
                scimRole.setGroup(group);
            }
        }
        return scimRole;
    } catch (IdentityRoleManagementException e) {
        if (StringUtils.equals(ROLE_NOT_FOUND.getCode(), e.getErrorCode())) {
            throw new NotFoundException(e.getMessage());
        }
        throw new CharonException(String.format("Error occurred while getting the role: %s", roleID), e);
    }
}
Also used : Group(org.wso2.charon3.core.objects.Group) User(org.wso2.charon3.core.objects.User) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) Role(org.wso2.charon3.core.objects.Role) UserBasicInfo(org.wso2.carbon.identity.role.mgt.core.UserBasicInfo) GroupBasicInfo(org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo) CharonException(org.wso2.charon3.core.exceptions.CharonException) IdentityRoleManagementException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)

Example 5 with GroupBasicInfo

use of org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo in project carbon-identity-framework by wso2.

the class RoleDAOTest method testGetGroupListOfRole.

@Test
public void testGetGroupListOfRole() throws Exception {
    try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
        Connection connection2 = DAOUtils.getConnection(DB_NAME);
        Connection connection3 = DAOUtils.getConnection(DB_NAME);
        Connection connection4 = DAOUtils.getConnection(DB_NAME);
        Connection connection5 = DAOUtils.getConnection(DB_NAME);
        Connection connection6 = DAOUtils.getConnection(DB_NAME)) {
        roleDAO = spy(RoleMgtDAOFactory.getInstance().getRoleDAO());
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection1);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection2);
        RoleBasicInfo role = addRole("role1");
        doReturn(true).when(roleDAO, "isExistingRoleName", anyString(), anyString());
        doCallRealMethod().when(roleDAO, "updateGroupListOfRole", anyString(), anyCollection(), anyCollection(), anyString());
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection3);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection4);
        mockStatic(IdentityUtil.class);
        when(IdentityUtil.getPrimaryDomainName()).thenReturn("PRIMARY");
        doReturn(groupNamesMap).when(roleDAO, "getGroupNamesByIDs", eq(groupIDsList), anyString());
        roleDAO.updateGroupListOfRole(role.getId(), groupIDsList, null, SAMPLE_TENANT_DOMAIN);
        mockRealmConfiguration();
        mockStatic(UserCoreUtil.class);
        when(UserCoreUtil.isEveryoneRole(anyString(), any(RealmConfiguration.class))).thenReturn(false);
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection5);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection6);
        doCallRealMethod().when(UserCoreUtil.class, "addDomainToName", anyString(), anyString());
        List<GroupBasicInfo> groups = roleDAO.getGroupListOfRole(role.getId(), SAMPLE_TENANT_DOMAIN);
        assertEquals(getGroupNamesList(groups), groupNamesList);
    }
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) GroupBasicInfo(org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo) Connection(java.sql.Connection) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

GroupBasicInfo (org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo)6 Connection (java.sql.Connection)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Test (org.testng.annotations.Test)2 RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)2 UserBasicInfo (org.wso2.carbon.identity.role.mgt.core.UserBasicInfo)2 RealmConfiguration (org.wso2.carbon.user.api.RealmConfiguration)2 Role (org.wso2.charon3.core.objects.Role)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)1 IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)1 IdentityRoleManagementException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)1 IdentityRoleManagementServerException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException)1 RoleManagementEventPublisherProxy (org.wso2.carbon.identity.role.mgt.core.RoleManagementEventPublisherProxy)1 CharonException (org.wso2.charon3.core.exceptions.CharonException)1 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)1 Group (org.wso2.charon3.core.objects.Group)1 User (org.wso2.charon3.core.objects.User)1