diff options
author | NavaL <ayoyo@thoughtworks.com> | 2016-06-24 18:37:25 +0200 |
---|---|---|
committer | NavaL <ayoyo@thoughtworks.com> | 2016-06-24 18:37:55 +0200 |
commit | 64c54186eff000762c291758973ca8e5db28f606 (patch) | |
tree | ca90b7c74583cfa170e90838b5722b96ba7138da /service/pixelated | |
parent | 12706c25ef02974ff54d1f42caac268f6eaa3a0c (diff) |
Issue #694 add an admin restricted resource for user stats
Diffstat (limited to 'service/pixelated')
-rw-r--r-- | service/pixelated/config/services.py | 6 | ||||
-rw-r--r-- | service/pixelated/resources/__init__.py | 4 | ||||
-rw-r--r-- | service/pixelated/resources/root_resource.py | 3 | ||||
-rw-r--r-- | service/pixelated/resources/users.py | 30 |
4 files changed, 43 insertions, 0 deletions
diff --git a/service/pixelated/config/services.py b/service/pixelated/config/services.py index b36eb0bb..a49e1df9 100644 --- a/service/pixelated/config/services.py +++ b/service/pixelated/config/services.py @@ -108,6 +108,9 @@ class ServicesFactory(object): def add_session(self, user_id, services): self._services_by_user[user_id] = services + def online_sessions(self): + return len(self._services_by_user.keys()) + @defer.inlineCallbacks def create_services_from(self, leap_session): _services = Services(leap_session) @@ -131,3 +134,6 @@ class SingleUserServicesFactory(object): def destroy_session(self, user_id, using_email=False): reactor.stop() + + def online_sessions(self): + return 1 diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py index 0015db16..0bd34619 100644 --- a/service/pixelated/resources/__init__.py +++ b/service/pixelated/resources/__init__.py @@ -77,6 +77,10 @@ class BaseResource(Resource): def get_session(self, request): return IPixelatedSession(request.getSession()) + def is_admin(self, request): + services = self._services(request) + return services._leap_session.user_auth.is_admin() + def _services(self, request): user_id = self._get_user_id_from_request(request) return self._services_factory.services(user_id) diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index aacf2b61..c9808a03 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -33,6 +33,8 @@ from pixelated.resources.tags_resource import TagsResource from pixelated.resources.keys_resource import KeysResource from twisted.web.static import File +from pixelated.resources.users import UsersResource + CSRF_TOKEN_LENGTH = 32 MODE_STARTUP = 1 @@ -90,6 +92,7 @@ class RootResource(BaseResource): self._child_resources.add('mail', MailResource(self._services_factory)) self._child_resources.add('feedback', FeedbackResource(self._services_factory)) self._child_resources.add('user-settings', UserSettingsResource(self._services_factory)) + self._child_resources.add('users', UsersResource(self._services_factory)) self._child_resources.add(LoginResource.BASE_URL, LoginResource(self._services_factory, portal, disclaimer_banner=disclaimer_banner)) self._child_resources.add(LogoutResource.BASE_URL, LogoutResource(self._services_factory)) diff --git a/service/pixelated/resources/users.py b/service/pixelated/resources/users.py new file mode 100644 index 00000000..a3e6118e --- /dev/null +++ b/service/pixelated/resources/users.py @@ -0,0 +1,30 @@ +# +# Copyright (c) 2016 ThoughtWorks, Inc. +# +# Pixelated is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pixelated is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Pixelated. If not, see <http://www.gnu.org/licenses/>. + +from pixelated.resources import respond_json_deferred, BaseResource, respond_json, UnAuthorizedResource +from twisted.web import server + + +class UsersResource(BaseResource): + isLeaf = True + + def __init__(self, services_factory): + BaseResource.__init__(self, services_factory) + + def render_GET(self, request): + if self.is_admin(request): + return respond_json({"count": self._services_factory.online_sessions()}, request) + return UnAuthorizedResource().render_GET(request) |