Skip to content

Commit

Permalink
Add DisplayName to ScopeDTO in publisher REST API. Keep display name …
Browse files Browse the repository at this point in the history
…in Scope.name and scope key in Scope.key.
  • Loading branch information
dushaniw committed May 5, 2020
1 parent db07d8b commit f2ab96c
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1123,10 +1123,10 @@ public void updateScope(Scope scope, String tenantDomain) throws APIManagementEx

// Get access token
AccessTokenInfo accessToken = getAccessTokenForScopeMgt(tenantDomain);
String scopeName = scope.getKey();
String scopeKey = scope.getKey();
String scopeEndpoint = getScopeManagementServiceEndpoint(tenantDomain)
+ (APIConstants.KEY_MANAGER_OAUTH2_SCOPES_REST_API_SCOPE_NAME
.replace(APIConstants.KEY_MANAGER_OAUTH2_SCOPES_SCOPE_NAME_PARAM, scopeName));
.replace(APIConstants.KEY_MANAGER_OAUTH2_SCOPES_SCOPE_NAME_PARAM, scopeKey));
try {
HttpPut httpPut = new HttpPut(scopeEndpoint);
httpPut.setHeader(HttpHeaders.AUTHORIZATION, getBearerAuthorizationHeader(accessToken));
Expand All @@ -1141,18 +1141,18 @@ public void updateScope(Scope scope, String tenantDomain) throws APIManagementEx
httpPut.setEntity(payload);
if (log.isDebugEnabled()) {
log.debug("Invoking Scope Management REST API of KM: " + scopeEndpoint + " to update scope "
+ scopeName);
+ scopeKey);
}
try (CloseableHttpResponse httpResponse = kmHttpClient.execute(httpPut)) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
String responseString = readHttpResponseAsString(httpResponse);
throw new APIManagementException("Error occurred while updating scope: " + scopeName + " via: "
throw new APIManagementException("Error occurred while updating scope: " + scopeKey + " via: "
+ scopeEndpoint + ". Error Status: " + statusCode + " . Error Response: " + responseString);
}
}
} catch (IOException e) {
String errorMessage = "Error occurred while updating scope: " + scopeName + " via " + scopeEndpoint;
String errorMessage = "Error occurred while updating scope: " + scopeKey + " via " + scopeEndpoint;
throw new APIManagementException(errorMessage, e);
}
}
Expand Down Expand Up @@ -1218,7 +1218,8 @@ public boolean isScopeExists(String scopeName, String tenantDomain) throws APIMa
public void validateScopes(Set<Scope> scopes, String tenantDomain) throws APIManagementException {

for (Scope scope : scopes) {
Scope sharedScope = getScopeByName(scope.getName(), tenantDomain);
Scope sharedScope = getScopeByName(scope.getKey(), tenantDomain);
scope.setName(sharedScope.getName());
scope.setDescription(sharedScope.getDescription());
scope.setRoles(sharedScope.getRoles());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8174,7 +8174,7 @@ public String addSharedScope(Scope scope, String tenantDomain) throws APIManagem

KeyManagerHolder.getKeyManagerInstance().registerScope(scope, tenantDomain);
if (log.isDebugEnabled()) {
log.debug("Adding shared scope mapping: " + scope.getName());
log.debug("Adding shared scope mapping: " + scope.getKey());
}
return ApiMgtDAO.getInstance().addSharedScope(scope, tenantDomain);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6988,7 +6988,6 @@ public ArrayList<URITemplate> getAllURITemplatesOldThrottle(String apiContext, S
!scopeToURL.get(key).contains(scopeName) && uriTemplateMap.containsKey(key)) {
scope = new Scope();
scope.setKey(scopeName);
scope.setName(scopeName);
uriTemplateMap.get(key).setScopes(scope);
scopeToURL.get(key).add(scopeName);
continue;
Expand All @@ -7007,7 +7006,6 @@ public ArrayList<URITemplate> getAllURITemplatesOldThrottle(String apiContext, S
if (StringUtils.isNotEmpty(scopeName)) {
scope = new Scope();
scope.setKey(scopeName);
scope.setName(scopeName);
uriTemplate.setScope(scope);
uriTemplate.setScopes(scope);
Set<String> templateScopes = new HashSet<>();
Expand Down Expand Up @@ -7126,7 +7124,6 @@ private ArrayList<URITemplate> extractURITemplates(ResultSet rs) throws SQLExcep
&& !scopeToURL.get(key).contains(scopeName) && uriTemplateMap.containsKey(key)) {
Scope scope = new Scope();
scope.setKey(scopeName);
scope.setName(scopeName);
uriTemplateMap.get(key).setScopes(scope);
scopeToURL.get(key).add(scopeName);
}
Expand All @@ -7143,7 +7140,6 @@ private ArrayList<URITemplate> extractURITemplates(ResultSet rs) throws SQLExcep
if (StringUtils.isNotEmpty(scopeName)) {
Scope scope = new Scope();
scope.setKey(scopeName);
scope.setName(scopeName);
uriTemplate.setScope(scope);
uriTemplate.setScopes(scope);
Set<String> templateScopes = new HashSet<>();
Expand Down Expand Up @@ -7701,7 +7697,6 @@ public Set<URITemplate> getURITemplatesOfAPI(APIIdentifier identifier)
&& uriTemplates.containsKey(uriTemplateId)) {
Scope scope = new Scope();
scope.setKey(scopeName);
scope.setName(scopeName);
scopeToURITemplateId.get(uriTemplateId).add(scopeName);
uriTemplates.get(uriTemplateId).setScopes(scope);
continue;
Expand All @@ -7718,7 +7713,6 @@ public Set<URITemplate> getURITemplatesOfAPI(APIIdentifier identifier)
if (StringUtils.isNotEmpty(scopeName)) {
Scope scope = new Scope();
scope.setKey(scopeName);
scope.setName(scopeName);
uriTemplate.setScope(scope);
uriTemplate.setScopes(scope);
Set<String> templateScopes = new HashSet<>();
Expand Down Expand Up @@ -9430,7 +9424,7 @@ public Map<String, Set<Scope>> getScopesForAPIS(String apiIdsString) throws APIM
String apiId = resultSet.getString(1);
Scope scope = new Scope();
scope.setId(String.valueOf(resultSet.getInt(2)));
scope.setName(resultSet.getString(3));
scope.setKey(resultSet.getString(3));
scope.setDescription(resultSet.getString(4));
Set<Scope> scopeList = apiScopeSet.get(apiId);
if (scopeList == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public String buildSchemaWithScopesAndRoles(API api) {

if (swaggerDef != null) {
for (URITemplate template : api.getUriTemplates()) {
String scopeInURITemplate = template.getScope() != null ? template.getScope().getName() : null;
String scopeInURITemplate = template.getScope() != null ? template.getScope().getKey() : null;
if (scopeInURITemplate != null) {
operationScopeMap.put(template.getUriTemplate(), scopeInURITemplate);
if (!scopeRoleMap.containsKey(scopeInURITemplate)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,8 @@ private void updateSwaggerSecurityDefinition(Swagger swagger, SwaggerData swagge
if (scopes != null && !scopes.isEmpty()) {
Map<String, String> scopeBindings = new HashMap<>();
for (Scope scope : scopes) {
oAuth2Definition.addScope(scope.getName(), scope.getDescription());
scopeBindings.put(scope.getName(), scope.getRoles());
oAuth2Definition.addScope(scope.getKey(), scope.getDescription());
scopeBindings.put(scope.getKey(), scope.getRoles());
}
oAuth2Definition.setVendorExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS, scopeBindings);
}
Expand Down Expand Up @@ -783,7 +783,7 @@ private void updateOperationManagedInfo(SwaggerData.Resource resource, Operation
if (resource.getScopes().isEmpty()) {
requirement.put(oauth2SchemeKey, Collections.EMPTY_LIST);
} else {
requirement.put(oauth2SchemeKey, resource.getScopes().stream().map(Scope::getName).collect(
requirement.put(oauth2SchemeKey, resource.getScopes().stream().map(Scope::getKey).collect(
Collectors.toList()));
}
return;
Expand All @@ -794,7 +794,7 @@ private void updateOperationManagedInfo(SwaggerData.Resource resource, Operation
if (resource.getScopes().isEmpty()) {
defaultRequirement.put(oauth2SchemeKey, Collections.EMPTY_LIST);
} else {
defaultRequirement.put(oauth2SchemeKey, resource.getScopes().stream().map(Scope::getName).collect(
defaultRequirement.put(oauth2SchemeKey, resource.getScopes().stream().map(Scope::getKey).collect(
Collectors.toList()));
}
security.add(defaultRequirement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,8 @@ private void updateSwaggerSecurityDefinition(OpenAPI openAPI, SwaggerData swagge
if (scopes != null && !scopes.isEmpty()) {
Map<String, String> scopeBindings = new HashMap<>();
for (Scope scope : scopes) {
oas3Scopes.put(scope.getName(), scope.getDescription());
scopeBindings.put(scope.getName(), scope.getRoles());
oas3Scopes.put(scope.getKey(), scope.getDescription());
scopeBindings.put(scope.getKey(), scope.getRoles());
}
oAuthFlow.addExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS, scopeBindings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ static JsonNode getResponseCacheConfig(String responseCache, int cacheTimeout) {
*/
static Set<Scope> sortScopes(Set<Scope> scopeSet) {
List<Scope> scopesSortedlist = new ArrayList<>(scopeSet);
scopesSortedlist.sort(Comparator.comparing(Scope::getName));
scopesSortedlist.sort(Comparator.comparing(Scope::getKey));
return new LinkedHashSet<>(scopesSortedlist);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ScopeDTO {

private String id = null;
private String name = null;
private String displayName = null;
private String description = null;
private List<String> bindings = new ArrayList<>();

Expand Down Expand Up @@ -59,6 +60,24 @@ public void setName(String name) {
this.name = name;
}

/**
* display name of Scope
**/
public ScopeDTO displayName(String displayName) {
this.displayName = displayName;
return this;
}


@ApiModelProperty(example = "api_view", value = "display name of Scope ")
@JsonProperty("displayName")
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}

/**
* description of Scope
**/
Expand Down Expand Up @@ -107,13 +126,14 @@ public boolean equals(java.lang.Object o) {
ScopeDTO scope = (ScopeDTO) o;
return Objects.equals(id, scope.id) &&
Objects.equals(name, scope.name) &&
Objects.equals(displayName, scope.displayName) &&
Objects.equals(description, scope.description) &&
Objects.equals(bindings, scope.bindings);
}

@Override
public int hashCode() {
return Objects.hash(id, name, description, bindings);
return Objects.hash(id, name, displayName, description, bindings);
}

@Override
Expand All @@ -123,6 +143,7 @@ public String toString() {

sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" displayName: ").append(toIndentedString(displayName)).append("\n");
sb.append(" description: ").append(toIndentedString(description)).append("\n");
sb.append(" bindings: ").append(toIndentedString(bindings)).append("\n");
sb.append("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4135,7 +4135,7 @@ private void validateScopes(API api) throws APIManagementException {
Set<Scope> sharedAPIScopes = new HashSet<>();

for (Scope scope : api.getScopes()) {
String scopeName = scope.getName();
String scopeName = scope.getKey();
if (!(APIUtil.isWhiteListedScope(scopeName))) {
// Check if each scope key is already assigned as a local scope to a different API which is also not a
// different version of the same API. If true, return error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ public Response deleteSharedScope(String scopeId, MessageContext messageContext)
ExceptionCodes.SHARED_SCOPE_ID_NOT_SPECIFIED);
}
Scope existingScope = apiProvider.getSharedScopeByUUID(scopeId, tenantDomain);
if (apiProvider.isScopeKeyAssignedToAPI(existingScope.getName(), tenantDomain)) {
if (apiProvider.isScopeKeyAssignedToAPI(existingScope.getKey(), tenantDomain)) {
throw new APIManagementException("Cannot remove the Shared Scope " + scopeId + " as it is used by one "
+ "or more APIs", ExceptionCodes.from(ExceptionCodes.SHARED_SCOPE_ALREADY_ATTACHED, scopeId));
}
apiProvider.deleteSharedScope(existingScope.getName(), tenantDomain);
apiProvider.deleteSharedScope(existingScope.getKey(), tenantDomain);
return Response.ok().build();
}

Expand Down Expand Up @@ -210,7 +210,7 @@ public Response updateSharedScope(String scopeId, ScopeDTO body, MessageContext
Scope existingScope = apiProvider.getSharedScopeByUUID(scopeId, tenantDomain);
//Override scope Id and name in request body from existing scope
body.setId(existingScope.getId());
body.setName(existingScope.getName());
body.setName(existingScope.getKey());
Scope scope = SharedScopeMappingUtil.fromDTOToScope(body);
apiProvider.updateSharedScope(scope, tenantDomain);
//Get updated shared scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ public static Set<Scope> getScopes(APIDTO apiDTO) {
Scope scope = new Scope();
ScopeDTO scopeDTO = apiScopeDTO.getScope();
scope.setKey(scopeDTO.getName());
scope.setName(scopeDTO.getName());
scope.setName(scopeDTO.getDisplayName());
scope.setDescription(scopeDTO.getDescription());
scope.setRoles(String.join(",", scopeDTO.getBindings()));
scopeSet.add(scope);
Expand All @@ -1340,7 +1340,7 @@ private static Set<Scope> getScopes(APIProductDTO apiProductDTO) {
for (ScopeDTO scopeDTO : apiProductDTO.getScopes()) {
Scope scope = new Scope();
scope.setKey(scopeDTO.getName());
scope.setName(scopeDTO.getName());
scope.setName(scopeDTO.getDisplayName());
scope.setDescription(scopeDTO.getDescription());
scope.setRoles(String.join(",", scopeDTO.getBindings()));
scopeSet.add(scope);
Expand Down Expand Up @@ -1729,7 +1729,7 @@ private static APIOperationsDTO getOperationFromURITemplate(URITemplate uriTempl
}
operationsDTO.setVerb(uriTemplate.getHTTPVerb());
operationsDTO.setTarget(uriTemplate.getUriTemplate());
operationsDTO.setScopes(uriTemplate.retrieveAllScopes().stream().map(Scope::getName).collect(
operationsDTO.setScopes(uriTemplate.retrieveAllScopes().stream().map(Scope::getKey).collect(
Collectors.toList()));
operationsDTO.setThrottlingPolicy(uriTemplate.getThrottlingTier());
Set<APIProductIdentifier> usedByProducts = uriTemplate.retrieveUsedByProducts();
Expand Down Expand Up @@ -2365,8 +2365,8 @@ private static List<ScopeDTO> getScopesFromSwagger(String swagger) throws APIMan
List<ScopeDTO> scopeDTOS = new ArrayList<>();
for (Scope aScope : scopes) {
ScopeDTO scopeDTO = new ScopeDTO();
String scopeName = aScope.getName();
scopeDTO.setName(scopeName);
scopeDTO.setName(aScope.getKey());
scopeDTO.setDisplayName(aScope.getName());
scopeDTO.setDescription(aScope.getDescription());
String roles = aScope.getRoles();
if (roles == null || roles.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static Scope fromDTOToScope(ScopeDTO scopeDTO) {
scope.setId(scopeDTO.getId());
scope.setDescription(scopeDTO.getDescription());
scope.setKey(scopeDTO.getName());
scope.setName(scopeDTO.getName());
scope.setName(scopeDTO.getDisplayName());
if (scopeDTO.getBindings() != null) {
scope.setRoles(String.join(",", scopeDTO.getBindings()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8792,6 +8792,11 @@ definitions:
description: |
name of Scope
example: apim:api_view
displayName:
type: string
description: |
display name of Scope
example: api_view
description:
type: string
description: |
Expand Down
15 changes: 15 additions & 0 deletions docs/apidocs/publisher/v1/models/Scope.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ <h2>Scope</h2>
<td class="parameter"> <p class="marked">null</p></td>
<td class="parameter"> <p class="marked">apim:api_view</p></td>
</tr>
<tr>
<td class="parameter">
<p>displayName</p>
<p class="param-required">

optional
</p>
</td>
<td class="parameter"><p class="marked">display name of Scope </p></td>
<td class="parameter">
<a href="#!/models#string">string</a>
</td>
<td class="parameter"> <p class="marked">null</p></td>
<td class="parameter"> <p class="marked">api_view</p></td>
</tr>
<tr>
<td class="parameter">
<p>description</p>
Expand Down

0 comments on commit f2ab96c

Please sign in to comment.