Apply Alex's missing feedback

This commit is contained in:
fdel-car 2023-01-19 17:21:08 +01:00
parent c43369165c
commit 5f86716429
7 changed files with 29 additions and 47 deletions

View File

@ -3,7 +3,6 @@
const { features } = require('@strapi/strapi/lib/utils/ee');
const enableFeatureMiddleware = (featureName) => (ctx, next) => {
console.log(featureName, features.isEnabled(featureName));
if (features.isEnabled(featureName)) {
return next();
}

View File

@ -13,13 +13,13 @@ describe('Passport', () => {
afterEach(() => {
// Reset the mock on passport.use.toHaveBeenCalledTimes
jest.resetAllMocks();
// Reset the mock on strapi/lib/utils/ee so we can change its behavior
// Reset the mock on strapi/ee so we can change its behavior
jest.resetModules();
});
describe('Init (SSO disabled)', () => {
beforeAll(() => {
jest.mock('@strapi/strapi/lib/utils/ee', () => ({
jest.mock('@strapi/strapi/ee', () => ({
features: {
// Disable the SSO feature
isEnabled: (feature) => feature !== 'sso',
@ -57,7 +57,7 @@ describe('Passport', () => {
describe('Init (SSO enabled)', () => {
beforeAll(() => {
jest.mock('@strapi/strapi/lib/utils/ee', () => ({
jest.mock('@strapi/strapi/ee', () => ({
features: {
// Enable all the features (including SSO)
isEnabled: () => true,

View File

@ -1,21 +1,15 @@
'use strict';
jest.mock('@strapi/strapi/lib/utils/ee', () => {
const eeModule = () => true;
Object.assign(eeModule, {
features: {
isEnabled() {
return true;
},
getEnabled() {
return ['sso'];
},
jest.mock('@strapi/strapi/ee', () => ({
features: {
isEnabled() {
return true;
},
});
return eeModule;
});
getEnabled() {
return ['sso'];
},
},
}));
const {
syncProviderRegistryWithConfig,

View File

@ -6,28 +6,6 @@ const { features } = require('@strapi/strapi/lib/utils/ee');
const createLocalStrategy = require('../../../server/services/passport/local-strategy');
const sso = require('./passport/sso');
// wrap functions with feature flag to allow execute code lazyly
// Looking at the code wrapped we probably can just add a condition in the functions
const wrapWithFeatureFlag = (flag, obj) => {
const newObj = {};
Object.keys(obj).forEach((key) => {
if (typeof obj[key] === 'function') {
newObj[key] = (...args) => {
if (!features.isEnabled(flag)) {
throw new Error(`${key} cannot be executed`);
}
return obj[key].apply(newObj, ...args);
};
} else {
newObj[key] = obj[key];
}
});
return newObj;
};
const getPassportStrategies = () => {
const localStrategy = createLocalStrategy(strapi);
@ -47,5 +25,5 @@ const getPassportStrategies = () => {
module.exports = {
getPassportStrategies,
...wrapWithFeatureFlag('sso', sso),
...sso,
};

View File

@ -1,13 +1,24 @@
'use strict';
const ee = require('@strapi/strapi/ee');
const { authEventsMapper } = require('../../../../server/services/passport');
const createProviderRegistry = require('./provider-registry');
const providerRegistry = createProviderRegistry();
const getStrategyCallbackURL = (providerName) => `/admin/connect/${providerName}`;
const getStrategyCallbackURL = (providerName) => {
if (!ee.features.isEnabled('sso')) {
throw new Error(`Cannot be executed`);
}
return `/admin/connect/${providerName}`;
};
const syncProviderRegistryWithConfig = () => {
if (!ee.features.isEnabled('sso')) {
throw new Error(`Cannot be executed`);
}
const { providers = [] } = strapi.config.get('admin.auth', {});
providerRegistry.registerMany(providers);

View File

@ -56,7 +56,6 @@ const onlineUpdate = async ({ strapi }) => {
const transaction = await strapi.db.transaction();
try {
// TODO: Use the core store interface instead, it does not support transactions and "FOR UPDATE" at the moment
const eeInfo = await strapi.db
.queryBuilder(eeStoreModel.uid)
.where({ key: 'ee_information' })

View File

@ -123,7 +123,10 @@ class Strapi {
createUpdateNotifier(this).notify();
Object.defineProperty(this, 'EE', {
get: () => ee.isEE,
get: () => {
ee.init(this.dirs.app.root, this.log);
return ee.isEE;
},
configurable: false,
});
}
@ -371,8 +374,6 @@ class Strapi {
}
async register() {
ee.init(this.dirs.app.root, this.log);
await Promise.all([
this.loadApp(),
this.loadSanitizers(),