fix(terms): Display template notice, support custom terms loading
Closes #1523
This commit is contained in:
@@ -57,6 +57,12 @@
|
||||
* format: uri
|
||||
* description: URL to the customer management panel (conditionally added for admins if configured)
|
||||
* example: https://panel.example.com
|
||||
* termsLanguages:
|
||||
* type: array
|
||||
* description: List of available language codes for terms localization
|
||||
* items:
|
||||
* type: string
|
||||
* example: [de-DE, en-US]
|
||||
* version:
|
||||
* type: string
|
||||
* description: Current version of the PLANKA application
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
* description: Language code for terms localization
|
||||
* schema:
|
||||
* type: string
|
||||
* enum: [de-DE, en-US]
|
||||
* example: en-US
|
||||
* responses:
|
||||
* 200:
|
||||
@@ -41,7 +40,6 @@
|
||||
* properties:
|
||||
* language:
|
||||
* type: string
|
||||
* enum: [de-DE, en-US]
|
||||
* description: Language code used
|
||||
* example: en-US
|
||||
* content:
|
||||
@@ -65,7 +63,6 @@ module.exports = {
|
||||
inputs: {
|
||||
language: {
|
||||
type: 'string',
|
||||
isIn: User.LANGUAGES,
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ module.exports = {
|
||||
fn(inputs) {
|
||||
const data = {
|
||||
oidc: inputs.oidc,
|
||||
termsLanguages: sails.hooks.terms.getLanguages(),
|
||||
version: sails.config.custom.version,
|
||||
};
|
||||
|
||||
|
||||
@@ -12,25 +12,35 @@
|
||||
*/
|
||||
|
||||
const fsPromises = require('fs').promises;
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const LANGUAGES = ['de-DE', 'en-US'];
|
||||
const DEFAULT_LANGUAGE = 'en-US';
|
||||
|
||||
const getContent = (language = DEFAULT_LANGUAGE) =>
|
||||
fsPromises.readFile(
|
||||
`${sails.config.appPath}/terms/${sails.config.custom.termsType}/${language}.md`,
|
||||
'utf8',
|
||||
);
|
||||
const PATH = path.join(sails.config.appPath, 'terms');
|
||||
const TEMPLATE_TYPE = '_template';
|
||||
|
||||
const hashContent = (content) => crypto.createHash('sha256').update(content).digest('hex');
|
||||
|
||||
module.exports = function defineTermsHook(sails) {
|
||||
let type;
|
||||
let languages;
|
||||
let defaultLanguage;
|
||||
let signature;
|
||||
|
||||
return {
|
||||
LANGUAGES,
|
||||
const getLanguages = async () => {
|
||||
const entries = await fsPromises.readdir(path.join(PATH, type), {
|
||||
withFileTypes: true,
|
||||
});
|
||||
|
||||
return entries
|
||||
.filter((entry) => entry.isFile() && path.extname(entry.name) === '.md')
|
||||
.map((entry) => path.basename(entry.name, '.md'))
|
||||
.sort();
|
||||
};
|
||||
|
||||
const getContent = (language) =>
|
||||
fsPromises.readFile(path.join(PATH, type, `${language}.md`), 'utf8');
|
||||
|
||||
return {
|
||||
/**
|
||||
* Runs when this Sails app loads/lifts.
|
||||
*/
|
||||
@@ -38,13 +48,32 @@ module.exports = function defineTermsHook(sails) {
|
||||
async initialize() {
|
||||
sails.log.info('Initializing custom hook (`terms`)');
|
||||
|
||||
const content = await getContent();
|
||||
type = sails.config.custom.termsType;
|
||||
|
||||
try {
|
||||
languages = await getLanguages();
|
||||
} catch (error) {
|
||||
/* empty */
|
||||
}
|
||||
|
||||
if (!languages || languages.length === 0) {
|
||||
sails.log.warn('Custom terms not found, falling back to template');
|
||||
|
||||
type = TEMPLATE_TYPE;
|
||||
languages = await getLanguages();
|
||||
}
|
||||
|
||||
defaultLanguage = languages.includes(sails.config.i18n.defaultLocale)
|
||||
? sails.config.i18n.defaultLocale
|
||||
: languages[0];
|
||||
|
||||
const content = await getContent(defaultLanguage);
|
||||
signature = hashContent(content);
|
||||
},
|
||||
|
||||
async getPayload(language = DEFAULT_LANGUAGE) {
|
||||
if (!LANGUAGES.includes(language)) {
|
||||
language = DEFAULT_LANGUAGE; // eslint-disable-line no-param-reassign
|
||||
async getPayload(language) {
|
||||
if (!language || !languages.includes(language)) {
|
||||
language = defaultLanguage; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
|
||||
const content = await getContent(language);
|
||||
@@ -56,6 +85,10 @@ module.exports = function defineTermsHook(sails) {
|
||||
};
|
||||
},
|
||||
|
||||
getLanguages() {
|
||||
return languages;
|
||||
},
|
||||
|
||||
isSignatureValid(value) {
|
||||
return value === signature;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user