2017-07-18 18:16:41 +00:00
|
|
|
import sys
|
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
from six import add_metaclass
|
|
|
|
|
|
|
|
|
|
|
|
class SaveException(Exception):
|
|
|
|
def __init__(self, other):
|
|
|
|
self.traceback = sys.exc_info()
|
|
|
|
super(SaveException, self).__init__(other.message)
|
|
|
|
|
|
|
|
class DeleteException(Exception):
|
|
|
|
def __init__(self, other):
|
|
|
|
self.traceback = sys.exc_info()
|
|
|
|
super(DeleteException, self).__init__(other.message)
|
|
|
|
|
|
|
|
|
|
|
|
class Role(namedtuple('Role', ['role_name'])):
|
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'role': self.role_name,
|
|
|
|
}
|
|
|
|
|
|
|
|
class UserPermission(namedtuple('UserPermission', [
|
|
|
|
'role_name',
|
|
|
|
'username',
|
|
|
|
'is_robot',
|
|
|
|
'avatar',
|
|
|
|
'is_org_member',
|
|
|
|
'has_org',
|
|
|
|
])):
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
perm_dict = {
|
|
|
|
'role': self.role_name,
|
|
|
|
'username': self.username,
|
|
|
|
'is_robot': False,
|
|
|
|
'avatar': self.avatar,
|
|
|
|
}
|
|
|
|
if self.has_org:
|
|
|
|
perm_dict['is_org_member'] = self.is_org_member
|
|
|
|
return perm_dict
|
|
|
|
|
|
|
|
|
|
|
|
class RobotPermission(namedtuple('RobotPermission', [
|
|
|
|
'role_name',
|
|
|
|
'username',
|
|
|
|
'is_robot',
|
|
|
|
'is_org_member',
|
|
|
|
])):
|
|
|
|
|
|
|
|
def to_dict(self, user=None, team=None, org_members=None):
|
|
|
|
return {
|
|
|
|
'role': self.role_name,
|
|
|
|
'username': self.username,
|
|
|
|
'is_robot': True,
|
|
|
|
'is_org_member': self.is_org_member,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class TeamPermission(namedtuple('TeamPermission', [
|
|
|
|
'role_name',
|
|
|
|
'team_name',
|
|
|
|
'avatar',
|
|
|
|
])):
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'role': self.role_name,
|
|
|
|
'name': self.team_name,
|
|
|
|
'avatar': self.avatar,
|
|
|
|
}
|
|
|
|
|
|
|
|
@add_metaclass(ABCMeta)
|
|
|
|
class PermissionDataInterface(object):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
Data interface used by permissions API
|
|
|
|
"""
|
|
|
|
|
2017-07-18 18:16:41 +00:00
|
|
|
@abstractmethod
|
|
|
|
def get_repo_permissions_by_user(self, namespace_name, repository_name):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list(UserPermission)
|
|
|
|
"""
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_repo_roles(self, username, namespace_name, repository_name):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
username: string
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list(Role) or None
|
|
|
|
"""
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_repo_permission_for_user(self, username, namespace_name, repository_name):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
username: string
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
UserPermission
|
|
|
|
"""
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def set_repo_permission_for_user(self, username, namespace_name, repository_name, role_name):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
username: string
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
role_name: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
UserPermission
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
SaveException
|
|
|
|
"""
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def delete_repo_permission_for_user(self, username, namespace_name, repository_name):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
username: string
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
void
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
DeleteException
|
|
|
|
"""
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_repo_permissions_by_team(self, namespace_name, repository_name):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list(TeamPermission)
|
|
|
|
"""
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_repo_role_for_team(self, team_name, namespace_name, repository_name):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
team_name: string
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Role
|
|
|
|
"""
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def set_repo_permission_for_team(self, team_name, namespace_name, repository_name, permission):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
team_name: string
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
permission: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TeamPermission
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
SaveException
|
|
|
|
"""
|
2017-07-18 18:16:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def delete_repo_permission_for_team(self, team_name, namespace_name, repository_name):
|
2017-07-26 18:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
team_name: string
|
|
|
|
namespace_name: string
|
|
|
|
repository_name: string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TeamPermission
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
DeleteException
|
|
|
|
"""
|