mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 23:24:03 +00:00
Implement strapi.queries
This commit is contained in:
parent
45e6533941
commit
af43649845
@ -1,97 +0,0 @@
|
||||
const _ = require('lodash');
|
||||
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
|
||||
|
||||
module.exports = ({ model }) => ({
|
||||
find(params, populate) {
|
||||
const filters = convertRestQueryParams(params);
|
||||
|
||||
return model
|
||||
.query(buildQuery({ model, filters }))
|
||||
.fetchAll({
|
||||
withRelated: populate || model.associations.map(x => x.alias),
|
||||
})
|
||||
.then(data => data.toJSON());
|
||||
},
|
||||
|
||||
count(params = {}) {
|
||||
const { where } = convertRestQueryParams(params);
|
||||
|
||||
return model.query(buildQuery({ model, filters: { where } })).count();
|
||||
},
|
||||
|
||||
async findOne(params, populate) {
|
||||
const primaryKey = params[model.primaryKey] || params.id;
|
||||
|
||||
if (primaryKey) {
|
||||
params = {
|
||||
[model.primaryKey]: primaryKey,
|
||||
};
|
||||
}
|
||||
|
||||
const record = await model.forge(params).fetch({
|
||||
withRelated: populate || model.associations.map(x => x.alias),
|
||||
});
|
||||
|
||||
return record ? record.toJSON() : record;
|
||||
},
|
||||
|
||||
async create(params) {
|
||||
return model
|
||||
.forge()
|
||||
.save(
|
||||
Object.keys(params).reduce((acc, current) => {
|
||||
if (
|
||||
_.get(model._attributes, [current, 'type']) ||
|
||||
_.get(model._attributes, [current, 'model'])
|
||||
) {
|
||||
acc[current] = params[current];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {})
|
||||
)
|
||||
.catch(err => {
|
||||
if (err.detail) {
|
||||
const field = _.last(_.words(err.detail.split('=')[0]));
|
||||
err = { message: `This ${field} is already taken`, field };
|
||||
}
|
||||
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
|
||||
async update(search, params = {}) {
|
||||
if (_.isEmpty(params)) {
|
||||
params = search;
|
||||
}
|
||||
|
||||
const primaryKey = search[model.primaryKey] || search.id;
|
||||
|
||||
if (primaryKey) {
|
||||
search = {
|
||||
[model.primaryKey]: primaryKey,
|
||||
};
|
||||
} else {
|
||||
const entry = await this.findOne(search);
|
||||
|
||||
search = {
|
||||
[model.primaryKey]: entry[model.primaryKey] || entry.id,
|
||||
};
|
||||
}
|
||||
|
||||
return model
|
||||
.forge(search)
|
||||
.save(params, {
|
||||
patch: true,
|
||||
})
|
||||
.catch(err => {
|
||||
if (err && err.detail) {
|
||||
const field = _.last(_.words(err.detail.split('=')[0]));
|
||||
const error = { message: `This ${field} is already taken`, field };
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
});
|
||||
@ -1,90 +0,0 @@
|
||||
const _ = require('lodash');
|
||||
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
|
||||
|
||||
module.exports = ({ model }) => ({
|
||||
find(params, populate) {
|
||||
const filters = convertRestQueryParams(params);
|
||||
|
||||
return buildQuery({
|
||||
model,
|
||||
filters,
|
||||
populate: populate || model.associations.map(x => x.alias),
|
||||
}).lean();
|
||||
},
|
||||
|
||||
count(params) {
|
||||
const filters = convertRestQueryParams(params);
|
||||
|
||||
return buildQuery({
|
||||
model,
|
||||
filters: { where: filters.where },
|
||||
}).count();
|
||||
},
|
||||
|
||||
findOne(params, populate) {
|
||||
const primaryKey = params[model.primaryKey] || params.id;
|
||||
|
||||
if (primaryKey) {
|
||||
params = {
|
||||
[model.primaryKey]: primaryKey,
|
||||
};
|
||||
}
|
||||
|
||||
return model
|
||||
.findOne(params)
|
||||
.populate(populate || model.associations.map(x => x.alias).join(' '))
|
||||
.lean();
|
||||
},
|
||||
|
||||
create(params) {
|
||||
return model
|
||||
.create(
|
||||
Object.keys(params).reduce((acc, current) => {
|
||||
if (
|
||||
_.get(model._attributes, [current, 'type']) ||
|
||||
_.get(model._attributes, [current, 'model'])
|
||||
) {
|
||||
acc[current] = params[current];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {})
|
||||
)
|
||||
.catch(err => {
|
||||
if (err.message.indexOf('index:') !== -1) {
|
||||
const message = err.message.split('index:');
|
||||
const field = _.words(_.last(message).split('_')[0]);
|
||||
const error = { message: `This ${field} is already taken`, field };
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
|
||||
update(search, params = {}) {
|
||||
if (_.isEmpty(params)) {
|
||||
params = search;
|
||||
}
|
||||
|
||||
const primaryKey = search[model.primaryKey] || search.id;
|
||||
|
||||
if (primaryKey) {
|
||||
search = {
|
||||
[model.primaryKey]: primaryKey,
|
||||
};
|
||||
}
|
||||
|
||||
return model
|
||||
.updateOne(search, params, {
|
||||
strict: false,
|
||||
})
|
||||
.catch(error => {
|
||||
const field = _.last(_.words(error.message.split('_')[0]));
|
||||
const err = { message: `This ${field} is already taken`, field };
|
||||
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
});
|
||||
@ -8,7 +8,7 @@ const _ = require('lodash');
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
getCurrentEnvironment: async ctx => {
|
||||
async getCurrentEnvironment(ctx) {
|
||||
try {
|
||||
const autoReload = strapi.config.autoReload;
|
||||
return ctx.send({ autoReload, currentEnvironment: strapi.app.env });
|
||||
@ -17,7 +17,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
getStrapiVersion: async ctx => {
|
||||
async getStrapiVersion(ctx) {
|
||||
try {
|
||||
const strapiVersion = _.get(strapi.config, 'info.strapi', null);
|
||||
return ctx.send({ strapiVersion });
|
||||
@ -28,7 +28,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
getGaConfig: async ctx => {
|
||||
async getGaConfig(ctx) {
|
||||
try {
|
||||
ctx.send({ uuid: _.get(strapi.config, 'uuid', false) });
|
||||
} catch (err) {
|
||||
@ -36,7 +36,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
getLayout: async ctx => {
|
||||
async getLayout(ctx) {
|
||||
try {
|
||||
const layout = require('../config/layout.js');
|
||||
|
||||
@ -48,7 +48,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
installPlugin: async ctx => {
|
||||
async installPlugin(ctx) {
|
||||
try {
|
||||
const { plugin } = ctx.request.body;
|
||||
strapi.reload.isWatching = false;
|
||||
@ -66,7 +66,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
plugins: async ctx => {
|
||||
async plugins(ctx) {
|
||||
try {
|
||||
const plugins = Object.keys(strapi.plugins).reduce((acc, key) => {
|
||||
acc[key] = _.get(strapi.plugins, [key, 'package', 'strapi'], {
|
||||
@ -83,7 +83,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
uninstallPlugin: async ctx => {
|
||||
async uninstallPlugin(ctx) {
|
||||
try {
|
||||
const { plugin } = ctx.params;
|
||||
strapi.reload.isWatching = false;
|
||||
@ -114,15 +114,17 @@ module.exports = {
|
||||
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 strapi
|
||||
.query('administrator', 'admin')
|
||||
.find({
|
||||
email: values.email,
|
||||
});
|
||||
|
||||
const adminsWithSameEmail = await adminQueries.find({
|
||||
email: values.email,
|
||||
});
|
||||
|
||||
const adminsWithSameUsername = await adminQueries.find({
|
||||
username: values.username,
|
||||
});
|
||||
const adminsWithSameUsername = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.find({
|
||||
username: values.username,
|
||||
});
|
||||
|
||||
if (adminsWithSameEmail.length > 0) {
|
||||
return ctx.badRequest(
|
||||
@ -164,10 +166,10 @@ module.exports = {
|
||||
password: await strapi.admin.services.auth.hashPassword(values.password),
|
||||
};
|
||||
|
||||
const data = await adminQueries.create(user);
|
||||
const data = await strapi.query('administrator', 'admin').create(user);
|
||||
|
||||
// Send 201 `created`
|
||||
ctx.created(_.omit(data, ['password']));
|
||||
ctx.created(strapi.admin.services.auth.sanitizeUser(data));
|
||||
},
|
||||
|
||||
/**
|
||||
@ -177,30 +179,29 @@ module.exports = {
|
||||
*/
|
||||
|
||||
async update(ctx) {
|
||||
const { id } = ctx.params;
|
||||
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;
|
||||
|
||||
const admin = await adminQueries.findOne(ctx.params);
|
||||
const admin = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.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,
|
||||
});
|
||||
const adminsWithSameEmail = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.findOne({
|
||||
email: values.email,
|
||||
});
|
||||
|
||||
if (
|
||||
adminsWithSameEmail &&
|
||||
adminsWithSameEmail[primaryKey] !== admin[primaryKey]
|
||||
) {
|
||||
if (adminsWithSameEmail && adminsWithSameEmail.id !== admin.id) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
ctx.request.admin
|
||||
@ -218,14 +219,13 @@ module.exports = {
|
||||
|
||||
// check there are not user with requested username
|
||||
if (values.username !== admin.username) {
|
||||
const adminsWithSameUsername = await adminQueries.findOne({
|
||||
username: values.username,
|
||||
});
|
||||
const adminsWithSameUsername = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.findOne({
|
||||
username: values.username,
|
||||
});
|
||||
|
||||
if (
|
||||
adminsWithSameUsername &&
|
||||
adminsWithSameUsername[primaryKey] !== admin[primaryKey]
|
||||
) {
|
||||
if (adminsWithSameUsername && adminsWithSameUsername.id !== admin.id) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
ctx.request.admin
|
||||
@ -256,7 +256,9 @@ module.exports = {
|
||||
);
|
||||
}
|
||||
|
||||
const data = await adminQueries.update(ctx.params, user);
|
||||
const data = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.update({ id }, user);
|
||||
|
||||
// Send 200 `ok`
|
||||
ctx.send(data);
|
||||
|
||||
@ -10,11 +10,10 @@
|
||||
const crypto = require('crypto');
|
||||
const _ = require('lodash');
|
||||
|
||||
|
||||
const emailRegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
|
||||
module.exports = {
|
||||
callback: async ctx => {
|
||||
async callback(ctx) {
|
||||
const params = ctx.request.body;
|
||||
|
||||
// The identifier is required.
|
||||
@ -50,9 +49,7 @@ module.exports = {
|
||||
}
|
||||
|
||||
// Check if the admin exists.
|
||||
const admin = await strapi.admin
|
||||
.queries('administrator', 'admin')
|
||||
.findOne(query);
|
||||
const admin = await strapi.query('administrator', 'admin').findOne(query);
|
||||
|
||||
if (!admin) {
|
||||
return ctx.badRequest(
|
||||
@ -94,7 +91,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
register: async ctx => {
|
||||
async register(ctx) {
|
||||
const params = ctx.request.body;
|
||||
|
||||
// Username is required.
|
||||
@ -137,8 +134,8 @@ module.exports = {
|
||||
}
|
||||
|
||||
// First, check if their is at least one admin
|
||||
const admins = await strapi.admin
|
||||
.queries('administrator', 'admin')
|
||||
const admins = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.find({ _limit: 1 });
|
||||
|
||||
if (admins.length > 0) {
|
||||
@ -154,7 +151,7 @@ module.exports = {
|
||||
params.password
|
||||
);
|
||||
|
||||
const admin = await strapi.admin.queries('administrator', 'admin').findOne({
|
||||
const admin = await strapi.query('administrator', 'admin').findOne({
|
||||
email: params.email,
|
||||
});
|
||||
|
||||
@ -168,9 +165,7 @@ module.exports = {
|
||||
}
|
||||
|
||||
try {
|
||||
const admin = await strapi.admin
|
||||
.queries('administrator', 'admin')
|
||||
.create(params);
|
||||
const admin = await strapi.query('administrator', 'admin').create(params);
|
||||
|
||||
admin.isAdmin = true;
|
||||
|
||||
@ -195,69 +190,63 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
changePassword: async ctx => {
|
||||
const params = _.assign({}, ctx.request.body, ctx.params);
|
||||
async changePassword(ctx) {
|
||||
const { password, passwordConfirmation, code } = {
|
||||
...ctx.request.body,
|
||||
...ctx.params,
|
||||
};
|
||||
|
||||
if (
|
||||
params.password &&
|
||||
params.passwordConfirmation &&
|
||||
params.password === params.passwordConfirmation &&
|
||||
params.code
|
||||
) {
|
||||
const admin = await strapi.admin
|
||||
.queries('administrator', 'admin')
|
||||
.findOne({ resetPasswordToken: params.code });
|
||||
if (!password) return ctx.badRequest('Missing password');
|
||||
if (!passwordConfirmation)
|
||||
return ctx.badRequest('Missing passwordConfirmation');
|
||||
if (!code) return ctx.badRequest('Missing code');
|
||||
|
||||
if (!admin) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
ctx.request.admin
|
||||
? [{ messages: [{ id: 'Auth.form.error.code.provide' }] }]
|
||||
: 'Incorrect code provided.'
|
||||
);
|
||||
}
|
||||
|
||||
// Delete the current code
|
||||
admin.resetPasswordToken = null;
|
||||
|
||||
admin.password = await strapi.admin.services.auth.hashPassword(
|
||||
params.password
|
||||
);
|
||||
|
||||
// Update the admin.
|
||||
await strapi.admin.queries('administrator', 'admin').update(admin);
|
||||
|
||||
ctx.send({
|
||||
jwt: strapi.admin.services.auth.createJwtToken(admin),
|
||||
user: strapi.admin.services.auth.sanitizeUser(admin),
|
||||
});
|
||||
} else if (
|
||||
params.password &&
|
||||
params.passwordConfirmation &&
|
||||
params.password !== params.passwordConfirmation
|
||||
) {
|
||||
if (password !== passwordConfirmation) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
ctx.request.admin
|
||||
? [{ messages: [{ id: 'Auth.form.error.password.matching' }] }]
|
||||
: 'Passwords do not match.'
|
||||
);
|
||||
} else {
|
||||
}
|
||||
|
||||
const admin = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.findOne({ resetPasswordToken: code });
|
||||
|
||||
if (!admin) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
ctx.request.admin
|
||||
? [{ messages: [{ id: 'Auth.form.error.params.provide' }] }]
|
||||
: 'Incorrect params provided.'
|
||||
? [{ messages: [{ id: 'Auth.form.error.code.provide' }] }]
|
||||
: 'Incorrect code provided.'
|
||||
);
|
||||
}
|
||||
|
||||
const data = {
|
||||
resetPasswordToken: null,
|
||||
password: await strapi.admin.services.auth.hashPassword(password),
|
||||
};
|
||||
|
||||
const updatedAdmin = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.update({ id: admin.id }, data);
|
||||
|
||||
return ctx.send({
|
||||
jwt: strapi.admin.services.auth.createJwtToken(updatedAdmin),
|
||||
user: strapi.admin.services.auth.sanitizeUser(updatedAdmin),
|
||||
});
|
||||
},
|
||||
|
||||
forgotPassword: async ctx => {
|
||||
async forgotPassword(ctx) {
|
||||
const { email, url } = ctx.request.body;
|
||||
|
||||
if (!email) return ctx.badRequest('Missing email');
|
||||
if (!url) return ctx.badRequest('Missing url');
|
||||
|
||||
// Find the admin thanks to his email.
|
||||
const admin = await strapi.admin
|
||||
.queries('administrator', 'admin')
|
||||
const admin = await strapi
|
||||
.query('administrator', 'admin')
|
||||
.findOne({ email });
|
||||
|
||||
// admin not found.
|
||||
@ -273,9 +262,6 @@ module.exports = {
|
||||
// Generate random token.
|
||||
const resetPasswordToken = crypto.randomBytes(64).toString('hex');
|
||||
|
||||
// Set the property code.
|
||||
admin.resetPasswordToken = resetPasswordToken;
|
||||
|
||||
const settings = {
|
||||
from: {
|
||||
name: 'Administration Panel',
|
||||
@ -310,7 +296,9 @@ module.exports = {
|
||||
}
|
||||
|
||||
// Update the admin.
|
||||
await strapi.admin.queries('administrator', 'admin').update(admin);
|
||||
await strapi
|
||||
.query('administrator', 'admin')
|
||||
.update({ id: admin.id }, { resetPasswordToken });
|
||||
|
||||
ctx.send({ ok: true });
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user