Skip to content

Commit

Permalink
Merge pull request #397 from ralphiee22/incorrect_creds_fix
Browse files Browse the repository at this point in the history
Fixed incorrect credentials data formatting.
  • Loading branch information
rtibbles authored Aug 15, 2016
2 parents 8b81ef9 + d6849cc commit a288ecc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
25 changes: 19 additions & 6 deletions kolibri/auth/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 28 additions & 5 deletions kolibri/core/assets/src/core-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']();
Expand All @@ -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'));
}
Expand Down Expand Up @@ -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'));
});
Expand All @@ -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 &&
Expand Down
16 changes: 14 additions & 2 deletions kolibri/core/assets/src/core-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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;
Expand Down

0 comments on commit a288ecc

Please sign in to comment.