use of org.wso2.carbon.user.api.Permission in project product-iots by wso2.
the class RoleManagement method testUpdateRolePermission.
@Test(description = "Test update permission role.", dependsOnMethods = { "testAddRole" })
public void testUpdateRolePermission() throws FileNotFoundException {
IOTResponse response = client.put(Constants.RoleManagement.ROLE_MANAGEMENT_END_POINT + "/" + ROLE_NAME, PayloadGenerator.getJsonPayload(Constants.RoleManagement.ROLE_PAYLOAD_FILE_NAME, Constants.HTTP_METHOD_PUT).toString());
Assert.assertEquals(HttpStatus.SC_OK, response.getStatus());
}
use of org.wso2.carbon.user.api.Permission in project carbon-apimgt by wso2.
the class AbstractAPIManager method getAllDocumentation.
public List<Documentation> getAllDocumentation(APIIdentifier apiId, String loggedUsername) throws APIManagementException {
List<Documentation> documentationList = new ArrayList<Documentation>();
try {
String tenantDomain = getTenantDomain(apiId);
Registry registryType;
/* If the API provider is a tenant, load tenant registry*/
boolean isTenantMode = (tenantDomain != null);
if ((isTenantMode && this.tenantDomain == null) || (isTenantMode && isTenantDomainNotMatching(tenantDomain))) {
// Tenant store anonymous mode
int tenantId = getTenantManager().getTenantId(tenantDomain);
registryType = getRegistryService().getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId);
} else {
registryType = registry;
}
String apiOrAPIProductDocPath = APIUtil.getAPIOrAPIProductDocPath(apiId);
String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
if (registry.resourceExists(apiOrAPIProductDocPath)) {
Resource resource = registry.get(apiOrAPIProductDocPath);
if (resource instanceof org.wso2.carbon.registry.core.Collection) {
String[] docsPaths = ((org.wso2.carbon.registry.core.Collection) resource).getChildren();
for (String docPath : docsPaths) {
if (!(docPath.equalsIgnoreCase(pathToContent) || docPath.equalsIgnoreCase(pathToDocFile))) {
Resource docResource = registry.get(docPath);
GenericArtifactManager artifactManager = getAPIGenericArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
GenericArtifact docArtifact = artifactManager.getGenericArtifact(docResource.getUUID());
Documentation doc = APIUtil.getDocumentation(docArtifact, apiId.getProviderName());
Date contentLastModifiedDate;
Date docLastModifiedDate = docResource.getLastModified();
if (Documentation.DocumentSourceType.INLINE.equals(doc.getSourceType())) {
String contentPath = APIUtil.getAPIDocContentPath(apiId, doc.getName());
try {
contentLastModifiedDate = registryType.get(contentPath).getLastModified();
doc.setLastUpdated((contentLastModifiedDate.after(docLastModifiedDate) ? contentLastModifiedDate : docLastModifiedDate));
} catch (org.wso2.carbon.registry.core.secure.AuthorizationFailedException e) {
// do nothing. Permission not allowed to access the doc.
}
} else if (Documentation.DocumentSourceType.MARKDOWN.equals(doc.getSourceType())) {
String contentPath = APIUtil.getAPIDocContentPath(apiId, doc.getName());
try {
contentLastModifiedDate = registryType.get(contentPath).getLastModified();
doc.setLastUpdated((contentLastModifiedDate.after(docLastModifiedDate) ? contentLastModifiedDate : docLastModifiedDate));
} catch (org.wso2.carbon.registry.core.secure.AuthorizationFailedException e) {
// do nothing. Permission not allowed to access the doc.
}
} else {
doc.setLastUpdated(docLastModifiedDate);
}
documentationList.add(doc);
}
}
}
}
} catch (RegistryException e) {
String msg = "Failed to get documentations for api " + apiId.getApiName();
throw new APIManagementException(msg, e);
} catch (org.wso2.carbon.user.api.UserStoreException e) {
String msg = "Failed to get documentations for api " + apiId.getApiName();
throw new APIManagementException(msg, e);
}
return documentationList;
}
use of org.wso2.carbon.user.api.Permission in project carbon-apimgt by wso2.
the class AbstractAPIManager method getAPIorAPIProductByUUID.
/**
* Get API or APIProduct by registry artifact id
*
* @param uuid Registry artifact id
* @param requestedTenantDomain tenantDomain for the registry
* @return ApiTypeWrapper wrapping the API or APIProduct of the provided artifact id
* @throws APIManagementException
*/
public ApiTypeWrapper getAPIorAPIProductByUUID(String uuid, String requestedTenantDomain) throws APIManagementException {
boolean tenantFlowStarted = false;
try {
Registry registry;
if (requestedTenantDomain != null) {
int id = getTenantManager().getTenantId(requestedTenantDomain);
startTenantFlow(requestedTenantDomain);
tenantFlowStarted = true;
if (APIConstants.WSO2_ANONYMOUS_USER.equals(this.username)) {
registry = getRegistryService().getGovernanceUserRegistry(this.username, id);
} else if (this.tenantDomain != null && !this.tenantDomain.equals(requestedTenantDomain)) {
registry = getRegistryService().getGovernanceSystemRegistry(id);
} else {
registry = this.registry;
}
} else {
registry = this.registry;
}
GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(uuid);
if (apiArtifact != null) {
String type = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE);
if (APIConstants.API_PRODUCT.equals(type)) {
APIProduct apiProduct = getApiProduct(registry, apiArtifact);
String productTenantDomain = getTenantDomain(apiProduct.getId());
if (APIConstants.API_GLOBAL_VISIBILITY.equals(apiProduct.getVisibility())) {
return new ApiTypeWrapper(apiProduct);
}
if (this.tenantDomain == null || !this.tenantDomain.equals(productTenantDomain)) {
throw new APIManagementException("User " + username + " does not have permission to view API Product : " + apiProduct.getId().getName());
}
return new ApiTypeWrapper(apiProduct);
} else {
API api = getApiForPublishing(registry, apiArtifact);
String apiTenantDomain = getTenantDomain(api.getId());
if (APIConstants.API_GLOBAL_VISIBILITY.equals(api.getVisibility())) {
return new ApiTypeWrapper(api);
}
if (this.tenantDomain == null || !this.tenantDomain.equals(apiTenantDomain)) {
throw new APIManagementException("User " + username + " does not have permission to view API : " + api.getId().getApiName());
}
return new ApiTypeWrapper(api);
}
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (RegistryException | org.wso2.carbon.user.api.UserStoreException e) {
String msg = "Failed to get API";
throw new APIManagementException(msg, e);
} finally {
if (tenantFlowStarted) {
endTenantFlow();
}
}
}
use of org.wso2.carbon.user.api.Permission in project carbon-apimgt by wso2.
the class RegistryPersistenceUtil method loadloadTenantAPIRXT.
public static void loadloadTenantAPIRXT(String tenant, int tenantID) throws APIManagementException {
RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
UserRegistry registry = null;
try {
registry = registryService.getGovernanceSystemRegistry(tenantID);
} catch (RegistryException e) {
throw new APIManagementException("Error when create registry instance ", e);
}
String rxtDir = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" + File.separator + "rxts";
File file = new File(rxtDir);
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// if the file extension is .rxt return true, else false
return name.endsWith(".rxt");
}
};
String[] rxtFilePaths = file.list(filenameFilter);
if (rxtFilePaths == null) {
throw new APIManagementException("rxt files not found in directory " + rxtDir);
}
for (String rxtPath : rxtFilePaths) {
String resourcePath = GovernanceConstants.RXT_CONFIGS_PATH + RegistryConstants.PATH_SEPARATOR + rxtPath;
// This is "registry" is a governance registry instance, therefore calculate the relative path to governance.
String govRelativePath = RegistryUtils.getRelativePathToOriginal(resourcePath, RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH));
try {
// calculate resource path
RegistryAuthorizationManager authorizationManager = new RegistryAuthorizationManager(ServiceReferenceHolder.getUserRealm());
resourcePath = authorizationManager.computePathOnMount(resourcePath);
org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID).getAuthorizationManager();
if (registry.resourceExists(govRelativePath)) {
// set anonymous user permission to RXTs
authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
continue;
}
String rxt = FileUtil.readFileToString(rxtDir + File.separator + rxtPath);
Resource resource = registry.newResource();
resource.setContent(rxt.getBytes(Charset.defaultCharset()));
resource.setMediaType(APIConstants.RXT_MEDIA_TYPE);
registry.put(govRelativePath, resource);
authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
} catch (UserStoreException e) {
throw new APIManagementException("Error while adding role permissions to API", e);
} catch (IOException e) {
String msg = "Failed to read rxt files";
throw new APIManagementException(msg, e);
} catch (RegistryException e) {
String msg = "Failed to add rxt to registry ";
throw new APIManagementException(msg, e);
}
}
}
use of org.wso2.carbon.user.api.Permission in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPolicyIdPut.
/**
* Updates a given Subscription level policy specified by uuid
*
* @param policyId u
* @param body DTO of policy to be updated
* @param contentType Content-Type header
* @return Updated policy
*/
@Override
public Response throttlingPoliciesSubscriptionPolicyIdPut(String policyId, String contentType, SubscriptionThrottlePolicyDTO body, MessageContext messageContext) throws APIManagementException {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
// will give PolicyNotFoundException if there's no policy exists with UUID
SubscriptionPolicy existingPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
}
// overridden properties
body.setPolicyId(policyId);
body.setPolicyName(existingPolicy.getPolicyName());
// validate if permission info exists and halt the execution in case of an error
validatePolicyPermissions(body);
// update the policy
SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
apiProvider.updatePolicy(subscriptionPolicy);
// update policy permissions
updatePolicyPermissions(body);
// retrieve the new policy and send back as the response
SubscriptionPolicy newSubscriptionPolicy = apiProvider.getSubscriptionPolicy(username, body.getPolicyName());
SubscriptionThrottlePolicyDTO policyDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(newSubscriptionPolicy);
// setting policy permissions
setPolicyPermissionsToDTO(policyDTO);
return Response.ok().entity(policyDTO).build();
} catch (APIManagementException | ParseException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
} else {
String errorMessage = "Error while updating Subscription level policy: " + body.getPolicyName();
throw new APIManagementException(errorMessage, e);
}
}
return null;
}
Aggregations