From 5ef2554643ea5dbac5a4c1271d5ab8b4b08d56a3 Mon Sep 17 00:00:00 2001 From: virgilchiriac Date: Thu, 25 Jul 2024 18:58:56 +0200 Subject: [PATCH] BC-7522 - use env vars for student visibility logic --- controllers/schools.js | 110 ++++++++++++++++++++++------------------- static/scripts/base.js | 6 ++- 2 files changed, 64 insertions(+), 52 deletions(-) diff --git a/controllers/schools.js b/controllers/schools.js index 17dbf54..d662ccc 100644 --- a/controllers/schools.js +++ b/controllers/schools.js @@ -17,7 +17,7 @@ const SCHOOL_FEATURES = [ 'rocketChat', 'videoconference', 'messenger', - // 'studentVisibility', + // 'studentVisibility', // is now handled instead with env vars / permission in school combination 'messengerSchoolRoom', 'oauthProvisioningEnabled', 'showOutdatedUsers', @@ -161,70 +161,78 @@ const getCreateHandler = (service) => { }; const getUpdateHandler = (service) => { - return function (req, res, next) { + return async function (req, res, next) { + try { + const configuration = await api(req, {version: 'v3'}).get(`/config/publicx`); + + if (configuration.TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE) { + await api(req, {version: 'v3'}).patch(`/school/${req.params.id}`, { + json: { + permissions: { + teacher: { + STUDENT_LIST: !!req.body.hasFeature_studentVisibility + } + } + }, + }) + } - // parse school features - req.body.features = []; - for (let feature of SCHOOL_FEATURES) { - let key = 'hasFeature_' + feature; - if (req.body[key]) { - req.body.features.push(feature); + // parse school features + req.body.features = []; + for (let feature of SCHOOL_FEATURES) { + let key = 'hasFeature_' + feature; + if (req.body[key]) { + req.body.features.push(feature); + } } - } - Promise.all([ - api(req, { version: 'v3' }).patch(`/school/${req.params.id}`, { - json: { - permissions: { - teacher: { - STUDENT_LIST: !!req.body.hasFeature_studentVisibility - } - }}, - }), - api(req) - .patch('/' + service + '/' + req.params.id, { + await api(req).patch('/' + service + '/' + req.params.id, { // TODO: sanitize json: req.body, }) - ]).then((data) => { - res.redirect(req.header('Referer')); - }) - .catch((err) => { - next(err); - }); + + res.redirect(req.header('Referer')); + } catch (err) { + next(err); + } }; }; const getDetailHandler = (service) => { - return function (req, res, next) { - api(req) - .get('/' + service + '/' + req.params.id) - .then((data) => { - // parse school features - for (let feature of SCHOOL_FEATURES) { - let key = 'hasFeature_' + feature; - if (data.features) { - data[key] = data.features.indexOf(feature) !== -1; - } else { - data[key] = false; - } + return async function (req, res, next) { + try { + const configuration = await api(req, { version: 'v3' }).get(`/config/public`); + const data = await api(req).get('/' + service + '/' + req.params.id) + + // parse school features + for (let feature of SCHOOL_FEATURES) { + let key = 'hasFeature_' + feature; + if (data.features) { + data[key] = data.features.indexOf(feature) !== -1; + } else { + data[key] = false; } + } - data.hasFeature_studentVisibility = false; - if (data.permissions && data.permissions.teacher && data.permissions.teacher.STUDENT_LIST) { - data.hasFeature_studentVisibility = true; - } + if (!configuration.TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE) { + data.hasFeature_studentVisibility_disabled = true; + } - if (data.county && data.county.name && data.county._id) { - data.county = data.county._id; - } + data.hasFeature_studentVisibility = !!configuration.TEACHER_STUDENT_VISIBILITY__IS_ENABLED_BY_DEFAULT; - res.json(data); - }) - .catch((err) => { - next(err); - }); - }; + if (data.permissions && data.permissions.teacher && data.permissions.teacher.STUDENT_LIST !== undefined) { + data.hasFeature_studentVisibility = data.permissions.teacher.STUDENT_LIST; + } + + if (data.county && data.county.name && data.county._id) { + data.county = data.county._id; + } + + res.json(data); + } catch (err) { + next(err); + } + } }; const getDeleteHandler = (service) => { diff --git a/static/scripts/base.js b/static/scripts/base.js index 151c917..83750e3 100644 --- a/static/scripts/base.js +++ b/static/scripts/base.js @@ -22,7 +22,7 @@ function updateQueryStringParameter(uri, key, value) { } function populateModalForm(modal, data) { - +console.log(data); var $title = modal.find('.modal-title'); var $btnSubmit = modal.find('.btn-submit'); var $btnClose = modal.find('.btn-close'); @@ -54,11 +54,15 @@ function populateModalForm(modal, data) { case "radio": case "checkbox": $(this).each(function () { + console.log($(this).attr('name'), $(this).prop('name'), value); if (($(this).attr('name') == $(this).prop('name')) && value) { $(this).attr("checked", value); } else { $(this).removeAttr("checked"); } + if (data.fields[$(this).attr('name') + "_disabled"] ){ + $(this).attr("disabled", true); + } }); break; case "datetime-local":