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

The following examples show how to use org.wso2.carbon.apimgt.impl.utils.APIUtil#getListOfRoles() . 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: APIAdminImpl.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * This method used to check the existence of the scope name for the particular user
 *
 * @param username      username to be validated
 * @param scopeName     scope to be validated
 * @throws APIManagementException
 */
public boolean isScopeExistsForUser(String username, String scopeName) throws APIManagementException {
    if (APIUtil.isUserExist(username)){
        Map<String, String> scopeRoleMapping =
                APIUtil.getRESTAPIScopesForTenant(MultitenantUtils.getTenantDomain(username));
        if (scopeRoleMapping.containsKey(scopeName)) {
            String[] userRoles = APIUtil.getListOfRoles(username);
            return getRoleScopeList(userRoles,scopeRoleMapping).contains(scopeName);
        } else {
            throw new APIManagementException("Scope Not Found.  Scope : " + scopeName + ",",
                    ExceptionCodes.SCOPE_NOT_FOUND);
        }
    } else {
        throw new APIManagementException("User Not Found. Username :" + username + ",",
                ExceptionCodes.USER_NOT_FOUND);
     }
}
 
Example 2
Source File: RestApiPublisherUtils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * To validate the roles against user roles and tenant roles.
 *
 * @param inputRoles Input roles.
 * @return relevant error string or empty string.
 * @throws APIManagementException API Management Exception.
 */
public static String validateUserRoles(List<String> inputRoles) throws APIManagementException {

    String userName = RestApiUtil.getLoggedInUsername();
    String[] tenantRoleList = APIUtil.getRoleNames(userName);
    boolean isMatched = false;
    String[] userRoleList = null;

    if (APIUtil.hasPermission(userName, APIConstants.Permissions.APIM_ADMIN)) {
        isMatched = true;
    } else {
        userRoleList = APIUtil.getListOfRoles(userName);
    }
    if (inputRoles != null && !inputRoles.isEmpty()) {
        if (tenantRoleList != null || userRoleList != null) {
            for (String inputRole : inputRoles) {
                if (!isMatched && userRoleList != null && APIUtil.compareRoleList(userRoleList, inputRole)) {
                    isMatched = true;
                }
                if (tenantRoleList != null && !APIUtil.compareRoleList(tenantRoleList, inputRole)) {
                    return "Invalid user roles found in accessControlRole list";
                }
            }
            return isMatched ? "" : "This user does not have at least one role specified in API access control.";
        } else {
            return "Invalid user roles found";
        }
    }
    return "";
}