Merge "Use context from_environ to load contexts"

This commit is contained in:
Jenkins 2016-08-26 22:03:03 +00:00 committed by Gerrit Code Review
commit 26c0a30952
2 changed files with 13 additions and 21 deletions

View File

@ -32,21 +32,20 @@ class ContextBase(oslo_context.RequestContext):
"""
def __init__(self, user_id, tenant_id, is_admin=None, roles=None,
timestamp=None, request_id=None, tenant_name=None,
user_name=None, overwrite=True, auth_token=None,
is_advsvc=None):
def __init__(self, user_id=None, tenant_id=None, is_admin=None,
timestamp=None, tenant_name=None, user_name=None,
is_advsvc=None, **kwargs):
"""Object initialization.
:param overwrite: Set to False to ensure that the greenthread local
copy of the index is not overwritten.
"""
super(ContextBase, self).__init__(auth_token=auth_token,
user=user_id, tenant=tenant_id,
is_admin=is_admin,
request_id=request_id,
overwrite=overwrite,
roles=roles)
# NOTE(jamielennox): We maintain these arguments in order for tests
# that pass arguments positionally.
kwargs.setdefault('user', user_id)
kwargs.setdefault('tenant', tenant_id)
super(ContextBase, self).__init__(is_admin=is_admin, **kwargs)
self.user_name = user_name
self.tenant_name = tenant_name

View File

@ -39,22 +39,15 @@ class ContextHook(hooks.PecanHook):
priority = 95
def before(self, state):
user_id = state.request.headers.get('X-User-Id')
user_id = state.request.headers.get('X-User', user_id)
user_name = state.request.headers.get('X-User-Name', '')
tenant_id = state.request.headers.get('X-Project-Id')
tenant_name = state.request.headers.get('X-Project-Name')
auth_token = state.request.headers.get('X-Auth-Token')
roles = state.request.headers.get('X-Roles', '').split(',')
roles = [r.strip() for r in roles]
creds = {'roles': roles}
req_id = state.request.headers.get(request_id.ENV_REQUEST_ID)
# TODO(kevinbenton): is_admin logic
# Create a context with the authentication data
ctx = context.Context(user_id, tenant_id=tenant_id,
roles=creds['roles'],
user_name=user_name, tenant_name=tenant_name,
request_id=req_id, auth_token=auth_token)
ctx = context.Context.from_environ(state.request.environ,
user_name=user_name,
tenant_name=tenant_name,
request_id=req_id)
# Inject the context...
state.request.context['neutron_context'] = ctx