Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the transformer middleware for individual content types for plu… #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 112 additions & 82 deletions server/register.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,128 @@
'use strict';
"use strict";

const _ = require('lodash');
const _ = require("lodash");

const { transform } = require('./middleware/transform');
const { getPluginService } = require('./util/getPluginService');
const { transform } = require("./middleware/transform");
const { getPluginService } = require("./util/getPluginService");

function addTransformMiddleware(route) {
// ensure path exists
if (!_.has(route, ['config', 'middlewares'])) {
_.set(route, ['config', 'middlewares'], []);
}
// ensure path exists
if (!_.has(route, ["config", "middlewares"])) {
_.set(route, ["config", "middlewares"], []);
}

// register route middleware
route.config.middlewares.push((ctx, next) => transform(strapi, ctx, next));
// register route middleware
route.config.middlewares.push((ctx, next) => transform(strapi, ctx, next));
}

// Helper functions
function isAllowableAPI({ mode, uid, filterValues }) {
// respect ct uid filter
const filterUID = _.get(filterValues, [uid], false);
if (mode === 'allow' && !filterUID && _.isBoolean(filterUID)) {
return false;
} else if (mode === 'deny' && filterUID && _.isBoolean(filterUID)) {
return false;
}

return true;
const filterUID = _.get(filterValues, [uid], false);

if (mode === "allow" && !filterUID && _.isBoolean(filterUID)) {
return false;
} else if (mode === "deny" && filterUID && _.isBoolean(filterUID)) {
return false;
}

return true;
}

function isAllowableMethod({ mode, uid, method, filterValues }) {
// respect ct uid method filter
const filterMethod = _.get(filterValues, [uid, method], null);
if (mode === 'allow' && !filterMethod && _.isBoolean(filterMethod)) {
return false;
} else if (mode === 'deny' && filterMethod && _.isBoolean(filterMethod)) {
return false;
}

return true;
const filterMethod = _.get(filterValues, [uid, method], null);
if (mode === "allow" && !filterMethod && _.isBoolean(filterMethod)) {
return false;
} else if (mode === "deny" && filterMethod && _.isBoolean(filterMethod)) {
return false;
}

return true;
}

function register({ strapi }) {
const settings = getPluginService('settingsService').get();
let ctFilterMode = _.get(settings, ['contentTypeFilter', 'mode'], 'none');
let pluginFilterMode = _.get(settings, ['plugins', 'mode'], 'allow');
const ctFilterUIDs = _.get(settings, ['contentTypeFilter', 'uids'], {});
const pluginFilterIDs = _.get(settings, ['plugins', 'ids'], {});
const apiTypes = ['api'];

// default uid list to all apis
if (_.size(ctFilterUIDs) === 0) {
ctFilterMode = 'none';
}

// default plugins list to none
if (_.size(pluginFilterIDs) !== 0) {
apiTypes.push('plugins');
}

_.forEach(apiTypes, (apiType) => {
const mode = apiType === 'api' ? ctFilterMode : pluginFilterMode;
const filterValues = apiType === 'api' ? ctFilterUIDs : pluginFilterIDs;
_.forEach(strapi[apiType], (api, apiName) => {
const uid = _.get(api, ['contentTypes', apiName, 'uid'], apiName);
if (!isAllowableAPI({ uid, mode, filterValues })) {
return;
}

_.forEach(api.routes, (router) => {
// skip admin routes
if (router.type && router.type === 'admin') {
return;
}

if (router.routes) {
// process routes
_.forEach(router.routes, (route) => {
if (!isAllowableMethod({ uid, mode, filterValues, method: route.method })) {
return;
}

addTransformMiddleware(route);
});
return;
}

if (!isAllowableMethod({ uid, mode, filterValues, method: router.method })) {
return;
}

// process route
addTransformMiddleware(router);
});
});
});
const settings = getPluginService("settingsService").get();

let ctFilterMode = _.get(settings, ["contentTypeFilter", "mode"], "none");
const pluginFilterMode = _.get(settings, ["plugins", "mode"], "allow");

const ctFilterUIDs = _.get(settings, ["contentTypeFilter", "uids"], {});
const pluginFilterIDs = _.get(settings, ["plugins", "ids"], {});

const apiTypes = ["api"];

if (_.size(ctFilterUIDs) === 0) {
ctFilterMode = "none";
}

if (_.size(pluginFilterIDs) !== 0) {
apiTypes.push("plugins");
}

_.forEach(apiTypes, (apiType) => {
const mode = apiType === "api" ? ctFilterMode : pluginFilterMode;
const filterValues = apiType === "api" ? ctFilterUIDs : pluginFilterIDs;

_.forEach(strapi[apiType], (api, apiName) => {
_.forEach(api.contentTypes, (contentType, contentTypeName) => {
let uid;

if (apiType === "plugins" && contentType.plugin === apiName) {
// Plugin content type
uid = contentType.uid;
const pluginUIDs = _.get(filterValues, [apiName, "uids"], {});

if (!isAllowableAPI({ uid, mode, filterValues: pluginUIDs })) {
return;
}
} else {
// General API content type
uid = contentType.uid;

if (!isAllowableAPI({ uid, mode, filterValues })) {
return;
}
}

_.forEach(api.routes, (router) => {
if (router.type && router.type === "admin") {
return;
}

if (router.routes) {
_.forEach(router.routes, (route) => {
if (
!isAllowableMethod({
uid,
mode,
filterValues,
method: route.method,
})
) {
return;
}
// Add transform middleware to the route
addTransformMiddleware(route);
});
return;
}

if (
!isAllowableMethod({
uid,
mode,
filterValues,
method: router.method,
})
) {
return;
}

// Add transform middleware to the router
addTransformMiddleware(router);
});
});
});
});
}

module.exports = register;
export default register;