diff --git a/kolibri/auth/api.py b/kolibri/auth/api.py index c2108a5189b..b35d906eb7d 100644 --- a/kolibri/auth/api.py +++ b/kolibri/auth/api.py @@ -158,23 +158,36 @@ def retrieve(self, request, pk=None): def get_session(self, request): user = get_user(request) if isinstance(user, AnonymousUser): - return {'id': None, 'username': '', 'full_name': '', 'user_id': None, 'facility_id': None, 'kind': ['ANONYMOUS'], 'error': '200'} - - session = {'id': 'current', 'username': user.username, + return {'id': None, + 'username': '', + 'full_name': '', + 'user_id': None, + 'facility_id': None, + 'kind': ['ANONYMOUS'], + 'error': '200'} + + session = {'id': 'current', + 'username': user.username, 'full_name': user.full_name, 'user_id': user.id} if isinstance(user, DeviceOwner): - session.update({'facility_id': None, 'kind': ['SUPERUSER'], 'error': '200'}) + session.update({'facility_id': None, + 'kind': ['SUPERUSER'], + 'error': '200'}) return session else: roles = Role.objects.filter(user_id=user.id) if len(roles) is not 0: - session.update({'facility_id': user.facility_id, 'kind': [], 'error': '200'}) + session.update({'facility_id': user.facility_id, + 'kind': [], + 'error': '200'}) for role in roles: if role.kind == 'admin': session['kind'].append('ADMIN') else: session['kind'].append('COACH') else: - session.update({'facility_id': user.facility_id, 'kind': ['LEARNER'], 'error': '200'}) + session.update({'facility_id': user.facility_id, + 'kind': ['LEARNER'], + 'error': '200'}) return session diff --git a/kolibri/core/assets/src/core-actions.js b/kolibri/core/assets/src/core-actions.js index 29a9259b643..7ff4329d718 100644 --- a/kolibri/core/assets/src/core-actions.js +++ b/kolibri/core/assets/src/core-actions.js @@ -2,6 +2,7 @@ const intervalTime = 5000; // Frequency at which time logging is updated const progressThreshold = 0.1; // Update logs if user has reached 20% more progress const timeThreshold = 30; // Update logs if 30 seconds have passed since last update const intervalTimer = require('./timer'); +const UserKinds = require('./constants').UserKinds; /** * Vuex State Mappers @@ -69,14 +70,31 @@ function _contentSessionModel(store) { return mapping; } +function _sessionState(data) { + const state = { + id: data.id, + username: data.username, + full_name: data.full_name, + user_id: data.user_id, + facility_id: data.facility_id, + kind: data.kind, + error: data.error, + }; + return state; +} + +/** + * Actions + * + * These methods are used to update client-side state + */ function kolibriLogin(store, Kolibri, sessionPayload) { const SessionResource = Kolibri.resources.SessionResource; const sessionModel = SessionResource.createModel(sessionPayload); const sessionPromise = sessionModel.save(sessionPayload); - const UserKinds = require('core-constants').UserKinds; sessionPromise.then((session) => { - store.dispatch('CORE_SET_SESSION', session); + store.dispatch('CORE_SET_SESSION', _sessionState(session)); /* Very hacky solution to redirect an admin or superuser to Manage tab on login*/ if (session.kind[0] === UserKinds.SUPERUSER || session.kind[0] === UserKinds.ADMIN) { const manageURL = Kolibri.urls['kolibri:managementplugin:management'](); @@ -88,7 +106,13 @@ function kolibriLogin(store, Kolibri, sessionPayload) { }).catch((error) => { // hack to handle invalid credentials if (error.status.code === 401) { - store.dispatch('CORE_HANDLE_WRONG_CREDS', { kind: 'ANONYMOUS', error: '401' }); + store.dispatch('CORE_HANDLE_WRONG_CREDS', { id: undefined, + username: '', + full_name: '', + user_id: undefined, + facility_id: undefined, + kind: [UserKinds.ANONYMOUS], + error: '401' }); } else { store.dispatch('CORE_SET_ERROR', JSON.stringify(error, null, '\t')); } @@ -116,7 +140,7 @@ function currentLoggedInUser(store, Kolibri) { const sessionModel = SessionResource.getModel(id); const sessionPromise = sessionModel.fetch(); sessionPromise.then((session) => { - store.dispatch('CORE_SET_SESSION', session); + store.dispatch('CORE_SET_SESSION', _sessionState(session)); }).catch((error) => { store.dispatch('CORE_SET_ERROR', JSON.stringify(error, null, '\t')); }); @@ -138,7 +162,6 @@ function togglemodal(store, bool) { function initContentSession(store, Kolibri, channelId, contentId, contentKind) { const ContentSessionLogResource = Kolibri.resources.ContentSessionLogResource; const ContentSummaryLogResource = Kolibri.resources.ContentSummaryLogResource; - const UserKinds = require('core-constants').UserKinds; /* Create summary log iff user exists */ if (store.state.core.session.user_id && diff --git a/kolibri/core/assets/src/core-store.js b/kolibri/core/assets/src/core-store.js index 4138fc344c4..16d616e57fa 100644 --- a/kolibri/core/assets/src/core-store.js +++ b/kolibri/core/assets/src/core-store.js @@ -7,7 +7,13 @@ const initialState = { core: { error: '', loading: true, - session: { kind: UserKinds.ANONYMOUS, error: '200' }, + session: { id: undefined, + username: '', + full_name: '', + user_id: undefined, + facility_id: undefined, + kind: [UserKinds.ANONYMOUS], + error: '200' }, login_modal_state: false, fullname: '', logging: { @@ -27,7 +33,13 @@ const mutations = { state.core.session = value; }, CORE_CLEAR_SESSION(state) { - state.core.session = { kind: UserKinds.ANONYMOUS, error: '200' }; + state.core.session = { id: undefined, + username: '', + full_name: '', + user_id: undefined, + facility_id: undefined, + kind: [UserKinds.ANONYMOUS], + error: '200' }; }, CORE_SET_PAGE_LOADING(state, value) { state.core.loading = value;