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"));
}
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;
}
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;
}
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;
}
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;
}
Aggregations