mirror of
https://github.com/strapi/strapi.git
synced 2025-12-26 06:35:47 +00:00
Add policy loader
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
ed616333b2
commit
5595f6ca94
1
examples/getstarted/.env
Normal file
1
examples/getstarted/.env
Normal file
@ -0,0 +1 @@
|
||||
ADMIN_AUTO_OPEN=false
|
||||
4
examples/getstarted/config/policies/test.js
Normal file
4
examples/getstarted/config/policies/test.js
Normal file
@ -0,0 +1,4 @@
|
||||
module.exports = (ctx, next) => {
|
||||
console.log('la');
|
||||
next();
|
||||
};
|
||||
@ -1,4 +1,7 @@
|
||||
module.exports = ({ env }) => ({
|
||||
host: env('HOST', 'localhost'),
|
||||
port: env.int('PORT', 1337),
|
||||
admin: {
|
||||
autoOpen: env.bool('ADMIN_AUTO_OPEN', true),
|
||||
},
|
||||
});
|
||||
|
||||
@ -145,12 +145,17 @@ module.exports = async () => {
|
||||
}
|
||||
|
||||
if (!(await pluginStore.get({ key: 'advanced' }))) {
|
||||
const host = strapi.config.get('server.host');
|
||||
const port = strapi.config.get('server.port');
|
||||
|
||||
const uri = `http://${host}:${port}/admin`;
|
||||
|
||||
const value = {
|
||||
unique_email: true,
|
||||
allow_register: true,
|
||||
email_confirmation: false,
|
||||
email_confirmation_redirection: `http://${strapi.config.currentEnvironment.server.host}:${strapi.config.currentEnvironment.server.port}/admin`,
|
||||
email_reset_password: `http://${strapi.config.currentEnvironment.server.host}:${strapi.config.currentEnvironment.server.port}/admin`,
|
||||
email_confirmation_redirection: uri,
|
||||
email_reset_password: uri,
|
||||
default_role: 'authenticated',
|
||||
};
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ const { createDatabaseManager } = require('strapi-database');
|
||||
|
||||
const utils = require('./utils');
|
||||
const loadModules = require('./core/load-modules');
|
||||
const loadConfiguration = require('./core/app-configuration');
|
||||
const bootstrap = require('./core/bootstrap');
|
||||
const initializeMiddlewares = require('./middlewares');
|
||||
const initializeHooks = require('./hooks');
|
||||
@ -25,7 +26,6 @@ const { createCoreStore, coreStoreModel } = require('./services/core-store');
|
||||
const createEntityService = require('./services/entity-service');
|
||||
const createEntityValidator = require('./services/entity-validator');
|
||||
const createTelemetry = require('./services/metrics');
|
||||
const loadConfiguration = require('./services/configuration-loader');
|
||||
|
||||
/**
|
||||
* Construct an Strapi instance.
|
||||
@ -202,7 +202,7 @@ class Strapi {
|
||||
|
||||
if (
|
||||
(this.config.environment === 'development' &&
|
||||
_.get(this.config.currentEnvironment, 'server.admin.autoOpen', true) !== false) ||
|
||||
this.config.get('server.admin.autoOpen', true) !== false) ||
|
||||
!isInitialised
|
||||
) {
|
||||
await utils.openBrowser.call(this);
|
||||
|
||||
@ -44,7 +44,7 @@ const loadJsFile = file => {
|
||||
|
||||
return jsModule;
|
||||
} catch (error) {
|
||||
throw new Error(`Imposibble to load js config file ${file}: ${error.message}`);
|
||||
throw new Error(`Could not load js config file ${file}: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
@ -52,6 +52,6 @@ const loadJSONFile = file => {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(file));
|
||||
} catch (error) {
|
||||
throw new Error(`Imposibble to load json config file ${file}: ${error.message}`);
|
||||
throw new Error(`Could not load json config file ${file}: ${error.message}`);
|
||||
}
|
||||
};
|
||||
@ -10,7 +10,9 @@ const path = require('path');
|
||||
|
||||
const createConfigProvider = require('./config-provider');
|
||||
const loadConfigDir = require('./config-loader');
|
||||
|
||||
const getPrefixedDeps = require('../../utils/get-prefixed-dependencies');
|
||||
const loadPolicies = require('../load-policies');
|
||||
|
||||
const CONFIG_PATHS = {
|
||||
admin: 'admin',
|
||||
@ -49,6 +51,7 @@ module.exports = (dir, initialConfig = {}) => {
|
||||
functions: {},
|
||||
routes: {},
|
||||
info: pkgJSON,
|
||||
policies: loadPolicies(path.resolve(dir, 'config', 'policies')),
|
||||
installedPlugins: getPrefixedDeps('strapi-plugin', pkgJSON),
|
||||
installedMiddlewares: getPrefixedDeps('strapi-middleware', pkgJSON),
|
||||
installedHooks: getPrefixedDeps('strapi-hook', pkgJSON),
|
||||
3
packages/strapi/lib/core/bootstrap.js
vendored
3
packages/strapi/lib/core/bootstrap.js
vendored
@ -162,9 +162,10 @@ module.exports = function(strapi) {
|
||||
strapi.config.middleware.settings = Object.keys(strapi.middleware).reduce((acc, current) => {
|
||||
// Try to find the settings in the current environment, then in the main configurations.
|
||||
const currentSettings = _.merge(
|
||||
_.get(_.cloneDeep(strapi.middleware[current]), ['defaults', current], {}),
|
||||
_.cloneDeep(_.get(strapi.middleware[current], ['defaults', current], {})),
|
||||
strapi.config.get(['middleware', 'settings', current], {})
|
||||
);
|
||||
|
||||
acc[current] = !_.isObject(currentSettings) ? {} : currentSettings;
|
||||
|
||||
// Ensure that enabled key exist by forcing to false.
|
||||
|
||||
32
packages/strapi/lib/core/load-policies.js
Normal file
32
packages/strapi/lib/core/load-policies.js
Normal file
@ -0,0 +1,32 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
module.exports = dir => {
|
||||
if (!fs.existsSync(dir)) return {};
|
||||
|
||||
return fs
|
||||
.readdirSync(dir, { withFileTypes: true })
|
||||
.filter(file => file.isFile())
|
||||
.reduce((acc, file) => {
|
||||
const key = path.basename(file.name, path.extname(file.name));
|
||||
|
||||
acc[key] = loadPolicy(path.resolve(dir, file.name));
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
};
|
||||
|
||||
const loadPolicy = file => {
|
||||
try {
|
||||
const policy = require(file);
|
||||
|
||||
assert(typeof policy === 'function', 'Policy must be a function.');
|
||||
|
||||
return policy;
|
||||
} catch (error) {
|
||||
throw `Could not load policy ${file}: ${error.message}`;
|
||||
}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user