Search in sources :

Example 6 with TemplateManagementException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException in project carbon-identity-framework by wso2.

the class TemplateManagerDAOImpl method deleteTemplate.

/**
 * Delete {@link Template} for a given template name and a tenant Id.
 *
 * @param templateName name of the {@link Template} to be deleted.the tenant
 * @param tenantId     tenant Id of the tenant which the {@link Template} resides.
 * @return TemplateInfo of the deleted {@link Template}.
 * @throws TemplateManagementException If error occurs while deleting the {@link Template}
 */
public TemplateInfo deleteTemplate(String templateName, Integer tenantId) throws TemplateManagementException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(DELETE_TEMPLATE, preparedStatement -> {
            preparedStatement.setString(1, templateName);
            preparedStatement.setInt(2, tenantId);
        });
    } catch (DataAccessException e) {
        throw new TemplateManagementServerException(String.format(ERROR_CODE_DELETE_TEMPLATE.getMessage(), tenantId.toString(), templateName), ERROR_CODE_DELETE_TEMPLATE.getCode(), e);
    }
    return new TemplateInfo(tenantId, templateName);
}
Also used : TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 7 with TemplateManagementException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException in project carbon-identity-framework by wso2.

the class TemplateManagerDAOImpl method getAllTemplates.

/**
 * List {@link TemplateInfo} items for a given search criteria.
 *
 * @param tenantId Tenant Id to be searched.
 * @param limit    Maximum number of results expected.
 * @param offset   Result offset.
 * @return List of {@link TemplateInfo} entries.
 * @throws TemplateManagementException If error occurs while searching the Templates.
 */
public List<TemplateInfo> getAllTemplates(Integer tenantId, Integer limit, Integer offset) throws TemplateManagementException {
    List<TemplateInfo> templates;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        String query;
        if (isH2DB() || isMySQLDB() || isPostgreSQLDB()) {
            query = LIST_PAGINATED_TEMPLATES_MYSQL;
        } else if (isDB2DB()) {
            query = LIST_PAGINATED_TEMPLATES_DB2;
            int initialOffset = offset;
            offset = offset + limit;
            limit = initialOffset + 1;
        } else if (isMSSqlDB()) {
            int initialOffset = offset;
            offset = limit + offset;
            limit = initialOffset + 1;
            query = LIST_PAGINATED_TEMPLATES_MSSQL;
        } else {
            // oracle
            query = LIST_PAGINATED_TEMPLATES_ORACLE;
            limit = offset + limit;
        }
        int finalLimit = limit;
        int finalOffset = offset;
        templates = jdbcTemplate.executeQuery(query, (resultSet, rowNumber) -> new TemplateInfo(resultSet.getString(1), resultSet.getString(2)), preparedStatement -> {
            preparedStatement.setInt(1, tenantId);
            preparedStatement.setInt(2, finalLimit);
            preparedStatement.setInt(3, finalOffset);
        });
    } catch (DataAccessException e) {
        throw new TemplateManagementServerException(String.format(ERROR_CODE_PAGINATED_LIST_TEMPLATES.getMessage(), tenantId, limit, offset), ERROR_CODE_PAGINATED_LIST_TEMPLATES.getCode(), e);
    }
    return templates;
}
Also used : LIST_PAGINATED_TEMPLATES_MYSQL(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_MYSQL) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) ERROR_CODE_SELECT_TEMPLATE_BY_NAME(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_SELECT_TEMPLATE_BY_NAME) LIST_PAGINATED_TEMPLATES_MSSQL(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_MSSQL) TemplateManagementSQLException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementSQLException) DELETE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.DELETE_TEMPLATE) TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) ERROR_CODE_UPDATE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_TEMPLATE) ERROR_CODE_INSERT_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE) JdbcUtils(org.wso2.carbon.identity.core.util.JdbcUtils) LIST_PAGINATED_TEMPLATES_DB2(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_DB2) ERROR_CODE_PAGINATED_LIST_TEMPLATES(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_PAGINATED_LIST_TEMPLATES) ERROR_CODE_DELETE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_DELETE_TEMPLATE) TemplateManagerDAO(org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO) IOException(java.io.IOException) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcUtils.isMSSqlDB(org.wso2.carbon.identity.core.util.JdbcUtils.isMSSqlDB) GET_TEMPLATE_BY_NAME(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.GET_TEMPLATE_BY_NAME) JdbcUtils.isMySQLDB(org.wso2.carbon.identity.core.util.JdbcUtils.isMySQLDB) LIST_PAGINATED_TEMPLATES_ORACLE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_ORACLE) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) TemplateManagementException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException) JdbcUtils.isPostgreSQLDB(org.wso2.carbon.identity.core.util.JdbcUtils.isPostgreSQLDB) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) TemplateMgtConstants(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants) TemplateMgtUtils(org.wso2.carbon.identity.template.mgt.util.TemplateMgtUtils) JdbcUtils.isDB2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isDB2DB) Template(org.wso2.carbon.identity.template.mgt.model.Template) InputStream(java.io.InputStream) TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 8 with TemplateManagementException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException in project carbon-identity-framework by wso2.

the class TemplateManagementServiceClient method updateTemplate.

public Template updateTemplate(String templateName, UpdateTemplateRequestDTO updateTemplateRequestDTO) throws TemplateManagementException {
    handleLoggedInUserAuthorization(TemplateMgtConstants.PERMISSION_TEMPLATE_MGT_UPDATE);
    Template updateTemplateRequest = new Template(updateTemplateRequestDTO.getTemplateName(), updateTemplateRequestDTO.getDescription(), updateTemplateRequestDTO.getTemplateScript());
    return getTemplateManager().updateTemplate(templateName, updateTemplateRequest);
}
Also used : Template(org.wso2.carbon.identity.template.mgt.model.Template)

Example 9 with TemplateManagementException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException in project product-is by wso2.

the class TemplateManagementServiceClient method updateTemplate.

public Template updateTemplate(String templateName, UpdateTemplateRequestDTO updateTemplateRequestDTO) throws TemplateManagementException {
    handleLoggedInUserAuthorization(TemplateMgtConstants.PERMISSION_TEMPLATE_MGT_UPDATE);
    Template updateTemplateRequest = new Template(updateTemplateRequestDTO.getTemplateName(), updateTemplateRequestDTO.getDescription(), updateTemplateRequestDTO.getTemplateScript());
    return getTemplateManager().updateTemplate(templateName, updateTemplateRequest);
}
Also used : Template(org.wso2.carbon.identity.template.mgt.model.Template)

Example 10 with TemplateManagementException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException in project product-is by wso2.

the class TemplateManagementServiceClient method handleLoggedInUserAuthorization.

private void handleLoggedInUserAuthorization(String permission) throws TemplateManagementException {
    try {
        int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        if (StringUtils.isBlank(loggedInUser)) {
            throw new TemplateManagementException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_NO_AUTH_USER_FOUND.getMessage(), TemplateMgtConstants.ErrorMessages.ERROR_CODE_NO_AUTH_USER_FOUND.getCode());
        }
        AuthorizationManager authorizationManager = TemplateManagementUIServiceDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getAuthorizationManager();
        if (!authorizationManager.isUserAuthorized(loggedInUser, permission, CarbonConstants.UI_PERMISSION_ACTION)) {
            throw new TemplateManagementException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_USER_NOT_AUTHORIZED.getMessage(), TemplateMgtConstants.ErrorMessages.ERROR_CODE_USER_NOT_AUTHORIZED.getCode());
        }
    } catch (UserStoreException e) {
        throw new TemplateManagementException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_UNEXPECTED.getMessage(), TemplateMgtConstants.ErrorMessages.ERROR_CODE_UNEXPECTED.getCode());
    }
}
Also used : TemplateManagementException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) AuthorizationManager(org.wso2.carbon.user.api.AuthorizationManager)

Aggregations

Template (org.wso2.carbon.identity.template.mgt.model.Template)16 TemplateManagementException (org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException)10 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 ConfigurationManager (org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)5 ConfigurationManagementException (org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException)5 TemplateManagerDAO (org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO)5 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)4 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)4 TemplateToResource (org.wso2.carbon.identity.template.mgt.function.TemplateToResource)4 List (java.util.List)3 IOUtils (org.apache.commons.io.IOUtils)3 IdentityProviderTemplate (org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderTemplate)3 Resource (org.wso2.carbon.identity.configuration.mgt.core.model.Resource)3 TemplateManager (org.wso2.carbon.identity.template.mgt.TemplateManager)3 TemplateMgtConstants (org.wso2.carbon.identity.template.mgt.TemplateMgtConstants)3 TemplateManagementServerException (org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JdbcUtils (org.wso2.carbon.identity.core.util.JdbcUtils)2 JdbcUtils.isDB2DB (org.wso2.carbon.identity.core.util.JdbcUtils.isDB2DB)2