Search in sources :

Example 76 with Permission

use of org.wso2.carbon.user.api.Permission 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 77 with Permission

use of org.wso2.carbon.user.api.Permission in project carbon-apimgt by wso2.

the class RestApiUtilTest method testHandleAuthorizationFailureArg.

@Test
public void testHandleAuthorizationFailureArg() {
    String apiId = "testapiid_4567ui456789";
    String expectedErrormessage = "You don't have permission to access the " + RestApiConstants.RESOURCE_API + " " + "with Id " + apiId;
    APIManagementException apiManagementException = new APIManagementException("API management exception test");
    Log log = Mockito.mock(Log.class);
    PowerMockito.mockStatic(LogFactory.class);
    PowerMockito.when(LogFactory.getLog(Mockito.any(Class.class))).thenReturn(log);
    try {
        RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, apiManagementException, log);
    } catch (ForbiddenException exception) {
        Assert.assertEquals(expectedErrormessage, exception.getMessage());
        Mockito.verify(log).error(expectedErrormessage, apiManagementException);
    }
}
Also used : ForbiddenException(org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Log(org.apache.commons.logging.Log) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 78 with Permission

use of org.wso2.carbon.user.api.Permission in project carbon-apimgt by wso2.

the class RestApiUtilTest method testbuildForbiddenException.

@Test
public void testbuildForbiddenException() {
    String apiId = "TesT_API_ID_45678";
    String expectedErrormessage = "You don't have permission to access the " + RestApiConstants.RESOURCE_API + " " + "with Id " + apiId;
    ForbiddenException forbiddenException = RestApiUtil.buildForbiddenException(RestApiConstants.RESOURCE_API, apiId);
    Assert.assertEquals(expectedErrormessage, forbiddenException.getMessage());
}
Also used : ForbiddenException(org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 79 with Permission

use of org.wso2.carbon.user.api.Permission in project carbon-apimgt by wso2.

the class RestApiUtilTest method testbuildForbiddenExceptionWithEmptyID.

@Test
public void testbuildForbiddenExceptionWithEmptyID() {
    String expectedErrormessage = "You don't have permission to access the " + RestApiConstants.RESOURCE_API;
    ForbiddenException forbiddenException = RestApiUtil.buildForbiddenException(RestApiConstants.RESOURCE_API, "");
    Assert.assertEquals(expectedErrormessage, forbiddenException.getMessage());
}
Also used : ForbiddenException(org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 80 with Permission

use of org.wso2.carbon.user.api.Permission in project carbon-apimgt by wso2.

the class APIProviderImpl method isAPIUpdateValid.

public boolean isAPIUpdateValid(API api) throws APIManagementException {
    String apiSourcePath = APIUtil.getAPIPath(api.getId());
    boolean isValid = false;
    try {
        Resource apiSourceArtifact = registry.get(apiSourcePath);
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when checking validity of API update for " + api.getId().getApiName();
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact artifact = artifactManager.getGenericArtifact(apiSourceArtifact.getUUID());
        String status = APIUtil.getLcStateFromArtifact(artifact);
        if (!APIConstants.CREATED.equals(status) && !APIConstants.PROTOTYPED.equals(status)) {
            // api at least is in published status
            if (APIUtil.hasPermission(getUserNameWithoutChange(), APIConstants.Permissions.API_PUBLISH)) {
                // user has publish permission
                isValid = true;
            }
        } else if (APIConstants.CREATED.equals(status) || APIConstants.PROTOTYPED.equals(status)) {
            // api in create status
            if (APIUtil.hasPermission(getUserNameWithoutChange(), APIConstants.Permissions.API_CREATE) || APIUtil.hasPermission(getUserNameWithoutChange(), APIConstants.Permissions.API_PUBLISH)) {
                // user has creat or publish permission
                isValid = true;
            }
        }
    } catch (RegistryException ex) {
        handleException("Error while validate user for API publishing", ex);
    }
    return isValid;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)29 HashSet (java.util.HashSet)18 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)16 Test (org.testng.annotations.Test)16 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)16 Test (org.junit.Test)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 API (org.wso2.carbon.apimgt.core.models.API)15 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)15 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)15 UserStoreException (org.wso2.carbon.user.api.UserStoreException)13 Map (java.util.Map)12 Resource (org.wso2.carbon.registry.core.Resource)12 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)12 API (org.wso2.carbon.apimgt.api.model.API)11 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)10 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)9 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)9 BusinessInformation (org.wso2.carbon.apimgt.core.models.BusinessInformation)8