242 lines
6.4 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* Documentation.js controller
*
* @description: A set of functions called "actions" of the `documentation` plugin.
*/
// Core dependencies.
const path = require('path');
const bcrypt = require('bcryptjs');
// Public dependencies.
2019-05-06 11:54:15 +02:00
const fs = require('fs-extra');
const _ = require('lodash');
2019-06-08 18:50:07 +02:00
const koaStatic = require('koa-static');
module.exports = {
async getInfos(ctx) {
try {
2021-09-02 11:25:24 +02:00
const docService = strapi.plugin('documentation').service('documentation');
const docVersions = docService.getDocumentationVersions();
const documentationAccess = await docService.getDocumentationAccess();
2019-06-08 18:50:07 +02:00
ctx.send({
docVersions,
2021-09-02 11:25:24 +02:00
currentVersion: docService.getDocumentationVersion(),
prefix: strapi.plugin('documentation').config('x-strapi-config').path,
documentationAccess,
2019-06-08 18:50:07 +02:00
});
} catch (err) {
ctx.badRequest(null, err.message);
}
},
async index(ctx, next) {
try {
/**
* We don't expose the specs using koa-static or something else due to security reasons.
* That's why, we need to read the file localy and send the specs through it when we serve the Swagger UI.
*/
const { major, minor, patch } = ctx.params;
2019-06-08 18:50:07 +02:00
const version =
major && minor && patch
? `${major}.${minor}.${patch}`
2022-08-08 23:33:39 +02:00
: strapi.plugin('documentation').service('documentation').getDocumentationVersion();
2021-10-14 10:48:29 -04:00
2019-06-08 18:50:07 +02:00
const openAPISpecsPath = path.join(
strapi.dirs.app.extensions,
2019-06-08 18:50:07 +02:00
'documentation',
'documentation',
version,
'full_documentation.json'
);
try {
const documentation = fs.readFileSync(openAPISpecsPath, 'utf8');
const layout = fs.readFileSync(
path.resolve(__dirname, '..', 'public', 'index.html'),
'utf8'
);
const filledLayout = _.template(layout)({
backendUrl: strapi.config.server.url,
spec: JSON.stringify(JSON.parse(documentation)),
});
try {
2019-06-08 18:50:07 +02:00
const layoutPath = path.resolve(
2022-03-14 17:54:35 +01:00
strapi.dirs.app.extensions,
2019-06-08 18:50:07 +02:00
'documentation',
'public',
'index.html'
);
2019-05-06 11:54:15 +02:00
await fs.ensureFile(layoutPath);
await fs.writeFile(layoutPath, filledLayout);
// Serve the file.
ctx.url = path.basename(`${ctx.url}/index.html`);
try {
2022-03-14 17:54:35 +01:00
const staticFolder = path.resolve(
strapi.dirs.app.extensions,
2022-03-14 17:54:35 +01:00
'documentation',
'public'
);
return koaStatic(staticFolder)(ctx, next);
} catch (e) {
strapi.log.error(e);
}
2019-06-08 18:50:07 +02:00
} catch (e) {
strapi.log.error(e);
}
} catch (e) {
strapi.log.error(e);
}
} catch (e) {
strapi.log.error(e);
}
},
async loginView(ctx, next) {
2021-09-03 11:11:37 +02:00
// lazy require cheerio
const cheerio = require('cheerio');
const { error } = ctx.query;
try {
const layout = fs.readFileSync(path.join(__dirname, '..', 'public', 'login.html'));
const filledLayout = _.template(layout)({
2021-07-20 12:12:30 +02:00
actionUrl: `${strapi.config.server.url}${
2021-08-06 18:09:49 +02:00
strapi.config.get('plugin.documentation.x-strapi-config').path
2021-07-20 12:12:30 +02:00
}/login`,
});
const $ = cheerio.load(filledLayout);
$('.error').text(_.isEmpty(error) ? '' : 'Wrong password...');
try {
2019-06-08 18:50:07 +02:00
const layoutPath = path.resolve(
strapi.dirs.app.extensions,
2019-06-08 18:50:07 +02:00
'documentation',
'public',
'login.html'
);
2019-05-06 11:54:15 +02:00
await fs.ensureFile(layoutPath);
await fs.writeFile(layoutPath, $.html());
ctx.url = path.basename(`${ctx.url}/login.html`);
try {
const staticFolder = path.resolve(strapi.dirs.app.extensions, 'documentation', 'public');
return koaStatic(staticFolder)(ctx, next);
} catch (e) {
strapi.log.error(e);
}
} catch (e) {
strapi.log.error(e);
}
} catch (e) {
strapi.log.error(e);
}
},
2019-06-08 18:50:07 +02:00
async login(ctx) {
const {
body: { password },
} = ctx.request;
const { password: hash } = await strapi
.store({ type: 'plugin', name: 'documentation', key: 'config' })
2019-06-08 18:50:07 +02:00
.get();
const isValid = await bcrypt.compare(password, hash);
let querystring = '?error=password';
if (isValid) {
ctx.session.documentation = {
logged: true,
};
querystring = '';
}
2019-06-08 18:50:07 +02:00
ctx.redirect(
2021-07-20 12:12:30 +02:00
`${strapi.config.server.url}${
2021-08-06 18:09:49 +02:00
strapi.config.get('plugin.documentation.x-strapi-config').path
2021-07-20 12:12:30 +02:00
}${querystring}`
2019-06-08 18:50:07 +02:00
);
},
async regenerateDoc(ctx) {
const { version } = ctx.request.body;
2021-09-02 11:25:24 +02:00
const service = strapi.service('plugin::documentation.documentation');
2022-08-08 23:33:39 +02:00
const documentationVersions = service.getDocumentationVersions().map((el) => el.version);
if (_.isEmpty(version)) {
return ctx.badRequest('Please provide a version.');
}
if (!documentationVersions.includes(version)) {
return ctx.badRequest('The version you are trying to generate does not exist.');
}
try {
strapi.reload.isWatching = false;
await service.generateFullDoc(version);
ctx.send({ ok: true });
} finally {
strapi.reload.isWatching = true;
}
},
async deleteDoc(ctx) {
const { version } = ctx.params;
const service = strapi.service('plugin::documentation.documentation');
2022-08-08 23:33:39 +02:00
const documentationVersions = service.getDocumentationVersions().map((el) => el.version);
if (_.isEmpty(version)) {
return ctx.badRequest('Please provide a version.');
}
if (!documentationVersions.includes(version)) {
return ctx.badRequest('The version you are trying to delete does not exist.');
}
try {
strapi.reload.isWatching = false;
await service.deleteDocumentation(version);
ctx.send({ ok: true });
} finally {
strapi.reload.isWatching = true;
}
},
async updateSettings(ctx) {
const { restrictedAccess, password } = ctx.request.body;
const pluginStore = strapi.store({ type: 'plugin', name: 'documentation' });
const config = {
restrictedAccess: Boolean(restrictedAccess),
};
if (restrictedAccess) {
if (_.isEmpty(password)) {
return ctx.badRequest('Please provide a password');
}
config.password = await bcrypt.hash(password, 10);
}
await pluginStore.set({ key: 'config', value: config });
return ctx.send({ ok: true });
},
};