Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Dec 16, 2024
1 parent b4c7215 commit 7dd69e3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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);
});
Expand Down Expand Up @@ -121,9 +120,8 @@ public List<UserDefinedLocalAuthenticatorConfig> getAllUserDefinedLocalAuthentic

try {
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
HashMap<Integer, UserDefinedLocalAuthenticatorConfig> authenticatorConfigMap = new HashMap<>();
List<UserDefinedLocalAuthenticatorConfig> allUserDefinedLocalConfigs = new ArrayList<>();
jdbcTemplate.withTransaction(
List<AuthenticatorConfigDaoModel> configDaoModels = jdbcTemplate.withTransaction(
template -> template.executeQuery(Query.GET_ALL_USER_DEFINED_AUTHENTICATOR_SQL,
(resultSet, rowNumber) -> {
UserDefinedLocalAuthenticatorConfig config = getLocalAuthenticatorConfigBasedOnType(
Expand All @@ -132,16 +130,16 @@ public List<UserDefinedLocalAuthenticatorConfig> 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<Integer, UserDefinedLocalAuthenticatorConfig> 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;
Expand Down Expand Up @@ -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<Integer, UserDefinedLocalAuthenticatorConfig> authenticatorConfigMap = new HashMap<>();
jdbcTemplate.withTransaction(template ->
AuthenticatorConfigDaoModel configDaoModel = jdbcTemplate.withTransaction(template ->
template.fetchSingleRecord(Query.GET_AUTHENTICATOR_SQL,
(resultSet, rowNumber) -> {
UserDefinedLocalAuthenticatorConfig config = getLocalAuthenticatorConfigBasedOnType(
Expand All @@ -185,21 +181,21 @@ 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);
statement.setInt(Column.TENANT_ID, tenantId);
statement.setString(Column.DEFINED_BY, DefinedByType.USER.toString());
}));

for (Map.Entry<Integer, UserDefinedLocalAuthenticatorConfig> 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) {
Expand Down Expand Up @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public List<UserDefinedLocalAuthenticatorConfig> getAllUserDefinedLocalAuthentic
return jdbcTemplate.withTransaction(template -> {
List<UserDefinedLocalAuthenticatorConfig> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit 7dd69e3

Please sign in to comment.