Skip to content

Commit

Permalink
Merge pull request wso2#5636 from wso2/revert-5632-revert-5619-role_org
Browse files Browse the repository at this point in the history
Revert "Revert "Modify the core to support removing the necessity of having role_org-application association""
  • Loading branch information
DMHP authored Apr 18, 2024
2 parents 74e2a9a + 105af42 commit b43ff38
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ApplicationBasicInfo implements Serializable {
private String clientId;
private String issuer;
private String tenantDomain;
private String uuid;

/**
* Get application id.
Expand Down Expand Up @@ -238,4 +239,25 @@ 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,6 +116,23 @@ 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,6 +340,36 @@ 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,13 +199,27 @@ 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
* @throws IdentityApplicationManagementException if building {@link ApplicationBasicInfo} fails.
*/
default ApplicationBasicInfo getApplicationBasicInfoByName(String name, String tenantDomain)
throws IdentityApplicationManagementException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@
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 @@ -122,7 +124,6 @@
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 @@ -193,6 +194,8 @@ 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 @@ -605,9 +608,14 @@ private void addApplicationConfigurations(Connection connection, ServiceProvider
serviceProvider.getPermissionAndRoleConfig().getPermissions());
}

// Update associated roles.
updateAssociatedRolesOfApplication(connection, serviceProvider.getApplicationResourceId(),
serviceProvider.getApplicationName(), serviceProvider.getAssociatedRolesConfig(), tenantDomain);
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);
}
}

updateConfigurationsAsServiceProperties(serviceProvider);
if (ArrayUtils.isNotEmpty(serviceProvider.getSpProperties())) {
Expand Down Expand Up @@ -2245,22 +2253,56 @@ private AssociatedRolesConfig getAssociatedRoles(String applicationId, Connectio
String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantID);
AssociatedRolesConfig associatedRolesConfig = new AssociatedRolesConfig();
List<String> associatedRoleIds = new ArrayList<>();
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));
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);
}
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));
}
} catch (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 @@ -3749,6 +3791,50 @@ 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 @@ -6102,7 +6188,7 @@ private void deleteApplicationCertificate(Connection connection, ServiceProvider

private String generateApplicationResourceId(ServiceProvider serviceProvider) {

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ 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,4 +454,22 @@ 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 @@ -335,8 +335,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

0 comments on commit b43ff38

Please sign in to comment.