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

Conditional View Access For Discoverable Applications in MyAccount #439

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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 @@ -123,7 +123,11 @@
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.testutil</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement;
import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException;
import org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.application.mgt.ApplicationConstants;
Expand All @@ -34,6 +35,7 @@
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.JdbcUtils;
import org.wso2.carbon.identity.organization.management.application.constant.SQLConstants;
import org.wso2.carbon.identity.organization.management.application.dao.OrgApplicationMgtDAO;
import org.wso2.carbon.identity.organization.management.application.model.MainApplicationDO;
import org.wso2.carbon.identity.organization.management.application.model.SharedApplicationDO;
Expand Down Expand Up @@ -322,15 +324,20 @@ public List<ApplicationBasicInfo> getDiscoverableSharedApplicationBasicInfo(int

try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
String databaseVendorType = connection.getMetaData().getDatabaseProductName();
String[] loggedInUserGroups = getLoggedInUserGroups();
String sqlStatement = buildDiscoverableGroupSQLCondition(
getDBVendorSpecificDiscoverableSharedAppRetrievalQueryByAppName(databaseVendorType),
loggedInUserGroups.length);

try (NamedPreparedStatement statement =
new NamedPreparedStatement(connection,
getDBVendorSpecificDiscoverableSharedAppRetrievalQueryByAppName(databaseVendorType))) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, sqlStatement)) {
statement.setString(1, tenantDomain);
statement.setString(2, filterResolvedForSQL);
statement.setString(3, rootOrgId);
statement.setInt(4, offset);
statement.setInt(5, limit);
for (int i = 0; i < loggedInUserGroups.length; i++) {
statement.setString(i + 4, loggedInUserGroups[i]);
}
statement.setInt(loggedInUserGroups.length + 4, offset);
statement.setInt(loggedInUserGroups.length + 5, limit);

try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
Expand Down Expand Up @@ -361,13 +368,18 @@ public int getCountOfDiscoverableSharedApplications(String filter, String tenant
int count = 0;
String filterResolvedForSQL = resolveSQLFilter(filter);
try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
String[] loggedInUserGroups = getLoggedInUserGroups();
String sqlStatement =
buildDiscoverableGroupSQLCondition(LOAD_DISCOVERABLE_SHARED_APP_COUNT_BY_APP_NAME_AND_TENANT,
loggedInUserGroups.length);

try (NamedPreparedStatement statement =
new NamedPreparedStatement(connection,
LOAD_DISCOVERABLE_SHARED_APP_COUNT_BY_APP_NAME_AND_TENANT)) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, sqlStatement)) {
statement.setString(1, tenantDomain);
statement.setString(2, filterResolvedForSQL);
statement.setString(3, rootOrgId);
for (int i = 0; i < loggedInUserGroups.length; i++) {
statement.setString(i + 4, loggedInUserGroups[i]);
}

try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
Expand Down Expand Up @@ -398,18 +410,23 @@ public void deleteSharedAppLinks(String organizationId) throws OrganizationManag
private int getCountOfDiscoverableSharedApplications(String tenantDomain, String rootOrgId)
throws OrganizationManagementException {

int count;
int count = 0;
try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
String[] loggedInUserGroups = getLoggedInUserGroups();
String sqlStatement = buildDiscoverableGroupSQLCondition(LOAD_DISCOVERABLE_SHARED_APP_COUNT_BY_TENANT,
loggedInUserGroups.length);

try (NamedPreparedStatement statement =
new NamedPreparedStatement(connection,
LOAD_DISCOVERABLE_SHARED_APP_COUNT_BY_TENANT)) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, sqlStatement)) {
statement.setString(1, tenantDomain);
statement.setString(2, rootOrgId);
for (int i = 0; i < loggedInUserGroups.length; i++) {
statement.setString(i + 3, loggedInUserGroups[i]);
}

try (ResultSet resultSet = statement.executeQuery()) {
resultSet.next();
count = resultSet.getInt(1);
if (resultSet.next()) {
count = resultSet.getInt(1);
}
}
}
} catch (SQLException e) {
Expand All @@ -427,14 +444,19 @@ private List<ApplicationBasicInfo> getDiscoverableSharedApplicationBasicInfo(int

try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
String databaseVendorType = connection.getMetaData().getDatabaseProductName();
String[] loggedInUserGroups = getLoggedInUserGroups();
String sqlStatement = buildDiscoverableGroupSQLCondition(
getDBVendorSpecificDiscoverableSharedAppRetrievalQuery(databaseVendorType),
loggedInUserGroups.length);

try (NamedPreparedStatement statement =
new NamedPreparedStatement(connection,
getDBVendorSpecificDiscoverableSharedAppRetrievalQuery(databaseVendorType))) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, sqlStatement)) {
statement.setString(1, tenantDomain);
statement.setString(2, rootOrgId);
statement.setInt(3, offset);
statement.setInt(4, limit);
for (int i = 0; i < loggedInUserGroups.length; i++) {
statement.setString(i + 3, loggedInUserGroups[i]);
}
statement.setInt(loggedInUserGroups.length + 3, offset);
statement.setInt(loggedInUserGroups.length + 4, limit);

try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
Expand All @@ -449,6 +471,48 @@ private List<ApplicationBasicInfo> getDiscoverableSharedApplicationBasicInfo(int
return Collections.unmodifiableList(applicationBasicInfoList);
}

/**
* Retrieve the group IDs list of the logged in user.
*
* @return The group IDs list.
* @throws OrganizationManagementException Error while retrieving the group IDs list.
*/
private String[] getLoggedInUserGroups() throws OrganizationManagementException {

try {
return ApplicationMgtUtil.getLoggedInUserGroupIDList();
} catch (IdentityApplicationManagementException e) {
throw new OrganizationManagementServerException(e.getErrorCode(), e.getMessage(), e);
}
}

/**
* Build the SQL condition for retrieving discoverable applications.
*
* @param sqlStatement SQL statement to replace the group id condition.
* @param numberOfGroups Number of groups for the group id condition.
* @return SQL statement with the group id condition.
*/
private String buildDiscoverableGroupSQLCondition(String sqlStatement, int numberOfGroups) {

String finalSqlStatement;
if (numberOfGroups == 0) {
finalSqlStatement = StringUtils.replace(sqlStatement,
SQLConstants.SQLPlaceholders.GROUP_ID_CONDITION_PLACEHOLDER,
SQLConstants.DISCOVERABLE_BY_ANY_USER);
} else {
String groupListNamedStatement = IntStream.range(0, numberOfGroups)
.mapToObj(i -> "?")
.collect(Collectors.joining(", "));
finalSqlStatement = StringUtils.replace(sqlStatement,
SQLConstants.SQLPlaceholders.GROUP_ID_CONDITION_PLACEHOLDER,
StringUtils.replace(SQLConstants.DISCOVERABLE_BY_USER_GROUPS,
SQLConstants.SQLPlaceholders.GROUP_ID_LIST_PLACEHOLDER,
groupListNamedStatement));
}
return finalSqlStatement;
}

/**
* Get the value for the shareWithAllChildren column according to the db type.
*
Expand Down
Loading
Loading