Search in sources :

Example 16 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement 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 17 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class RoleDAOImpl method processBatchUpdateForGroups.

private void processBatchUpdateForGroups(String roleName, List<String> groupNamesList, int tenantId, String primaryDomainName, Connection connection, String sql) throws SQLException {
    try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, sql, RoleTableColumns.UM_ID)) {
        for (String groupName : groupNamesList) {
            // 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.setString(RoleTableColumns.UM_ROLE_NAME, roleName);
            statement.setInt(RoleTableColumns.UM_TENANT_ID, tenantId);
            statement.setString(RoleTableColumns.UM_DOMAIN_NAME, domainName);
            statement.addBatch();
        }
        statement.executeBatch();
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)

Example 18 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method getDiscoverableApplicationBasicInfoByResourceId.

@Override
public ApplicationBasicInfo getDiscoverableApplicationBasicInfoByResourceId(String resourceId, String tenantDomain) throws IdentityApplicationManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Getting application basic information for resourceId: " + resourceId + " in tenantDomain: " + tenantDomain + " if discoverable.");
    }
    ApplicationBasicInfo applicationBasicInfo = null;
    boolean isDiscoverable = false;
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, LOAD_APP_BY_TENANT_AND_UUID)) {
            statement.setInt(ApplicationTableColumns.TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
            statement.setString(ApplicationTableColumns.UUID, resourceId);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    applicationBasicInfo = buildApplicationBasicInfo(resultSet);
                    isDiscoverable = getBooleanValue(resultSet.getString(ApplicationTableColumns.IS_DISCOVERABLE));
                }
            }
        }
    } catch (SQLException e) {
        throw new IdentityApplicationManagementServerException("Error while getting discoverable application " + "basic information for resourceId: " + resourceId + " in tenantDomain: " + tenantDomain, e);
    }
    if (applicationBasicInfo != null && !isDiscoverable) {
        throw new IdentityApplicationManagementClientException(APPLICATION_NOT_DISCOVERABLE.getCode(), "Requested application resource " + resourceId + " is not discoverable.");
    }
    return applicationBasicInfo;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) IdentityApplicationManagementClientException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IdentityApplicationManagementServerException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException) ApplicationBasicInfo(org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)

Example 19 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method isApplicationDiscoverable.

@Override
public boolean isApplicationDiscoverable(String resourceId, String tenantDomain) throws IdentityApplicationManagementException {
    int count = 0;
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, IS_APP_BY_TENANT_AND_UUID_DISCOVERABLE)) {
            statement.setInt(ApplicationTableColumns.TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
            statement.setString(ApplicationTableColumns.UUID, resourceId);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    count = resultSet.getInt(1);
                }
            }
        }
    } catch (SQLException e) {
        throw new IdentityApplicationManagementServerException("Error while getting discoverable application " + "basic information for resourceId: " + resourceId + " in tenantDomain: " + tenantDomain, e);
    }
    return count > 0;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IdentityApplicationManagementServerException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException)

Example 20 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method getApplicationBasicInfoByName.

@Override
public ApplicationBasicInfo getApplicationBasicInfoByName(String name, String tenantDomain) throws IdentityApplicationManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Getting application basic information for name: " + name + " in tenantDomain: " + tenantDomain);
    }
    ApplicationBasicInfo applicationBasicInfo = null;
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, LOAD_APP_BY_TENANT_AND_NAME)) {
            statement.setInt(ApplicationTableColumns.TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
            statement.setString(ApplicationTableColumns.APP_NAME, name);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    applicationBasicInfo = buildApplicationBasicInfo(resultSet);
                }
            }
        }
    } catch (SQLException e) {
        String message = "Error while getting application basic information for name: %s in " + "tenantDomain: %s";
        throw new IdentityApplicationManagementException(String.format(message, name, tenantDomain), e);
    }
    return applicationBasicInfo;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ApplicationBasicInfo(org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)

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