feat: Allow defining active plugins in config (#10767)

* Revert "Revert "feat: cross origin opener policy options (#10710)""

This reverts commit 46050ace1a.

* Revert "Revert "chore(i18n): fallback strings for new resources: nodebb.admin-settings-advanced""

This reverts commit 9f291c07d3.

* feat: closes #10719, don't trim children if category is marked section

* feat: fire hook to allow plugins to filter the pids returned in a user profile

/cc julianlam/nodebb-plugin-support-forum#14

* fix: use `user.hidePrivateData();` more consistently across user retrieval endpoints

* feat: Allow defining active plugins in config

resolves #10766

* fix: assign the db result to files properly

* test: add tests with plugins in config

* feat: better theme change handling

* feat: add visual indication that plugins can't be activated

* test: correct hooks

* test: fix test definitions

* test: remove instead of resetting nconf to avoid affecting other tests

* test: ... I forgot how nconf worked

* fix: remove negation

* docs: improve wording of error message

* feat: reduce code duplication

* style: remove a redundant space

* fix: remove unused imports

* fix: use nconf instead of requiring config.json

* fix: await...

* fix: second missed await

* fix: move back from getActiveIds to getActive

* fix: use paths again?

* fix: typo

* fix: move require into the function

* fix: forgot to change back to getActive

* test: getActive returns only id

* test: accedently commented out some stuff

* feat: added note to top of plugins page if \!canChangeState

Co-authored-by: Julian Lam <julian@nodebb.org>
Co-authored-by: Barış Soner Uşaklı <barisusakli@gmail.com>
This commit is contained in:
Opliko
2022-07-25 20:04:55 +02:00
committed by Julian Lam
parent a6af47da02
commit 23cb67a112
16 changed files with 176 additions and 42 deletions

View File

@@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const winston = require('winston');
const _ = require('lodash');
const nconf = require('nconf');
const db = require('../database');
const file = require('../file');
@@ -13,11 +14,19 @@ const Data = module.exports;
const basePath = path.join(__dirname, '../../');
// to get this functionality use `plugins.getActive()` from `src/plugins/install.js` instead
// this method duplicates that one, because requiring that file here would have side effects
async function getActiveIds() {
if (nconf.get('plugins:active')) {
return nconf.get('plugins:active');
}
return await db.getSortedSetRange('plugins:active', 0, -1);
}
Data.getPluginPaths = async function () {
const plugins = await db.getSortedSetRange('plugins:active', 0, -1);
const plugins = await getActiveIds();
const pluginPaths = plugins.filter(plugin => plugin && typeof plugin === 'string')
.map(plugin => path.join(paths.nodeModules, plugin));
const exists = await Promise.all(pluginPaths.map(file.exists));
exists.forEach((exists, i) => {
if (!exists) {
@@ -96,7 +105,6 @@ Data.getStaticDirectories = async function (pluginData) {
route}. Path must adhere to: ${validMappedPath.toString()}`);
return;
}
const dirPath = await resolveModulePath(pluginData.path, pluginData.staticDirs[route]);
try {
const stats = await fs.promises.stat(dirPath);

View File

@@ -56,6 +56,10 @@ module.exports = function (Plugins) {
}
Plugins.toggleActive = async function (id) {
if (nconf.get('plugins:active')) {
winston.error('Cannot activate plugins while plugin state is set in the configuration (config.json, environmental variables or terminal arguments), please modify the configuration instead');
throw new Error('[[error:plugins-set-in-configuration]]');
}
const isActive = await Plugins.isActive(id);
if (isActive) {
await db.sortedSetRemove('plugins:active', id);
@@ -144,10 +148,16 @@ module.exports = function (Plugins) {
};
Plugins.isActive = async function (id) {
if (nconf.get('plugins:active')) {
return nconf.get('plugins:active').includes(id);
}
return await db.isSortedSetMember('plugins:active', id);
};
Plugins.getActive = async function () {
if (nconf.get('plugins:active')) {
return nconf.get('plugins:active');
}
return await db.getSortedSetRange('plugins:active', 0, -1);
};