diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2016-01-18 15:39:59 +0100 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2016-01-18 15:39:59 +0100 |
commit | c5d61bc8001b16e3dcedda9fd1229c27cca4bd3b (patch) | |
tree | a36fb87a31b7692d124b959ba32ba49f48a14ed8 /service/pixelated | |
parent | 0b06feff629f91d2fa4e7bdbe3b8e46b412e6158 (diff) |
Refactory KeyResource to get services from session
- Issue #576
- Only first step (other services not changed yet)
- Faked session access for now
Diffstat (limited to 'service/pixelated')
-rw-r--r-- | service/pixelated/application.py | 25 | ||||
-rw-r--r-- | service/pixelated/resources/__init__.py | 17 | ||||
-rw-r--r-- | service/pixelated/resources/keys_resource.py | 14 | ||||
-rw-r--r-- | service/pixelated/resources/root_resource.py | 4 |
4 files changed, 51 insertions, 9 deletions
diff --git a/service/pixelated/application.py b/service/pixelated/application.py index 7c8af78d..17dbacbf 100644 --- a/service/pixelated/application.py +++ b/service/pixelated/application.py @@ -41,6 +41,27 @@ import logging log = logging.getLogger(__name__) +class ServicesFactory(object): + + def __init__(self): + self._services_by_user = {} + + def is_logged_in(self, user_id): + return user_id in self._services_by_user + + def services(self, user_id): + return self._services_by_user[user_id] + + def log_out_user(self, user_id): + if self.is_logged_in(user_id): + services = self._services_by_user[user_id] + services.close() + del self._services_by_user[user_id] + + def add_session(self, user_id, services): + self._services_by_user[user_id] = services + + @defer.inlineCallbacks def start_user_agent(root_resource, leap_home, leap_session): log.info('Bootstrap done, loading services for user %s' % leap_session.user_auth.username) @@ -51,7 +72,11 @@ def start_user_agent(root_resource, leap_home, leap_session): if leap_session.fresh_account: yield add_welcome_mail(leap_session.mail_store) + servicesFactory = ServicesFactory() + servicesFactory.add_session(leap_session.user_auth.uuid, services) + root_resource.initialize( + servicesFactory, services.keymanager, services.search_engine, services.mail_service, diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py index c65e19f3..556469c1 100644 --- a/service/pixelated/resources/__init__.py +++ b/service/pixelated/resources/__init__.py @@ -16,6 +16,8 @@ import json +from twisted.web.resource import Resource + class SetEncoder(json.JSONEncoder): def default(self, obj): @@ -37,3 +39,18 @@ def respond_json_deferred(entity, request, status_code=200): request.code = status_code request.write(json_response) request.finish() + + +class BaseResource(Resource): + + def __init__(self, services_factory): + Resource.__init__(self) + self._services_factory = services_factory + + def keymanager(self, request): + user_id = self._get_user_id_from_request() + return self._services_factory.services(user_id).keymanager + + def _get_user_id_from_request(self): + # currently we are faking this + return self._services_factory._services_by_user.keys()[0] diff --git a/service/pixelated/resources/keys_resource.py b/service/pixelated/resources/keys_resource.py index 6df95b28..d6f469fe 100644 --- a/service/pixelated/resources/keys_resource.py +++ b/service/pixelated/resources/keys_resource.py @@ -1,18 +1,18 @@ from email.utils import parseaddr -from pixelated.resources import respond_json_deferred +from pixelated.resources import respond_json_deferred, BaseResource from twisted.web import server -from twisted.web.resource import Resource -class KeysResource(Resource): +class KeysResource(BaseResource): isLeaf = True - def __init__(self, keymanager): - Resource.__init__(self) - self._keymanager = keymanager + def __init__(self, services_factory): + BaseResource.__init__(self, services_factory) def render_GET(self, request): + _keymanager = self.keymanager(request) + def finish_request(key): if key.private: respond_json_deferred(None, request, status_code=401) @@ -23,7 +23,7 @@ class KeysResource(Resource): respond_json_deferred(None, request, status_code=404) _, key_to_find = parseaddr(request.args.get('search')[0]) - d = self._keymanager.fetch_key(key_to_find) + d = _keymanager.fetch_key(key_to_find) d.addCallback(finish_request) d.addErrback(key_not_found) diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index 56da5cc7..fac63bd7 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -36,11 +36,11 @@ class RootResource(Resource): return self return Resource.getChild(self, path, request) - def initialize(self, keymanager, search_engine, mail_service, draft_service, feedback_service): + def initialize(self, services_factory, keymanager, search_engine, mail_service, draft_service, feedback_service): self.account_email = mail_service.account_email self.putChild('assets', File(self._static_folder)) - self.putChild('keys', KeysResource(keymanager)) + self.putChild('keys', KeysResource(services_factory)) self.putChild(AttachmentsResource.BASE_URL, AttachmentsResource(mail_service)) self.putChild('contacts', ContactsResource(search_engine)) self.putChild('features', FeaturesResource()) |