2013-09-20 22:38:17 +00:00
|
|
|
import logging
|
|
|
|
|
2013-10-15 18:48:49 +00:00
|
|
|
from flask.ext.principal import (identity_loaded, UserNeed, Permission,
|
|
|
|
Identity, identity_changed)
|
2013-09-20 22:38:17 +00:00
|
|
|
from collections import namedtuple
|
2013-09-25 20:46:28 +00:00
|
|
|
from functools import partial
|
2013-09-20 22:38:17 +00:00
|
|
|
|
2013-09-25 16:45:12 +00:00
|
|
|
from data import model
|
2013-09-20 22:38:17 +00:00
|
|
|
from app import app
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2013-09-25 20:46:28 +00:00
|
|
|
_ResourceNeed = namedtuple('resource', ['type', 'namespace', 'name', 'role'])
|
|
|
|
_RepositoryNeed = partial(_ResourceNeed, 'repository')
|
2013-11-04 20:42:08 +00:00
|
|
|
_OrganizationNeed = namedtuple('organization', ['orgname', 'role'])
|
2013-11-04 21:18:40 +00:00
|
|
|
_TeamNeed = namedtuple('orgteam', ['orgname', 'teamname', 'role'])
|
2013-09-20 22:38:17 +00:00
|
|
|
|
|
|
|
|
2013-10-15 18:48:49 +00:00
|
|
|
class QuayDeferredPermissionUser(Identity):
|
|
|
|
def __init__(self, id, auth_type=None):
|
|
|
|
super(QuayDeferredPermissionUser, self).__init__(id, auth_type)
|
|
|
|
|
|
|
|
self._permissions_loaded = False
|
|
|
|
|
|
|
|
def can(self, permission):
|
|
|
|
if not self._permissions_loaded:
|
|
|
|
logger.debug('Loading user permissions after deferring.')
|
|
|
|
user_object = model.get_user(self.id)
|
|
|
|
|
2013-11-04 20:42:08 +00:00
|
|
|
# Add the user specific permissions
|
|
|
|
user_grant = UserNeed(user_object.username)
|
|
|
|
self.provides.add(user_grant)
|
|
|
|
|
|
|
|
# Every user is the admin of their own 'org'
|
|
|
|
user_namespace = _OrganizationNeed(user_object.username, 'admin')
|
|
|
|
self.provides.add(user_namespace)
|
|
|
|
|
|
|
|
# Add repository permissions
|
|
|
|
for perm in model.get_all_user_permissions(user_object):
|
|
|
|
grant = _RepositoryNeed(perm.repository.namespace,
|
|
|
|
perm.repository.name, perm.role.name)
|
2013-10-15 18:48:49 +00:00
|
|
|
logger.debug('User added permission: {0}'.format(grant))
|
|
|
|
self.provides.add(grant)
|
|
|
|
|
2013-11-04 20:42:08 +00:00
|
|
|
# Add namespace permissions derived
|
|
|
|
for team in model.get_org_wide_permissions(user_object):
|
|
|
|
grant = _OrganizationNeed(team.organization.username, team.role.name)
|
|
|
|
logger.debug('Organization team added permission: {0}'.format(grant))
|
|
|
|
self.provides.add(grant)
|
|
|
|
|
2013-11-04 21:18:40 +00:00
|
|
|
team_grant = _TeamNeed(team.organization.username, team.name,
|
|
|
|
team.role.name)
|
|
|
|
logger.debug('Team added permission: {0}'.format(team_grant))
|
|
|
|
self.provides.add(team_grant)
|
|
|
|
|
2013-10-15 18:48:49 +00:00
|
|
|
self._permissions_loaded = True
|
|
|
|
|
|
|
|
return super(QuayDeferredPermissionUser, self).can(permission)
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
class ModifyRepositoryPermission(Permission):
|
|
|
|
def __init__(self, namespace, name):
|
|
|
|
admin_need = _RepositoryNeed(namespace, name, 'admin')
|
|
|
|
write_need = _RepositoryNeed(namespace, name, 'write')
|
2013-11-04 20:42:08 +00:00
|
|
|
org_admin_need = _OrganizationNeed(namespace, 'admin')
|
2013-09-20 22:38:17 +00:00
|
|
|
super(ModifyRepositoryPermission, self).__init__(admin_need, write_need)
|
|
|
|
|
|
|
|
|
|
|
|
class ReadRepositoryPermission(Permission):
|
|
|
|
def __init__(self, namespace, name):
|
|
|
|
admin_need = _RepositoryNeed(namespace, name, 'admin')
|
|
|
|
write_need = _RepositoryNeed(namespace, name, 'write')
|
|
|
|
read_need = _RepositoryNeed(namespace, name, 'read')
|
2013-11-04 20:42:08 +00:00
|
|
|
org_admin_need = _OrganizationNeed(namespace, 'admin')
|
2013-09-20 22:43:43 +00:00
|
|
|
super(ReadRepositoryPermission, self).__init__(admin_need, write_need,
|
2013-11-04 20:42:08 +00:00
|
|
|
read_need, org_admin_need)
|
2013-09-20 22:38:17 +00:00
|
|
|
|
|
|
|
|
2013-09-27 17:24:07 +00:00
|
|
|
class AdministerRepositoryPermission(Permission):
|
|
|
|
def __init__(self, namespace, name):
|
|
|
|
admin_need = _RepositoryNeed(namespace, name, 'admin')
|
2013-11-04 20:42:08 +00:00
|
|
|
org_admin_need = _OrganizationNeed(namespace, 'admin')
|
|
|
|
super(AdministerRepositoryPermission, self).__init__(admin_need,
|
|
|
|
org_admin_need)
|
|
|
|
|
|
|
|
|
|
|
|
class CreateRepositoryPermission(Permission):
|
|
|
|
def __init__(self, namespace):
|
|
|
|
admin_org = _OrganizationNeed(namespace, 'admin')
|
|
|
|
create_repo_org = _OrganizationNeed(namespace, 'creator')
|
|
|
|
super(CreateRepositoryPermission, self).__init__(admin_org,
|
|
|
|
create_repo_org)
|
2013-09-27 17:24:07 +00:00
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
class UserPermission(Permission):
|
|
|
|
def __init__(self, username):
|
|
|
|
user_need = UserNeed(username)
|
|
|
|
super(UserPermission, self).__init__(user_need)
|
|
|
|
|
|
|
|
|
2013-11-04 21:18:40 +00:00
|
|
|
class AdministerOrganizationPermission(Permission):
|
|
|
|
def __init__(self, org_name):
|
|
|
|
admin_org = _OrganizationNeed(org_name, 'admin')
|
|
|
|
super(AdministerOrganizationPermission, self).__init__(admin_org)
|
|
|
|
|
|
|
|
|
|
|
|
class OrganizationMemberPermission(Permission):
|
|
|
|
def __init__(self, org_name):
|
|
|
|
admin_org = _OrganizationNeed(org_name, 'admin')
|
|
|
|
repo_creator_org = _OrganizationNeed(org_name, 'creator')
|
|
|
|
org_member = _OrganizationNeed(org_name, 'member')
|
|
|
|
super(OrganizationMemberPermission, self).__init__(admin_org, org_member,
|
|
|
|
repo_creator_org)
|
|
|
|
|
|
|
|
|
|
|
|
class ViewTeamPermission(Permission):
|
|
|
|
def __init__(self, org_name, team_name):
|
|
|
|
team_admin = _TeamNeed(org_name, team_name, 'admin')
|
2013-11-04 21:39:52 +00:00
|
|
|
team_creator = _TeamNeed(org_name, team_name, 'creator')
|
|
|
|
team_member = _TeamNeed(org_name, team_name, 'member')
|
2013-11-04 21:44:38 +00:00
|
|
|
admin_org = _OrganizationNeed(org_name, 'admin')
|
2013-11-04 21:18:40 +00:00
|
|
|
super(ViewTeamPermission, self).__init__(team_admin, team_creator,
|
2013-11-04 21:44:38 +00:00
|
|
|
team_member, admin_org)
|
2013-11-04 21:18:40 +00:00
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
@identity_loaded.connect_via(app)
|
|
|
|
def on_identity_loaded(sender, identity):
|
2013-09-25 20:46:28 +00:00
|
|
|
logger.debug('Identity loaded: %s' % identity)
|
2013-09-20 22:38:17 +00:00
|
|
|
# We have verified an identity, load in all of the permissions
|
|
|
|
|
2013-10-15 18:48:49 +00:00
|
|
|
if isinstance(identity, QuayDeferredPermissionUser):
|
|
|
|
logger.debug('Deferring permissions for user: %s' % identity.id)
|
2013-09-26 20:32:09 +00:00
|
|
|
|
2013-10-15 18:48:49 +00:00
|
|
|
elif identity.auth_type == 'username':
|
|
|
|
switch_to_deferred = QuayDeferredPermissionUser(identity.id, 'username')
|
|
|
|
identity_changed.send(app, identity=switch_to_deferred)
|
2013-09-20 22:38:17 +00:00
|
|
|
|
2013-09-26 20:32:09 +00:00
|
|
|
elif identity.auth_type == 'token':
|
2013-10-16 18:24:10 +00:00
|
|
|
logger.debug('Loading permissions for token: %s' % identity.id)
|
|
|
|
token_data = model.load_token_data(identity.id)
|
|
|
|
|
|
|
|
repo_grant = _RepositoryNeed(token_data.repository.namespace,
|
|
|
|
token_data.repository.name,
|
|
|
|
token_data.role.name)
|
|
|
|
logger.debug('Delegate token added permission: {0}'.format(repo_grant))
|
|
|
|
identity.provides.add(repo_grant)
|
2013-09-26 20:32:09 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
logger.error('Unknown identity auth type: %s' % identity.auth_type)
|