Restrict config access

This commit is contained in:
pierreburgy 2015-11-09 17:50:30 +01:00
parent 379677729e
commit f3d3247246

View File

@ -5,16 +5,53 @@
*/ */
module.exports = function * () { 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 { try {
// Init output object. // Init output object.
const output = {}; const output = {};
// Set the config. // Set the config.
output.settings = { output.settings = {};
url: strapi.config.url, output.settings.url = strapi.config.url;
i18n: strapi.config.i18n
}; // 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. // Set the models.
output.models = strapi.models; output.models = strapi.models;
@ -36,17 +73,6 @@ module.exports = function * () {
}); });
}); });
// User count.
const promises = [];
promises.push(strapi.orm.collections.user.count());
// Execute promises.
const response = yield promises;
// Define if the app is considered as new.
const userCount = response[0];
output.settings.isNewApp = !userCount;
// Finally send the result in the callback. // Finally send the result in the callback.
this.body = output; this.body = output;
} catch (err) { } catch (err) {