Java Code Examples for org.wso2.carbon.apimgt.impl.utils.APIUtil#getTenantDomainFromTenantId()

The following examples show how to use org.wso2.carbon.apimgt.impl.utils.APIUtil#getTenantDomainFromTenantId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestApiAdminUtils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether given policy is allowed to access to user
 *
 * @param user   username with tenant domain
 * @param policy policy to check
 * @return true if user is allowed to access the policy
 */
public static boolean isPolicyAccessibleToUser(String user, Policy policy) {
    //This block checks whether policy's tenant domain and user's tenant domain are same
    String userTenantDomain = MultitenantUtils.getTenantDomain(user);
    if (!StringUtils.isBlank(policy.getTenantDomain())) {
        return policy.getTenantDomain().equals(userTenantDomain);
    } else {
        String tenantDomainFromId = APIUtil.getTenantDomainFromTenantId(policy.getTenantId());
        return !StringUtils.isBlank(tenantDomainFromId) && tenantDomainFromId.equals(userTenantDomain);
    }
}
 
Example 2
Source File: SubscriptionValidationDAO.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public List<URLMapping> getAllURLMappings(int tenantId) {

        List<URLMapping> urlMappings = new ArrayList<>();
        String sql = SubscriptionValidationSQLConstants.GET_TENANT_API_URL_MAPPING_SQL;
        String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
        String contextParam = null;
        if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
            sql = SubscriptionValidationSQLConstants.GET_ST_API_URL_MAPPING_SQL;
            contextParam = "%/t/%";
        } else if (tenantId > 0) {
            contextParam = "%" + tenantDomain + "%";
        } else {
            sql = SubscriptionValidationSQLConstants.GET_ALL_API_URL_MAPPING_SQL;
        }
        try (Connection conn = APIMgtDBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql);
        ) {
            if (contextParam != null) {
                ps.setString(1, contextParam);
            }
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                URLMapping urlMapping = new URLMapping();
                urlMapping.setAuthScheme(resultSet.getString("AUTH_SCHEME"));
                urlMapping.setHttpMethod(resultSet.getString("HTTP_METHOD"));
                urlMapping.setThrottlingPolicy(resultSet.getString("POLICY"));
                urlMappings.add(urlMapping);
            }
        } catch (SQLException e) {
            log.error("Error in loading URLMappings for tenantId : " + tenantId, e);
        }

        return urlMappings;
    }
 
Example 3
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Check whether the given shared scope name exists in the tenant domain.
 * If the scope does not exists in API-M (AM_DB) as a shared scope, check the existence of scope name in the KM.
 *
 * @param scopeKey     candidate scope key
 * @param tenantid     tenant id
 * @return true if the scope key is already available
 * @throws APIManagementException if failed to check the context availability
 */
@Override
public boolean isScopeKeyExist(String scopeKey, int tenantid) throws APIManagementException {

    String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantid);
    if (!apiMgtDAO.isSharedScopeExists(scopeKey, tenantid)) {
        return KeyManagerHolder.getKeyManagerInstance(tenantDomain).isScopeExists(scopeKey);
    }
    if (log.isDebugEnabled()) {
        log.debug("Scope name: " + scopeKey + " exists as a shared scope in tenant: " + tenantDomain);
    }
    return true;
}
 
Example 4
Source File: RestApiAdminUtils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether given policy is allowed to access to user
 * 
 * @param user username with tenant domain
 * @param policy policy to check
 * @return true if user is allowed to access the policy
 */
public static boolean isPolicyAccessibleToUser(String user, Policy policy) {
    //This block checks whether policy's tenant domain and user's tenant domain are same
    String userTenantDomain = MultitenantUtils.getTenantDomain(user);
    if (!StringUtils.isBlank(policy.getTenantDomain())) {
        return policy.getTenantDomain().equals(userTenantDomain);
    } else {
        String tenantDomainFromId = APIUtil.getTenantDomainFromTenantId(policy.getTenantId());
        return !StringUtils.isBlank(tenantDomainFromId) && tenantDomainFromId.equals(userTenantDomain);
    }
}