Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/services/user-group/hooks/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ const restrictChangesToArchivedCourse = async (context) => {
return context;
};

const restrictLockedCourse = async (context) => {
// TODO except Admin and Superhero?
const course = await context.app.service('courses').get(context.id);
if (!course.teacherIds || course.teacherIds.length === 0) {
return Promise.reject(new Forbidden('This course is locked as it has no assigned teachers.'));
}
return context;
};

const filterOutLockedCourses = (context) => {
// TODO excfilterOutLockedCoursesept admin and superhero?
if (context.method === 'find') {
context.params.query.$and = context.params.query.$and || [];
context.params.query.$and.push({
teacherIds: { $size: { $gt: 0 } },
});
}
return context;
};

module.exports = {
splitClassIdsInGroupsAndClasses,
addWholeClassToCourse,
Expand All @@ -287,4 +307,6 @@ module.exports = {
patchPermissionHook,
restrictChangesToArchivedCourse,
restrictChangesToSyncedCourse,
restrictLockedCourse,
filterOutLockedCourses,
};
9 changes: 5 additions & 4 deletions src/services/user-group/services/courseModelService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { iff, isProvider, disallow } = require('feathers-hooks-common');
const auth = require('@feathersjs/authentication');

const { courseModel } = require('../model');
const { restrictLockedCourse, filterOutLockedCourses } = require('../hooks/courses');

const courseModelService = service({
Model: courseModel,
Expand All @@ -16,11 +17,11 @@ const courseModelService = service({
const courseModelServiceHooks = {
before: {
all: [auth.hooks.authenticate('jwt')],
find: [iff(isProvider('external'), disallow())],
get: [iff(isProvider('external'), disallow())],
find: [filterOutLockedCourses, iff(isProvider('external'), disallow())],
get: [restrictLockedCourse, iff(isProvider('external'), disallow())],
create: [iff(isProvider('external'), disallow())],
update: [iff(isProvider('external'), disallow())],
patch: [iff(isProvider('external'), disallow())],
update: [restrictLockedCourse, iff(isProvider('external'), disallow())],
patch: [restrictLockedCourse, iff(isProvider('external'), disallow())],
remove: [iff(isProvider('external'), disallow())],
},
after: {
Expand Down
11 changes: 10 additions & 1 deletion src/services/user-group/services/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const {
restrictChangesToArchivedCourse,
removeSubstitutionDuplicates,
restrictChangesToSyncedCourse,
restrictLockedCourse,
filterOutLockedCourses,
} = require('../hooks/courses');

const { checkScopePermissions } = require('../../helpers/scopePermissions/hooks');
Expand Down Expand Up @@ -89,13 +91,18 @@ const courseHooks = {
all: [authenticate('jwt')],
find: [
hasPermission('COURSE_VIEW'),
filterOutLockedCourses,
restrictToCurrentSchoolIfNotLocal,
restrictToUsersOwnCoursesIfNotLocal,
iff(isProvider('external'), getRestrictPopulatesHook(populateWhitelist)),
mapPaginationQuery,
addCollation,
],
get: [courseInviteHook, iff(isProvider('external'), getRestrictPopulatesHook(populateWhitelist))],
get: [
restrictLockedCourse,
courseInviteHook,
iff(isProvider('external'), getRestrictPopulatesHook(populateWhitelist)),
],
create: [
injectUserId,
hasPermission('COURSE_CREATE'),
Expand All @@ -105,13 +112,15 @@ const courseHooks = {
iff(isProvider('external'), preventPopulate),
],
update: [
restrictLockedCourse,
checkScopePermissions(['COURSE_EDIT']),
restrictToCurrentSchoolIfNotLocal,
restrictToUsersOwnCoursesIfNotLocal,
restrictChangesToArchivedCourse,
iff(isProvider('external'), preventPopulate),
],
patch: [
restrictLockedCourse,
patchPermissionHook,
restrictToCurrentSchoolIfNotLocal,
restrictChangesToArchivedCourse,
Expand Down
Loading