GUAC-932: Provide flag-based view for PermissionSets. Use ngModel instead of ngChecked for permission checkboxes in user edit UI.

This commit is contained in:
Michael Jumper
2014-12-23 01:40:23 -08:00
parent c26d5a77ab
commit f564e26fd1
5 changed files with 170 additions and 71 deletions

View File

@@ -27,8 +27,9 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
function manageUserController($scope, $injector) {
// Required types
var ConnectionGroup = $injector.get('ConnectionGroup');
var PermissionSet = $injector.get('PermissionSet');
var ConnectionGroup = $injector.get('ConnectionGroup');
var PermissionFlagSet = $injector.get('PermissionFlagSet');
var PermissionSet = $injector.get('PermissionSet');
// Required services
var $location = $injector.get('$location');
@@ -63,7 +64,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
// Pull user permissions
permissionService.getPermissions(username).success(function gotPermissions(permissions) {
$scope.permissions = permissions;
$scope.permissionFlags = PermissionFlagSet.fromPermissionSet(permissions);
});
// Retrieve all connections for which we have UPDATE permission
@@ -101,76 +102,19 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
$scope.groupListContext = {
/**
* Determines whether read permission for the connection having the
* given identifier is granted for the user being edited.
*
* @param {String} identifier
* The identifier of the connection to check.
*
* @returns {Boolean}
* true if the user has read permission for the given connection,
* false if the user lacks read permission, or the permissions have
* not yet been loaded.
* Returns the PermissionFlagSet that contains the current state of
* granted permissions.
*
* @returns {PermissionFlagSet}
* The PermissionFlagSet describing the current state of granted
* permissions for the user being edited.
*/
canReadConnection : function canReadConnection(identifier) {
// Assume no permission if permissions not available yet
if (!$scope.permissions)
return false;
// Return whether READ permission is present
return PermissionSet.hasConnectionPermission($scope.permissions, PermissionSet.ObjectPermissionType.READ, identifier);
},
/**
* Determines whether read permission for the connection group having
* the given identifier is granted for the user being edited.
*
* @param {String} identifier
* The identifier of the connection group to check.
*
* @returns {Boolean}
* true if the user has read permission for the given connection
* group, false if the user lacks read permission, or the
* permissions have not yet been loaded.
*/
canReadConnectionGroup : function canReadConnectionGroup(identifier) {
// Assume no permission if permissions not available yet
if (!$scope.permissions)
return false;
// Return whether READ permission is present
return PermissionSet.hasConnectionGroupPermission($scope.permissions, PermissionSet.ObjectPermissionType.READ, identifier);
getPermissionFlags : function getPermissionFlags() {
return $scope.permissionFlags;
}
};
/**
* Determines whether the given system permission is granted for the
* user being edited.
*
* @param {String} type
* The type string of the system permission to check.
*
* @returns {Boolean}
* true if the user has the given system permission, false if the
* user lacks the given system permission, or the permissions have
* not yet been loaded.
*/
$scope.hasSystemPermission = function hasSystemPermission(type) {
// Assume no permission if permissions not available yet
if (!$scope.permissions)
return false;
// Return whether given permission is present
return PermissionSet.hasSystemPermission($scope.permissions, type);
};
/**
* Cancels all pending edits, returning to the management page.
*/

View File

@@ -21,6 +21,6 @@
THE SOFTWARE.
-->
<input type="checkbox" ng-checked="context.canReadConnectionGroup(item.identifier)"/>
<input type="checkbox" ng-model="context.getPermissionFlags().connectionGroupPermissions.READ[item.identifier]"/>
<span class="name">{{item.name}}</span>
</div>

View File

@@ -27,7 +27,7 @@
</div>
<!-- Checkbox -->
<input type="checkbox" ng-checked="context.canReadConnection(item.identifier)"/>
<input type="checkbox" ng-model="context.getPermissionFlags().connectionPermissions.READ[item.identifier]"/>
<!-- Connection name -->
<span class="name">{{item.name}}</span>

View File

@@ -53,7 +53,7 @@ THE SOFTWARE.
<table class="properties">
<tr ng-repeat="systemPermissionType in systemPermissionTypes">
<th>{{systemPermissionType.label | translate}}</th>
<td><input type="checkbox" ng-checked="hasSystemPermission(systemPermissionType.value)"/></td>
<td><input type="checkbox" ng-model="permissionFlags.systemPermissions[systemPermissionType.value]"/></td>
</tr>
</table>
</div>

View File

@@ -0,0 +1,155 @@
/*
* Copyright (C) 2014 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* A service for defining the PermissionFlagSet class.
*/
angular.module('rest').factory('PermissionFlagSet', ['PermissionSet',
function definePermissionFlagSet(PermissionSet) {
/**
* Alternative view of a @link{PermissionSet} which allows manipulation of
* each permission through the setting (or retrieval) of boolean property
* values.
*
* @constructor
* @param {PermissionFlagSet|Object} template
* The object whose properties should be copied within the new
* PermissionFlagSet.
*/
var PermissionFlagSet = function PermissionFlagSet(template) {
// Use empty object by default
template = template || {};
/**
* The granted state of each system permission, as a map of system
* permission type string to boolean value. A particular permission is
* granted if its corresponding boolean value is set to true. Valid
* permission type strings are defined within
* PermissionSet.SystemPermissionType. Permissions which are not
* granted may be set to false, but this is not required.
*
* @type Object.<String, Boolean>
*/
this.systemPermissions = template.systemPermissions || {};
/**
* The granted state of each permission for each connection, as a map
* of object permission type string to permission map. The permission
* map is, in turn, a map of connection identifier to boolean value. A
* particular permission is granted if its corresponding boolean value
* is set to true. Valid permission type strings are defined within
* PermissionSet.ObjectPermissionType. Permissions which are not
* granted may be set to false, but this is not required.
*
* @type Object.<String, Object.<String, Boolean>>
*/
this.connectionPermissions = template.connectionPermissions || {};
/**
* The granted state of each permission for each connection group, as a
* map of object permission type string to permission map. The
* permission map is, in turn, a map of connection group identifier to
* boolean value. A particular permission is granted if its
* corresponding boolean value is set to true. Valid permission type
* strings are defined within PermissionSet.ObjectPermissionType.
* Permissions which are not granted may be set to false, but this is
* not required.
*
* @type Object.<String, Object.<String, Boolean>>
*/
this.connectionGroupPermissions = template.connectionGroupPermissions || {};
/**
* The granted state of each permission for each user, as a map of
* object permission type string to permission map. The permission map
* is, in turn, a map of username to boolean value. A particular
* permission is granted if its corresponding boolean value is set to
* true. Valid permission type strings are defined within
* PermissionSet.ObjectPermissionType. Permissions which are not
* granted may be set to false, but this is not required.
*
* @type Object.<String, Object.<String, Boolean>>
*/
this.userPermissions = template.userPermissions || {};
};
var addObjectPermissions = function addObjectPermissions(permMap, flagMap) {
// For each defined identifier in the permission map
for (var identifier in permMap) {
// Pull the permission array and loop through each permission
var permissions = permMap[identifier];
permissions.forEach(function addObjectPermission(type) {
// Get identifier/flag mapping, creating first if necessary
var objectFlags = flagMap[type] = flagMap[type] || {};
// Set flag for current permission
objectFlags[identifier] = true;
});
}
};
/**
* Creates a new PermissionFlagSet, populating it with all the permissions
* indicated as granted within the given PermissionSet.
*
* @param {PermissionSet} permissionSet
* The PermissionSet containing the permissions to be copied into a new
* PermissionFlagSet.
*
* @returns {PermissionFlagSet}
* A new PermissionFlagSet containing flags representing all granted
* permissions from the given PermissionSet.
*/
PermissionFlagSet.fromPermissionSet = function fromPermissionSet(permissionSet) {
var permissionFlagSet = new PermissionFlagSet();
// Add all granted system permissions
permissionSet.systemPermissions.forEach(function addSystemPermission(type) {
permissionFlagSet.systemPermissions[type] = true;
});
// Add all granted connection permissions
addObjectPermissions(permissionSet.connectionPermissions, permissionFlagSet.connectionPermissions);
// Add all granted connection group permissions
addObjectPermissions(permissionSet.connectionGroupPermissions, permissionFlagSet.connectionGroupPermissions);
// Add all granted user permissions
addObjectPermissions(permissionSet.userPermissions, permissionFlagSet.userPermissions);
return permissionFlagSet;
};
return PermissionFlagSet;
}]);