Search in sources :

Example 11 with NamedPreparedStatement

use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class RoleDAOImpl method deleteGroup.

@Override
public void deleteGroup(String groupID, String tenantDomain) throws IdentityRoleManagementException {
    String groupName = getGroupNameByID(groupID, tenantDomain);
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    String primaryDomainName = IdentityUtil.getPrimaryDomainName();
    if (primaryDomainName != null) {
        primaryDomainName = primaryDomainName.toUpperCase(Locale.ENGLISH);
    }
    try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(true)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, DELETE_GROUP_SQL, RoleTableColumns.UM_ID)) {
            // Add domain if not set.
            groupName = UserCoreUtil.addDomainToName(groupName, primaryDomainName);
            // Get domain from name.
            String domainName = UserCoreUtil.extractDomainFromName(groupName);
            if (domainName != null) {
                domainName = domainName.toUpperCase(Locale.ENGLISH);
            }
            String nameWithoutDomain = UserCoreUtil.removeDomainFromName(groupName);
            statement.setString(RoleTableColumns.UM_GROUP_NAME, nameWithoutDomain);
            statement.setInt(RoleTableColumns.UM_TENANT_ID, tenantId);
            statement.setString(RoleTableColumns.UM_DOMAIN_NAME, domainName);
            statement.executeUpdate();
            IdentityDatabaseUtil.commitUserDBTransaction(connection);
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackUserDBTransaction(connection);
            String errorMessage = "Error while removing the group: %s in the tenantDomain: %s";
            throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, groupName, tenantDomain), e);
        }
    } catch (SQLException e) {
        String errorMessage = "Error while removing the group: %s in the tenantDomain: %s";
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, groupName, tenantDomain), e);
    }
    clearUserRolesCacheByTenant(tenantId);
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) Connection(java.sql.Connection)

Example 12 with NamedPreparedStatement

use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class RoleDAOImpl method deleteSCIMRole.

protected void deleteSCIMRole(String roleName, String tenantDomain) throws IdentityRoleManagementException {
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    // Append internal domain in order to maintain the backward compatibility.
    roleName = appendInternalDomain(roleName);
    if (log.isDebugEnabled()) {
        log.debug("Deleting the role: " + roleName + " for the role: " + roleName + " in the tenantDomain: " + tenantDomain);
    }
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, DELETE_SCIM_ROLE_SQL)) {
            statement.setInt(RoleTableColumns.TENANT_ID, tenantId);
            statement.setString(RoleTableColumns.ROLE_NAME, roleName);
            statement.executeUpdate();
            IdentityDatabaseUtil.commitTransaction(connection);
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            String errorMessage = "Error while deleting the the role: %s for the role: %s in the tenantDomain: %s";
            throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, roleName, tenantDomain), e);
        }
    } catch (SQLException e) {
        String errorMessage = "Error while deleting the the role: %s for the role: %s in the tenantDomain: %s";
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, roleName, tenantDomain), e);
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) Connection(java.sql.Connection)

Example 13 with NamedPreparedStatement

use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class RoleDAOImpl method buildRolesList.

private List<RoleBasicInfo> buildRolesList(NamedPreparedStatement statement, String tenantDomain) throws SQLException, IdentityRoleManagementException {
    List<RoleBasicInfo> roles = new ArrayList<>();
    List<String> roleNames = new ArrayList<>();
    try (ResultSet resultSet = statement.executeQuery()) {
        while (resultSet.next()) {
            String roleName = resultSet.getString(1);
            roleNames.add(appendInternalDomain(roleName));
        }
    }
    Map<String, String> roleNamesToIDs = getRoleIDsByNames(roleNames, tenantDomain);
    // Filter scim disabled roles.
    roleNames.removeAll(new ArrayList<>(roleNamesToIDs.keySet()));
    // Add roleIDs for scim disabled roles.
    for (String roleName : roleNames) {
        roleNamesToIDs.put(roleName, addRoleID(roleName, tenantDomain));
    }
    roleNamesToIDs.forEach((roleName, roleID) -> roles.add(new RoleBasicInfo(roleID, removeInternalDomain(roleName))));
    return roles;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) RoleBasicInfo(org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)

Example 14 with NamedPreparedStatement

use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class GroupDAOImpl method getGroupIDByName.

@Override
public String getGroupIDByName(String name, String tenantDomain) throws IdentityRoleManagementException {
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    String groupID = null;
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, GET_GROUP_ID_BY_NAME_SQL)) {
            statement.setInt(RoleConstants.RoleTableColumns.TENANT_ID, tenantId);
            statement.setString(RoleConstants.RoleTableColumns.ROLE_NAME, name);
            statement.setString(RoleConstants.RoleTableColumns.ATTR_NAME, RoleConstants.ID_URI);
            int count = 0;
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    // Handle multiple matching groups.
                    count++;
                    if (count > 1) {
                        String errorMessage = "Invalid scenario. Multiple groups found for the given group name: " + name + " " + "and tenantDomain: " + tenantDomain;
                        throw new IdentityRoleManagementClientException(INVALID_REQUEST.getCode(), errorMessage);
                    }
                    groupID = resultSet.getString(1);
                }
            }
        }
    } catch (SQLException e) {
        String errorMessage = "Error while resolving the group ID for the given group name: " + name + " and tenantDomain: " + tenantDomain;
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), errorMessage, e);
    }
    return groupID;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IdentityRoleManagementClientException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)

Example 15 with NamedPreparedStatement

use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class GroupDAOImpl method batchProcessGroupNames.

private Map<String, String> batchProcessGroupNames(List<String> names, String tenantDomain, Connection connection) throws SQLException, IdentityRoleManagementException {
    Map<String, String> groupNamesToIDs = new HashMap<>();
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    String groupID;
    for (String name : names) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, GET_GROUP_ID_BY_NAME_SQL)) {
            statement.setInt(RoleConstants.RoleTableColumns.TENANT_ID, tenantId);
            statement.setString(RoleConstants.RoleTableColumns.ROLE_NAME, name);
            statement.setString(RoleConstants.RoleTableColumns.ATTR_NAME, RoleConstants.ID_URI);
            int count = 0;
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    // Handle multiple matching groups.
                    count++;
                    if (count > 1) {
                        String errorMessage = "Invalid scenario. Multiple groups found for the given group name: " + name + " " + "and tenantDomain: " + tenantDomain;
                        throw new IdentityRoleManagementClientException(INVALID_REQUEST.getCode(), errorMessage);
                    }
                    groupID = resultSet.getString(1);
                    groupNamesToIDs.put(name, groupID);
                }
            }
        }
    }
    return groupNamesToIDs;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) IdentityRoleManagementClientException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)

Aggregations

NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)45 SQLException (java.sql.SQLException)40 Connection (java.sql.Connection)39 ResultSet (java.sql.ResultSet)33 IdentityRoleManagementServerException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException)17 ArrayList (java.util.ArrayList)12 IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)12 PreparedStatement (java.sql.PreparedStatement)7 CORSOrigin (org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.testng.annotations.Test)6 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)6 HashMap (java.util.HashMap)5 IdentityApplicationManagementServerException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException)4 ApplicationBasicInfo (org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)4 RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)4 NamedPreparedStatement (org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)3 IdentityRoleManagementException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)3 UserRealm (org.wso2.carbon.user.api.UserRealm)3 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3