Skip to content

Commit

Permalink
Add application sharing with the organization
Browse files Browse the repository at this point in the history
  • Loading branch information
chamilaadhi committed Aug 17, 2024
1 parent 62cd1b1 commit 197b95c
Show file tree
Hide file tree
Showing 13 changed files with 861 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,13 @@ OAuthApplicationInfo updateAuthClient(String userId, Application application,
* @param offset
* @param groupingId the groupId to which the applications must belong.
* @param organization Identifier of an organization
* @param sharedOrganization
* @return Applications
* @throws APIManagementException if failed to applications for given subscriber
*/

Application[] getApplicationsWithPagination(Subscriber subscriber, String groupingId, int start, int offset,
String search, String sortColumn, String sortOrder, String organization)
String search, String sortColumn, String sortOrder, String organization, String sharedOrganization)
throws APIManagementException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public Map<String, Map<String, OAuthApplicationInfo>> getKeyManagerWiseOAuthApp(
private String keyType;
private int subscriptionCount;
private String keyManager;
private String sharedOrganization;

public String getCreatedTime() {
return createdTime;
}
Expand Down Expand Up @@ -320,4 +322,12 @@ public void setOrganization(String organization) {

this.organization = organization;
}

public String getSharedOrganization() {
return sharedOrganization;
}

public void setSharedOrganization(String sharedOrganization) {
this.sharedOrganization = sharedOrganization;
}
}
648 changes: 648 additions & 0 deletions components/apimgt/org.wso2.carbon.apimgt.impl/code

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ private Permissions() {
"internal_application_mgt_delete", "internal_application_mgt_update", "internal_application_mgt_view",
"internal_user_mgt_list"};
public static final String KEY_MANAGER_CLIENT_APPLICATION_PREFIX = "wso2_apim_km_";
public static final String DEFAULT_APP_SHARING_KEYWORD = "private";

public static final String TOKEN_URL = "TokenURL";
public static final String REVOKE_URL = "RevokeURL";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,9 @@ public int addApplication(Application application, String userId, String organiz
if (StringUtils.isBlank(application.getCallbackUrl())) {
application.setCallbackUrl(null);
}
if (StringUtils.isEmpty(application.getSharedOrganization())) {
application.setSharedOrganization(APIConstants.DEFAULT_APP_SHARING_KEYWORD);
}
int applicationId = apiMgtDAO.addApplication(application, userId, organization);
Application createdApplication = apiMgtDAO.getApplicationById(applicationId);

Expand Down Expand Up @@ -1894,6 +1897,9 @@ public void updateApplication(Application application) throws APIManagementExcep
application.setApplicationAttributes(null);
}
validateApplicationPolicy(application, existingApp.getOrganization());
if (StringUtils.isEmpty(application.getSharedOrganization())) {
application.setSharedOrganization(APIConstants.DEFAULT_APP_SHARING_KEYWORD);
}
apiMgtDAO.updateApplication(application);
Application updatedApplication = apiMgtDAO.getApplicationById(application.getId());
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -2786,19 +2792,20 @@ public String[] getGroupIds(String response) throws APIManagementException {
* @param sortColumn The sort column.
* @param sortOrder The sort order.
* @param organization Identifier of an Organization
* @param sharedOrganization
* @return Application[] The Applications.
* @throws APIManagementException
*/
@Override
public Application[] getApplicationsWithPagination(Subscriber subscriber, String groupingId, int start, int offset
, String search, String sortColumn, String sortOrder, String organization)
, String search, String sortColumn, String sortOrder, String organization, String sharedOrganization)
throws APIManagementException {

if (APIUtil.isOnPremResolver()) {
organization = tenantDomain;
}
return apiMgtDAO.getApplicationsWithPagination(subscriber, groupingId, start, offset,
search, sortColumn, sortOrder, organization);
search, sortColumn, sortOrder, organization, sharedOrganization);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3497,6 +3497,7 @@ public int addApplication(Application application, String userId, Connection con
ps.setString(11, application.getUUID());
ps.setString(12, String.valueOf(application.getTokenType()));
ps.setString(13, organization);
ps.setString(14, application.getSharedOrganization());
ps.executeUpdate();

rs = ps.getGeneratedKeys();
Expand Down Expand Up @@ -3537,7 +3538,8 @@ public void updateApplication(Application application) throws APIManagementExcep
ps.setString(5, null);
ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
ps.setString(7, application.getTokenType());
ps.setInt(8, application.getId());
ps.setString(8, application.getSharedOrganization());
ps.setInt(9, application.getId());

ps.executeUpdate();

Expand Down Expand Up @@ -4118,15 +4120,16 @@ public boolean updateApplicationOwner(String userName, Application application)
* @throws APIManagementException
*/
public Application[] getApplicationsWithPagination(Subscriber subscriber, String groupingId, int start,
int offset, String search, String sortColumn, String sortOrder, String organization)
int offset, String search, String sortColumn, String sortOrder, String organization,
String sharedOrganization)
throws APIManagementException {

Connection connection = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
Application[] applications = null;
String sqlQuery = null;

boolean isOrgSharingEnabled = true; //TODO need to come from config or from user info
if (groupingId != null && !"null".equals(groupingId) && !groupingId.isEmpty()) {
if (multiGroupAppSharingEnabled) {
if (forceCaseInsensitiveComparisons) {
Expand All @@ -4147,9 +4150,19 @@ public Application[] getApplicationsWithPagination(Subscriber subscriber, String
}
} else {
if (forceCaseInsensitiveComparisons) {
sqlQuery = SQLConstantManagerFactory.getSQlString("GET_APPLICATIONS_PREFIX_NONE_CASESENSITVE");
if (isOrgSharingEnabled) {
sqlQuery = SQLConstantManagerFactory
.getSQlString("GET_APPLICATIONS_PREFIX_NONE_CASESENSITVE_WITH_ORGSHARING");
} else {
sqlQuery = SQLConstantManagerFactory.getSQlString("GET_APPLICATIONS_PREFIX_NONE_CASESENSITVE");
}
} else {
sqlQuery = SQLConstantManagerFactory.getSQlString("GET_APPLICATIONS_PREFIX_CASESENSITVE");
if (isOrgSharingEnabled) {
sqlQuery = SQLConstantManagerFactory
.getSQlString("GET_APPLICATIONS_PREFIX_CASESENSITVE_WITH_ORGSHARING");
} else {
sqlQuery = SQLConstantManagerFactory.getSQlString("GET_APPLICATIONS_PREFIX_CASESENSITVE");
}
}
}

Expand Down Expand Up @@ -4196,12 +4209,22 @@ public Application[] getApplicationsWithPagination(Subscriber subscriber, String
prepStmt.setInt(6, offset);
}
} else {
prepStmt = connection.prepareStatement(sqlQuery);
prepStmt.setString(1, subscriber.getName());
prepStmt.setString(2, organization);
prepStmt.setString(3, "%" + search + "%");
prepStmt.setInt(4, start);
prepStmt.setInt(5, offset);
if (isOrgSharingEnabled) {
prepStmt = connection.prepareStatement(sqlQuery);
prepStmt.setString(1, subscriber.getName());
prepStmt.setString(2, sharedOrganization);
prepStmt.setString(3, organization);
prepStmt.setString(4, "%" + search + "%");
prepStmt.setInt(5, start);
prepStmt.setInt(6, offset);
} else {
prepStmt = connection.prepareStatement(sqlQuery);
prepStmt.setString(1, subscriber.getName());
prepStmt.setString(2, organization);
prepStmt.setString(3, "%" + search + "%");
prepStmt.setInt(4, start);
prepStmt.setInt(5, offset);
}
}
if (log.isDebugEnabled()) {
log.debug("Query: " + sqlQuery);
Expand Down Expand Up @@ -4238,6 +4261,7 @@ public Application[] getApplicationsWithPagination(Subscriber subscriber, String
// Get custom attributes of application
Map<String, String> applicationAttributes = getApplicationAttributes(connection, applicationId);
application.setApplicationAttributes(applicationAttributes);
application.setSharedOrganization(rs.getString("SHARED_ORGANIZATION"));

applicationsList.add(application);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,8 @@ public class SQLConstants {
public static final String APP_APPLICATION_SQL =
" INSERT INTO AM_APPLICATION (NAME, SUBSCRIBER_ID, APPLICATION_TIER, " +
" CALLBACK_URL, DESCRIPTION, APPLICATION_STATUS, GROUP_ID, CREATED_BY, CREATED_TIME, UPDATED_TIME, " +
"UUID, TOKEN_TYPE, ORGANIZATION)" +
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
"UUID, TOKEN_TYPE, ORGANIZATION, SHARED_ORGANIZATION)" +
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

public static final String UPDATE_APPLICATION_SQL =
" UPDATE " +
Expand All @@ -1022,7 +1022,8 @@ public class SQLConstants {
" DESCRIPTION = ?, " +
" UPDATED_BY = ?, " +
" UPDATED_TIME = ?, " +
" TOKEN_TYPE = ? " +
" TOKEN_TYPE = ?, " +
" SHARED_ORGANIZATION = ? " +
" WHERE" +
" APPLICATION_ID = ?";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public class SQLConstantsH2MySQL extends SQLConstants{
" GROUP_ID, " +
" UUID, " +
" APP.CREATED_BY AS CREATED_BY, " +
" APP.TOKEN_TYPE AS TOKEN_TYPE " +
" APP.TOKEN_TYPE AS TOKEN_TYPE, " +
" APP.SHARED_ORGANIZATION AS SHARED_ORGANIZATION " +
" FROM" +
" AM_APPLICATION APP, " +
" AM_SUBSCRIBER SUB " +
Expand Down Expand Up @@ -212,7 +213,8 @@ public class SQLConstantsH2MySQL extends SQLConstants{
" GROUP_ID, " +
" UUID, " +
" APP.CREATED_BY AS CREATED_BY, " +
" APP.TOKEN_TYPE AS TOKEN_TYPE " +
" APP.TOKEN_TYPE AS TOKEN_TYPE, " +
" APP.SHARED_ORGANIZATION AS SHARED_ORGANIZATION " +
" FROM" +
" AM_APPLICATION APP, " +
" AM_SUBSCRIBER SUB " +
Expand All @@ -227,6 +229,68 @@ public class SQLConstantsH2MySQL extends SQLConstants{
" )x left join AM_BLOCK_CONDITIONS bl on ( bl.TYPE = 'APPLICATION' AND bl.BLOCK_CONDITION = concat(concat(x.USER_ID,':'),x.name)) " +
" ORDER BY $1 $2 limit ? , ?";

public static final String GET_APPLICATIONS_PREFIX_CASESENSITVE_WITH_ORGSHARING =
"select distinct x.*,bl.ENABLED from (" +
"SELECT " +
" APPLICATION_ID, " +
" NAME," +
" APPLICATION_TIER," +
" APP.SUBSCRIBER_ID, " +
" APP.CREATED_TIME AS APP_CREATED_TIME, " +
" APP.UPDATED_TIME AS APP_UPDATED_TIME, " +
" CALLBACK_URL, " +
" DESCRIPTION, " +
" APPLICATION_STATUS, " +
" USER_ID, " +
" GROUP_ID, " +
" UUID, " +
" APP.CREATED_BY AS CREATED_BY, " +
" APP.TOKEN_TYPE AS TOKEN_TYPE, " +
" APP.SHARED_ORGANIZATION AS SHARED_ORGANIZATION " +
" FROM" +
" AM_APPLICATION APP, " +
" AM_SUBSCRIBER SUB " +
" WHERE " +
" SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID " +
" AND (LOWER(SUB.USER_ID) = LOWER(?) OR APP.SHARED_ORGANIZATION = ? )" +
" AND " +
" APP.ORGANIZATION = ? " +
" And "+
" LOWER (NAME) like LOWER (?)"+
" )x left join AM_BLOCK_CONDITIONS bl on ( bl.TYPE = 'APPLICATION' AND bl.BLOCK_CONDITION = concat(concat(x.USER_ID,':'),x.name)) " +
" ORDER BY $1 $2 limit ? , ?";

public static final String GET_APPLICATIONS_PREFIX_NONE_CASESENSITVE_WITH_ORGSHARING =
"select distinct x.*,bl.ENABLED from (" +
"SELECT " +
" APPLICATION_ID, " +
" NAME," +
" APPLICATION_TIER," +
" APP.SUBSCRIBER_ID, " +
" APP.CREATED_TIME AS APP_CREATED_TIME, " +
" APP.UPDATED_TIME AS APP_UPDATED_TIME, " +
" CALLBACK_URL, " +
" DESCRIPTION, " +
" APPLICATION_STATUS, " +
" USER_ID, " +
" GROUP_ID, " +
" UUID, " +
" APP.CREATED_BY AS CREATED_BY, " +
" APP.TOKEN_TYPE AS TOKEN_TYPE, " +
" APP.SHARED_ORGANIZATION AS SHARED_ORGANIZATION " +
" FROM" +
" AM_APPLICATION APP, " +
" AM_SUBSCRIBER SUB " +
" WHERE " +
" SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID " +
" AND (LOWER(SUB.USER_ID) = LOWER(?) OR APP.SHARED_ORGANIZATION = ? )" +
" AND " +
" APP.ORGANIZATION = ? " +
" And "+
" LOWER (NAME) like LOWER (?)"+
" )x left join AM_BLOCK_CONDITIONS bl on ( bl.TYPE = 'APPLICATION' AND bl.BLOCK_CONDITION = concat(concat(x.USER_ID,':'),x.name)) " +
" ORDER BY $1 $2 limit ? , ?";


public static final String GET_APPLICATIONS_BY_TENANT_ID =
" SELECT " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ public static TokenTypeEnum fromValue(String v) {
private String createdTime = null;
private String updatedTime = null;

@XmlType(name="VisibilityEnum")
@XmlEnum(String.class)
public enum VisibilityEnum {
PRIVATE("PRIVATE"),
SHARED_WITH_ORG("SHARED_WITH_ORG");
private String value;

VisibilityEnum (String v) {
value = v;
}

public String value() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static VisibilityEnum fromValue(String v) {
for (VisibilityEnum b : VisibilityEnum.values()) {
if (String.valueOf(b.value).equals(v)) {
return b;
}
}
return null;
}
}
private VisibilityEnum visibility = null;

/**
**/
public ApplicationDTO applicationId(String applicationId) {
Expand Down Expand Up @@ -334,6 +366,23 @@ public void setUpdatedTime(String updatedTime) {
this.updatedTime = updatedTime;
}

/**
**/
public ApplicationDTO visibility(VisibilityEnum visibility) {
this.visibility = visibility;
return this;
}


@ApiModelProperty(value = "")
@JsonProperty("visibility")
public VisibilityEnum getVisibility() {
return visibility;
}
public void setVisibility(VisibilityEnum visibility) {
this.visibility = visibility;
}


@Override
public boolean equals(java.lang.Object o) {
Expand All @@ -358,12 +407,13 @@ public boolean equals(java.lang.Object o) {
Objects.equals(owner, application.owner) &&
Objects.equals(hashEnabled, application.hashEnabled) &&
Objects.equals(createdTime, application.createdTime) &&
Objects.equals(updatedTime, application.updatedTime);
Objects.equals(updatedTime, application.updatedTime) &&
Objects.equals(visibility, application.visibility);
}

@Override
public int hashCode() {
return Objects.hash(applicationId, name, throttlingPolicy, description, tokenType, status, groups, subscriptionCount, keys, attributes, subscriptionScopes, owner, hashEnabled, createdTime, updatedTime);
return Objects.hash(applicationId, name, throttlingPolicy, description, tokenType, status, groups, subscriptionCount, keys, attributes, subscriptionScopes, owner, hashEnabled, createdTime, updatedTime, visibility);
}

@Override
Expand All @@ -386,6 +436,7 @@ public String toString() {
sb.append(" hashEnabled: ").append(toIndentedString(hashEnabled)).append("\n");
sb.append(" createdTime: ").append(toIndentedString(createdTime)).append("\n");
sb.append(" updatedTime: ").append(toIndentedString(updatedTime)).append("\n");
sb.append(" visibility: ").append(toIndentedString(visibility)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Loading

0 comments on commit 197b95c

Please sign in to comment.