2015-05-14 20:47:38 +00:00
|
|
|
""" Manage repository permissions. """
|
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from flask import request
|
|
|
|
|
|
|
|
from endpoints.api import (resource, nickname, require_repo_admin, RepositoryParamResource,
|
2016-04-11 20:20:11 +00:00
|
|
|
log_action, request_error, validate_json_request, path_param)
|
|
|
|
from endpoints.exception import NotFound
|
2017-07-18 18:16:41 +00:00
|
|
|
from permission_models_pre_oci import pre_oci_model as model
|
|
|
|
from permission_models_interface import DeleteException, SaveException
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/permissions/team/')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryTeamPermissionList(RepositoryParamResource):
|
|
|
|
""" Resource for repository team permissions. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('listRepoTeamPermissions')
|
2017-07-18 18:16:41 +00:00
|
|
|
def get(self, namespace_name, repository_name):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" List all team permission. """
|
2017-07-18 18:16:41 +00:00
|
|
|
repo_perms = model.get_repo_permissions_by_team(namespace_name, repository_name)
|
2015-03-30 21:55:04 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
return {
|
2017-07-18 18:16:41 +00:00
|
|
|
'permissions': {repo_perm.team_name: repo_perm.to_dict()
|
2014-03-14 17:06:58 +00:00
|
|
|
for repo_perm in repo_perms}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/permissions/user/')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryUserPermissionList(RepositoryParamResource):
|
|
|
|
""" Resource for repository user permissions. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('listRepoUserPermissions')
|
2017-07-18 18:16:41 +00:00
|
|
|
def get(self, namespace_name, repository_name):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" List all user permissions. """
|
2017-07-18 18:16:41 +00:00
|
|
|
perms = model.get_repo_permissions_by_user(namespace_name, repository_name)
|
|
|
|
return {'permissions': {p.username: p.to_dict() for p in perms}}
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/permissions/user/<username>/transitive')
|
2015-07-01 08:39:12 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('username', 'The username of the user to which the permissions apply')
|
|
|
|
class RepositoryUserTransitivePermission(RepositoryParamResource):
|
|
|
|
""" Resource for retrieving whether a user has access to a repository, either directly
|
|
|
|
or via a team. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('getUserTransitivePermission')
|
2017-07-18 18:16:41 +00:00
|
|
|
def get(self, namespace_name, repository_name, username):
|
2015-07-01 08:39:12 +00:00
|
|
|
""" Get the fetch the permission for the specified user. """
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
roles = model.get_repo_roles(username, namespace_name, repository_name)
|
|
|
|
|
|
|
|
if not roles:
|
2015-07-01 08:39:12 +00:00
|
|
|
raise NotFound
|
2017-07-18 18:16:41 +00:00
|
|
|
|
2015-07-01 08:39:12 +00:00
|
|
|
return {
|
2017-07-18 18:16:41 +00:00
|
|
|
'permissions': [r.to_dict() for r in roles]
|
2015-07-01 08:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/permissions/user/<username>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('username', 'The username of the user to which the permission applies')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryUserPermission(RepositoryParamResource):
|
|
|
|
""" Resource for managing individual user permissions. """
|
|
|
|
schemas = {
|
|
|
|
'UserPermission': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Description of a user permission.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'role',
|
|
|
|
],
|
2014-03-14 17:06:58 +00:00
|
|
|
'properties': {
|
|
|
|
'role': {
|
|
|
|
'type': 'string',
|
2014-03-14 17:24:01 +00:00
|
|
|
'description': 'Role to use for the user',
|
2014-03-14 17:06:58 +00:00
|
|
|
'enum': [
|
|
|
|
'read',
|
|
|
|
'write',
|
|
|
|
'admin',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_admin
|
2014-03-18 18:22:14 +00:00
|
|
|
@nickname('getUserPermissions')
|
2017-07-18 18:16:41 +00:00
|
|
|
def get(self, namespace_name, repository_name, username):
|
|
|
|
""" Get the permission for the specified user. """
|
|
|
|
logger.debug('Get repo: %s/%s permissions for user %s', namespace_name, repository_name, username)
|
|
|
|
perm = model.get_repo_permission_for_user(username, namespace_name, repository_name)
|
|
|
|
return perm.to_dict()
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('changeUserPermissions')
|
|
|
|
@validate_json_request('UserPermission')
|
2017-07-18 18:16:41 +00:00
|
|
|
def put(self, namespace_name, repository_name, username): # Also needs to respond to post
|
2014-03-14 17:06:58 +00:00
|
|
|
""" Update the perimssions for an existing repository. """
|
|
|
|
new_permission = request.get_json()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
logger.debug('Setting permission to: %s for user %s', new_permission['role'], username)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-03-18 19:58:37 +00:00
|
|
|
try:
|
2017-07-18 18:16:41 +00:00
|
|
|
perm = model.set_repo_permission_for_user(username, namespace_name, repository_name,
|
|
|
|
new_permission['role'])
|
|
|
|
resp = perm.to_dict()
|
|
|
|
except SaveException as ex:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(exception=ex)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2017-07-18 18:16:41 +00:00
|
|
|
log_action('change_repo_permission', namespace_name,
|
|
|
|
{'username': username, 'repo': repository_name,
|
|
|
|
'namespace': namespace_name,
|
2014-03-14 17:06:58 +00:00
|
|
|
'role': new_permission['role']},
|
2017-07-18 18:16:41 +00:00
|
|
|
repo_name=repository_name)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2017-07-18 18:16:41 +00:00
|
|
|
return resp, 200
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('deleteUserPermissions')
|
2017-07-18 18:16:41 +00:00
|
|
|
def delete(self, namespace_name, repository_name, username):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" Delete the permission for the user. """
|
|
|
|
try:
|
2017-07-18 18:16:41 +00:00
|
|
|
model.delete_repo_permission_for_user(username, namespace_name, repository_name)
|
|
|
|
except DeleteException as ex:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(exception=ex)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2017-07-18 18:16:41 +00:00
|
|
|
log_action('delete_repo_permission', namespace_name,
|
|
|
|
{'username': username, 'repo': repository_name, 'namespace': namespace_name},
|
|
|
|
repo_name=repository_name)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/permissions/team/<teamname>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('teamname', 'The name of the team to which the permission applies')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryTeamPermission(RepositoryParamResource):
|
|
|
|
""" Resource for managing individual team permissions. """
|
|
|
|
schemas = {
|
|
|
|
'TeamPermission': {
|
|
|
|
'type': 'object',
|
2014-03-14 17:24:01 +00:00
|
|
|
'description': 'Description of a team permission.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'role',
|
|
|
|
],
|
2014-03-14 17:06:58 +00:00
|
|
|
'properties': {
|
|
|
|
'role': {
|
|
|
|
'type': 'string',
|
2014-03-14 17:24:01 +00:00
|
|
|
'description': 'Role to use for the team',
|
2014-03-14 17:06:58 +00:00
|
|
|
'enum': [
|
|
|
|
'read',
|
|
|
|
'write',
|
|
|
|
'admin',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('getTeamPermissions')
|
2017-07-18 18:16:41 +00:00
|
|
|
def get(self, namespace_name, repository_name, teamname):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" Fetch the permission for the specified team. """
|
2017-07-18 18:16:41 +00:00
|
|
|
logger.debug('Get repo: %s/%s permissions for team %s', namespace_name, repository_name, teamname)
|
|
|
|
role = model.get_repo_role_for_team(teamname, namespace_name, repository_name)
|
|
|
|
return role.to_dict()
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('changeTeamPermissions')
|
|
|
|
@validate_json_request('TeamPermission')
|
2017-07-18 18:16:41 +00:00
|
|
|
def put(self, namespace_name, repository_name, teamname):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" Update the existing team permission. """
|
|
|
|
new_permission = request.get_json()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
logger.debug('Setting permission to: %s for team %s', new_permission['role'], teamname)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2017-07-18 18:16:41 +00:00
|
|
|
try:
|
|
|
|
perm = model.set_repo_permission_for_team(teamname, namespace_name, repository_name,
|
|
|
|
new_permission['role'])
|
|
|
|
resp = perm.to_dict()
|
|
|
|
except SaveException as ex:
|
|
|
|
raise request_error(exception=ex)
|
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2017-07-18 18:16:41 +00:00
|
|
|
log_action('change_repo_permission', namespace_name,
|
|
|
|
{'team': teamname, 'repo': repository_name,
|
2014-03-14 17:06:58 +00:00
|
|
|
'role': new_permission['role']},
|
2017-07-18 18:16:41 +00:00
|
|
|
repo_name=repository_name)
|
|
|
|
return resp, 200
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('deleteTeamPermissions')
|
2017-07-18 18:16:41 +00:00
|
|
|
def delete(self, namespace_name, repository_name, teamname):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" Delete the permission for the specified team. """
|
2017-07-18 18:16:41 +00:00
|
|
|
try:
|
|
|
|
model.delete_repo_permission_for_team(teamname, namespace_name, repository_name)
|
|
|
|
except DeleteException as ex:
|
|
|
|
raise request_error(exception=ex)
|
|
|
|
|
|
|
|
log_action('delete_repo_permission', namespace_name,
|
|
|
|
{'team': teamname, 'repo': repository_name},
|
|
|
|
repo_name=repository_name)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|