From 40ee0d377522fdf6b81d5c59125f9ed181fa2a74 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 23 Jun 2019 12:39:44 -0700 Subject: [PATCH] GUACAMOLE-723: Consider entire connection hierarchy when determining whether only one connection is available. --- .../navigation/services/userPageService.js | 135 ++++++++++-------- 1 file changed, 77 insertions(+), 58 deletions(-) diff --git a/guacamole/src/main/webapp/app/navigation/services/userPageService.js b/guacamole/src/main/webapp/app/navigation/services/userPageService.js index 2af426ed4..f91303a85 100644 --- a/guacamole/src/main/webapp/app/navigation/services/userPageService.js +++ b/guacamole/src/main/webapp/app/navigation/services/userPageService.js @@ -67,7 +67,6 @@ angular.module('navigation').factory('userPageService', ['$injector', */ var generateHomePage = function generateHomePage(rootGroups, permissions) { - var homePage = null; var settingsPages = generateSettingsPages(permissions); // If user has access to settings pages, return home page and skip @@ -79,67 +78,87 @@ angular.module('navigation').factory('userPageService', ['$injector', if (settingsPages.length > 2) return SYSTEM_HOME_PAGE; + // If exactly one connection or balancing group is available, use + // that as the home page + var clientPages = service.getClientPages(rootGroups); + return (clientPages.length === 1) ? clientPages[0] : SYSTEM_HOME_PAGE; + + }; + + /** + * Adds to the given array all pages that the current user may use to + * access connections or balancing groups that are descendants of the given + * connection group. + * + * @param {PageDefinition[]} clientPages + * The array that pages should be added to. + * + * @param {String} dataSource + * The data source containing the given connection group. + * + * @param {ConnectionGroup} connectionGroup + * The connection group ancestor of the connection or balancing group + * descendants whose pages should be added to the given array. + */ + var addClientPages = function addClientPages(clientPages, dataSource, connectionGroup) { + + // Add pages for all child connections + angular.forEach(connectionGroup.childConnections, function addConnectionPage(connection) { + clientPages.push(new PageDefinition({ + name : connection.name, + url : '/client/' + ClientIdentifier.toString({ + dataSource : dataSource, + type : ClientIdentifier.Types.CONNECTION, + id : connection.identifier + }) + })); + }); + + // Add pages for all child balancing groups, as well as the connectable + // descendants of all balancing groups of any type + angular.forEach(connectionGroup.childConnectionGroups, function addConnectionGroupPage(connectionGroup) { + + if (connectionGroup.type === ConnectionGroup.Type.BALANCING) { + clientPages.push(new PageDefinition({ + name : connectionGroup.name, + url : '/client/' + ClientIdentifier.toString({ + dataSource : dataSource, + type : ClientIdentifier.Types.CONNECTION_GROUP, + id : connectionGroup.identifier + }) + })); + } + + addClientPages(clientPages, dataSource, connectionGroup); + + }); + + }; + + /** + * Returns a full list of all pages that the current user may use to access + * a connection or balancing group, regardless of the depth of those + * connections/groups within the connection hierarchy. + * + * @param {Object.} rootGroups + * A map of all root connection groups visible to the current user, + * where each key is the identifier of the corresponding data source. + * + * @returns {PageDefinition[]} + * A list of all pages that the current user may use to access a + * connection or balancing group. + */ + service.getClientPages = function getClientPages(rootGroups) { + + var clientPages = []; + // Determine whether a connection or balancing group should serve as // the home page for (var dataSource in rootGroups) { + addClientPages(clientPages, dataSource, rootGroups[dataSource]); + } - // Get corresponding root group - var rootGroup = rootGroups[dataSource]; - - // Get children - var connections = rootGroup.childConnections || []; - var connectionGroups = rootGroup.childConnectionGroups || []; - - // Calculate total number of root-level objects - var totalRootObjects = connections.length + connectionGroups.length; - - // If exactly one connection or balancing group is available, use - // that as the home page - if (homePage === null && totalRootObjects === 1) { - - var connection = connections[0]; - var connectionGroup = connectionGroups[0]; - - // Only one connection present, use as home page - if (connection) { - homePage = new PageDefinition({ - name : connection.name, - url : '/client/' + ClientIdentifier.toString({ - dataSource : dataSource, - type : ClientIdentifier.Types.CONNECTION, - id : connection.identifier - }) - }); - } - - // Only one balancing group present, use as home page - if (connectionGroup - && connectionGroup.type === ConnectionGroup.Type.BALANCING - && _.isEmpty(connectionGroup.childConnections) - && _.isEmpty(connectionGroup.childConnectionGroups)) { - homePage = new PageDefinition({ - name : connectionGroup.name, - url : '/client/' + ClientIdentifier.toString({ - dataSource : dataSource, - type : ClientIdentifier.Types.CONNECTION_GROUP, - id : connectionGroup.identifier - }) - }); - } - - } - - // Otherwise, a connection or balancing group cannot serve as the - // home page - else if (totalRootObjects >= 1) { - homePage = null; - break; - } - - } // end for each data source - - // Use default home page if no other is available - return homePage || SYSTEM_HOME_PAGE; + return clientPages; };