129 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-11-14 11:11:22 +01:00
'use strict';
/**
* User.js controller
*
* @description: A set of functions called "actions" for managing `User`.
*/
2017-12-04 15:35:45 +01:00
const _ = require('lodash');
2021-04-29 13:51:12 +02:00
const { sanitizeEntity } = require('@strapi/utils');
2021-07-08 18:15:32 +02:00
const { getService } = require('../utils');
const adminUserController = require('./user/admin');
const apiUserController = require('./user/api');
2019-09-12 10:50:52 +02:00
const sanitizeUser = user =>
sanitizeEntity(user, {
2021-07-08 18:15:32 +02:00
model: strapi.getModel('plugins::users-permissions.user'),
2019-09-12 10:50:52 +02:00
});
2017-12-04 15:35:45 +01:00
const resolveController = ctx => {
const {
state: { isAuthenticatedAdmin },
} = ctx;
return isAuthenticatedAdmin ? adminUserController : apiUserController;
};
const resolveControllerMethod = method => ctx => {
const controller = resolveController(ctx);
const callbackFn = controller[method];
if (!_.isFunction(callbackFn)) {
return ctx.notFound();
}
return callbackFn(ctx);
};
2017-11-14 11:11:22 +01:00
module.exports = {
create: resolveControllerMethod('create'),
update: resolveControllerMethod('update'),
2017-11-14 11:11:22 +01:00
/**
* Retrieve user records.
* @return {Object|Array}
*/
async find(ctx, next, { populate } = {}) {
let users;
2017-11-14 11:11:22 +01:00
2019-05-21 16:18:18 +02:00
if (_.has(ctx.query, '_q')) {
// use core strapi query to search for users
2021-07-08 18:15:32 +02:00
// FIXME:
users = await strapi.query('plugins::users-permissions.user').search(ctx.query, populate);
2019-05-21 16:18:18 +02:00
} else {
2021-07-08 18:15:32 +02:00
users = await getService('user').fetchAll(ctx.query, populate);
2019-05-21 16:18:18 +02:00
}
ctx.body = users.map(sanitizeUser);
},
2017-11-14 11:11:22 +01:00
/**
* Retrieve a user record.
* @return {Object}
*/
async findOne(ctx) {
const { id } = ctx.params;
2021-07-08 22:07:52 +02:00
let data = await getService('user').fetch({ id });
if (data) {
data = sanitizeUser(data);
}
2017-11-14 11:11:22 +01:00
// Send 200 `ok`
ctx.body = data;
2017-11-14 11:11:22 +01:00
},
/**
* Retrieve user count.
* @return {Number}
2017-11-14 11:11:22 +01:00
*/
async count(ctx) {
if (_.has(ctx.query, '_q')) {
2021-07-08 18:15:32 +02:00
return await getService('user').countSearch(ctx.query);
2017-12-06 15:11:55 +01:00
}
2021-07-08 18:15:32 +02:00
ctx.body = await getService('user').count(ctx.query);
2017-11-14 11:11:22 +01:00
},
/**
* Destroy a/an user record.
2017-11-14 11:11:22 +01:00
* @return {Object}
*/
async destroy(ctx) {
const { id } = ctx.params;
2021-07-08 18:15:32 +02:00
const data = await getService('user').remove({ id });
ctx.send(sanitizeUser(data));
},
async destroyAll(ctx) {
const {
request: { query },
} = ctx;
const toRemove = Object.values(_.omit(query, 'source'));
2021-07-08 18:15:32 +02:00
// FIXME: delete many
const finalQuery = { id: toRemove };
const data = await getService('user').removeAll(finalQuery);
ctx.send(data);
2017-11-14 11:11:22 +01:00
},
/**
* Retrieve authenticated user.
* @return {Object|Array}
2017-11-14 11:11:22 +01:00
*/
async me(ctx) {
const user = ctx.state.user;
if (!user) {
return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
}
ctx.body = sanitizeUser(user);
},
2017-11-14 11:11:22 +01:00
};