diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementDAOImpl.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementDAOImpl.java index f6807c2a742e..797a7c218a05 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementDAOImpl.java @@ -33,9 +33,7 @@ import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildServerException; @@ -62,13 +60,14 @@ public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator( statement.setString(Column.DEFINED_BY, authenticatorConfig.getDefinedByType().toString()); statement.setString(Column.AUTHENTICATION_TYPE, authenticatorConfig.getAuthenticationType() .toString()); - statement.setString(Column.IS_ENABLED, String.valueOf(authenticatorConfig.isEnabled() ? 1 : 0)); + statement.setString(Column.IS_ENABLED, + authenticatorConfig.isEnabled() ? IS_TRUE_VALUE : IS_FALSE_VALUE); statement.setString(Column.IDP_NAME, LOCAL_IDP_NAME); statement.setInt(Column.TENANT_ID, tenantId); }, null, true)); if (authenticatorConfigID == 0) { - getAuthenticatorEntryId(authenticatorConfig.getName(), tenantId); + authenticatorConfigID = getAuthenticatorEntryId(authenticatorConfig.getName(), tenantId); } addAuthenticatorProperty(authenticatorConfigID, authenticatorConfig.getProperties(), tenantId); @@ -91,7 +90,7 @@ public UserDefinedLocalAuthenticatorConfig updateUserDefinedLocalAuthenticator( statement -> { statement.setString(Column.DISPLAY_NAME, updatedAuthenticatorConfig.getDisplayName()); statement.setString(Column.IS_ENABLED, - String.valueOf(updatedAuthenticatorConfig.isEnabled() ? 1 : 0)); + updatedAuthenticatorConfig.isEnabled() ? IS_TRUE_VALUE : IS_FALSE_VALUE); statement.setString(Column.NAME, existingAuthenticatorConfig.getName()); statement.setInt(Column.TENANT_ID, tenantId); }); @@ -121,9 +120,8 @@ public List getAllUserDefinedLocalAuthentic try { NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource()); - HashMap authenticatorConfigMap = new HashMap<>(); List allUserDefinedLocalConfigs = new ArrayList<>(); - jdbcTemplate.withTransaction( + List configDaoModels = jdbcTemplate.withTransaction( template -> template.executeQuery(Query.GET_ALL_USER_DEFINED_AUTHENTICATOR_SQL, (resultSet, rowNumber) -> { UserDefinedLocalAuthenticatorConfig config = getLocalAuthenticatorConfigBasedOnType( @@ -132,16 +130,16 @@ public List getAllUserDefinedLocalAuthentic config.setDisplayName(resultSet.getString(Column.DISPLAY_NAME)); config.setEnabled(resultSet.getString(Column.IS_ENABLED).equals(IS_TRUE_VALUE)); config.setDefinedByType(DefinedByType.valueOf(resultSet.getString(Column.DEFINED_BY))); - return authenticatorConfigMap.put(resultSet.getInt(Column.ID), config); + return new AuthenticatorConfigDaoModel(resultSet.getInt(Column.ID), config); }, statement -> { statement.setString(Column.DEFINED_BY, DefinedByType.USER.toString()); statement.setInt(Column.TENANT_ID, tenantId); })); - for (Map.Entry entry: authenticatorConfigMap.entrySet()) { - UserDefinedLocalAuthenticatorConfig retrievedConfigs = entry.getValue(); - retrievedConfigs.setProperties(getAuthenticatorProperties(entry.getKey(), tenantId)); + for (AuthenticatorConfigDaoModel config: configDaoModels) { + UserDefinedLocalAuthenticatorConfig retrievedConfigs = config.getConfig(); + retrievedConfigs.setProperties(getAuthenticatorProperties(config.getEntryId(), tenantId)); allUserDefinedLocalConfigs.add(retrievedConfigs); } return allUserDefinedLocalConfigs; @@ -171,12 +169,10 @@ public void deleteUserDefinedLocalAuthenticator(String authenticatorConfigName, } private UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticatorByName(String authenticatorConfigName, - int tenantId) throws AuthenticatorMgtServerException, TransactionException { + int tenantId) throws TransactionException { NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource()); - UserDefinedLocalAuthenticatorConfig retrievedConfigs = null; - HashMap authenticatorConfigMap = new HashMap<>(); - jdbcTemplate.withTransaction(template -> + AuthenticatorConfigDaoModel configDaoModel = jdbcTemplate.withTransaction(template -> template.fetchSingleRecord(Query.GET_AUTHENTICATOR_SQL, (resultSet, rowNumber) -> { UserDefinedLocalAuthenticatorConfig config = getLocalAuthenticatorConfigBasedOnType( @@ -185,7 +181,7 @@ private UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticatorByNa config.setDisplayName(resultSet.getString(Column.DISPLAY_NAME)); config.setEnabled(resultSet.getString(Column.IS_ENABLED).equals(IS_TRUE_VALUE)); config.setDefinedByType(DefinedByType.USER); - return authenticatorConfigMap.put(resultSet.getInt(Column.ID), config); + return new AuthenticatorConfigDaoModel(resultSet.getInt(Column.ID), config); }, statement -> { statement.setString(Column.NAME, authenticatorConfigName); @@ -193,13 +189,13 @@ private UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticatorByNa statement.setString(Column.DEFINED_BY, DefinedByType.USER.toString()); })); - for (Map.Entry entry: authenticatorConfigMap.entrySet()) { - retrievedConfigs = entry.getValue(); - retrievedConfigs.setProperties(getAuthenticatorProperties(entry.getKey(), tenantId)); - break; + if (configDaoModel == null) { + return null; } - return retrievedConfigs; + UserDefinedLocalAuthenticatorConfig config = configDaoModel.getConfig(); + config.setProperties(getAuthenticatorProperties(configDaoModel.getEntryId(), tenantId)); + return config; } private UserDefinedLocalAuthenticatorConfig getLocalAuthenticatorConfigBasedOnType(String authenticationType) { @@ -263,4 +259,27 @@ private Property[] getAuthenticatorProperties(int authenticatorConfigID, int ten })); return properties.toArray(new Property[0]); } + + /** + * This class represents the user defined local authenticator configuration with entry id from DAO. + */ + private static class AuthenticatorConfigDaoModel { + + private final int entryId; + private final UserDefinedLocalAuthenticatorConfig config; + + private AuthenticatorConfigDaoModel(int entryId, UserDefinedLocalAuthenticatorConfig config) { + this.entryId = entryId; + this.config = config; + } + + public int getEntryId() { + return entryId; + } + + public UserDefinedLocalAuthenticatorConfig getConfig() { + return config; + } + } + } diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementFacade.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementFacade.java index 62d3c8949509..cd182863ec43 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementFacade.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementFacade.java @@ -151,6 +151,7 @@ public List getAllUserDefinedLocalAuthentic return jdbcTemplate.withTransaction(template -> { List configList = dao.getAllUserDefinedLocalAuthenticators(tenantId); + // TODO: Utilize a batch operation once issue:https://github.com/wso2/product-is/issues/21783 is done. for (UserDefinedLocalAuthenticatorConfig config : configList) { endpointConfigManager.resolveEndpointConfigurations(config, tenantId); } diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/AuthenticatorMgtExceptionBuilder.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/AuthenticatorMgtExceptionBuilder.java index 7e5fd775ae89..eb32b47d8d22 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/AuthenticatorMgtExceptionBuilder.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/AuthenticatorMgtExceptionBuilder.java @@ -82,8 +82,8 @@ public enum AuthenticatorMgtError { // Client errors. ERROR_NOT_FOUND_AUTHENTICATOR("60001", "No Authenticator found.", "No Authenticator found by given authenticator name: %s."), - ERROR_OP_ON_SYSTEM_AUTHENTICATOR("60002", "No operations allowed on system authenticators.", - "Do not allow to perform any operation on system defined authenticator: %s."), + ERROR_OPERATION_ALLOWED_FOR_SYSTEM_AUTHENTICATOR("60002", "No operations allowed on system " + + "authenticators.", "Do not allow to perform any operation on system defined authenticator: %s."), ERROR_AUTHENTICATOR_ALREADY_EXIST("60003", "The authenticator already exists.", "The authenticator already exists for the given name: %s."), ERROR_INVALID_AUTHENTICATOR_NAME("60004", "Authenticator name is invalid.",