Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Modify the core to support removing the necessity of having role_org-application association" #5632

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class ApplicationBasicInfo implements Serializable {
private String clientId;
private String issuer;
private String tenantDomain;
private String uuid;

/**
* Get application id.
Expand Down Expand Up @@ -239,25 +238,4 @@ public void setTenantDomain(String tenantDomain) {

this.tenantDomain = tenantDomain;
}

/**
* Get application uuid.
*
* @return uuid application uuid
*/
public String getUuid() {

return uuid;
}

/**
* Set application uuid.
*
* @param uuid application uuid
*/
public void setUuid(String uuid) {

this.uuid = uuid;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,6 @@ public abstract ApplicationBasicInfo[] getAllApplicationBasicInfo(String tenantD
public abstract ApplicationBasicInfo[] getApplicationBasicInfo(String tenantDomain, String username, String filter)
throws IdentityApplicationManagementException;

/**
* Get all basic application information based on the 'SP Property Key' and 'Value'.
*
* @param tenantDomain Tenant Domain
* @param username User Name
* @param key SP Property key
* @param value SP Property value
* @return ApplicationBasicInfo Object.
* @throws IdentityApplicationManagementException if loading application
*/
public ApplicationBasicInfo[] getApplicationBasicInfoBySPProperty(String tenantDomain, String username,
String key, String value)
throws IdentityApplicationManagementException {

throw new NotImplementedException();
}

/**
* Update Application
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,36 +340,6 @@ public ApplicationBasicInfo[] getApplicationBasicInfo(String tenantDomain, Strin
return ((AbstractApplicationDAOImpl) appDAO).getApplicationBasicInfo(filter);
}

@Override
public ApplicationBasicInfo[] getApplicationBasicInfoBySPProperty(String tenantDomain, String username,
String key, String value)
throws IdentityApplicationManagementException {

ApplicationDAO appDAO;
// invoking the listeners
Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
for (ApplicationMgtListener listener : listeners) {
if (listener.isEnable() && !listener.getApplicationBasicInfoBySPProperty(tenantDomain, username, key,
value)) {
return new ApplicationBasicInfo[0];
}
}

try {
startTenantFlow(tenantDomain, username);
appDAO = ApplicationMgtSystemConfig.getInstance().getApplicationDAO();
} finally {
endTenantFlow();
}

if (!(appDAO instanceof AbstractApplicationDAOImpl)) {
log.error("Get application basic info service is not supported.");
throw new IdentityApplicationManagementException("This service is not supported.");
}

return ((AbstractApplicationDAOImpl) appDAO).getApplicationBasicInfoBySPProperty(key, value);
}

/**
* Get All Application Basic Information with pagination
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,27 +199,13 @@ default ApplicationBasicInfo getApplicationBasicInfoByResourceId(String resource
return null;
}

/**
* Retrieve application basic information using the sp metadata property key and value.
*
* @param key Name of the sp metadata property key
* @param value Value of the sp metadata property
* @return ApplicationBasicInfo containing the basic app information
* @throws IdentityApplicationManagementException if building {@link ApplicationBasicInfo} fails.
*/
default ApplicationBasicInfo[] getApplicationBasicInfoBySPProperty(String key, String value)
throws IdentityApplicationManagementException {

return null;
}

/**
* Retrieve application basic information using the application name.
*
* @param name Name of the application
* @param tenantDomain Tenant domain of the application
* @return ApplicationBasicInfo containing the basic app information
* @throws IdentityApplicationManagementException if building {@link ApplicationBasicInfo} fails.
* @throws IdentityApplicationManagementException
*/
default ApplicationBasicInfo getApplicationBasicInfoByName(String name, String tenantDomain)
throws IdentityApplicationManagementException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.core.util.JdbcUtils;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.identity.role.v2.mgt.core.exception.IdentityRoleManagementException;
import org.wso2.carbon.identity.role.v2.mgt.core.model.RoleBasicInfo;
import org.wso2.carbon.identity.secret.mgt.core.SecretManager;
import org.wso2.carbon.identity.secret.mgt.core.exception.SecretManagementException;
import org.wso2.carbon.identity.secret.mgt.core.model.ResolvedSecret;
Expand Down Expand Up @@ -124,6 +122,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -194,8 +193,6 @@ public class ApplicationDAOImpl extends AbstractApplicationDAOImpl implements Pa

private static final String SP_PROPERTY_NAME_CERTIFICATE = "CERTIFICATE";
private static final String APPLICATION_NAME_CONSTRAINT = "APPLICATION_NAME_CONSTRAINT";
private static final String UUID = "UUID";
private static final String SPACE = " ";

private Log log = LogFactory.getLog(ApplicationDAOImpl.class);
private static final Log AUDIT_LOG = CarbonConstants.AUDIT_LOG;
Expand Down Expand Up @@ -608,14 +605,9 @@ private void addApplicationConfigurations(Connection connection, ServiceProvider
serviceProvider.getPermissionAndRoleConfig().getPermissions());
}

if (serviceProvider.getAssociatedRolesConfig() != null) {
String appAudience = serviceProvider.getAssociatedRolesConfig().getAllowedAudience();
// Update associated roles.
if (RoleConstants.APPLICATION.equalsIgnoreCase(appAudience)) {
updateAssociatedRolesOfApplication(connection, serviceProvider.getApplicationResourceId(),
serviceProvider.getApplicationName(), serviceProvider.getAssociatedRolesConfig(), tenantDomain);
}
}
// Update associated roles.
updateAssociatedRolesOfApplication(connection, serviceProvider.getApplicationResourceId(),
serviceProvider.getApplicationName(), serviceProvider.getAssociatedRolesConfig(), tenantDomain);

updateConfigurationsAsServiceProperties(serviceProvider);
if (ArrayUtils.isNotEmpty(serviceProvider.getSpProperties())) {
Expand Down Expand Up @@ -2253,56 +2245,22 @@ private AssociatedRolesConfig getAssociatedRoles(String applicationId, Connectio
String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantID);
AssociatedRolesConfig associatedRolesConfig = new AssociatedRolesConfig();
List<String> associatedRoleIds = new ArrayList<>();
String allowedAudience =
getSPPropertyValueByPropertyKey(applicationId, ALLOWED_ROLE_AUDIENCE_PROPERTY_NAME, tenantDomain);
if (RoleConstants.APPLICATION.equalsIgnoreCase(allowedAudience)) {
try (NamedPreparedStatement preparedStatement = new NamedPreparedStatement(connection,
ApplicationMgtDBQueries.LOAD_ASSOCIATED_ROLES)) {
preparedStatement.setString(ApplicationMgtDBQueries.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID,
applicationId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
associatedRoleIds.add(resultSet.getString(1));
}
}

associatedRolesConfig.setRoles(buildAssociatedRolesWithRoleName(associatedRoleIds, tenantDomain));
} catch (SQLException | IdentityRoleManagementException e) {
throw new IdentityApplicationManagementException(
"Error while retrieving associated roles for application ID: " + applicationId, e);
}
} else if (RoleConstants.ORGANIZATION.equalsIgnoreCase(allowedAudience)) {
ApplicationManagementServiceComponentHolder holder = ApplicationManagementServiceComponentHolder.
getInstance();
RoleManagementService roleManagementService = holder.getRoleManagementServiceV2();
try {
List<RoleBasicInfo> chunkOfRoles;
int offset = 0;
int maximumPage = IdentityUtil.getMaximumItemPerPage();
List<RoleBasicInfo> allRoles = new ArrayList<>();
if (roleManagementService != null) {
do {
chunkOfRoles = roleManagementService.
getRoles(RoleConstants.AUDIENCE + SPACE + RoleConstants.EQ + SPACE +
RoleConstants.ORGANIZATION, maximumPage, offset, null, null,
tenantDomain);
if (!chunkOfRoles.isEmpty()) {
allRoles.addAll(chunkOfRoles);
offset += chunkOfRoles.size(); // Move to the next chunk
}
} while (!chunkOfRoles.isEmpty());

List<String> roleIds = allRoles.stream().map(RoleBasicInfo::getId).collect(Collectors.
toList());
associatedRolesConfig.setRoles(buildAssociatedRolesWithRoleName(roleIds, tenantDomain));
try (NamedPreparedStatement preparedStatement = new NamedPreparedStatement(connection,
ApplicationMgtDBQueries.LOAD_ASSOCIATED_ROLES)) {
preparedStatement.setString(ApplicationMgtDBQueries.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_APP_ID,
applicationId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
associatedRoleIds.add(resultSet.getString(1));
}
} catch (IdentityRoleManagementException e) {
throw new IdentityApplicationManagementException("Error while retrieving associated roles for " +
"application ID: " + applicationId, e);
}

associatedRolesConfig.setRoles(buildAssociatedRolesWithRoleName(associatedRoleIds, tenantDomain));
} catch (SQLException | IdentityRoleManagementException e) {
throw new IdentityApplicationManagementException(
"Error while retrieving associated roles for application ID: " + applicationId, e);
}

String allowedAudience =
getSPPropertyValueByPropertyKey(applicationId, ALLOWED_ROLE_AUDIENCE_PROPERTY_NAME, tenantDomain);
associatedRolesConfig.setAllowedAudience(
StringUtils.isNotBlank(allowedAudience) ? allowedAudience.toLowerCase() : RoleConstants.ORGANIZATION);
return associatedRolesConfig;
Expand Down Expand Up @@ -3791,50 +3749,6 @@ public int getCountOfApplications(String filter) throws IdentityApplicationManag
return count;
}

/**
* Retrieve application basic information using the sp metadata property key and value.
*
* @param key Name of the sp metadata property key
* @param value Value of the sp metadata property value
* @return Filtered applications' basic information.
* @throws IdentityApplicationManagementException if loading application information based on the
* SP properties is failed.
*/
@Override
public ApplicationBasicInfo[] getApplicationBasicInfoBySPProperty(String key, String value)
throws IdentityApplicationManagementException {

int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();
if (log.isDebugEnabled()) {
log.debug(String.format("Getting all applications matching property: %s with value %s in tenant %d",
key, value, tenantID));
}

try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
PreparedStatement getAppNamesStmt = connection.prepareStatement(JdbcUtils.isH2DB() ?
ApplicationMgtDBQueries.LOAD_APP_IDS_BY_SP_PROPERTY_H2 :
ApplicationMgtDBQueries.LOAD_APP_IDS_BY_SP_PROPERTY)) {

getAppNamesStmt.setString(1, key);
getAppNamesStmt.setString(2, value);
getAppNamesStmt.setInt(3, tenantID);

try (ResultSet appNameResultSet = getAppNamesStmt.executeQuery()) {
ArrayList<ApplicationBasicInfo> appInfo = new ArrayList<>();

while (appNameResultSet.next()) {
ApplicationBasicInfo basicInfo = new ApplicationBasicInfo();
basicInfo.setUuid(appNameResultSet.getString(UUID));
appInfo.add(basicInfo);
}

return appInfo.toArray(new ApplicationBasicInfo[0]);
}
} catch (SQLException | DataAccessException e) {
throw new IdentityApplicationManagementException("Error while getting applications from DB", e);
}
}

/**
* Get application Names for user which has the filter string
*
Expand Down Expand Up @@ -6188,7 +6102,7 @@ private void deleteApplicationCertificate(Connection connection, ServiceProvider

private String generateApplicationResourceId(ServiceProvider serviceProvider) {

return java.util.UUID.randomUUID().toString();
return UUID.randomUUID().toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,6 @@ public class ApplicationMgtDBQueries {
"TENANT_ID = ? AND APP_NAME != ? ORDER BY ID DESC";
public static final String LOAD_APP_NAMES_BY_TENANT_AND_APP_NAME = "SELECT ID, APP_NAME, DESCRIPTION FROM SP_APP " +
"WHERE TENANT_ID = ? AND APP_NAME != ? AND (%s) ORDER BY ID DESC";

public static final String LOAD_APP_IDS_BY_SP_PROPERTY_H2 = "SELECT SP_APP.UUID FROM SP_APP JOIN " +
"SP_METADATA ON SP_APP.ID = SP_METADATA.SP_ID WHERE SP_METADATA.NAME=? and " +
"SP_METADATA.`VALUE`=? AND SP_METADATA.TENANT_ID = ?";

public static final String LOAD_APP_IDS_BY_SP_PROPERTY = "SELECT SP_APP.UUID FROM SP_APP JOIN " +
"SP_METADATA ON SP_APP.ID = SP_METADATA.SP_ID WHERE SP_METADATA.NAME=? and " +
"SP_METADATA.VALUE=? AND SP_METADATA.TENANT_ID = ?";

public static final String LOAD_APP_COUNT_BY_TENANT = "SELECT COUNT(*) FROM SP_APP WHERE TENANT_ID = ? AND " +
"APP_NAME != ? ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,22 +454,4 @@ default boolean doPostGetAssociatedRolesOfApplication(List<RoleV2> associatedRol

return true;
}

/**
* Define any additional actions before getting all applications' basic information for the sp property
* key and value
*
* @param tenantDomain tenant domain
* @param username username
* @param key sp property key
* @param value sp property value
* @return true
* @throws IdentityApplicationManagementException
*/
default boolean getApplicationBasicInfoBySPProperty(String tenantDomain, String username,
String key, String value)
throws IdentityApplicationManagementException {

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ public List<RoleBasicInfo> getRoles(List<ExpressionNode> expressionNodes, Intege
}

private List<RoleBasicInfo> getFilteredRolesBasicInfo(List<ExpressionNode> expressionNodes, Integer limit,
Integer offset, String sortBy, String sortOrder,
String tenantDomain) throws IdentityRoleManagementException {
Integer offset, String sortBy, String sortOrder,
String tenantDomain) throws IdentityRoleManagementException {

int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
FilterQueryBuilder filterQueryBuilder = new FilterQueryBuilder();
Expand Down
Loading