265 lines
6.5 KiB
JavaScript
Raw Normal View History

2016-08-26 14:19:03 +02:00
'use strict';
const execa = require('execa');
2018-03-23 12:44:17 +01:00
const _ = require('lodash');
2016-08-26 14:19:03 +02:00
/**
* A set of functions called "actions" for `Admin`
*/
module.exports = {
2018-03-08 10:32:01 +01:00
getCurrentEnvironment: async ctx => {
try {
2019-05-13 17:32:52 +02:00
const autoReload = strapi.config.autoReload;
2019-04-02 22:45:21 +02:00
return ctx.send({ autoReload, currentEnvironment: strapi.app.env });
} catch (err) {
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
2018-03-08 10:32:01 +01:00
}
},
2018-04-12 23:45:17 +02:00
getStrapiVersion: async ctx => {
try {
const strapiVersion = _.get(strapi.config, 'info.strapi', null);
return ctx.send({ strapiVersion });
2019-04-02 22:45:21 +02:00
} catch (err) {
return ctx.badRequest(null, [
{ messages: [{ id: 'The version is not available' }] },
]);
2018-04-12 23:45:17 +02:00
}
},
2018-04-05 11:13:49 +02:00
getGaConfig: async ctx => {
2018-03-23 12:44:17 +01:00
try {
ctx.send({ uuid: _.get(strapi.config, 'uuid', false) });
2019-04-02 22:45:21 +02:00
} catch (err) {
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
2018-03-23 12:44:17 +01:00
}
},
2018-04-05 11:13:49 +02:00
getLayout: async ctx => {
try {
const layout = require('../config/layout.js');
return ctx.send({ layout });
2019-04-02 22:45:21 +02:00
} catch (err) {
return ctx.badRequest(null, [
{ messages: [{ id: 'An error occurred' }] },
]);
2018-04-05 11:13:49 +02:00
}
},
2017-12-16 17:35:16 +01:00
installPlugin: async ctx => {
try {
const { plugin } = ctx.request.body;
2017-12-16 17:35:16 +01:00
strapi.reload.isWatching = false;
strapi.log.info(`Installing ${plugin}...`);
await execa('npm', ['run', 'strapi', '--', 'install', plugin]);
2017-12-16 17:35:16 +01:00
ctx.send({ ok: true });
strapi.reload();
2019-04-02 22:45:21 +02:00
} catch (err) {
strapi.log.error(err);
2017-12-16 17:35:16 +01:00
strapi.reload.isWatching = true;
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
2017-12-16 17:35:16 +01:00
}
},
plugins: async ctx => {
try {
const plugins = Object.keys(strapi.plugins).reduce((acc, key) => {
acc[key] = _.get(strapi.plugins, [key, 'package', 'strapi'], {
name: key,
});
return acc;
}, {});
ctx.send({ plugins });
2019-04-02 22:45:21 +02:00
} catch (err) {
strapi.log.error(err);
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
}
},
uninstallPlugin: async ctx => {
try {
const { plugin } = ctx.params;
2017-11-02 17:05:37 +01:00
strapi.reload.isWatching = false;
strapi.log.info(`Uninstalling ${plugin}...`);
await execa('npm', ['run', 'strapi', '--', 'uninstall', plugin, '-d']);
2017-11-02 17:05:37 +01:00
ctx.send({ ok: true });
2017-11-02 17:05:37 +01:00
strapi.reload();
2019-04-02 22:45:21 +02:00
} catch (err) {
strapi.log.error(err);
2017-11-02 17:05:37 +01:00
strapi.reload.isWatching = true;
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
}
2019-04-02 22:45:21 +02:00
},
/**
* Create a/an admin record.
*
* @return {Object}
*/
async create(ctx) {
const values = ctx.request.body;
if (!values.email) return ctx.badRequest('Missing email');
if (!values.username) return ctx.badRequest('Missing username');
if (!values.password) return ctx.badRequest('Missing password');
const adminQueries = strapi.admin.queries('administrator', 'admin');
const adminsWithSameEmail = await adminQueries.find({
email: values.email,
});
const adminsWithSameUsername = await adminQueries.find({
username: values.username,
});
if (adminsWithSameEmail.length > 0) {
return ctx.badRequest(
null,
ctx.request.admin
? [
{
messages: [
{ id: 'Auth.form.error.email.taken', field: ['email'] },
],
},
]
: 'Email is already taken.'
);
}
if (adminsWithSameUsername.length > 0) {
return ctx.badRequest(
null,
ctx.request.admin
? [
{
messages: [
{
id: 'Auth.form.error.username.taken',
field: ['username'],
},
],
},
]
: 'Username is already taken.'
);
}
const user = {
email: values.email,
username: values.username,
blocked: values.blocked === true ? true : false,
password: await strapi.admin.services.auth.hashPassword(values.password),
};
const data = await adminQueries.create(user);
// Send 201 `created`
ctx.created(_.omit(data, ['password']));
},
/**
* Update a/an admin record.
*
* @return {Object}
*/
async update(ctx) {
const values = ctx.request.body;
if (!values.email) return ctx.badRequest('Missing email');
if (!values.username) return ctx.badRequest('Missing username');
if (!values.password) return ctx.badRequest('Missing password');
const adminQueries = strapi.admin.queries('administrator', 'admin');
const { primaryKey } = adminQueries;
2019-05-14 09:30:10 +02:00
const admin = await adminQueries.findOne(ctx.params);
// check the user exists
if (!admin) return ctx.notFound('Administrator not found');
// check there are not user with requested email
if (values.email !== admin.email) {
const adminsWithSameEmail = await adminQueries.findOne({
email: values.email,
});
if (
adminsWithSameEmail &&
adminsWithSameEmail[primaryKey] !== admin[primaryKey]
) {
return ctx.badRequest(
null,
ctx.request.admin
? [
{
messages: [
{ id: 'Auth.form.error.email.taken', field: ['email'] },
],
},
]
: 'Email is already taken.'
);
}
2019-05-14 09:30:10 +02:00
}
// check there are not user with requested username
if (values.username !== admin.username) {
const adminsWithSameUsername = await adminQueries.findOne({
username: values.username,
});
if (
adminsWithSameUsername &&
adminsWithSameUsername[primaryKey] !== admin[primaryKey]
) {
return ctx.badRequest(
null,
ctx.request.admin
? [
{
messages: [
{
id: 'Auth.form.error.username.taken',
field: ['username'],
},
],
},
]
: 'Username is already taken.'
);
}
}
const user = {
email: values.email,
username: values.username,
blocked: values.blocked === true ? true : false,
};
if (values.password !== admin.password) {
user.password = await strapi.admin.services.auth.hashPassword(
values.password
);
}
const data = await adminQueries.update(ctx.params, values);
// Send 200 `ok`
ctx.send(data);
},
2016-08-26 14:19:03 +02:00
};