Skip to content

Commit

Permalink
N21-1836 extends ctl with description, media id and publisher (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegns authored Apr 2, 2024
1 parent b33e030 commit 5a06e42
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 42 deletions.
69 changes: 33 additions & 36 deletions controllers/ctltools.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,62 @@ const { api } = require('../api');
const moment = require('moment');
moment.locale('de');

const trimWhitespaces = (object) => {
const clearEmptyInputs = (object) => {
Object.keys(object).forEach((key) => {
const type = typeof object[key];
if (type === 'object') {
trimWhitespaces(object[key]);
} else if (type === 'string') {
object[key] = object[key].trim();

switch (type) {
case 'object':
clearEmptyInputs(object[key]);

if(JSON.stringify(object[key]) === JSON.stringify({})) {
object[key] = undefined;
}
break;
case 'string':
object[key] = object[key].trim();

if(object[key] === "") {
object[key] = undefined;
}
break;
case 'boolean':
case 'number':
break;
default:
console.log('Unsupported type for sanitization');
break;
}
});
};
}

const sanitizeToolInputs = (id, body) => {
const transformToolInputs = (id, body) => {
body.id = id;
body.url = body.url || undefined;
body.logoUrl = body.logoUrl || undefined;

body.openNewTab = !!body.openNewTab;
body.isHidden = !!body.isHidden;
body.isDeactivated = !!body.isDeactivated;
body.config.baseUrl = body.config.baseUrl || undefined;
body.restrictToContexts = [].concat(body.restrictToContexts || []);

switch (body.config.type) {
case 'oauth2':
body.config.clientId = body.config.clientId || undefined;
body.config.clientSecret = body.config.clientSecret || undefined;
body.config.skipConsent = !!body.config.skipConsent;
body.config.redirectUris = body.config.redirectUris.split(';');
body.config.scope = body.config.scope || undefined;
body.config.frontchannelLogoutUri = body.config.frontchannelLogoutUri || undefined;
break;
case 'lti11':
body.config.secret = body.config.secret || undefined;
break;
case 'basic':
break;
default:
// eslint-disable-next-line no-console
console.log('Config type was not set! Server will crash');
throw new Error('Unknown tool config type');
if (body.config.type === 'oauth2') {
body.config.skipConsent = !!body.config.skipConsent;
body.config.redirectUris = body.config.redirectUris.split(';');
}

if(body.parameters && Array.isArray(body.parameters)) {
body.parameters.forEach((param) => {
param.isOptional = !!param.isOptional;
param.isProtected = !!param.isProtected;
param.defaultValue = param.defaultValue || undefined;
param.regex = param.regex || undefined;
if (!param.regex) {
param.regexComment = param.regexComment || undefined;
}
});
}

trimWhitespaces(body);
clearEmptyInputs(body);

return body;
};

const getUpdateHandler = (req, res, next) => {
req.body = sanitizeToolInputs(req.params.id, req.body);
req.body = transformToolInputs(req.params.id, req.body);

api(req, { version: 'v3' }).post(`/tools/external-tools/${req.params.id}`, {
json: req.body
Expand Down Expand Up @@ -114,7 +110,7 @@ const getDeleteHandler = (req, res, next) => {
};

const getCreateHandler = (req, res, next) => {
req.body = sanitizeToolInputs(undefined, req.body);
req.body = transformToolInputs(undefined, req.body);

api(req, { version: 'v3' }).post('/tools/external-tools/', {
json: req.body
Expand Down Expand Up @@ -190,6 +186,7 @@ const customParameterTypes = [
{ label: 'Context Name', value: 'auto_contextname' },
{ label: 'Schul Id', value: 'auto_schoolid' },
{ label: 'Offizielle Schulnummer', value: 'auto_schoolnumber' },
{ label: 'Medium Id', value: 'auto_mediumid' },
];

const customParameterLocations = [
Expand Down
31 changes: 25 additions & 6 deletions views/ctltools/forms/add-ctl-tool.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@
<label class="control-label" for="name">Name *</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Learn App" required>
</div>
<div class="form-group">
<label class="control-label" for="description">Beschreibung</label>
<textarea type="text" name="description" id="description" class="form-control" rows="4" placeholder="Sein Name steht für die größte Wende der Astronomie-Geschichte: Nikolaus Kopernikus begründete nichts Geringeres als das neue Weltbild, wonach die Sonne - nicht die Erde im Zentrum des Planetensystems steht."></textarea>
</div>
<div class="form-group">
<label class="control-label" for="url">URL</label>
<input type="text" name="url" id="url" class="form-control" placeholder="https://app.com">
</div>
<div class="form-group">
<label class="control-label" for="logoUrl">Logo-URL</label>
<input type="text" name="logoUrl" id="logoUrl" class="form-control" placeholder="">
<input type="text" name="logoUrl" id="logoUrl" class="form-control">
<p class="text-danger" style="font-size:0.75rem;">
Maximale Dateigröße 300 kByte; Zulässige Formate: jpg, png
</p>
</div>

<h3>Medium</h3>
<hr/>
<div class="form-group">
<label class="control-label" for="mediumId">Medium-Id</label>
<input type="text" name="medium[mediumId]" id="mediumId" class="form-control" placeholder="urn:bilo:medium:123456789">
</div>
<div class="form-group">
<label class="control-label" for="publisher">Anbieter</label>
<input type="text" name="medium[publisher]" id="publisher" class="form-control">
</div>

<h3>Verfügbarkeit</h3>
<hr/>
<div class="form-group">
<label class="control-label">Direkt in neuem Tab öffnen?</label>
<label style="width: 100%">
Expand Down Expand Up @@ -44,7 +62,9 @@
{{/each}}
</select>
</div>

<h3>Konfiguration</h3>
<hr/>
<div class="form-group">
<label class="control-label" for="baseUrl">BaseURL *</label>
<input type="text" name="config[baseUrl]" id="baseUrl" class="form-control" placeholder="http://base.url" required>
Expand All @@ -65,15 +85,15 @@
<h4>OAuth2</h4>
<div class="form-group">
<label class="control-label" for="oauth-client-id">ClientId *</label>
<input type="text" name="config[clientId]" id="oauth-client-id" class="form-control required" placeholder="">
<input type="text" name="config[clientId]" id="oauth-client-id" class="form-control required">
</div>
<div class="form-group">
<label class="control-label" for="oauth-client-secret">ClientSecret{{#if requireSecret}} *{{/if}}</label>
<input type="text" name="config[clientSecret]" id="oauth-client-secret" class="form-control {{#if requireSecret}}required{{/if}}" {{#unless requireSecret}}placeholder="*****"{{/unless}}>
</div>
<div class="form-group">
<label class="control-label" for="redirect-url">Redirect-URLs (Semikolon-separiert) *</label>
<input type="text" name="config[redirectUris]" id="redirect-url" class="form-control required" placeholder="">
<input type="text" name="config[redirectUris]" id="redirect-url" class="form-control required">
</div>
<div class="form-group">
<label class="control-label" for="token_endpoint_auth_method">Token Endpoint Auth Method *</label>
Expand Down Expand Up @@ -137,9 +157,8 @@
</div>
</div>

<h3 class="mt-1">Custom Parameters</h3>
<hr/>

<h3>Custom Parameters</h3>
<div class="custom-parameter-list"></div>
<button type="button" class="btn btn-primary btn-add-custom-parameter mt-1">+</button>
<div id="custom-parameter-template" style="display:none;">
Expand Down Expand Up @@ -200,7 +219,7 @@
</div>
<div class="form-group">
<label class="control-label parameters-regex-label">Regex (nur für Strings relevant)</label>
<input type="text" class="form-control parameters-regex" placeholder="" title="Regex">
<input type="text" class="form-control parameters-regex" title="Regex">
</div>
<div class="form-group">
<label class="control-label parameters-regex-comment-label">Regex Beschreibung</label>
Expand Down

0 comments on commit 5a06e42

Please sign in to comment.