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,
|
2014-08-19 23:05:28 +00:00
|
|
|
log_action, request_error, validate_json_request, path_param)
|
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:
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath: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. """
|
|
|
|
repo_perms = model.get_all_repo_teams(namespace, repository)
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath: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:
|
|
|
|
org = model.get_organization(namespace) # Will raise an error if not org
|
|
|
|
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:
|
|
|
|
org_members = model.get_organization_member_set(namespace)
|
|
|
|
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.
|
|
|
|
repo_perms = model.get_all_repo_users(namespace, repository)
|
|
|
|
return {
|
|
|
|
'permissions': {perm.user.username: role_view_func(perm)
|
|
|
|
for perm in repo_perms}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath: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': {
|
|
|
|
'id': '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. """
|
2014-03-14 17:06:58 +00:00
|
|
|
logger.debug('Get repo: %s/%s permissions for user %s' %
|
|
|
|
(namespace, repository, username))
|
|
|
|
perm = model.get_user_reponame_permission(username, namespace, repository)
|
|
|
|
perm_view = wrap_role_view_user(role_view(perm), perm.user)
|
|
|
|
|
|
|
|
try:
|
|
|
|
model.get_organization(namespace)
|
|
|
|
org_members = model.get_organization_member_set(namespace)
|
|
|
|
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()
|
|
|
|
|
|
|
|
logger.debug('Setting permission to: %s for user %s' %
|
|
|
|
(new_permission['role'], username))
|
|
|
|
|
2014-03-18 19:58:37 +00:00
|
|
|
try:
|
|
|
|
perm = model.set_user_repo_permission(username, namespace, repository,
|
|
|
|
new_permission['role'])
|
|
|
|
except model.InvalidUsernameException as ex:
|
|
|
|
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:
|
|
|
|
model.get_organization(namespace)
|
|
|
|
org_members = model.get_organization_member_set(namespace)
|
|
|
|
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']},
|
|
|
|
repo=model.get_repository(namespace, repository))
|
|
|
|
|
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:
|
|
|
|
model.delete_user_permission(username, namespace, repository)
|
|
|
|
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},
|
|
|
|
repo=model.get_repository(namespace, repository))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
return 'Deleted', 204
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath: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': {
|
|
|
|
'id': '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. """
|
|
|
|
logger.debug('Get repo: %s/%s permissions for team %s' %
|
|
|
|
(namespace, repository, teamname))
|
|
|
|
perm = model.get_team_reponame_permission(teamname, namespace, repository)
|
|
|
|
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()
|
|
|
|
|
|
|
|
logger.debug('Setting permission to: %s for team %s' %
|
|
|
|
(new_permission['role'], teamname))
|
|
|
|
|
|
|
|
perm = model.set_team_repo_permission(teamname, namespace, repository,
|
|
|
|
new_permission['role'])
|
|
|
|
|
|
|
|
log_action('change_repo_permission', namespace,
|
|
|
|
{'team': teamname, 'repo': repository,
|
|
|
|
'role': new_permission['role']},
|
|
|
|
repo=model.get_repository(namespace, repository))
|
|
|
|
|
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. """
|
|
|
|
model.delete_team_permission(teamname, namespace, repository)
|
|
|
|
|
|
|
|
log_action('delete_repo_permission', namespace,
|
|
|
|
{'team': teamname, 'repo': repository},
|
|
|
|
repo=model.get_repository(namespace, repository))
|
|
|
|
|
|
|
|
return 'Deleted', 204
|