diff --git a/guacamole/src/main/webapp/app/home/controllers/homeController.js b/guacamole/src/main/webapp/app/home/controllers/homeController.js index 82782be95..95ef1e525 100644 --- a/guacamole/src/main/webapp/app/home/controllers/homeController.js +++ b/guacamole/src/main/webapp/app/home/controllers/homeController.js @@ -30,10 +30,8 @@ angular.module('home').controller('homeController', ['$scope', '$injector', var ConnectionGroup = $injector.get("ConnectionGroup"); // Get required services - var $location = $injector.get("$location"); var authenticationService = $injector.get("authenticationService"); var connectionGroupService = $injector.get("connectionGroupService"); - var permissionService = $injector.get("permissionService"); var userPageService = $injector.get("userPageService"); /** @@ -44,14 +42,6 @@ angular.module('home').controller('homeController', ['$scope', '$injector', */ $scope.rootConnectionGroup = null; - /** - * All permissions associated with the current user, or null if the user's - * permissions have not yet been loaded. - * - * @type PermissionSet - */ - $scope.permissions = null; - /** * Returns whether critical data has completed being loaded. * @@ -61,27 +51,20 @@ angular.module('home').controller('homeController', ['$scope', '$injector', */ $scope.isLoaded = function isLoaded() { - return $scope.rootConnectionGroup !== null - && $scope.permissions !== null; + return $scope.rootConnectionGroup !== null; }; // Retrieve root group and all descendants connectionGroupService.getConnectionGroupTree(ConnectionGroup.ROOT_IDENTIFIER) .success(function rootGroupRetrieved(rootConnectionGroup) { - $scope.rootConnectionGroup = rootConnectionGroup; + }); - // Navigate to home page, if not already there - var homePage = userPageService.getHomePage(rootConnectionGroup); + // Navigate to home page, if not already there + userPageService.getHomePage() + .then(function homePageRetrieved(homePage) { $location.url(homePage.url); - }); - - // Retrieve current permissions - permissionService.getPermissions(authenticationService.getCurrentUserID()) - .success(function permissionsRetrieved(permissions) { - $scope.permissions = permissions; - }); - + }]); diff --git a/guacamole/src/main/webapp/app/navigation/directives/guacUserMenu.js b/guacamole/src/main/webapp/app/navigation/directives/guacUserMenu.js index 847c0d097..c9c09f7d2 100644 --- a/guacamole/src/main/webapp/app/navigation/directives/guacUserMenu.js +++ b/guacamole/src/main/webapp/app/navigation/directives/guacUserMenu.js @@ -36,18 +36,16 @@ angular.module('navigation').directive('guacUserMenu', [function guacUserMenu() controller: ['$scope', '$injector', '$element', function guacUserMenuController($scope, $injector, $element) { // Get required types - var ConnectionGroup = $injector.get('ConnectionGroup'); - var PermissionSet = $injector.get('PermissionSet'); + var PermissionSet = $injector.get('PermissionSet'); // Get required services - var $document = $injector.get('$document'); - var $location = $injector.get('$location'); - var authenticationService = $injector.get('authenticationService'); - var connectionGroupService = $injector.get("connectionGroupService"); - var guacNotification = $injector.get('guacNotification'); - var permissionService = $injector.get("permissionService"); - var userService = $injector.get('userService'); - var userPageService = $injector.get('userPageService'); + var $document = $injector.get('$document'); + var $location = $injector.get('$location'); + var authenticationService = $injector.get('authenticationService'); + var guacNotification = $injector.get('guacNotification'); + var permissionService = $injector.get("permissionService"); + var userService = $injector.get('userService'); + var userPageService = $injector.get('userPageService'); /** * An action to be provided along with the object sent to @@ -75,22 +73,6 @@ angular.module('navigation').directive('guacUserMenu', [function guacUserMenu() */ var document = $document[0]; - /** - * The root connection group, or null if the connection group hierarchy has - * not yet been loaded. - * - * @type ConnectionGroup - */ - var rootConnectionGroup = null; - - /** - * All permissions associated with the current user, or null if the user's - * permissions have not yet been loaded. - * - * @type PermissionSet - */ - var permissions = null; - /** * Whether the current user has sufficient permissions to change * his/her own password. If permissions have not yet been loaded, @@ -143,49 +125,22 @@ angular.module('navigation').directive('guacUserMenu', [function guacUserMenu() */ $scope.pages = null; - /** - * Updates the visible menu items based on the permissions and root - * group on the scope, if available. If either the permissions or - * the root group are not yet available, this function has no - * effect. - */ - var updateMenuItems = function updateMenuItems() { - - // Menu items are unknown until permissions and rootConnectionGroup are both available - if (!permissions || !rootConnectionGroup) { - $scope.canChangePassword = null; - $scope.pages = []; - return; - } - - // Retrieve the main pages from the user page service - $scope.pages = userPageService.getMainPages(rootConnectionGroup, permissions); - + // Retrieve current permissions + permissionService.getPermissions(authenticationService.getCurrentUserID()) + .success(function permissionsRetrieved(permissions) { + // Check whether the current user can change their own password $scope.canChangePassword = PermissionSet.hasUserPermission( permissions, PermissionSet.ObjectPermissionType.UPDATE, authenticationService.getCurrentUserID() ); - }; - // Retrieve root group and all descendants - connectionGroupService.getConnectionGroupTree(ConnectionGroup.ROOT_IDENTIFIER) - .success(function rootConnectionGroupRetrieved(retrievedRootConnectionGroup) { - - rootConnectionGroup = retrievedRootConnectionGroup; - - // Navigate to home page, if not already there - var homePage = userPageService.getHomePage(rootConnectionGroup); - $location.url(homePage.url); - - updateMenuItems(); }); - - // Retrieve current permissions - permissionService.getPermissions(authenticationService.getCurrentUserID()) - .success(function permissionsRetrieved(retrievedPermissions) { - permissions = retrievedPermissions; - updateMenuItems(); + + // Retrieve the main pages from the user page service + userPageService.getMainPages() + .then(function retrievedMainPages(pages) { + $scope.pages = pages; }); /** diff --git a/guacamole/src/main/webapp/app/navigation/services/userPageService.js b/guacamole/src/main/webapp/app/navigation/services/userPageService.js index 8a6d37b4f..90e6710a4 100644 --- a/guacamole/src/main/webapp/app/navigation/services/userPageService.js +++ b/guacamole/src/main/webapp/app/navigation/services/userPageService.js @@ -31,7 +31,10 @@ angular.module('navigation').factory('userPageService', ['$injector', var PermissionSet = $injector.get('PermissionSet'); // Get required services - var authenticationService = $injector.get('authenticationService'); + var $q = $injector.get('$q'); + var authenticationService = $injector.get('authenticationService'); + var connectionGroupService = $injector.get("connectionGroupService"); + var permissionService = $injector.get("permissionService"); var service = {}; @@ -52,7 +55,7 @@ angular.module('navigation').factory('userPageService', ['$injector', this.name = name; this.url = url; }; - + /** * Returns an appropriate home page for the current user. * @@ -62,7 +65,7 @@ angular.module('navigation').factory('userPageService', ['$injector', * @returns {Page} * The user's home page. */ - service.getHomePage = function getHomePage(rootGroup) { + var generateHomePage = function generateHomePage(rootGroup) { // Get children var connections = rootGroup.childConnections || []; @@ -96,7 +99,7 @@ angular.module('navigation').factory('userPageService', ['$injector', } - // Default home page + // Resolve promise with default home page return new Page( 'USER_MENU.ACTION_NAVIGATE_HOME', '/' @@ -104,6 +107,27 @@ angular.module('navigation').factory('userPageService', ['$injector', }; + /** + * Returns a promise which resolves with an appropriate home page for the + * current user. + * + * @returns {Promise.} + * A promise which resolves with the user's default home page. + */ + service.getHomePage = function getHomePage() { + + var deferred = $q.defer(); + + // Resolve promise using home page derived from root connection group + connectionGroupService.getConnectionGroupTree(ConnectionGroup.ROOT_IDENTIFIER) + .success(function rootConnectionGroupRetrieved(rootGroup) { + deferred.resolve(generateHomePage(rootGroup)); + }); + + return deferred.promise; + + }; + /** * Returns all the main pages that the current user can visit. This can * include the home page, manage pages, etc. In the case that there are no @@ -115,10 +139,10 @@ angular.module('navigation').factory('userPageService', ['$injector', * @param {PermissionSet} permissions * The permissions for the current user. * - * @returns {Array} - * An array of objects like this + * @returns {Page[]} + * An array of all main pages that the current user can visit. */ - service.getMainPages = function getMainPages(rootGroup, permissions) { + var generateMainPages = function generateMainPages(rootGroup, permissions) { var pages = []; @@ -172,7 +196,7 @@ angular.module('navigation').factory('userPageService', ['$injector', PermissionSet.hasSystemPermission(permissions, PermissionSet.SystemPermissionType.ADMINISTER); // Add home page - pages.push(service.getHomePage(rootGroup)); + pages.push(generateHomePage(rootGroup)); // If user can manage users, add link to user management page if (canManageUsers) { @@ -200,7 +224,51 @@ angular.module('navigation').factory('userPageService', ['$injector', return pages; }; - + + /** + * Returns a promise which resolves to an array of all main pages that the + * current user can visit. This can include the home page, manage pages, + * etc. In the case that there are no applicable pages of this sort, it may + * return a client page. + * + * @returns {Promise.} + * A promise which resolves to an array of all main pages that the + * current user can visit. + */ + service.getMainPages = function getMainPages() { + + var deferred = $q.defer(); + + var rootGroup = null; + var permissions = null; + + /** + * Resolves the main pages retrieval promise, if possible. If + * insufficient data is available, this function does nothing. + */ + var resolveMainPages = function resolveMainPages() { + if (rootGroup && permissions) + deferred.resolve(generateMainPages(rootGroup, permissions)); + }; + + // Retrieve root group, resolving main pages if possible + connectionGroupService.getConnectionGroupTree(ConnectionGroup.ROOT_IDENTIFIER) + .success(function rootConnectionGroupRetrieved(retrievedRootGroup) { + rootGroup = retrievedRootGroup; + resolveMainPages(); + }); + + // Retrieve current permissions, resolving main pages if possible + permissionService.getPermissions(authenticationService.getCurrentUserID()) + .success(function permissionsRetrieved(retrievedPermissions) { + permissions = retrievedPermissions; + resolveMainPages(); + }); + + return deferred.promise; + + }; + return service; }]); \ No newline at end of file