Search in sources :

Example 6 with RoleBasicInfo

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

the class RoleDAOImpl method addRole.

@Override
public RoleBasicInfo addRole(String roleName, List<String> userList, List<String> groupList, List<String> permissions, String tenantDomain) throws IdentityRoleManagementException {
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    if (log.isDebugEnabled()) {
        log.debug("Creating the role: " + roleName + " in the tenantDomain: " + tenantDomain);
    }
    String primaryDomainName = IdentityUtil.getPrimaryDomainName();
    if (primaryDomainName != null) {
        primaryDomainName = primaryDomainName.toUpperCase(Locale.ENGLISH);
    }
    // Remove internal domain before persisting in order to maintain the backward compatibility.
    roleName = removeInternalDomain(roleName);
    String roleID;
    if (!isExistingRoleName(roleName, tenantDomain)) {
        try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(true)) {
            try {
                try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, ADD_ROLE_SQL, RoleTableColumns.UM_ID)) {
                    statement.setString(RoleTableColumns.UM_ROLE_NAME, roleName);
                    statement.setInt(RoleTableColumns.UM_TENANT_ID, tenantId);
                    statement.executeUpdate();
                }
                String databaseProductName = connection.getMetaData().getDatabaseProductName();
                // Add users to the created role.
                if (CollectionUtils.isNotEmpty(userList)) {
                    List<String> userNamesList = getUserNamesByIDs(userList, tenantDomain);
                    String addUsersSQL = ADD_USER_TO_ROLE_SQL;
                    if (MICROSOFT.equals(databaseProductName)) {
                        addUsersSQL = ADD_USER_TO_ROLE_SQL_MSSQL;
                    }
                    processBatchUpdateForUsers(roleName, userNamesList, tenantId, primaryDomainName, connection, addUsersSQL);
                    for (String username : userNamesList) {
                        clearUserRolesCache(username, tenantId);
                    }
                }
                // Add groups to the created role.
                if (CollectionUtils.isNotEmpty(groupList)) {
                    Map<String, String> groupIdsToNames = getGroupNamesByIDs(groupList, tenantDomain);
                    List<String> groupNamesList = new ArrayList<>(groupIdsToNames.values());
                    String addGroupsSQL = ADD_GROUP_TO_ROLE_SQL;
                    if (MICROSOFT.equals(databaseProductName)) {
                        addGroupsSQL = ADD_GROUP_TO_ROLE_SQL_MSSQL;
                    }
                    processBatchUpdateForGroups(roleName, groupNamesList, tenantId, primaryDomainName, connection, addGroupsSQL);
                }
                // Add role ID.
                roleID = addRoleID(roleName, tenantDomain);
                // Add role permissions.
                if (CollectionUtils.isNotEmpty(permissions)) {
                    setPermissions(roleID, permissions, tenantDomain, roleName);
                }
                IdentityDatabaseUtil.commitUserDBTransaction(connection);
            } catch (SQLException | IdentityRoleManagementException e) {
                IdentityDatabaseUtil.rollbackTransaction(connection);
                String errorMessage = "Error while creating the role: %s in the tenantDomain: %s";
                throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
            }
        } catch (SQLException e) {
            String errorMessage = "Error while creating the role: %s in the tenantDomain: %s";
            throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
        }
    } else {
        throw new IdentityRoleManagementClientException(ROLE_ALREADY_EXISTS.getCode(), "Role already exist for the role name: " + roleName);
    }
    return new RoleBasicInfo(roleID, roleName);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) IdentityRoleManagementException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException) IdentityRoleManagementClientException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)

Example 7 with RoleBasicInfo

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

the class RoleDAOTest method testUpdateRoleName.

@Test
public void testUpdateRoleName() 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());
        mockCacheClearing();
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection1);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection2);
        RoleBasicInfo role = addRole("role1");
        mockRealmConfiguration();
        AuthorizationManager authorizationManager = mock(JDBCAuthorizationManager.class);
        when(mockUserRealm.getAuthorizationManager()).thenReturn(authorizationManager);
        doNothing().when(authorizationManager).resetPermissionOnUpdateRole(anyString(), anyString());
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection3);
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection4);
        doReturn(true).when(roleDAO, "isExistingRoleID", eq(role.getId()), anyString());
        doReturn(false).when(roleDAO, "isExistingRoleName", eq("newRole"), anyString());
        roleDAO.updateRoleName(role.getId(), "newRole", SAMPLE_TENANT_DOMAIN);
        doCallRealMethod().when(roleDAO, "isExistingRoleName", anyString(), anyString());
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection5);
        assertFalse(roleDAO.isExistingRoleName("role1", SAMPLE_TENANT_DOMAIN));
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection6);
        assertTrue(roleDAO.isExistingRoleName("newRole", SAMPLE_TENANT_DOMAIN));
    }
}
Also used : Connection(java.sql.Connection) AuthorizationManager(org.wso2.carbon.user.api.AuthorizationManager) JDBCAuthorizationManager(org.wso2.carbon.user.core.authorization.JDBCAuthorizationManager) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with RoleBasicInfo

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

the class RoleDAOTest method testDeleteUser.

@Test
public void testDeleteUser() 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);
        Connection connection7 = DAOUtils.getConnection(DB_NAME)) {
        roleDAO = spy(RoleMgtDAOFactory.getInstance().getRoleDAO());
        mockCacheClearing();
        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, "updateUserListOfRole", 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(userNamesList).when(roleDAO, "getUserNamesByIDs", eq(userIDsList), anyString());
        roleDAO.updateUserListOfRole(role.getId(), userIDsList, null, SAMPLE_TENANT_DOMAIN);
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection5);
        doReturn("user1").when(roleDAO, "getUserNameByID", anyString(), anyString());
        roleDAO.deleteUser("userID1", SAMPLE_TENANT_DOMAIN);
        userNamesList.remove("user1");
        mockRealmConfiguration();
        mockStatic(UserCoreUtil.class);
        when(UserCoreUtil.isEveryoneRole(anyString(), any(RealmConfiguration.class))).thenReturn(false);
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection6);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection7);
        doCallRealMethod().when(UserCoreUtil.class, "addDomainToName", anyString(), anyString());
        doReturn("userID1").when(roleDAO, "getUserIDByName", eq(userNamesList.get(0)), anyString());
        List<UserBasicInfo> users = roleDAO.getUserListOfRole(role.getId(), SAMPLE_TENANT_DOMAIN);
        assertEquals(getUserNamesList(users), userNamesList);
    }
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) UserBasicInfo(org.wso2.carbon.identity.role.mgt.core.UserBasicInfo) 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)

Example 9 with RoleBasicInfo

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

the class RoleDAOTest method testGetRoles2.

@Test(dataProvider = "filterData")
public void testGetRoles2(String expectedResult, String filter) 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);
        Connection connection7 = DAOUtils.getConnection(DB_NAME);
        Connection connection8 = DAOUtils.getConnection(DB_NAME)) {
        roleDAO = spy(RoleMgtDAOFactory.getInstance().getRoleDAO());
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection1);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection2);
        addRole("login");
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection3);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection4);
        addRole("viewRole");
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection5);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection6);
        addRole("editRole");
        List<String> expectedRoles = new ArrayList<>();
        expectedRoles.add(expectedResult);
        mockRealmConfiguration();
        mockStatic(UserCoreUtil.class);
        when(UserCoreUtil.isEveryoneRole(anyString(), any(RealmConfiguration.class))).thenReturn(false);
        mockStatic(IdentityUtil.class);
        when(IdentityUtil.getDefaultItemsPerPage()).thenReturn(IdentityCoreConstants.DEFAULT_ITEMS_PRE_PAGE);
        when(IdentityUtil.getMaximumItemPerPage()).thenReturn(IdentityCoreConstants.DEFAULT_MAXIMUM_ITEMS_PRE_PAGE);
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection7);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection8);
        doCallRealMethod().when(IdentityUtil.class, "extractDomainFromName", anyString());
        doCallRealMethod().when(UserCoreUtil.class, "removeDomainFromName", anyString());
        List<RoleBasicInfo> roles = roleDAO.getRoles(filter, 3, 0, null, null, SAMPLE_TENANT_DOMAIN);
        assertEquals(getRoleNamesList(roles), expectedRoles);
    }
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with RoleBasicInfo

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

the class RoleDAOTest method testGetRoleNameByID.

@Test
public void testGetRoleNameByID() throws Exception {
    try (Connection connection1 = DAOUtils.getConnection(DB_NAME);
        Connection connection2 = DAOUtils.getConnection(DB_NAME);
        Connection connection3 = 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");
        doCallRealMethod().when(roleDAO, "getRoleIDByName", anyString(), anyString());
        when(IdentityDatabaseUtil.getUserDBConnection(anyBoolean())).thenReturn(connection3);
        when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection3);
        assertEquals(roleDAO.getRoleNameByID(role.getId(), SAMPLE_TENANT_DOMAIN), "role1");
    }
}
Also used : 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

RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)34 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 Test (org.testng.annotations.Test)17 Connection (java.sql.Connection)15 ArrayList (java.util.ArrayList)8 IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)8 Matchers.anyString (org.mockito.Matchers.anyString)7 RealmConfiguration (org.wso2.carbon.user.api.RealmConfiguration)7 Role (org.wso2.charon3.core.objects.Role)7 IdentityRoleManagementServerException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException)6 RoleManagementEventPublisherProxy (org.wso2.carbon.identity.role.mgt.core.RoleManagementEventPublisherProxy)6 SQLException (java.sql.SQLException)5 IdentityRoleManagementException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)5 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)3 CharonException (org.wso2.charon3.core.exceptions.CharonException)3 GroupBasicInfo (org.wso2.carbon.identity.role.mgt.core.GroupBasicInfo)2 UserBasicInfo (org.wso2.carbon.identity.role.mgt.core.UserBasicInfo)2 AuthorizationManager (org.wso2.carbon.user.api.AuthorizationManager)2 JDBCAuthorizationManager (org.wso2.carbon.user.core.authorization.JDBCAuthorizationManager)2 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)2