Skip to content

Commit

Permalink
BC-4784 Use api/v3 for getting schools for login and school existence…
Browse files Browse the repository at this point in the history
… check (#3397)
  • Loading branch information
dyedwiper authored Feb 5, 2024
1 parent f683d69 commit f5cb13b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 48 deletions.
29 changes: 2 additions & 27 deletions controllers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,6 @@ const determineRedirectUrl = (req) => {
return '/dashboard';
};

const filterSchoolsWithLdapLogin = (schools) => schools
// eslint-disable-next-line max-len
.filter((school) => school.systems?.some((system) => system.type === 'ldap' && !system.oauthConfig));

async function getOauthSystems(req) {
return api(req, { version: 'v3' })
.get('/systems/public?onlyOauth=true')
Expand All @@ -338,7 +334,7 @@ router.all('/', async (req, res, next) => {
const oauthSystems = await getOauthSystems(req);

res.render('authentication/home', {
schools: filterSchoolsWithLdapLogin(schools),
schools,
systems: [],
oauthSystems: oauthSystems.data || [],
inline: true,
Expand Down Expand Up @@ -370,7 +366,7 @@ const renderLogin = async (req, res) => {

res.render('authentication/login', {
pageTitle: res.$t('home.header.link.login'),
schools: filterSchoolsWithLdapLogin(schools),
schools,
systems: [],
oauthSystems,
oauthErrorLogout,
Expand Down Expand Up @@ -446,27 +442,6 @@ router.get('/login/success', authHelper.authChecker, async (req, res) => {
return res.redirect(`/firstLogin?redirect=${redirectUrl}`);
}

// if this happens: SSO
const {
accountId,
systemId,
schoolId,
} = res.locals.currentPayload || {};
if (accountId && systemId && schoolId) {
const schools = await LoginSchoolsCache.get(req);
if (schools.length > 0) {
const checkSchool = schools.find((school) => school._id === schoolId);
if (checkSchool && checkSchool.systems) {
const schoolWithSystem = checkSchool.systems.find(
(system) => system._id === systemId,
);
if (schoolWithSystem) {
res.redirect(`/registration/${schoolId}/sso/${accountId}`);
}
}
}
}

const redirectUrl = determineRedirectUrl(req);
res.redirect(redirectUrl);

Expand Down
11 changes: 3 additions & 8 deletions controllers/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const { getCurrentLanguage } = require('../helpers/i18n');
const { setCookie } = require('../helpers/cookieHelper');
const { logger, formatError } = require('../helpers');

const { LoginSchoolsCache } = require('../helpers/cache');

let invalid = false;
const isProduction = NODE_ENV === 'production';

Expand Down Expand Up @@ -192,12 +190,9 @@ router.post(
);

const schoolExists = async (req, schoolId) => {
const schools = await LoginSchoolsCache.get(req);
if (schools.length > 0) {
const checkSchool = schools.find((school) => school._id === schoolId);
return checkSchool !== undefined;
}
return false;
const res = await api(req, { version: 'v3' }).get(`/school/exists/id/${schoolId}`);

return res.exists;
};

router.get(['/registration/:classOrSchoolId/byparent', '/registration/:classOrSchoolId/byparent/:sso/:accountId'],
Expand Down
2 changes: 1 addition & 1 deletion helpers/cache/schools.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { logger, formatError } = require('..');
const getLoginSchools = async (req) => {
let schools = [];
try {
schools = await api(req).get('/schoolsList');
schools = await api(req, { version: 'v3' }).get('/school/list-for-ldap-login');
} catch (err) {
logger.error('error getting schools', formatError(err));
}
Expand Down
14 changes: 6 additions & 8 deletions static/scripts/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,21 @@ $(document).ready(() => {
incTimer();
}

const loadSystems = (systems) => {
const setSystemOptions = (systems) => {
$systems.empty();

const ldapSystems = systems.filter((system) => system.type === 'ldap');

ldapSystems.forEach((system) => {
systems.forEach((system) => {
const systemAlias = system.alias ? ` (${system.alias})` : '';
let selected = false;
if (storage.local.getItem('loginSystem') === system._id) {
if (storage.local.getItem('loginSystem') === `${system.id}//${system.type}`) {
selected = true;
}
// eslint-disable-next-line max-len
$systems.append(`<option ${selected ? 'selected' : ''} value="${system._id}//${system.type}">${system.type}${systemAlias}</option>`);
$systems.append(`<option ${selected ? 'selected' : ''} value="${system.id}//${system.type}">${system.type}${systemAlias}</option>`);
});

// eslint-disable-next-line no-unused-expressions
ldapSystems.length < 2 ? $systems.parent().hide() : $systems.parent().show();
systems.length < 2 ? $systems.parent().hide() : $systems.parent().show();

$systems.trigger('chosen:updated');
};
Expand Down Expand Up @@ -223,7 +221,7 @@ $(document).ready(() => {
enableDisableLdapBtn(id);
const dataSystems = $(event.target).find(':selected').data('systems');
if (id !== '' && dataSystems) {
loadSystems(dataSystems);
setSystemOptions(dataSystems);
} else {
$systems.parent().hide();
}
Expand Down
8 changes: 4 additions & 4 deletions views/authentication/forms/login.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<select id="school" name="schoolId" class="school search-enabled" data-placeholder="{{$t "login.input.school"}}">
<option selected value="">{{$t "login.input.noSchoolsSelection"}}</option>
{{#each schools}}
<option value="{{this._id}}" data-systems='{{{json this.systems}}}'>{{this.name}}</option>
<option value="{{this.id}}" data-systems='{{{json this.systems}}}'>{{this.name}}</option>
{{else}}
<option disabled>{{$t "login.input.noSchoolsAvailable"}}</option>
{{/each}}
Expand All @@ -57,7 +57,7 @@
<label for="system">{{$t "login.label.system"}}</label>
<select id="system" name="system" class="system" data-placeholder="{{$t "login.input.system"}}">
{{#each systems}}
<option value="{{this._id}}//{{this.type}}">{{this.type}}{{#if this.alias}}({{this.alias}}){{/if}}</option>
<option value="{{this.id}}//{{this.type}}">{{this.type}}{{#if this.alias}}({{this.alias}}){{/if}}</option>
{{else}}
<option selected disabled>{{$t "login.input.noSystemsAvailable"}}</option>
{{/each}}
Expand Down Expand Up @@ -204,7 +204,7 @@
<select id="school" name="schoolId" class="school search-enabled" data-placeholder="{{$t "login.input.school"}}">
<option selected value="">{{$t "login.input.noSchoolsSelection"}}</option>
{{#each schools}}
<option value="{{this._id}}" data-systems='{{{json this.systems}}}'>{{this.name}}</option>
<option value="{{this.id}}" data-systems='{{{json this.systems}}}'>{{this.name}}</option>
{{else}}
<option disabled>{{$t "login.input.noSchoolsAvailable"}}</option>
{{/each}}
Expand All @@ -214,7 +214,7 @@
<div class="system-display">
<select id="system" name="system" class="system" data-placeholder="{{$t "login.input.system"}}">
{{#each systems}}
<option value="{{this._id}}//{{this.type}}">{{this.type}}{{#if this.alias}}({{this.alias}}){{/if}}</option>
<option value="{{this.id}}//{{this.type}}">{{this.type}}{{#if this.alias}}({{this.alias}}){{/if}}</option>
{{else}}
<option selected disabled>{{$t "login.input.noSystemsAvailable"}}</option>
{{/each}}
Expand Down

0 comments on commit f5cb13b

Please sign in to comment.