use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class AbstractAPIManager method searchPaginatedAPIProducts.
@Override
public Map<String, Object> searchPaginatedAPIProducts(String searchQuery, String requestedTenantDomain, int start, int end) throws APIManagementException {
Map<String, Object> result = new HashMap<String, Object>();
boolean isTenantFlowStarted = false;
if (log.isDebugEnabled()) {
log.debug("Original search query received : " + searchQuery);
}
try {
boolean isTenantMode = (requestedTenantDomain != null);
if (isTenantMode && !org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(requestedTenantDomain)) {
isTenantFlowStarted = true;
startTenantFlow(requestedTenantDomain);
} else {
requestedTenantDomain = org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
isTenantFlowStarted = true;
startTenantFlow(requestedTenantDomain);
}
Registry userRegistry;
int tenantIDLocal = 0;
String userNameLocal = this.username;
if ((isTenantMode && this.tenantDomain == null) || (isTenantMode && isTenantDomainNotMatching(requestedTenantDomain))) {
// Tenant store anonymous mode
tenantIDLocal = getTenantManager().getTenantId(requestedTenantDomain);
userRegistry = getRegistryService().getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantIDLocal);
userNameLocal = CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME;
} else {
userRegistry = this.registry;
tenantIDLocal = tenantId;
}
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userNameLocal);
result = searchPaginatedAPIProducts(userRegistry, getSearchQuery(searchQuery), start, end);
} catch (Exception e) {
String msg = "Failed to Search APIs";
throw new APIManagementException(msg, e);
} finally {
if (isTenantFlowStarted) {
endTenantFlow();
}
}
return result;
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIMConfigServiceImpl method updateWorkflowConfig.
@Override
public void updateWorkflowConfig(String organization, String workflowConfig) throws APIManagementException {
if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
APIUtil.loadTenantRegistry(tenantId);
}
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (registry.resourceExists(APIConstants.WORKFLOW_EXECUTOR_LOCATION)) {
Resource resource = registry.get(APIConstants.WORKFLOW_EXECUTOR_LOCATION);
byte[] data = IOUtils.toByteArray(new StringReader(workflowConfig));
resource.setContent(data);
resource.setMediaType(APIConstants.WORKFLOW_MEDIA_TYPE);
registry.put(APIConstants.WORKFLOW_EXECUTOR_LOCATION, resource);
}
} catch (RegistryException | IOException e) {
String msg = "Error while retrieving External Stores Configuration from registry";
log.error(msg, e);
throw new APIManagementException(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIMConfigServiceImpl method addSelfSighupConfig.
@Override
public void addSelfSighupConfig(String organization, String selfSignUpConfig) throws APIManagementException {
if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
APIUtil.loadTenantRegistry(tenantId);
}
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (!registry.resourceExists(APIConstants.SELF_SIGN_UP_CONFIG_LOCATION)) {
byte[] data = IOUtils.toByteArray(new StringReader(selfSignUpConfig));
Resource resource = registry.newResource();
resource.setContent(data);
resource.setMediaType(APIConstants.SELF_SIGN_UP_CONFIG_MEDIA_TYPE);
registry.put(APIConstants.SELF_SIGN_UP_CONFIG_LOCATION, resource);
}
} catch (RegistryException | IOException e) {
String msg = "Error while adding Self-SignUp Configuration from registry";
log.error(msg, e);
throw new APIManagementException(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIMConfigServiceImpl method getExternalStoreConfig.
@Override
public String getExternalStoreConfig(String organization) throws APIManagementException {
if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
APIUtil.loadTenantRegistry(tenantId);
}
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (registry.resourceExists(APIConstants.EXTERNAL_API_STORES_LOCATION)) {
Resource resource = registry.get(APIConstants.EXTERNAL_API_STORES_LOCATION);
return new String((byte[]) resource.getContent(), Charset.defaultCharset());
} else {
return null;
}
} catch (RegistryException e) {
String msg = "Error while retrieving External Stores Configuration from registry";
log.error(msg, e);
throw new APIManagementException(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetDeniedTiers.
@Test
public void testGetDeniedTiers() throws APIManagementException, org.wso2.carbon.user.core.UserStoreException {
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("true", "false");
APIConsumerImpl apiConsumer = new UserAwareAPIConsumerWrapper(userRegistry, apiMgtDAO);
Mockito.when(userRegistry.getUserRealm()).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
String[] currentUserRoles = { "role1", "role2" };
PowerMockito.when(APIUtil.getListOfRoles(Mockito.anyString())).thenReturn(currentUserRoles);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
TierPermissionDTO tierPermissionDTO = new TierPermissionDTO();
TierPermissionDTO tierPermissionDTO1 = new TierPermissionDTO();
tierPermissionDTO.setRoles(new String[] { "role1" });
Set<TierPermissionDTO> tierPermissionDTOs = new HashSet<TierPermissionDTO>();
tierPermissionDTOs.add(tierPermissionDTO);
Mockito.when(apiMgtDAO.getThrottleTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
tierPermissionDTO.setRoles(new String[] { "role3" });
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
Mockito.when(apiMgtDAO.getTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
tierPermissionDTO.setPermissionType(APIConstants.TIER_PERMISSION_ALLOW);
Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[0]);
tierPermissionDTOs.add(tierPermissionDTO1);
tierPermissionDTO1.setRoles(new String[] { "role4" });
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[] { "role1", "role2" });
tierPermissionDTO1.setRoles(new String[] { "role2" });
tierPermissionDTO1.setTierName("Silver");
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 2);
}
Aggregations