feat: Displaying currently logged in user, role and groups.
*/
package com.synopsys.integration.detect.lifecycle.boot.product;
+import java.util.List;
+import java.util.stream.Collectors;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.synopsys.integration.blackduck.api.generated.component.AssignedUserGroup;
import com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery;
import com.synopsys.integration.blackduck.api.generated.response.CurrentVersionView;
+import com.synopsys.integration.blackduck.api.generated.view.RoleAssignmentView;
+import com.synopsys.integration.blackduck.api.generated.view.UserGroupView;
+import com.synopsys.integration.blackduck.api.generated.view.UserView;
import com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig;
import com.synopsys.integration.blackduck.configuration.ConnectionResult;
import com.synopsys.integration.blackduck.service.BlackDuckService;
import com.synopsys.integration.blackduck.service.BlackDuckServicesFactory;
+import com.synopsys.integration.blackduck.service.UserGroupService;
import com.synopsys.integration.detect.exception.DetectUserFriendlyException;
import com.synopsys.integration.detect.exitcode.ExitCodeType;
import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.log.SilentIntLogger;
import com.synopsys.integration.log.Slf4jIntLogger;
+import com.synopsys.integration.polaris.common.service.RoleAssignmentService;
public class BlackDuckConnectivityChecker {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -60,9 +69,21 @@ public class BlackDuckConnectivityChecker {
try {
final BlackDuckService blackDuckService = blackDuckServicesFactory.createBlackDuckService();
final CurrentVersionView currentVersion = blackDuckService.getResponse(ApiDiscovery.CURRENT_VERSION_LINK_RESPONSE);
+
logger.info(String.format("Successfully connected to BlackDuck (version %s)!", currentVersion.getVersion()));
+
+ final UserView userView = blackDuckService.getResponse(ApiDiscovery.CURRENT_USER_LINK_RESPONSE);
+ logger.info("Connected as: " + userView.getUserName());
+
+ final UserGroupService userGroupService = blackDuckServicesFactory.createUserGroupService();
+ List<RoleAssignmentView> response = userGroupService.getRolesForUser(userView);
+ logger.info("Roles: " + response.stream().map(RoleAssignmentView::getName).distinct().collect(Collectors.joining(", ")));
+
+ List<UserGroupView> groups = blackDuckService.getAllResponses(userView.getFirstLink("usergroups").get(), UserGroupView.class);
+ logger.info("Group: " + groups.stream().map(UserGroupView::getName).distinct().collect(Collectors.joining(", ")));
+
} catch (final IntegrationException e) {
- throw new DetectUserFriendlyException("Could not determine which version of Black Duck detect connected to.", e, ExitCodeType.FAILURE_BLACKDUCK_CONNECTIVITY);
+ 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);
