Search in sources :

Example 1 with UserGroupView

use of com.synopsys.integration.blackduck.api.generated.view.UserGroupView in project blackduck-common by blackducksoftware.

the class ProjectUsersService method getAllActiveUsersForProject.

/**
 * This will get all explicitly assigned users for a project, as well as all users who are assigned to groups that are explicitly assigned to a project.
 */
public Set<UserView> getAllActiveUsersForProject(ProjectView projectView) throws IntegrationException {
    Set<UserView> users = new HashSet<>();
    List<AssignedUserGroupView> assignedGroups = getAssignedGroupsToProject(projectView);
    for (AssignedUserGroupView assignedUserGroupView : assignedGroups) {
        if (assignedUserGroupView.getActive()) {
            HttpUrl groupUrl = new HttpUrl(assignedUserGroupView.getGroup());
            UserGroupView userGroupView = blackDuckApiClient.getResponse(groupUrl, UserGroupView.class);
            if (userGroupView.getActive()) {
                List<UserView> groupUsers = blackDuckApiClient.getAllResponses(userGroupView.metaUsersLink());
                users.addAll(groupUsers);
            }
        }
    }
    List<AssignedUserView> assignedUsers = getAssignedUsersToProject(projectView);
    for (AssignedUserView assignedUser : assignedUsers) {
        HttpUrl userUrl = new HttpUrl(assignedUser.getUser());
        UserView userView = blackDuckApiClient.getResponse(userUrl, UserView.class);
        users.add(userView);
    }
    return users.stream().filter(UserView::getActive).collect(Collectors.toSet());
}
Also used : UserGroupView(com.synopsys.integration.blackduck.api.generated.view.UserGroupView) AssignedUserGroupView(com.synopsys.integration.blackduck.api.manual.temporary.response.AssignedUserGroupView) AssignedUserGroupView(com.synopsys.integration.blackduck.api.manual.temporary.response.AssignedUserGroupView) AssignedUserView(com.synopsys.integration.blackduck.api.manual.temporary.view.AssignedUserView) UserView(com.synopsys.integration.blackduck.api.generated.view.UserView) AssignedUserView(com.synopsys.integration.blackduck.api.manual.temporary.view.AssignedUserView) HttpUrl(com.synopsys.integration.rest.HttpUrl) HashSet(java.util.HashSet)

Example 2 with UserGroupView

use of com.synopsys.integration.blackduck.api.generated.view.UserGroupView in project blackduck-common by blackducksoftware.

the class ProjectUsersService method addGroupToProject.

public void addGroupToProject(ProjectView projectView, String groupName) throws IntegrationException {
    Optional<UserGroupView> optionalUserGroupView = userGroupService.getGroupByName(groupName);
    UserGroupView userGroupView = optionalUserGroupView.orElseThrow(() -> new IntegrationException(String.format("The supplied group name (%s) does not exist.", groupName)));
    HttpUrl userGroupUrl = userGroupView.getHref();
    HttpUrl createUrl = projectView.getFirstLink(ProjectView.USERGROUPS_LINK);
    AssignedUserGroupRequest userGroupRequest = new AssignedUserGroupRequest();
    userGroupRequest.setGroup(userGroupUrl.string());
    blackDuckApiClient.post(createUrl, userGroupRequest);
}
Also used : AssignedUserGroupRequest(com.synopsys.integration.blackduck.api.manual.temporary.component.AssignedUserGroupRequest) UserGroupView(com.synopsys.integration.blackduck.api.generated.view.UserGroupView) AssignedUserGroupView(com.synopsys.integration.blackduck.api.manual.temporary.response.AssignedUserGroupView) IntegrationException(com.synopsys.integration.exception.IntegrationException) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 3 with UserGroupView

use of com.synopsys.integration.blackduck.api.generated.view.UserGroupView in project blackduck-common by blackducksoftware.

the class ProjectUsersService method getGroupsForProject.

public List<UserGroupView> getGroupsForProject(ProjectView project) throws IntegrationException {
    logger.debug("Attempting to get the assigned users for Project: " + project.getName());
    List<AssignedUserGroupView> assignedGroups = getAssignedGroupsToProject(project);
    List<UserGroupView> resolvedGroupViews = new ArrayList<>();
    for (AssignedUserGroupView assigned : assignedGroups) {
        HttpUrl groupUrl = new HttpUrl(assigned.getGroup());
        UserGroupView groupView = blackDuckApiClient.getResponse(groupUrl, UserGroupView.class);
        if (groupView != null) {
            resolvedGroupViews.add(groupView);
        }
    }
    return resolvedGroupViews;
}
Also used : UserGroupView(com.synopsys.integration.blackduck.api.generated.view.UserGroupView) AssignedUserGroupView(com.synopsys.integration.blackduck.api.manual.temporary.response.AssignedUserGroupView) AssignedUserGroupView(com.synopsys.integration.blackduck.api.manual.temporary.response.AssignedUserGroupView) ArrayList(java.util.ArrayList) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 4 with UserGroupView

use of com.synopsys.integration.blackduck.api.generated.view.UserGroupView in project synopsys-detect by blackducksoftware.

the class BlackDuckConnectivityChecker method determineConnectivity.

public BlackDuckConnectivityResult determineConnectivity(BlackDuckServerConfig blackDuckServerConfig) throws DetectUserFriendlyException {
    logger.debug("Detect will check communication with the Black Duck server.");
    ConnectionResult connectionResult = blackDuckServerConfig.attemptConnection(new SilentIntLogger());
    if (connectionResult.isFailure()) {
        // TODO: For the logs, when connection result returns the client, can drop this.
        blackDuckServerConfig.attemptConnection(new Slf4jIntLogger(logger));
        logger.error("Failed to connect to the Black Duck server");
        return BlackDuckConnectivityResult.failure(connectionResult.getFailureMessage().orElse("Could not reach the Black Duck server or the credentials were invalid."));
    }
    BlackDuckServicesFactory blackDuckServicesFactory = blackDuckServerConfig.createBlackDuckServicesFactory(new Slf4jIntLogger(logger));
    BlackDuckRegistrationService blackDuckRegistrationService = blackDuckServicesFactory.createBlackDuckRegistrationService();
    UserService userService = blackDuckServicesFactory.createUserService();
    try {
        String version = blackDuckRegistrationService.getBlackDuckServerData().getVersion();
        logger.info(String.format("Successfully connected to Black Duck (version %s)!", version));
        if (logger.isDebugEnabled()) {
            // These (particularly fetching roles) can be very slow operations
            UserView userView = userService.findCurrentUser();
            logger.debug("Connected as: " + userView.getUserName());
            UserGroupService userGroupService = blackDuckServicesFactory.createUserGroupService();
            List<RoleAssignmentView> roles = userGroupService.getRolesForUser(userView);
            logger.debug("Roles: " + roles.stream().map(RoleAssignmentView::getName).distinct().collect(Collectors.joining(", ")));
            BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
            List<UserGroupView> groups = blackDuckApiClient.getAllResponses(userView.metaMultipleResponses(USERGROUPS));
            logger.debug("Group: " + groups.stream().map(UserGroupView::getName).distinct().collect(Collectors.joining(", ")));
        }
    } catch (IntegrationException e) {
        throw new DetectUserFriendlyException("Could not determine which version of Black Duck detect connected to or which user is connecting.", e, ExitCodeType.FAILURE_BLACKDUCK_CONNECTIVITY);
    }
    return BlackDuckConnectivityResult.success(blackDuckServicesFactory, blackDuckServerConfig);
}
Also used : IntegrationException(com.synopsys.integration.exception.IntegrationException) UserService(com.synopsys.integration.blackduck.service.dataservice.UserService) RoleAssignmentView(com.synopsys.integration.blackduck.api.generated.view.RoleAssignmentView) UserView(com.synopsys.integration.blackduck.api.generated.view.UserView) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) BlackDuckRegistrationService(com.synopsys.integration.blackduck.service.dataservice.BlackDuckRegistrationService) SilentIntLogger(com.synopsys.integration.log.SilentIntLogger) UserGroupView(com.synopsys.integration.blackduck.api.generated.view.UserGroupView) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) UserGroupService(com.synopsys.integration.blackduck.service.dataservice.UserGroupService) ConnectionResult(com.synopsys.integration.rest.client.ConnectionResult)

Example 5 with UserGroupView

use of com.synopsys.integration.blackduck.api.generated.view.UserGroupView in project blackduck-common by blackducksoftware.

the class UserGroupService method getGroupByName.

public Optional<UserGroupView> getGroupByName(String groupName) throws IntegrationException {
    List<UserGroupView> allGroups = blackDuckApiClient.getAllResponses(apiDiscovery.metaUsergroupsLink());
    for (UserGroupView group : allGroups) {
        if (group.getName().equalsIgnoreCase(groupName)) {
            return Optional.of(group);
        }
    }
    logger.error(String.format("The group (%s) does not exist.", groupName));
    return Optional.empty();
}
Also used : UserGroupView(com.synopsys.integration.blackduck.api.generated.view.UserGroupView)

Aggregations

UserGroupView (com.synopsys.integration.blackduck.api.generated.view.UserGroupView)7 HttpUrl (com.synopsys.integration.rest.HttpUrl)4 AssignedUserGroupView (com.synopsys.integration.blackduck.api.manual.temporary.response.AssignedUserGroupView)3 UserView (com.synopsys.integration.blackduck.api.generated.view.UserView)2 IntegrationException (com.synopsys.integration.exception.IntegrationException)2 ProjectView (com.synopsys.integration.blackduck.api.generated.view.ProjectView)1 RoleAssignmentView (com.synopsys.integration.blackduck.api.generated.view.RoleAssignmentView)1 AssignedUserGroupRequest (com.synopsys.integration.blackduck.api.manual.temporary.component.AssignedUserGroupRequest)1 UserGroupRequest (com.synopsys.integration.blackduck.api.manual.temporary.component.UserGroupRequest)1 AssignedUserView (com.synopsys.integration.blackduck.api.manual.temporary.view.AssignedUserView)1 BlackDuckIntegrationException (com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException)1 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)1 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)1 BlackDuckRegistrationService (com.synopsys.integration.blackduck.service.dataservice.BlackDuckRegistrationService)1 UserGroupService (com.synopsys.integration.blackduck.service.dataservice.UserGroupService)1 UserService (com.synopsys.integration.blackduck.service.dataservice.UserService)1 DetectUserFriendlyException (com.synopsys.integration.detect.configuration.DetectUserFriendlyException)1 SilentIntLogger (com.synopsys.integration.log.SilentIntLogger)1 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)1 ConnectionResult (com.synopsys.integration.rest.client.ConnectionResult)1