Skip to content

Commit

Permalink
Merge pull request #55 from eduNEXT/and/PE-123_logout_user
Browse files Browse the repository at this point in the history
Logout method
  • Loading branch information
diegomillan authored Sep 25, 2018
2 parents 753c6ba + 76fda60 commit 5549070
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
11 changes: 11 additions & 0 deletions rocketc/api_rocket_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 39 additions & 5 deletions rocketc/rocketc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
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

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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand All @@ -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):
Expand Down Expand Up @@ -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)
7 changes: 3 additions & 4 deletions rocketc/static/html/rocketc.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
{% if ui_is_block and team_view %}

{% if "authToken" in response %}
<div class="embed-container">
<iframe style="width:0;height:0;border:0; border:none;" src="{{public_url_service}}/home?authToken={{response.authToken}}&userId={{response.userId}}" >{% trans "IFrame is not supported" %}" %}</iframe>
<iframe id="myframe" src="{{public_url_service}}/group/{{response.default_group}}?layout=embedded" allow="camera; microphone">{% trans "IFrame is not supported" %}</iframe>
<div class="embed-container" data-beacon-rc="{{key}}">
<iframe id="myframe" src="{{public_url_service}}/group/{{response.default_group}}?layout=embedded&authToken={{response.authToken}}&userId={{response.userId}}" allow="camera; microphone">{% trans "IFrame is not supported" %}</iframe>
</div>
<div id="tool-buttons" data-domain="{{public_url_service}}/group/" data-user-id="{{response.userId}}" data-token="{{response.authToken}}" data-default="{{response.default_group}}">
<div>
Expand Down Expand Up @@ -59,7 +58,7 @@
{% else %}

{% if "authToken" in response %}
<div class="embed-container">
<div class="embed-container" data-beacon-rc="{{key}}">
<iframe id="myframe" src="{{public_url_service}}/home?authToken={{response.authToken}}&userId={{response.userId}}" allow="camera; microphone">{% trans "IFrame is not supported" %}</iframe>
</div>
{% else %}
Expand Down
16 changes: 16 additions & 0 deletions rocketc/static/js/src/rocketc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ function RocketChatXBlock(runtime, element) {

$(function ($) {
/* Here's where you'd do things on page load. */
$( document ).ajaxStop(function() {
var beacon_rc = localStorage.getItem("beacon_rc");
var beacon = $(".rocketc_block .embed-container").attr("data-beacon-rc");
if (beacon_rc != null && beacon_rc != beacon) {
var logoutUser= runtime.handlerUrl(element, "logout_user");
var data = {"key": beacon_rc};
$.ajax({
type: "GET",
url: logoutUser,
data: {beacon_rc},
});
localStorage.setItem("beacon_rc", beacon);
} else {
localStorage.setItem("beacon_rc", beacon);
}
});
});

var setDefaultChannel = runtime.handlerUrl(element, "set_default_channel");
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.2.22
current_version = 0.2.23
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from setuptools import setup

__version__ = '0.2.22'
__version__ = '0.2.23'


def package_data(pkg, roots):
Expand Down

0 comments on commit 5549070

Please sign in to comment.