Search in sources :

Example 6 with TierPermissionDTO

use of org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testIsTierDenied.

@Test
public void testIsTierDenied() 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);
    Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(new String[] { "role1", "role2" });
    Assert.assertFalse(apiConsumer.isTierDeneid("tier1"));
    TierPermissionDTO tierPermissionDTO = new TierPermissionDTO();
    tierPermissionDTO.setRoles(new String[] { "role1" });
    Mockito.when(apiMgtDAO.getThrottleTierPermission(Mockito.anyString(), Mockito.anyInt())).thenReturn(tierPermissionDTO);
    Assert.assertTrue(apiConsumer.isTierDeneid("tier1"));
    tierPermissionDTO.setRoles(new String[] { "role3" });
    Assert.assertFalse(apiConsumer.isTierDeneid("tier1"));
    Assert.assertFalse(apiConsumer.isTierDeneid("tier1"));
    tierPermissionDTO.setPermissionType(APIConstants.TIER_PERMISSION_ALLOW);
    Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[0]);
    Assert.assertTrue(apiConsumer.isTierDeneid("tier1"));
}
Also used : TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with TierPermissionDTO

use of org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method getTierPermissions.

public Set<TierPermissionDTO> getTierPermissions(int tenantId) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet resultSet = null;
    Set<TierPermissionDTO> tierPermissions = new HashSet<TierPermissionDTO>();
    try {
        String getTierPermissionQuery = SQLConstants.GET_TIER_PERMISSIONS_SQL;
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(getTierPermissionQuery);
        ps.setInt(1, tenantId);
        resultSet = ps.executeQuery();
        while (resultSet.next()) {
            TierPermissionDTO tierPermission = new TierPermissionDTO();
            tierPermission.setTierName(resultSet.getString("TIER"));
            tierPermission.setPermissionType(resultSet.getString("PERMISSIONS_TYPE"));
            String roles = resultSet.getString("ROLES");
            if (roles != null && !roles.isEmpty()) {
                String[] roleList = roles.split(",");
                tierPermission.setRoles(roleList);
            }
            tierPermissions.add(tierPermission);
        }
    } catch (SQLException e) {
        handleException("Failed to get Tier permission information ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
    }
    return tierPermissions;
}
Also used : TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 8 with TierPermissionDTO

use of org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method getThrottleTierPermissions.

public Set<TierPermissionDTO> getThrottleTierPermissions(int tenantId) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet resultSet = null;
    Set<TierPermissionDTO> tierPermissions = new HashSet<TierPermissionDTO>();
    try {
        String getTierPermissionQuery = SQLConstants.GET_THROTTLE_TIER_PERMISSIONS_SQL;
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(getTierPermissionQuery);
        ps.setInt(1, tenantId);
        resultSet = ps.executeQuery();
        while (resultSet.next()) {
            TierPermissionDTO tierPermission = new TierPermissionDTO();
            tierPermission.setTierName(resultSet.getString("TIER"));
            tierPermission.setPermissionType(resultSet.getString("PERMISSIONS_TYPE"));
            String roles = resultSet.getString("ROLES");
            if (roles != null && !roles.isEmpty()) {
                String[] roleList = roles.split(",");
                tierPermission.setRoles(roleList);
            }
            tierPermissions.add(tierPermission);
        }
    } catch (SQLException e) {
        handleException("Failed to get Tier permission information ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
    }
    return tierPermissions;
}
Also used : TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 9 with TierPermissionDTO

use of org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method getTierPermission.

public TierPermissionDTO getTierPermission(String tierName, int tenantId) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet resultSet = null;
    TierPermissionDTO tierPermission = null;
    try {
        String getTierPermissionQuery = SQLConstants.GET_PERMISSION_OF_TIER_SQL;
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(getTierPermissionQuery);
        ps.setString(1, tierName);
        ps.setInt(2, tenantId);
        resultSet = ps.executeQuery();
        while (resultSet.next()) {
            tierPermission = new TierPermissionDTO();
            tierPermission.setTierName(tierName);
            tierPermission.setPermissionType(resultSet.getString("PERMISSIONS_TYPE"));
            String roles = resultSet.getString("ROLES");
            if (roles != null) {
                String[] roleList = roles.split(",");
                tierPermission.setRoles(roleList);
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get Tier permission information for Tier " + tierName, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
    }
    return tierPermission;
}
Also used : TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 10 with TierPermissionDTO

use of org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO in project carbon-apimgt by wso2.

the class APIConsumerImpl method isTierDeneid.

/**
 * Check whether given Tier is denied for the user
 *
 * @param tierName
 * @return
 * @throws APIManagementException if failed to get the tiers
 */
@Override
public boolean isTierDeneid(String tierName) throws APIManagementException {
    String[] currentUserRoles;
    try {
        if (tenantId != 0) {
            /* Get the roles of the Current User */
            currentUserRoles = ((UserRegistry) ((UserAwareAPIConsumer) this).registry).getUserRealm().getUserStoreManager().getRoleListOfUser(((UserRegistry) this.registry).getUserName());
            TierPermissionDTO tierPermission = apiMgtDAO.getThrottleTierPermission(tierName, tenantId);
            if (tierPermission == null) {
                return false;
            } else {
                List<String> currentRolesList = new ArrayList<String>(Arrays.asList(currentUserRoles));
                List<String> roles = new ArrayList<String>(Arrays.asList(tierPermission.getRoles()));
                currentRolesList.retainAll(roles);
                if (APIConstants.TIER_PERMISSION_ALLOW.equals(tierPermission.getPermissionType())) {
                    if (currentRolesList.isEmpty()) {
                        return true;
                    }
                } else {
                    if (currentRolesList.size() > 0) {
                        return true;
                    }
                }
            }
        }
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        log.error("cannot retrieve user role list for tenant" + tenantDomain, e);
    }
    return false;
}
Also used : TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ArrayList(java.util.ArrayList) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry)

Aggregations

TierPermissionDTO (org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO)11 HashSet (java.util.HashSet)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 LinkedHashSet (java.util.LinkedHashSet)4 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 ArrayList (java.util.ArrayList)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 Matchers.anyString (org.mockito.Matchers.anyString)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 TierPermission (org.wso2.carbon.apimgt.api.model.TierPermission)1 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)1