2016-08-18 19:59:00 +00:00
|
|
|
/**
|
2016-08-22 18:42:35 +00:00
|
|
|
* An element which displays a table for setting permissions for an entity to repositories under
|
2016-08-18 19:59:00 +00:00
|
|
|
* a namespace.
|
|
|
|
*/
|
2016-08-22 18:42:35 +00:00
|
|
|
angular.module('quay').directive('setRepoPermissions', function () {
|
2016-08-18 19:59:00 +00:00
|
|
|
var directiveDefinitionObject = {
|
|
|
|
priority: 0,
|
2016-08-22 18:42:35 +00:00
|
|
|
templateUrl: '/static/directives/set-repo-permissions.html',
|
2016-08-18 19:59:00 +00:00
|
|
|
replace: false,
|
|
|
|
transclude: true,
|
|
|
|
restrict: 'C',
|
|
|
|
scope: {
|
|
|
|
'namespace': '=namespace',
|
|
|
|
'entityName': '=entityName',
|
|
|
|
'entityKind': '=entityKind',
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
'setPermissions': '=setPermissions',
|
2016-08-18 19:59:00 +00:00
|
|
|
|
|
|
|
'hasCheckedRepositories': '=hasCheckedRepositories',
|
2016-08-22 18:42:35 +00:00
|
|
|
'hasChangedRepositories': '=hasChangedRepositories',
|
2016-08-18 19:59:00 +00:00
|
|
|
|
|
|
|
'repositoriesLoaded': '&repositoriesLoaded',
|
2016-08-22 18:42:35 +00:00
|
|
|
'settingPermissions': '&settingPermissions',
|
|
|
|
'permissionsSet': '&permissionsSet',
|
2016-08-18 19:59:00 +00:00
|
|
|
},
|
|
|
|
|
2016-08-18 21:44:36 +00:00
|
|
|
controller: function($scope, $element, ApiService, UIService, TableService, RolesService, UserService) {
|
2016-08-18 19:59:00 +00:00
|
|
|
$scope.TableService = TableService;
|
|
|
|
|
|
|
|
$scope.options = {
|
|
|
|
'predicate': 'last_modified_datetime',
|
|
|
|
'reverse': false,
|
|
|
|
'filter': ''
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.repositories = null;
|
|
|
|
$scope.currentNamespace = null;
|
2016-08-22 18:42:35 +00:00
|
|
|
$scope.currentEntityName = null;
|
|
|
|
|
|
|
|
var checkForChanges = function() {
|
|
|
|
var hasChanges = false;
|
|
|
|
|
|
|
|
$scope.repositories.forEach(function(repo) {
|
|
|
|
if (repo['permission'] != repo['original_permission']) {
|
|
|
|
hasChanges = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.hasCheckedRepositories = !!$scope.checkedRepos.checked.length;
|
|
|
|
$scope.hasChangedRepositories = hasChanges;
|
|
|
|
};
|
2016-08-18 19:59:00 +00:00
|
|
|
|
|
|
|
var handleRepoCheckChange = function() {
|
|
|
|
$scope.repositories.forEach(function(repo) {
|
|
|
|
if ($scope.checkedRepos.isChecked(repo)) {
|
|
|
|
if (repo['permission'] == 'none') {
|
|
|
|
repo['permission'] = 'read';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
repo['permission'] = 'none';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
checkForChanges();
|
2016-08-18 19:59:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var setRepoState = function() {
|
|
|
|
if (!$scope.repositories) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.orderedRepositories = TableService.buildOrderedItems(
|
|
|
|
$scope.repositories, $scope.options,
|
|
|
|
['name', 'permission'],
|
|
|
|
['last_modified_datetime']);
|
|
|
|
};
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
var loadRepositoriesAndPermissions = function() {
|
2016-08-18 19:59:00 +00:00
|
|
|
if (!$scope.namespace || !$scope.entityName || !$scope.entityKind) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
if (($scope.entityName == $scope.currentEntityName) &&
|
|
|
|
($scope.namespace == $scope.currentNamespace)) {
|
2016-08-18 19:59:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
$scope.currentNamespace = $scope.namespace;
|
|
|
|
$scope.currentEntityName = $scope.entityName;
|
|
|
|
|
|
|
|
// Load the repository permissions for the entity first. We then load the full repo list
|
|
|
|
// and compare.
|
|
|
|
RolesService.getRepoPermissions($scope.namespace, $scope.entityKind, $scope.entityName,
|
|
|
|
function(permissions) {
|
|
|
|
if (permissions == null) {
|
|
|
|
$scope.currentNamespace = null;
|
|
|
|
$scope.currentEntityName = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var existingPermissionsMap = {};
|
|
|
|
permissions.forEach(function(existingPermission) {
|
|
|
|
existingPermissionsMap[existingPermission.repository.name] = existingPermission.role;
|
|
|
|
});
|
|
|
|
|
|
|
|
loadRepositories(existingPermissionsMap);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var loadRepositories = function(existingPermissionsMap) {
|
2016-08-18 21:44:36 +00:00
|
|
|
$scope.namespaceInfo = UserService.getNamespace($scope.namespace);
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
// Load the repositories under the entity's namespace, along with the current repo
|
|
|
|
// permissions for the entity.
|
2016-08-18 19:59:00 +00:00
|
|
|
var params = {
|
|
|
|
'namespace': $scope.namespace,
|
|
|
|
'last_modified': true
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.listRepos(null, params).then(function(resp) {
|
|
|
|
$scope.currentNamespace = $scope.namespace;
|
|
|
|
|
|
|
|
var repos = [];
|
|
|
|
resp['repositories'].forEach(function(repo) {
|
2016-08-22 18:42:35 +00:00
|
|
|
var existingPermission = existingPermissionsMap[repo.name] || 'none';
|
|
|
|
|
2016-08-18 19:59:00 +00:00
|
|
|
repos.push({
|
|
|
|
'namespace': repo.namespace,
|
|
|
|
'name': repo.name,
|
|
|
|
'last_modified': repo.last_modified,
|
|
|
|
'last_modified_datetime': TableService.getReversedTimestamp(repo.last_modified),
|
2016-08-22 18:42:35 +00:00
|
|
|
'permission': existingPermission,
|
|
|
|
'original_permission': existingPermission
|
2016-08-18 19:59:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (repos.length == 0) {
|
|
|
|
$scope.repositoriesLoaded({'repositories': repos});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.repositories = repos;
|
|
|
|
$scope.checkedRepos = UIService.createCheckStateController($scope.repositories, 'name');
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
repos.forEach(function(repo) {
|
|
|
|
if (repo.permission != 'none') {
|
|
|
|
$scope.checkedRepos.checkItem(repo);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.checkedRepos.listen(handleRepoCheckChange);
|
2016-08-18 19:59:00 +00:00
|
|
|
|
|
|
|
setRepoState();
|
|
|
|
$scope.repositoriesLoaded({'repositories': repos});
|
|
|
|
}, ApiService.errorDisplay('Could not load repositories'));
|
|
|
|
};
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
var setPermissions = function() {
|
2016-08-18 19:59:00 +00:00
|
|
|
if (!$scope.checkedRepos || !$scope.namespace || !$scope.repositories) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
$scope.settingPermissions();
|
2016-08-18 19:59:00 +00:00
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
var repos = $scope.repositories;
|
2016-08-18 19:59:00 +00:00
|
|
|
var counter = 0;
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
var setPerm = function() {
|
2016-08-18 19:59:00 +00:00
|
|
|
if (counter >= repos.length) {
|
2016-08-22 18:42:35 +00:00
|
|
|
$scope.permissionsSet({'repositories': $scope.checkedRepos.checked});
|
|
|
|
$scope.checkedRepos.setChecked([]);
|
2016-08-18 19:59:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var repo = repos[counter];
|
2016-08-22 18:42:35 +00:00
|
|
|
if (repo['permission'] == repo['original_permission']) {
|
|
|
|
// Skip changing it.
|
|
|
|
counter++;
|
|
|
|
setPerm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 19:59:00 +00:00
|
|
|
RolesService.setRepositoryRole(repo, repo.permission, $scope.entityKind,
|
2016-08-22 18:42:35 +00:00
|
|
|
$scope.entityName, function(status) {
|
|
|
|
if (status) {
|
|
|
|
counter++;
|
|
|
|
setPerm();
|
|
|
|
}
|
|
|
|
});
|
2016-08-18 19:59:00 +00:00
|
|
|
};
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
setPerm();
|
2016-08-18 19:59:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.setRole = function(role, repo) {
|
|
|
|
repo['permission'] = role;
|
|
|
|
|
|
|
|
if (role == 'none') {
|
|
|
|
$scope.checkedRepos.uncheckItem(repo);
|
|
|
|
} else {
|
|
|
|
$scope.checkedRepos.checkItem(repo);
|
|
|
|
}
|
2016-08-22 18:42:35 +00:00
|
|
|
|
|
|
|
checkForChanges();
|
2016-08-18 19:59:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.allRepositoriesFilter = function(item) {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.noRepositoriesFilter = function(item) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.missingPermsRepositoriesFilter = function(item) {
|
|
|
|
return !item.perm;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.$watch('options.predicate', setRepoState);
|
|
|
|
$scope.$watch('options.reverse', setRepoState);
|
|
|
|
$scope.$watch('options.filter', setRepoState);
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
$scope.$watch('namespace', loadRepositoriesAndPermissions);
|
|
|
|
$scope.$watch('entityName', loadRepositoriesAndPermissions);
|
|
|
|
$scope.$watch('entityKind', loadRepositoriesAndPermissions);
|
2016-08-18 19:59:00 +00:00
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
$scope.$watch('setPermissions', function(value) {
|
2016-08-18 19:59:00 +00:00
|
|
|
if (value) {
|
2016-08-22 18:42:35 +00:00
|
|
|
setPermissions();
|
2016-08-18 19:59:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return directiveDefinitionObject;
|
|
|
|
});
|