314 lines
7.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');
const formatError = error => [
{ messages: [{ id: error.id, message: error.message, field: error.field }] },
];
const PLUGIN_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-_]+$/;
/**
* Validates a plugin name format
*/
const isValidPluginName = plugin => {
return _.isString(plugin) && !_.isEmpty(plugin) && PLUGIN_NAME_REGEX.test(plugin);
};
2016-08-26 14:19:03 +02:00
/**
* A set of functions called "actions" for `Admin`
*/
module.exports = {
2019-09-09 16:06:54 +02:00
async init(ctx) {
const uuid = _.get(strapi, ['config', 'uuid'], false);
const currentEnvironment = strapi.app.env;
const autoReload = _.get(strapi, ['config', 'autoReload'], false);
const strapiVersion = _.get(strapi.config, 'info.strapi', null);
return ctx.send({
data: { uuid, currentEnvironment, autoReload, strapiVersion },
});
},
2019-07-15 19:33:07 +02:00
async getCurrentEnvironment(ctx) {
2018-03-08 10:32:01 +01:00
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
}
},
2019-07-15 19:33:07 +02:00
async getStrapiVersion(ctx) {
2018-04-12 23:45:17 +02:00
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
}
},
2019-07-15 19:33:07 +02:00
async getGaConfig(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
}
},
2019-07-15 19:33:07 +02:00
async getLayout(ctx) {
2018-04-05 11:13:49 +02:00
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
}
},
2019-07-15 19:33:07 +02:00
async installPlugin(ctx) {
2017-12-16 17:35:16 +01:00
try {
const { plugin } = ctx.request.body;
2019-12-01 23:05:58 +01:00
if (!isValidPluginName(plugin)) {
2019-12-01 23:05:58 +01:00
return ctx.badRequest('Invalid plugin name');
}
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
}
},
2019-07-15 19:33:07 +02:00
async plugins(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' }] }]);
}
},
2019-07-15 19:33:07 +02:00
async uninstallPlugin(ctx) {
try {
const { plugin } = ctx.params;
2019-12-01 23:05:58 +01:00
if (!isValidPluginName(plugin)) {
2019-12-01 23:05:58 +01:00
return ctx.badRequest('Invalid plugin name');
}
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 { email, username, password, blocked } = ctx.request.body;
if (!email) {
return ctx.badRequest(
null,
formatError({
id: 'missing.email',
message: 'Missing email',
field: ['email'],
})
);
}
if (!username) {
return ctx.badRequest(
null,
formatError({
id: 'missing.username',
message: 'Missing username',
field: ['username'],
})
);
}
if (!password) {
return ctx.badRequest(
null,
formatError({
id: 'missing.password',
message: 'Missing password',
field: ['password'],
})
);
}
const adminsWithSameEmail = await strapi.query('administrator', 'admin').findOne({ email });
2019-07-15 19:33:07 +02:00
const adminsWithSameUsername = await strapi
.query('administrator', 'admin')
.findOne({ username });
if (adminsWithSameEmail) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.taken',
message: 'Email already taken',
field: ['email'],
})
);
}
if (adminsWithSameUsername) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.username.taken',
message: 'Username already taken',
field: ['username'],
})
);
}
const user = {
email: email,
username: username,
blocked: blocked === true ? true : false,
password: await strapi.admin.services.auth.hashPassword(password),
};
2019-07-15 19:33:07 +02:00
const data = await strapi.query('administrator', 'admin').create(user);
// Send 201 `created`
2019-07-15 19:33:07 +02:00
ctx.created(strapi.admin.services.auth.sanitizeUser(data));
},
/**
* Update a/an admin record.
*
* @return {Object}
*/
async update(ctx) {
2019-07-15 19:33:07 +02:00
const { id } = ctx.params;
const { email, username, password, blocked } = ctx.request.body;
if (!email) {
return ctx.badRequest(
null,
formatError({
id: 'missing.email',
message: 'Missing email',
field: ['email'],
})
);
}
if (!username) {
return ctx.badRequest(
null,
formatError({
id: 'missing.username',
message: 'Missing username',
field: ['username'],
})
);
}
if (!password) {
return ctx.badRequest(
null,
formatError({
id: 'missing.password',
message: 'Missing password',
field: ['password'],
})
);
}
const admin = await strapi.query('administrator', 'admin').findOne({ id });
// check the user exists
if (!admin) return ctx.notFound('Administrator not found');
// check there are not user with requested email
if (email !== admin.email) {
const adminsWithSameEmail = await strapi.query('administrator', 'admin').findOne({ email });
2019-07-15 19:33:07 +02:00
if (adminsWithSameEmail && adminsWithSameEmail.id !== admin.id) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.taken',
message: 'Email already taken',
field: ['email'],
})
);
}
2019-05-14 09:30:10 +02:00
}
// check there are not user with requested username
if (username !== admin.username) {
2019-07-15 19:33:07 +02:00
const adminsWithSameUsername = await strapi
.query('administrator', 'admin')
.findOne({ username });
2019-07-15 19:33:07 +02:00
if (adminsWithSameUsername && adminsWithSameUsername.id !== admin.id) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.username.taken',
message: 'Username already taken',
field: ['username'],
})
);
}
}
const user = {
email: email,
username: username,
blocked: blocked === true ? true : false,
};
if (password !== admin.password) {
user.password = await strapi.admin.services.auth.hashPassword(password);
}
const data = await strapi.query('administrator', 'admin').update({ id }, user);
// Send 200 `ok`
ctx.send(data);
},
2016-08-26 14:19:03 +02:00
};