GUAC-1126: Query root group and permissions directly within user page service. Return promises instead of pages.

This commit is contained in:
Michael Jumper
2015-04-06 14:32:55 -07:00
parent cff71345d1
commit 9c7e98c97e
3 changed files with 100 additions and 94 deletions

View File

@@ -30,10 +30,8 @@ angular.module('home').controller('homeController', ['$scope', '$injector',
var ConnectionGroup = $injector.get("ConnectionGroup"); var ConnectionGroup = $injector.get("ConnectionGroup");
// Get required services // Get required services
var $location = $injector.get("$location");
var authenticationService = $injector.get("authenticationService"); var authenticationService = $injector.get("authenticationService");
var connectionGroupService = $injector.get("connectionGroupService"); var connectionGroupService = $injector.get("connectionGroupService");
var permissionService = $injector.get("permissionService");
var userPageService = $injector.get("userPageService"); var userPageService = $injector.get("userPageService");
/** /**
@@ -44,14 +42,6 @@ angular.module('home').controller('homeController', ['$scope', '$injector',
*/ */
$scope.rootConnectionGroup = null; $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. * Returns whether critical data has completed being loaded.
* *
@@ -61,27 +51,20 @@ angular.module('home').controller('homeController', ['$scope', '$injector',
*/ */
$scope.isLoaded = function isLoaded() { $scope.isLoaded = function isLoaded() {
return $scope.rootConnectionGroup !== null return $scope.rootConnectionGroup !== null;
&& $scope.permissions !== null;
}; };
// Retrieve root group and all descendants // Retrieve root group and all descendants
connectionGroupService.getConnectionGroupTree(ConnectionGroup.ROOT_IDENTIFIER) connectionGroupService.getConnectionGroupTree(ConnectionGroup.ROOT_IDENTIFIER)
.success(function rootGroupRetrieved(rootConnectionGroup) { .success(function rootGroupRetrieved(rootConnectionGroup) {
$scope.rootConnectionGroup = rootConnectionGroup; $scope.rootConnectionGroup = rootConnectionGroup;
});
// Navigate to home page, if not already there // Navigate to home page, if not already there
var homePage = userPageService.getHomePage(rootConnectionGroup); userPageService.getHomePage()
.then(function homePageRetrieved(homePage) {
$location.url(homePage.url); $location.url(homePage.url);
}); });
// Retrieve current permissions
permissionService.getPermissions(authenticationService.getCurrentUserID())
.success(function permissionsRetrieved(permissions) {
$scope.permissions = permissions;
});
}]); }]);

View File

@@ -36,18 +36,16 @@ angular.module('navigation').directive('guacUserMenu', [function guacUserMenu()
controller: ['$scope', '$injector', '$element', function guacUserMenuController($scope, $injector, $element) { controller: ['$scope', '$injector', '$element', function guacUserMenuController($scope, $injector, $element) {
// Get required types // Get required types
var ConnectionGroup = $injector.get('ConnectionGroup'); var PermissionSet = $injector.get('PermissionSet');
var PermissionSet = $injector.get('PermissionSet');
// Get required services // Get required services
var $document = $injector.get('$document'); var $document = $injector.get('$document');
var $location = $injector.get('$location'); var $location = $injector.get('$location');
var authenticationService = $injector.get('authenticationService'); var authenticationService = $injector.get('authenticationService');
var connectionGroupService = $injector.get("connectionGroupService"); var guacNotification = $injector.get('guacNotification');
var guacNotification = $injector.get('guacNotification'); var permissionService = $injector.get("permissionService");
var permissionService = $injector.get("permissionService"); var userService = $injector.get('userService');
var userService = $injector.get('userService'); var userPageService = $injector.get('userPageService');
var userPageService = $injector.get('userPageService');
/** /**
* An action to be provided along with the object sent to * 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]; 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 * Whether the current user has sufficient permissions to change
* his/her own password. If permissions have not yet been loaded, * 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; $scope.pages = null;
/** // Retrieve current permissions
* Updates the visible menu items based on the permissions and root permissionService.getPermissions(authenticationService.getCurrentUserID())
* group on the scope, if available. If either the permissions or .success(function permissionsRetrieved(permissions) {
* 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);
// Check whether the current user can change their own password // Check whether the current user can change their own password
$scope.canChangePassword = PermissionSet.hasUserPermission( $scope.canChangePassword = PermissionSet.hasUserPermission(
permissions, PermissionSet.ObjectPermissionType.UPDATE, permissions, PermissionSet.ObjectPermissionType.UPDATE,
authenticationService.getCurrentUserID() 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 // Retrieve the main pages from the user page service
permissionService.getPermissions(authenticationService.getCurrentUserID()) userPageService.getMainPages()
.success(function permissionsRetrieved(retrievedPermissions) { .then(function retrievedMainPages(pages) {
permissions = retrievedPermissions; $scope.pages = pages;
updateMenuItems();
}); });
/** /**

View File

@@ -31,7 +31,10 @@ angular.module('navigation').factory('userPageService', ['$injector',
var PermissionSet = $injector.get('PermissionSet'); var PermissionSet = $injector.get('PermissionSet');
// Get required services // 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 = {}; var service = {};
@@ -52,7 +55,7 @@ angular.module('navigation').factory('userPageService', ['$injector',
this.name = name; this.name = name;
this.url = url; this.url = url;
}; };
/** /**
* Returns an appropriate home page for the current user. * Returns an appropriate home page for the current user.
* *
@@ -62,7 +65,7 @@ angular.module('navigation').factory('userPageService', ['$injector',
* @returns {Page} * @returns {Page}
* The user's home page. * The user's home page.
*/ */
service.getHomePage = function getHomePage(rootGroup) { var generateHomePage = function generateHomePage(rootGroup) {
// Get children // Get children
var connections = rootGroup.childConnections || []; 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( return new Page(
'USER_MENU.ACTION_NAVIGATE_HOME', '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.<Page>}
* 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 * 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 * 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 * @param {PermissionSet} permissions
* The permissions for the current user. * The permissions for the current user.
* *
* @returns {Array} * @returns {Page[]}
* An array of objects like this * 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 = []; var pages = [];
@@ -172,7 +196,7 @@ angular.module('navigation').factory('userPageService', ['$injector',
PermissionSet.hasSystemPermission(permissions, PermissionSet.SystemPermissionType.ADMINISTER); PermissionSet.hasSystemPermission(permissions, PermissionSet.SystemPermissionType.ADMINISTER);
// Add home page // Add home page
pages.push(service.getHomePage(rootGroup)); pages.push(generateHomePage(rootGroup));
// If user can manage users, add link to user management page // If user can manage users, add link to user management page
if (canManageUsers) { if (canManageUsers) {
@@ -200,7 +224,51 @@ angular.module('navigation').factory('userPageService', ['$injector',
return pages; 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.<Page[]>}
* 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; return service;
}]); }]);