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
|
|
|
|
|
2015-03-30 21:55:04 +00:00
|
|
|
from app import avatar
|
2014-03-14 17:06:58 +00:00
|
|
|
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
|
2014-03-14 17:06:58 +00:00
|
|
|
from data import model
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def role_view(repo_perm_obj):
|
|
|
|
return {
|
|
|
|
'role': repo_perm_obj.role.name,
|
|
|
|
}
|
|
|
|
|
|
|
|
def wrap_role_view_user(role_json, user):
|
2015-04-01 18:23:39 +00:00
|
|
|
role_json['name'] = user.username
|
2014-03-14 17:06:58 +00:00
|
|
|
role_json['is_robot'] = user.robot
|
2015-03-30 21:55:04 +00:00
|
|
|
if not user.robot:
|
2015-07-15 21:25:41 +00:00
|
|
|
role_json['avatar'] = avatar.get_data_for_user(user)
|
2014-03-14 17:06:58 +00:00
|
|
|
return role_json
|
|
|
|
|
|
|
|
|
|
|
|
def wrap_role_view_org(role_json, user, org_members):
|
|
|
|
role_json['is_org_member'] = user.robot or user.username in org_members
|
|
|
|
return role_json
|
|
|
|
|
|
|
|
|
2015-03-30 21:55:04 +00:00
|
|
|
def wrap_role_view_team(role_json, team):
|
2015-04-01 18:23:39 +00:00
|
|
|
role_json['name'] = team.name
|
2015-03-30 21:55:04 +00:00
|
|
|
role_json['avatar'] = avatar.get_data_for_team(team)
|
|
|
|
return role_json
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
def get(self, namespace, repository):
|
|
|
|
""" List all team permission. """
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_perms = model.permission.get_all_repo_teams(namespace, repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2015-03-30 21:55:04 +00:00
|
|
|
def wrapped_role_view(repo_perm):
|
|
|
|
return wrap_role_view_team(role_view(repo_perm), repo_perm.team)
|
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
return {
|
2015-03-30 21:55:04 +00:00
|
|
|
'permissions': {repo_perm.team.name: wrapped_role_view(repo_perm)
|
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')
|
|
|
|
def get(self, namespace, repository):
|
|
|
|
""" List all user permissions. """
|
|
|
|
# Lookup the organization (if any).
|
|
|
|
org = None
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
org = model.organization.get_organization(namespace) # Will raise an error if not org
|
2014-03-14 17:06:58 +00:00
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
# This repository isn't under an org
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Determine how to wrap the role(s).
|
|
|
|
def wrapped_role_view(repo_perm):
|
|
|
|
return wrap_role_view_user(role_view(repo_perm), repo_perm.user)
|
|
|
|
|
|
|
|
role_view_func = wrapped_role_view
|
|
|
|
|
|
|
|
if org:
|
2015-07-15 21:25:41 +00:00
|
|
|
org_members = model.organization.get_organization_member_set(namespace)
|
2014-03-14 17:06:58 +00:00
|
|
|
current_func = role_view_func
|
|
|
|
|
|
|
|
def wrapped_role_org_view(repo_perm):
|
|
|
|
return wrap_role_view_org(current_func(repo_perm), repo_perm.user,
|
|
|
|
org_members)
|
|
|
|
|
|
|
|
role_view_func = wrapped_role_org_view
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
# Load and return the permissions.
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_perms = model.user.get_all_repo_users(namespace, repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
return {
|
|
|
|
'permissions': {perm.user.username: role_view_func(perm)
|
|
|
|
for perm in repo_perms}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
def get(self, namespace, repository, username):
|
|
|
|
""" Get the fetch the permission for the specified user. """
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_user(username)
|
2015-07-01 08:39:12 +00:00
|
|
|
if not user:
|
|
|
|
raise NotFound
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2015-07-01 08:39:12 +00:00
|
|
|
if not repo:
|
|
|
|
raise NotFound
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
permissions = list(model.permission.get_user_repo_permissions(user, repo))
|
2015-07-01 08:39:12 +00:00
|
|
|
return {
|
|
|
|
'permissions': [role_view(permission) for permission in permissions]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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')
|
2014-03-14 17:06:58 +00:00
|
|
|
def get(self, namespace, repository, username):
|
2014-03-14 17:27:56 +00:00
|
|
|
""" Get the Fetch the permission for the specified user. """
|
2015-07-15 21:25:41 +00:00
|
|
|
logger.debug('Get repo: %s/%s permissions for user %s', namespace, repository, username)
|
|
|
|
perm = model.permission.get_user_reponame_permission(username, namespace, repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
perm_view = wrap_role_view_user(role_view(perm), perm.user)
|
|
|
|
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
model.organization.get_organization(namespace)
|
|
|
|
org_members = model.organization.get_organization_member_set(namespace)
|
2014-03-14 17:06:58 +00:00
|
|
|
perm_view = wrap_role_view_org(perm_view, perm.user, org_members)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
# This repository is not part of an organization
|
|
|
|
pass
|
|
|
|
|
|
|
|
return perm_view
|
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('changeUserPermissions')
|
|
|
|
@validate_json_request('UserPermission')
|
|
|
|
def put(self, namespace, repository, username): # Also needs to respond to post
|
|
|
|
""" 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:
|
2015-07-15 21:25:41 +00:00
|
|
|
perm = model.permission.set_user_repo_permission(username, namespace, repository,
|
|
|
|
new_permission['role'])
|
|
|
|
except model.DataModelException as ex:
|
2014-03-18 19:58:37 +00:00
|
|
|
raise request_error(exception=ex)
|
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
perm_view = wrap_role_view_user(role_view(perm), perm.user)
|
|
|
|
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
model.organization.get_organization(namespace)
|
|
|
|
org_members = model.organization.get_organization_member_set(namespace)
|
2014-03-14 17:06:58 +00:00
|
|
|
perm_view = wrap_role_view_org(perm_view, perm.user, org_members)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
# This repository is not part of an organization
|
|
|
|
pass
|
|
|
|
except model.DataModelException as ex:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(exception=ex)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
log_action('change_repo_permission', namespace,
|
|
|
|
{'username': username, 'repo': repository,
|
|
|
|
'role': new_permission['role']},
|
2015-07-15 21:25:41 +00:00
|
|
|
repo=model.repository.get_repository(namespace, repository))
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-03-14 22:39:31 +00:00
|
|
|
return perm_view, 200
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('deleteUserPermissions')
|
2014-03-17 17:10:12 +00:00
|
|
|
def delete(self, namespace, repository, username):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" Delete the permission for the user. """
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
model.permission.delete_user_permission(username, namespace, repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
except model.DataModelException as ex:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(exception=ex)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
log_action('delete_repo_permission', namespace,
|
|
|
|
{'username': username, 'repo': repository},
|
2015-07-15 21:25:41 +00:00
|
|
|
repo=model.repository.get_repository(namespace, repository))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
return 'Deleted', 204
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
def get(self, namespace, repository, teamname):
|
|
|
|
""" Fetch the permission for the specified team. """
|
2015-07-15 21:25:41 +00:00
|
|
|
logger.debug('Get repo: %s/%s permissions for team %s', namespace, repository, teamname)
|
|
|
|
perm = model.permission.get_team_reponame_permission(teamname, namespace, repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
return role_view(perm)
|
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('changeTeamPermissions')
|
|
|
|
@validate_json_request('TeamPermission')
|
|
|
|
def put(self, namespace, repository, teamname):
|
|
|
|
""" 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
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
perm = model.permission.set_team_repo_permission(teamname, namespace, repository,
|
|
|
|
new_permission['role'])
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
log_action('change_repo_permission', namespace,
|
|
|
|
{'team': teamname, 'repo': repository,
|
|
|
|
'role': new_permission['role']},
|
2015-07-15 21:25:41 +00:00
|
|
|
repo=model.repository.get_repository(namespace, repository))
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2015-03-31 18:36:09 +00:00
|
|
|
return wrap_role_view_team(role_view(perm), perm.team), 200
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('deleteTeamPermissions')
|
|
|
|
def delete(self, namespace, repository, teamname):
|
|
|
|
""" Delete the permission for the specified team. """
|
2015-07-15 21:25:41 +00:00
|
|
|
model.permission.delete_team_permission(teamname, namespace, repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
log_action('delete_repo_permission', namespace,
|
|
|
|
{'team': teamname, 'repo': repository},
|
2015-07-15 21:25:41 +00:00
|
|
|
repo=model.repository.get_repository(namespace, repository))
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
return 'Deleted', 204
|