mirror of
https://github.com/strapi/strapi.git
synced 2025-12-26 14:44:31 +00:00
Add logic for dashboard Saas
This commit is contained in:
parent
2f369eba38
commit
04ca6e6ce2
10
lib/configuration/hooks/dashboard/explorer/count.js
Normal file
10
lib/configuration/hooks/dashboard/explorer/count.js
Normal file
@ -0,0 +1,10 @@
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
12
lib/configuration/hooks/dashboard/explorer/create.js
Normal file
12
lib/configuration/hooks/dashboard/explorer/create.js
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Create a new entry.
|
||||
*/
|
||||
|
||||
module.exports = function * () {
|
||||
try {
|
||||
const entry = yield strapi.hooks.blueprints.create(this);
|
||||
this.body = entry;
|
||||
} catch (err) {
|
||||
this.body = err;
|
||||
}
|
||||
};
|
||||
12
lib/configuration/hooks/dashboard/explorer/destroy.js
Normal file
12
lib/configuration/hooks/dashboard/explorer/destroy.js
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Destroy a specific entry.
|
||||
*/
|
||||
|
||||
module.exports = function * () {
|
||||
try {
|
||||
const entry = yield strapi.hooks.blueprints.destroy(this);
|
||||
this.body = entry;
|
||||
} catch (err) {
|
||||
this.body = err;
|
||||
}
|
||||
};
|
||||
12
lib/configuration/hooks/dashboard/explorer/find.js
Normal file
12
lib/configuration/hooks/dashboard/explorer/find.js
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
};
|
||||
12
lib/configuration/hooks/dashboard/explorer/findOne.js
Normal file
12
lib/configuration/hooks/dashboard/explorer/findOne.js
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Show a specific entry.
|
||||
*/
|
||||
|
||||
module.exports = function * () {
|
||||
try {
|
||||
const entry = yield strapi.hooks.blueprints.findOne(this);
|
||||
this.body = entry;
|
||||
} catch (err) {
|
||||
this.body = err;
|
||||
}
|
||||
};
|
||||
12
lib/configuration/hooks/dashboard/explorer/index.js
Normal file
12
lib/configuration/hooks/dashboard/explorer/index.js
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* 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')
|
||||
};
|
||||
12
lib/configuration/hooks/dashboard/explorer/update.js
Normal file
12
lib/configuration/hooks/dashboard/explorer/update.js
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Update a specific entry.
|
||||
*/
|
||||
|
||||
module.exports = function * () {
|
||||
try {
|
||||
const entry = yield strapi.hooks.blueprints.update(this);
|
||||
this.body = entry;
|
||||
} catch (err) {
|
||||
this.body = err;
|
||||
}
|
||||
};
|
||||
81
lib/configuration/hooks/dashboard/index.js
Normal file
81
lib/configuration/hooks/dashboard/index.js
Normal file
@ -0,0 +1,81 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
// Node.js core.
|
||||
const path = require('path');
|
||||
|
||||
// Local dependencies.
|
||||
const explorerActions = require('./explorer/index');
|
||||
const routesActions = require('./routes/index');
|
||||
|
||||
/**
|
||||
* Public explorer hook
|
||||
*/
|
||||
|
||||
module.exports = function (strapi) {
|
||||
const hook = {
|
||||
|
||||
/**
|
||||
* Default options
|
||||
*/
|
||||
|
||||
defaults: {
|
||||
routes: {
|
||||
// Data explorer routes.
|
||||
'GET /dashboard/explorer/:model/count': {
|
||||
controller: explorerActions.count,
|
||||
policies: ['isAuthorized']
|
||||
},
|
||||
'POST /dashboard/explorer/:model': {
|
||||
controller: explorerActions.create,
|
||||
policies: ['isAuthorized']
|
||||
},
|
||||
'DELETE /dashboard/explorer/:model/:id': {
|
||||
controller: explorerActions.destroy,
|
||||
policies: ['isAuthorized']
|
||||
},
|
||||
'GET /dashboard/explorer/:model': {
|
||||
controller: explorerActions.find,
|
||||
policies: ['isAuthorized']
|
||||
},
|
||||
'GET /dashboard/explorer/:model/:id': {
|
||||
controller: explorerActions.findOne,
|
||||
policies: ['isAuthorized']
|
||||
},
|
||||
'PUT /dashboard/explorer/:model': {
|
||||
controller: explorerActions.update,
|
||||
policies: ['isAuthorized']
|
||||
},
|
||||
// Routes and permissions routes.
|
||||
'GET /dashboard/routes': {
|
||||
controller: routesActions.find,
|
||||
'action': 'find',
|
||||
'policies': ['isAuthorized']
|
||||
},
|
||||
'PUT /dashboard/routes': {
|
||||
controller: routesActions.update,
|
||||
'action': 'update',
|
||||
'policies': ['isAuthorized']
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialize the hook
|
||||
*/
|
||||
|
||||
initialize: function (cb) {
|
||||
|
||||
_.forEach(strapi.hooks.dashboard.defaults.routes, function (route, key) {
|
||||
strapi.config.routes[key] = route;
|
||||
});
|
||||
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
||||
return hook;
|
||||
};
|
||||
29
lib/configuration/hooks/dashboard/routes/find.js
Normal file
29
lib/configuration/hooks/dashboard/routes/find.js
Normal file
@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
// Public node modules.
|
||||
const _ = require('lodash');
|
||||
|
||||
// Local node modules.
|
||||
const routeService = require('./helpers');
|
||||
|
||||
/**
|
||||
* Returns the config of the application used
|
||||
* by the admin panel
|
||||
*/
|
||||
|
||||
module.exports = function * () {
|
||||
let routes;
|
||||
|
||||
try {
|
||||
routes = yield routeService.find();
|
||||
|
||||
this.body = routes;
|
||||
} catch (err) {
|
||||
this.status = 500;
|
||||
this.body = err;
|
||||
}
|
||||
};
|
||||
135
lib/configuration/hooks/dashboard/routes/helpers.js
Normal file
135
lib/configuration/hooks/dashboard/routes/helpers.js
Normal file
@ -0,0 +1,135 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
// Public node modules.
|
||||
const _ = require('lodash');
|
||||
|
||||
module.exports = {
|
||||
find: find,
|
||||
update: update
|
||||
};
|
||||
|
||||
/**
|
||||
* Find routes.
|
||||
*
|
||||
* @returns {Function|promise}
|
||||
*/
|
||||
function * find() {
|
||||
|
||||
let deferred = Promise.defer();
|
||||
|
||||
try {
|
||||
const verbs = ['get', 'put', 'post', 'delete', 'options', 'patch'];
|
||||
let routes = {};
|
||||
let dbRoutes = yield strapi.orm.collections.route.find().populate('roles');
|
||||
let 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;
|
||||
let promises = [];
|
||||
let 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;
|
||||
}
|
||||
8
lib/configuration/hooks/dashboard/routes/index.js
Normal file
8
lib/configuration/hooks/dashboard/routes/index.js
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Index of the explorer hook routes actions.
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
find: require('./find'),
|
||||
update: require('./update')
|
||||
};
|
||||
31
lib/configuration/hooks/dashboard/routes/update.js
Normal file
31
lib/configuration/hooks/dashboard/routes/update.js
Normal file
@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
// Public node modules.
|
||||
const _ = require('lodash');
|
||||
|
||||
// Local node modules.
|
||||
const routeService = require('./helpers');
|
||||
|
||||
/**
|
||||
* Returns the config of the application used
|
||||
* by the admin panel
|
||||
*/
|
||||
|
||||
module.exports = function * () {
|
||||
let routes;
|
||||
let routesFound;
|
||||
|
||||
try {
|
||||
routes = this.request.body;
|
||||
yield updateRoutes(routes);
|
||||
routesFound = yield routeService.find();
|
||||
this.body = routesFound;
|
||||
} catch (err) {
|
||||
this.status = 500;
|
||||
this.body = err;
|
||||
}
|
||||
};
|
||||
@ -23,6 +23,7 @@ module.exports = {
|
||||
cron: true,
|
||||
logger: true,
|
||||
blueprints: true,
|
||||
dashboard: true,
|
||||
views: true,
|
||||
router: true,
|
||||
static: true,
|
||||
|
||||
@ -62,8 +62,16 @@ module.exports = function (strapi) {
|
||||
_.forEach(strapi.config.routes, function (value, endpoint) {
|
||||
try {
|
||||
route = regex.detectRoute(endpoint);
|
||||
controller = strapi.controllers[value.controller.toLowerCase()];
|
||||
action = controller[value.action];
|
||||
|
||||
// Check if the controller is a function.
|
||||
if (typeof value.controller === 'function') {
|
||||
action = value.controller;
|
||||
} else {
|
||||
controller = strapi.controllers[value.controller.toLowerCase()];
|
||||
action = controller[value.action];
|
||||
}
|
||||
|
||||
// Init policies array.
|
||||
policies = [];
|
||||
|
||||
// Add the `globalPolicy`.
|
||||
|
||||
@ -345,6 +345,71 @@ module.exports = function (strapi) {
|
||||
cb(null, obj);
|
||||
},
|
||||
|
||||
/**
|
||||
* Pull global strapi variable from local server
|
||||
* in order to provide them to the dashboard
|
||||
*
|
||||
* @param {Object} data
|
||||
*
|
||||
* @return {Function} cb
|
||||
*/
|
||||
|
||||
pullServerForDashboard: function (data, cb) {
|
||||
try {
|
||||
|
||||
// Init ouptput object
|
||||
const obj = {};
|
||||
|
||||
// Set the config.
|
||||
obj.settings = {
|
||||
url: strapi.config.url,
|
||||
i18n: strapi.config.i18n
|
||||
};
|
||||
|
||||
// Set the models.
|
||||
obj.models = strapi.models;
|
||||
|
||||
// Init filtered model attributes variable.
|
||||
let filteredModelAttributes;
|
||||
|
||||
// Format `config.api` for multi templates models.
|
||||
_.forEach(strapi.api, function (api, key) {
|
||||
if (api.templates) {
|
||||
obj.models[key].templates = {};
|
||||
}
|
||||
|
||||
// Assign the template attributes with the model attributes.
|
||||
_.forEach(api.templates, function (template, templateName) {
|
||||
obj.models[key].templates[templateName] = {};
|
||||
obj.models[key].templates[templateName].attributes = {};
|
||||
filteredModelAttributes = {};
|
||||
_.forEach(template.attributes, function (value, attributeKey) {
|
||||
obj.models[key].templates[templateName].attributes[attributeKey] = _.cloneDeep(obj.models[key].attributes[attributeKey]);
|
||||
});
|
||||
obj.models[key].templates[templateName].displayedAttribute = template.displayedAttribute;
|
||||
});
|
||||
});
|
||||
|
||||
// User count.
|
||||
const promises = [];
|
||||
promises.push(strapi.orm.collections.user.count());
|
||||
|
||||
Promise.all(promises)
|
||||
.then(function (response) {
|
||||
// Define if the app is considered as new.
|
||||
const userCount = response[0];
|
||||
obj.settings.isNewApp = userCount ? false : true;
|
||||
|
||||
// Finally send the result in the callback.
|
||||
cb(null, obj);
|
||||
}, function (err) {
|
||||
cb(err);
|
||||
});
|
||||
} catch (err) {
|
||||
cb(err);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Rebuild dictionary
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user