2017-12-14 16:29:13 +01:00

101 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* User.js controller
*
* @description: A set of functions called "actions" for managing `User`.
*/
const _ = require('lodash');
module.exports = {
/**
* Retrieve user records.
*
* @return {Object|Array}
*/
find: async (ctx) => {
let data = await strapi.plugins['users-permissions'].services.user.fetchAll(ctx.query);
data.reduce((acc, user) => {
acc.push(_.omit(user.toJSON ? user.toJSON() : user, ['password', 'resetPasswordToken']));
return acc;
}, []);
// Send 200 `ok`
ctx.send(data);
},
/**
}
* Retrieve a user record.
*
* @return {Object}
*/
findOne: async (ctx) => {
let data = await strapi.plugins['users-permissions'].services.user.fetch(ctx.params);
if (data) {
data = _.omit(data.toJSON ? data.toJSON() : data, ['password', 'resetPasswordToken']);
}
// Send 200 `ok`
ctx.send(data);
},
/**
* Create a/an user record.
*
* @return {Object}
*/
create: async (ctx) => {
try {
const data = await strapi.plugins['users-permissions'].services.user.add(ctx.request.body);
// Send 201 `created`
ctx.created(data);
} catch(error) {
ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: error.message, field: error.field }] }] : error.message);
}
},
/**
* Update a/an user record.
*
* @return {Object}
*/
update: async (ctx, next) => {
try {
const user = await strapi.plugins['users-permissions'].services.user.fetch(ctx.params);
if (_.get(ctx.request, 'body.password') === user.password) {
delete ctx.request.body.password;
}
const data = await strapi.plugins['users-permissions'].services.user.edit(ctx.params, ctx.request.body) ;
// Send 200 `ok`
ctx.send(data);
} catch(error) {
ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: error.message, field: error.field }] }] : error.message);
}
},
/**
* Destroy a/an user record.
*
* @return {Object}
*/
destroy: async (ctx, next) => {
const data = await strapi.plugins['users-permissions'].services.user.remove(ctx.params);
// Send 200 `ok`
ctx.send(data);
}
};