Merge pull request #133 from glyptodon/client-user-menu

GUAC-1126: Add user menu to Guacamole menu
This commit is contained in:
James Muehlner
2015-04-06 20:24:20 -07:00
17 changed files with 164 additions and 153 deletions

View File

@@ -69,7 +69,7 @@
}
.menu-header {
.menu-content .header {
-ms-flex: 0 0 auto;
-moz-box-flex: 0;
@@ -77,61 +77,8 @@
-webkit-flex: 0 0 auto;
flex: 0 0 auto;
/* IE10 */
display: -ms-flexbox;
-ms-flex-align: center;
-ms-flex-direction: row;
/* Ancient Mozilla */
display: -moz-box;
-moz-box-align: center;
-moz-box-orient: horizontal;
margin-bottom: 0;
/* Ancient WebKit */
display: -webkit-box;
-webkit-box-align: center;
-webkit-box-orient: horizontal;
/* Old WebKit */
display: -webkit-flex;
-webkit-align-items: center;
-webkit-flex-direction: row;
/* W3C */
display: flex;
align-items: center;
flex-direction: row;
padding: 0.75em 0.5em;
border-bottom: 1px solid rgba(0, 0, 0, 0.125);
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.125);
background: rgba(0, 0, 0, 0.04);
}
.menu-header > * {
-ms-flex: 0 0 auto;
-moz-box-flex: 0;
-webkit-box-flex: 0;
-webkit-flex: 0 0 auto;
flex: 0 0 auto;
}
.menu-header > h2 {
-ms-flex: 1 1 auto;
-moz-box-flex: 1;
-webkit-box-flex: 1;
-webkit-flex: 1 1 auto;
flex: 1 1 auto;
border: none;
padding: 0;
box-shadow: none;
background: none;
margin: 0;
}
.menu-body {

View File

@@ -57,10 +57,9 @@
<div class="menu-content">
<!-- Stationary header -->
<div class="menu-header">
<div class="header">
<h2>{{client.name}}</h2>
<a class="home button" href="#/">{{'CLIENT.ACTION_NAVIGATE_HOME' | translate}}</a>
<a class="disconnect danger button" ng-click="disconnect()">{{'CLIENT.ACTION_DISCONNECT' | translate}}</a>
<guac-user-menu></guac-user-menu>
</div>
<!-- Scrollable body -->

View File

@@ -30,11 +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");
/**
* The root connection group, or null if the connection group hierarchy has
@@ -44,14 +41,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 +50,14 @@ 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);
$location.url(homePage.url);
});
// Retrieve current permissions
permissionService.getPermissions(authenticationService.getCurrentUserID())
.success(function permissionsRetrieved(permissions) {
$scope.permissions = permissions;
});
}]);

View File

@@ -27,7 +27,7 @@
<!-- The recent connections for this user -->
<div class="header">
<h2>{{'HOME.SECTION_HEADER_RECENT_CONNECTIONS' | translate}}</h2>
<guac-user-menu permissions="permissions" root-group="rootConnectionGroup"></guac-user-menu>
<guac-user-menu></guac-user-menu>
</div>
<div class="recent-connections">
<guac-recent-connections root-group="rootConnectionGroup"></guac-recent-connections>

View File

@@ -62,6 +62,57 @@ angular.module('index').config(['$routeProvider', '$locationProvider',
}];
/**
* Redirects the user to their home page. This necessarily requires
* attempting to re-authenticate with the Guacamole server, as the user's
* credentials may have changed, and thus their most-appropriate home page
* may have changed as well.
*
* @param {Service} $injector
* The Angular $injector service.
*
* @returns {Promise}
* A promise which resolves successfully only after an attempt to
* re-authenticate and determine the user's proper home page has been
* made.
*/
var routeToUserHomePage = ['$injector', function routeToUserHomePage($injector) {
// Required services
var $location = $injector.get('$location');
var $q = $injector.get('$q');
var userPageService = $injector.get('userPageService');
// Promise for redirection attempt
var redirect = $q.defer();
// Re-authenticate including any parameters in URL
$injector.invoke(updateCurrentToken)
.then(function tokenUpdateComplete() {
// Redirect to home page
userPageService.getHomePage()
.then(function homePageRetrieved(homePage) {
$location.url(homePage.url);
})
// If retrieval of home page fails, assume '/'
['catch'](function homePageFailed() {
$location.url('/');
})
// Resolve promise in either case
['finally'](function retrievalAttemptComplete() {
redirect.resolve();
});
});
// Return promise that will resolve regardless of success/failure
return redirect.promise;
}];
// Configure each possible route
$routeProvider
@@ -71,7 +122,7 @@ angular.module('index').config(['$routeProvider', '$locationProvider',
bodyClassName : 'home',
templateUrl : 'app/home/templates/home.html',
controller : 'homeController',
resolve : { updateCurrentToken: updateCurrentToken }
resolve : { routeToUserHomePage: routeToUserHomePage }
})
// Connection management screen
@@ -147,9 +198,7 @@ angular.module('index').config(['$routeProvider', '$locationProvider',
// Redirect to home screen if page not found
.otherwise({
redirectTo : '/'
resolve : { routeToUserHomePage: routeToUserHomePage }
});
}]);

View File

@@ -29,6 +29,7 @@ angular.module('index', [
'home',
'login',
'manage',
'navigation',
'ngRoute',
'ngTouch',
'notification',

View File

@@ -26,6 +26,7 @@ angular.module('login').controller('loginController', ['$scope', '$injector',
// Required services
var $location = $injector.get("$location");
var authenticationService = $injector.get("authenticationService");
var userPageService = $injector.get('userPageService');
/**
* Whether an error occurred during login.
@@ -52,7 +53,10 @@ angular.module('login').controller('loginController', ['$scope', '$injector',
// Redirect to main view upon success
.success(function success(data, status, headers, config) {
$location.path('/');
userPageService.getHomePage()
.then(function homePageRetrieved(homePage) {
$location.url(homePage.url);
});
})
// Reset and focus password upon failure

View File

@@ -23,4 +23,4 @@
/**
* The module for the login functionality.
*/
angular.module('login', ['element']);
angular.module('login', ['element', 'navigation']);

View File

@@ -25,7 +25,7 @@ THE SOFTWARE.
<!-- Main property editor -->
<div class="header">
<h2>{{'MANAGE_CONNECTION.SECTION_HEADER_EDIT_CONNECTION' | translate}}</h2>
<guac-user-menu permissions="permissions"></guac-user-menu>
<guac-user-menu></guac-user-menu>
</div>
<div class="section">
<table class="properties">

View File

@@ -25,7 +25,7 @@ THE SOFTWARE.
<!-- Main property editor -->
<div class="header">
<h2>{{'MANAGE_CONNECTION_GROUP.SECTION_HEADER_EDIT_CONNECTION_GROUP' | translate}}</h2>
<guac-user-menu permissions="permissions"></guac-user-menu>
<guac-user-menu></guac-user-menu>
</div>
<div class="section">
<table class="properties">

View File

@@ -24,7 +24,7 @@ THE SOFTWARE.
<div class="header">
<h2>{{'MANAGE_CONNECTION.SECTION_HEADER_CONNECTIONS' | translate}}</h2>
<guac-user-menu permissions="permissions"></guac-user-menu>
<guac-user-menu></guac-user-menu>
</div>
<!-- Connection management -->

View File

@@ -24,7 +24,7 @@ THE SOFTWARE.
<div class="header">
<h2>{{'MANAGE_SESSION.SECTION_HEADER_SESSIONS' | translate}}</h2>
<guac-user-menu permissions="permissions"></guac-user-menu>
<guac-user-menu></guac-user-menu>
</div>
<!-- User Session management -->

View File

@@ -25,7 +25,7 @@ THE SOFTWARE.
<!-- Main property editor -->
<div class="header">
<h2>{{'MANAGE_USER.SECTION_HEADER_EDIT_USER' | translate}}</h2>
<guac-user-menu permissions="permissions"></guac-user-menu>
<guac-user-menu></guac-user-menu>
</div>
<div class="section">
<table class="properties">

View File

@@ -24,7 +24,7 @@ THE SOFTWARE.
<div class="header">
<h2>{{'MANAGE_USER.SECTION_HEADER_USERS' | translate}}</h2>
<guac-user-menu permissions="permissions"></guac-user-menu>
<guac-user-menu></guac-user-menu>
</div>
<!-- User management -->

View File

@@ -30,36 +30,20 @@ angular.module('navigation').directive('guacUserMenu', [function guacUserMenu()
restrict: 'E',
replace: true,
scope: {
/**
* The permissions associated with the user for whom this menu is
* being displayed.
*
* @type PermissionSet
*/
permissions : '=',
/**
* The root of the connection group tree.
*
* @type ConnectionGroup
*/
rootGroup : '='
},
templateUrl: 'app/navigation/templates/guacUserMenu.html',
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 guacNotification = $injector.get('guacNotification');
var permissionService = $injector.get("permissionService");
var userService = $injector.get('userService');
var userPageService = $injector.get('userPageService');
@@ -141,41 +125,24 @@ 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 rootGroup are both available
if (!$scope.permissions || !$scope.rootGroup) {
$scope.canChangePassword = null;
$scope.pages = [];
return;
}
// Retrieve the main pages from the user page service
$scope.pages = userPageService.getMainPages($scope.rootGroup, $scope.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(
$scope.permissions, PermissionSet.ObjectPermissionType.UPDATE,
permissions, PermissionSet.ObjectPermissionType.UPDATE,
authenticationService.getCurrentUserID()
);
};
// Update available menu options when permissions are changed
$scope.$watch('permissions', function permissionsChanged() {
updateMenuItems();
});
// Retrieve the main pages from the user page service
userPageService.getMainPages()
.then(function retrievedMainPages(pages) {
$scope.pages = pages;
});
// Update available menu options when root group is changed
$scope.$watch('rootGroup', function rootGroupChanged() {
updateMenuItems();
});
/**
* Toggles visibility of the user menu.
*/

View File

@@ -23,4 +23,4 @@
/**
* Module for generating and implementing user navigation options.
*/
angular.module('navigation', ['notification']);
angular.module('navigation', ['notification', 'rest']);

View File

@@ -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.<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
* 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.<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;
}]);