Remove dashboard hook

This commit is contained in:
pierreburgy 2015-11-19 13:34:36 +01:00
parent b0d562ea48
commit 56ef28f6c3
19 changed files with 3 additions and 522 deletions

View File

@ -1,87 +0,0 @@
'use strict';
/**
* Returns the config for the dashboard.
*/
module.exports = function * () {
let user;
let isAdmin = false;
try {
user = yield strapi.api.user.services.jwt.getToken(this, true);
if (user && user.id) {
// Find the user in the database.
user = yield strapi.orm.collections.user.findOne(user.id).populate('roles');
// Check if the user has the role `admin`.
isAdmin = _.findWhere(user.roles, {name: 'admin'});
if (!isAdmin) {
this.status = 403;
this.body = {
message: 'You must be have the role admin to get the config of the app.'
};
return;
}
}
} catch (err) {
}
try {
// Init output object.
const output = {};
// Set the config.
output.settings = {};
output.settings.url = strapi.config.url;
// Define if the app is considered as new.
const userCount = yield strapi.orm.collections.user.count();
output.settings.isNewApp = !userCount;
// User is not connected.
if (!user) {
output.connected = false;
this.body = output;
return;
} else {
output.connected = true;
}
// i18n config.
output.settings.i18n = strapi.config.i18n;
// Set the models.
output.models = strapi.models;
// Delete `toJSON` attribute in every models.
_.forEach(output.models, function (model) {
delete model.attributes.toJSON;
});
// Format `config.api` for multi templates models.
_.forEach(strapi.api, function (api, key) {
if (api.templates) {
output.models[key].templates = {};
}
// Assign the template attributes with the model attributes.
_.forEach(api.templates, function (template, templateName) {
output.models[key].templates[templateName] = {};
output.models[key].templates[templateName].attributes = {};
_.forEach(template.attributes, function (value, attributeKey) {
output.models[key].templates[templateName].attributes[attributeKey] = _.cloneDeep(output.models[key].attributes[attributeKey]);
});
output.models[key].templates[templateName].displayedAttribute = template.displayedAttribute;
});
});
// Finally send the result in the callback.
this.body = output;
} catch (err) {
this.status = 500;
this.body = err;
}
};

View File

@ -1,9 +0,0 @@
'use strict';
/**
* Index of the explorer hook config actions.
*/
module.exports = {
config: require('./config')
};

View File

@ -1,12 +0,0 @@
'use strict';
/**
* Count entries of a model.
*/
module.exports = function * () {
const Model = strapi.hooks.blueprints.actionUtil.parseModel(this);
const countQuery = Model.count().where(strapi.hooks.blueprints.actionUtil.parseCriteria(this));
const count = yield countQuery;
this.body = count;
};

View File

@ -1,14 +0,0 @@
'use strict';
/**
* Create a new entry.
*/
module.exports = function * () {
try {
const entry = yield strapi.hooks.blueprints.create(this);
this.body = entry;
} catch (err) {
this.body = err;
}
};

View File

@ -1,14 +0,0 @@
'use strict';
/**
* Destroy a specific entry.
*/
module.exports = function * () {
try {
const entry = yield strapi.hooks.blueprints.destroy(this);
this.body = entry;
} catch (err) {
this.body = err;
}
};

View File

@ -1,14 +0,0 @@
'use strict';
/**
* List every entries of a model.
*/
module.exports = function * () {
try {
const entry = yield strapi.hooks.blueprints.find(this);
this.body = entry;
} catch (err) {
this.body = err;
}
};

View File

@ -1,14 +0,0 @@
'use strict';
/**
* Show a specific entry.
*/
module.exports = function * () {
try {
const entry = yield strapi.hooks.blueprints.findOne(this);
this.body = entry;
} catch (err) {
this.body = err;
}
};

View File

@ -1,14 +0,0 @@
'use strict';
/**
* Index of the explorer hook explorer actions.
*/
module.exports = {
count: require('./count'),
create: require('./create'),
destroy: require('./destroy'),
find: require('./find'),
findOne: require('./findOne'),
update: require('./update')
};

View File

@ -1,14 +0,0 @@
'use strict';
/**
* Update a specific entry.
*/
module.exports = function * () {
try {
const entry = yield strapi.hooks.blueprints.update(this);
this.body = entry;
} catch (err) {
this.body = err;
}
};

View File

@ -1,86 +0,0 @@
'use strict';
/**
* Module dependencies
*/
// Local dependencies.
const explorerActions = require('./explorer/index');
const routesActions = require('./routes/index');
const configActions = require('./config/index');
/**
* Public explorer hook
*/
module.exports = function (strapi) {
const hook = {
/**
* Default options
*/
defaults: {
dashboard: {
enabled: true,
token: ''
},
routes: {
'GET /dashboard/explorer/:model/count': {
controller: explorerActions.count,
policies: ['dashboardToken', 'isAuthorized']
},
'POST /dashboard/explorer/:model': {
controller: explorerActions.create,
policies: ['dashboardToken', 'isAuthorized', 'addDataCreate']
},
'DELETE /dashboard/explorer/:model/:id': {
controller: explorerActions.destroy,
policies: ['dashboardToken', 'isAuthorized']
},
'GET /dashboard/explorer/:model': {
controller: explorerActions.find,
policies: ['dashboardToken', 'isAuthorized']
},
'GET /dashboard/explorer/:model/:id': {
controller: explorerActions.findOne,
policies: ['dashboardToken', 'isAuthorized']
},
'PUT /dashboard/explorer/:model/:id': {
controller: explorerActions.update,
policies: ['dashboardToken', 'isAuthorized', 'addDataUpdate']
},
'GET /dashboard/routes': {
controller: routesActions.find,
action: 'find',
policies: ['dashboardToken', 'isAuthorized']
},
'PUT /dashboard/routes': {
controller: routesActions.update,
action: 'update',
policies: ['dashboardToken', 'isAuthorized']
},
'GET /dashboard/config': {
controller: configActions.config,
action: 'index',
policies: ['dashboardToken']
}
}
},
/**
* Initialize the hook
*/
initialize: function (cb) {
_.forEach(strapi.hooks.dashboard.defaults.routes, function (route, key) {
strapi.config.routes[key] = route;
});
cb();
}
};
return hook;
};

View File

@ -1,27 +0,0 @@
'use strict';
/**
* Policy used to check if the `dashboardToken` field is valid.
*
* @param next
*/
module.exports = function * (next) {
// Format dashboardToken variables.
const dashboardTokenParam = this.header.dashboardtoken;
const dashboardTokenConfig = strapi.config.dashboard && strapi.config.dashboard.token;
// Check dashboardToken for security purposes.
if (!dashboardTokenParam || !dashboardTokenConfig || dashboardTokenParam !== dashboardTokenConfig) {
this.status = 401;
this.body = {
message: 'dashboardToken parameter is invalid.'
};
} else {
// Delete `dashboardToken` field.
delete this.request.query.dashboardToken;
delete this.request.body.dashboardToken;
yield next;
}
};

View File

@ -1,23 +0,0 @@
'use strict';
/**
* Module dependencies
*/
// Local node modules.
const routeService = require('./helpers');
/**
* Returns the config of the application used
* by the dashboard
*/
module.exports = function * () {
try {
const routes = yield routeService.find();
this.body = routes;
} catch (err) {
this.status = 500;
this.body = err;
}
};

View File

@ -1,135 +0,0 @@
'use strict';
/**
* Module dependencies
*/
// Public node modules.
const _ = require('lodash');
module.exports = {
find: find,
update: update
};
/**
* Find routes.
*
* @returns {Function|promise}
*/
function * find() {
const deferred = Promise.defer();
try {
const verbs = ['get', 'put', 'post', 'delete', 'options', 'patch'];
const routes = {};
const dbRoutes = yield strapi.orm.collections.route.find().populate('roles');
const apis = strapi.api;
let dbRoute;
let index;
let firstWord;
let routeNameSplitted;
let verb;
// Format verb.
_.forEach(dbRoutes, function (route) {
// Split the name with `/`.
routeNameSplitted = route.name.split('/');
// Verb.
verb = _.includes(verbs, routeNameSplitted[0] && _.trim(routeNameSplitted[0].toLowerCase())) ? _.trim(routeNameSplitted[0]) : '';
route.verb = verb;
route.path = route.verb ? routeNameSplitted.splice(0, 1) && _.trim('/' + routeNameSplitted.join('/')) : _.trim(routeNameSplitted.join('/'));
});
// For each API.
_.forEach(apis, function (api, key) {
// Init the array object.
routes[key] = [];
// For each routes of the current API.
_.forEach(api.config.routes, function (route, routeName) {
// Find routes of the APIs in the `routes` object.
dbRoute = _.find(dbRoutes, {name: routeName});
// If the route is found.
if (dbRoute) {
// Find the index.
index = _.indexOf(dbRoutes, dbRoute);
// Assign them to the key of the `route` object.
routes[key].push(dbRoute);
// Remove the pushed route from the list of routes.
dbRoutes.splice(index, 1);
}
});
});
// Then filter by `begin` with.
_.forEach(_.clone(dbRoutes), function (route) {
// Prevent errors.
if (!route) {
return;
}
// Split the name with `/`.
routeNameSplitted = route.name.split('/');
// Fetch the first word of the URL.
firstWord = route.verb ? _.trim(routeNameSplitted[1]) : _.trim(routeNameSplitted[0]);
// Set an empty array for this object if it is not
// already defined.
routes[firstWord] = routes[firstWord] || [];
// Set the index value.
index = _.indexOf(dbRoutes, route);
// Assign them to the key of the `route` object.
routes[firstWord].push(_.clone(route));
// Remove the pushed route from the list of routes.
dbRoutes.splice(index, 1);
});
// Set the non-filtered routes in the `others` object.
if (dbRoutes.length) {
routes.others = dbRoutes;
}
deferred.resolve(routes);
} catch (err) {
deferred.reject(err);
}
return deferred.promise;
}
/**
* Update routes.
*
* @param routes
* @returns {Function|promise}
*/
function * update(routes) {
let id;
const promises = [];
const deferred = Promise.defer();
_.forEach(routes, function (route) {
id = route.id;
promises.push(strapi.orm.collections.route.update({id: id}, route));
});
Promise.all(promises)
.then(function (results) {
deferred.resolve(results);
})
.catch(function (error) {
deferred.reject(error);
});
return deferred.promise;
}

View File

@ -1,10 +0,0 @@
'use strict';
/**
* Index of the explorer hook routes actions.
*/
module.exports = {
find: require('./find'),
update: require('./update')
};

View File

@ -1,28 +0,0 @@
'use strict';
/**
* Module dependencies
*/
// Local node modules.
const routeService = require('./helpers');
/**
* Returns the config of the application used
* by the dashboard
*/
module.exports = function *() {
let routes;
let routesFound;
try {
routes = this.request.body;
yield routeService.update(routes);
routesFound = yield routeService.find();
this.body = routesFound;
} catch (err) {
this.status = 500;
this.body = err;
}
};

View File

@ -23,7 +23,6 @@ module.exports = {
cron: true,
logger: true,
blueprints: true,
dashboard: true,
views: true,
router: true,
static: true,

View File

@ -12,7 +12,6 @@ const _ = require('lodash');
// Local utilities.
const regex = require('../../../../util/regex');
const dashboardTokenPolicy = require('../dashboard/policies/dashboardToken');
/**
* Router hook
@ -68,11 +67,6 @@ module.exports = function (strapi) {
});
}
// Add the `dashboardPolicy` to the list of policies.
if (strapi.config.dashboard.enabled) {
strapi.policies.dashboardToken = dashboardTokenPolicy;
}
// Parse each route from the user config, load policies if any
// and match the controller and action to the desired endpoint.
_.forEach(strapi.config.routes, function (value, endpoint) {

View File

@ -285,7 +285,6 @@ module.exports = function (strapi) {
appName: strapi.config.name,
publicKey: strapi.rsa.exportKey('private'),
secretKey: strapi.config.studio.secretKey,
dashboardToken: strapi.config.dashboard.token,
token: strapi.token,
env: strapi.config.environment
};

View File

@ -131,19 +131,9 @@ module.exports = function (strapi) {
loadHook('studio', cb);
},
// Load the dashboard hook.
dashboard: function loadRouterHook(cb) {
if (!hooks.dashboard) {
return cb();
}
prepareHook('dashboard');
applyDefaults(hooks.dashboard);
loadHook('dashboard', cb);
},
// Prepare all other hooks.
prepare: function prepareHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'dashboard', 'router'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
prepareHook(id);
process.nextTick(cb);
}, cb);
@ -151,7 +141,7 @@ module.exports = function (strapi) {
// Apply the default config for all other hooks.
defaults: function defaultConfigHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'dashboard', 'router'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
const hook = hooks[id];
applyDefaults(hook);
process.nextTick(cb);
@ -160,7 +150,7 @@ module.exports = function (strapi) {
// Load all other hooks.
load: function loadOtherHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'dashboard', 'router'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
loadHook(id, cb);
}, cb);
},