2015-04-20 21:42:33 +00:00
|
|
|
/**
|
|
|
|
* Service which defines the various role groups.
|
|
|
|
*/
|
2016-08-22 18:42:35 +00:00
|
|
|
angular.module('quay').factory('RolesService', ['UtilService', 'Restangular', 'ApiService', 'UserService',
|
|
|
|
function(UtilService, Restangular, ApiService, UserService) {
|
2015-04-20 21:42:33 +00:00
|
|
|
var roleService = {};
|
|
|
|
|
2016-05-12 21:59:49 +00:00
|
|
|
roleService.repoRolesOrNone = [
|
|
|
|
{ 'id': 'none', 'title': 'None', 'kind': 'default', 'description': 'No permissions on the repository' },
|
|
|
|
|
2015-04-20 21:42:33 +00:00
|
|
|
{ 'id': 'read', 'title': 'Read', 'kind': 'success', 'description': 'Can view and pull from the repository' },
|
|
|
|
{ 'id': 'write', 'title': 'Write', 'kind': 'success', 'description': 'Can view, pull and push to the repository' },
|
|
|
|
{ 'id': 'admin', 'title': 'Admin', 'kind': 'primary', 'description': 'Full admin access, pull and push on the repository' }
|
|
|
|
];
|
|
|
|
|
2016-05-12 21:59:49 +00:00
|
|
|
roleService.repoRoles = roleService.repoRolesOrNone.slice(1);
|
|
|
|
|
2015-04-20 21:42:33 +00:00
|
|
|
roleService.teamRoles = [
|
|
|
|
{ 'id': 'member', 'title': 'Member', 'kind': 'default', 'description': 'Inherits all permissions of the team' },
|
|
|
|
{ 'id': 'creator', 'title': 'Creator', 'kind': 'success', 'description': 'Member and can create new repositories' },
|
|
|
|
{ 'id': 'admin', 'title': 'Admin', 'kind': 'primary', 'description': 'Full admin access to the organization' }
|
|
|
|
];
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
var getPermissionEndpoint = function(repository, entityName, entityKind) {
|
|
|
|
if (entityKind == 'robot') {
|
|
|
|
entityKind = 'user';
|
|
|
|
}
|
|
|
|
|
2016-05-12 21:59:49 +00:00
|
|
|
var namespace = repository.namespace;
|
|
|
|
var name = repository.name;
|
2016-08-22 18:42:35 +00:00
|
|
|
var url = UtilService.getRestUrl('repository', namespace, name, 'permissions', entityKind, entityName);
|
2018-05-22 17:09:48 +00:00
|
|
|
return Restangular.one(url.toString());
|
2016-05-12 21:59:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
roleService.deleteRepositoryRole = function(repository, entityKind, entityName, callback) {
|
2016-08-22 18:42:35 +00:00
|
|
|
if (entityKind == 'robot') {
|
|
|
|
entityKind = 'user';
|
|
|
|
}
|
|
|
|
|
2016-05-12 21:59:49 +00:00
|
|
|
var errorDisplay = ApiService.errorDisplay('Cannot change permission', function(resp) {
|
|
|
|
callback(false);
|
|
|
|
});
|
|
|
|
|
2016-05-24 15:44:59 +00:00
|
|
|
var endpoint = getPermissionEndpoint(repository, entityName, entityKind);
|
2016-05-12 21:59:49 +00:00
|
|
|
endpoint.customDELETE().then(function() {
|
|
|
|
callback(true);
|
2016-05-24 15:44:59 +00:00
|
|
|
}, errorDisplay);
|
2016-05-12 21:59:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
roleService.setRepositoryRole = function(repository, role, entityKind, entityName, callback) {
|
2016-08-22 18:42:35 +00:00
|
|
|
if (role == 'none') {
|
|
|
|
roleService.deleteRepositoryRole(repository, entityKind, entityName, callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entityKind == 'robot') {
|
|
|
|
entityKind = 'user';
|
|
|
|
}
|
|
|
|
|
2016-05-12 21:59:49 +00:00
|
|
|
var errorDisplay = ApiService.errorDisplay('Cannot change permission', function(resp) {
|
|
|
|
callback(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
var permission = {
|
|
|
|
'role': role
|
|
|
|
};
|
|
|
|
|
|
|
|
var endpoint = getPermissionEndpoint(repository, entityName, entityKind);
|
|
|
|
endpoint.customPUT(permission).then(function(resp) {
|
|
|
|
callback(true, resp);
|
|
|
|
}, errorDisplay);
|
|
|
|
};
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
roleService.getRepoPermissions = function(namespace, entityKind, entityName, callback) {
|
|
|
|
var errorHandler = ApiService.errorDisplay('Could not load permissions', callback);
|
|
|
|
|
|
|
|
if (entityKind == 'team') {
|
|
|
|
var params = {
|
|
|
|
'orgname': namespace,
|
|
|
|
'teamname': entityName
|
|
|
|
};
|
|
|
|
|
2016-11-10 20:38:27 +00:00
|
|
|
ApiService.getOrganizationTeamPermissions(null, params).then(function(resp) {
|
2016-08-22 18:42:35 +00:00
|
|
|
callback(resp.permissions);
|
|
|
|
}, errorHandler);
|
|
|
|
} else if (entityKind == 'robot') {
|
|
|
|
var parts = entityName.split('+');
|
|
|
|
var shortName = parts[1];
|
|
|
|
|
|
|
|
var orgname = UserService.isOrganization(namespace) ? namespace : null;
|
|
|
|
ApiService.getRobotPermissions(orgname, null, {'robot_shortname': shortName}).then(function(resp) {
|
|
|
|
callback(resp.permissions);
|
|
|
|
}, errorHandler);
|
|
|
|
} else {
|
|
|
|
throw Error('Unknown entity kind ' + entityKind);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-20 21:42:33 +00:00
|
|
|
return roleService;
|
|
|
|
}]);
|