Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-4784 Use api/v3 for getting schools for login #3397

Merged
merged 8 commits into from
Feb 5, 2024
Merged
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
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}`);
dyedwiper marked this conversation as resolved.
Show resolved Hide resolved

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
Loading