Skip to content

Commit 3276592

Browse files
committed
Expose granular permissions to frontend.
1 parent a011d6f commit 3276592

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

kolibri/auth/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ def get_session(self, request):
228228
session = {'id': 'current',
229229
'username': user.username,
230230
'full_name': user.full_name,
231-
'user_id': user.id}
231+
'user_id': user.id,
232+
'can_manage_content': user.can_manage_content}
232233
roles = Role.objects.filter(user_id=user.id)
233234
if len(roles) is not 0 or user.is_superuser:
234235
session.update({'facility_id': user.facility_id,

kolibri/auth/models.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ class Meta:
221221
is_superuser = False
222222
is_facility_user = False
223223

224+
can_manage_content = False
225+
224226
def get_short_name(self):
225227
return self.full_name.split(' ', 1)[0]
226228

@@ -478,13 +480,20 @@ def calculate_partition(self):
478480
def infer_dataset(self, *args, **kwargs):
479481
return self.facility.dataset
480482

481-
@property
482-
def is_superuser(self):
483+
def get_permission(self, permission):
483484
try:
484-
return self.devicepermissions.is_superuser
485+
return getattr(self.devicepermissions, 'is_superuser') or getattr(self.devicepermissions, permission)
485486
except ObjectDoesNotExist:
486487
return False
487488

489+
@property
490+
def can_manage_content(self):
491+
return self.get_permission('can_manage_content')
492+
493+
@property
494+
def is_superuser(self):
495+
return self.get_permission('is_superuser')
496+
488497
@property
489498
def is_staff(self):
490499
return self.is_superuser

kolibri/core/assets/src/state/actions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ function _sessionState(data) {
9898
facility_id: data.facility_id,
9999
kind: data.kind,
100100
error: data.error,
101+
can_manage_content: data.can_manage_content,
101102
};
102103
return state;
103104
}

kolibri/core/assets/src/state/store.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@ const baseLoggingState = {
77
attempt: {},
88
};
99

10+
const baseSessionState = {
11+
id: undefined,
12+
username: '',
13+
full_name: '',
14+
user_id: undefined,
15+
facility_id: undefined,
16+
kind: [UserKinds.ANONYMOUS],
17+
can_manage_content: false,
18+
};
19+
1020
// core state is namespaced, and merged with a particular app's state
1121
const initialState = {
1222
core: {
1323
error: '',
1424
loading: true,
1525
title: '',
1626
pageSessionId: 0,
17-
session: {
18-
id: undefined,
19-
username: '',
20-
full_name: '',
21-
user_id: undefined,
22-
facility_id: undefined,
23-
kind: [UserKinds.ANONYMOUS],
24-
},
27+
session: baseSessionState,
2528
loginError: null,
2629
logging: baseLoggingState,
2730
totalProgress: null,
@@ -36,7 +39,7 @@ const initialState = {
3639

3740
const mutations = {
3841
CORE_SET_SESSION(state, value) {
39-
state.core.session = value;
42+
Object.assign(state.core.session, value);
4043
},
4144
CORE_SET_FACILITY_CONFIG(state, facilityConfig) {
4245
state.core.facilityConfig = facilityConfig;
@@ -49,14 +52,7 @@ const mutations = {
4952
state.core.loginError = value;
5053
},
5154
CORE_CLEAR_SESSION(state) {
52-
state.core.session = {
53-
id: undefined,
54-
username: '',
55-
full_name: '',
56-
user_id: undefined,
57-
facility_id: undefined,
58-
kind: [UserKinds.ANONYMOUS],
59-
};
55+
Object.assign(state.core.session, baseSessionState);
6056
},
6157
CORE_SET_PAGE_LOADING(state, value) {
6258
const update = { loading: value };

0 commit comments

Comments
 (0)