use of org.wso2.carbon.database.utils.jdbc.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);
}
use of org.wso2.carbon.database.utils.jdbc.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();
}
}
use of org.wso2.carbon.database.utils.jdbc.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;
}
use of org.wso2.carbon.database.utils.jdbc.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;
}
use of org.wso2.carbon.database.utils.jdbc.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;
}
Aggregations