diff --git a/rocketc/api_rocket_chat.py b/rocketc/api_rocket_chat.py index d887e109..a168d794 100644 --- a/rocketc/api_rocket_chat.py +++ b/rocketc/api_rocket_chat.py @@ -256,3 +256,14 @@ def set_custom_fields(self, room_id, custom_fields): response = self._request_rocket_chat(method, url_path, data) LOG.info("Method set_custom_fields: %s with this data: %s", response, data) return response + + def logout_user(self, user_id, login_token): + """ + This method allows to logout an user + """ + url_path = "logout" + headers = {"X-Auth-Token": login_token, "X-User-Id": user_id} + url = "{}/{}/{}".format(self.server_url, self.API_PATH, url_path) + response = requests.get(url=url, headers=headers) + LOG.info("Method logout User: %s ", response) + return response diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index 7d94954b..230d699f 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -5,6 +5,7 @@ import json import re import pkg_resources +import hashlib from api_teams import ApiTeams # pylint: disable=relative-import from api_rocket_chat import ApiRocketChat # pylint: disable=relative-import @@ -12,6 +13,8 @@ from django.conf import settings from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ +from django.core.cache import cache +from webob.response import Response from xblock.core import XBlock from xblock.fields import Scope, String, Boolean, DateTime, Integer, Float @@ -23,6 +26,8 @@ LOADER = ResourceLoader(__name__) LOG = logging.getLogger(__name__) +ROCKET_CHAT_DATA = "rocket_chat_data" +CACHE_TIMEOUT = 86400 @XBlock.wants("user") # pylint: disable=too-many-ancestors, too-many-instance-attributes @@ -168,7 +173,8 @@ def student_view(self, context=None): "user_data": self.user_data, "ui_is_block": self.ui_is_block, "team_view": self.team_view, - "public_url_service": self.server_data["public_url_service"] + "public_url_service": self.server_data["public_url_service"], + "key": hashlib.sha1("{}_{}".format(ROCKET_CHAT_DATA, self.user_data["username"])).hexdigest() } frag = Fragment(LOADER.render_template( @@ -271,7 +277,7 @@ def user_data(self): user_data["email"] = user.emails[0] user_data["role"] = runtime.get_user_role() user_data["course_id"] = runtime.course_id - user_data["course"] = re.sub('[^A-Za-z0-9]+', '', runtime.course_id._to_string()) # pylint: disable=protected-access + user_data["course"] = re.sub('[^A-Za-z0-9]+', '', runtime.course_id.to_deprecated_string()) # pylint: disable=protected-access user_data["username"] = user.opt_attrs['edx-platform.username'] user_data["anonymous_student_id"] = runtime.anonymous_student_id return user_data @@ -353,9 +359,13 @@ def login(self, user_data): rocket_chat_user = api.search_rocket_chat_user(user_data["username"]) LOG.info("Login method: result search user: %s", rocket_chat_user["success"]) - if rocket_chat_user['success']: - data = api.create_token(user_data["username"]) + key = hashlib.sha1("{}_{}".format(ROCKET_CHAT_DATA, user_data["username"])).hexdigest() + data = cache.get(key) + if data: + return data + elif rocket_chat_user['success']: + data = api.create_token(user_data["username"]) else: response = api.create_user(user_data["anonymous_student_id"], user_data[ "email"], user_data["username"]) @@ -364,7 +374,7 @@ def login(self, user_data): data = api.create_token(user_data["username"]) LOG.info("Login method: result create token: %s", data) - + cache.set(key, data, CACHE_TIMEOUT) return data def _add_user_to_course_group(self, group_name, user_id): @@ -730,3 +740,27 @@ def _grading_discussions(self, graded_group): def max_score(self): return self.weight + + @XBlock.handler + def logout_user(self, request=None, suffix=None): + """ + This method allows to invalidate the user token + """ + # pylint: disable=unused-argument + key = request.GET.get("beacon_rc") + data = cache.get(key) + if data: + api = self._api_rocket_chat() + user_data = data.get("data") + login_token = user_data.get("authToken") + user_id = user_data.get("userId") + response = api.logout_user(user_id, login_token) + try: + response = response.json() + if response.get("status") == "success": + cache.delete(key) + return Response(status=202) + except AttributeError: + return Response(status=503) + + return Response(status=404) diff --git a/rocketc/static/html/rocketc.html b/rocketc/static/html/rocketc.html index 6e6b7144..742b7165 100644 --- a/rocketc/static/html/rocketc.html +++ b/rocketc/static/html/rocketc.html @@ -5,9 +5,8 @@ {% if ui_is_block and team_view %} {% if "authToken" in response %} -