192 lines
4.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');
const utils = require('@strapi/utils');
2021-07-08 18:15:32 +02:00
const { getService } = require('../utils');
const { validateCreateUserBody, validateUpdateUserBody } = require('./validation/user');
2019-09-12 10:50:52 +02:00
const { sanitize } = utils;
const { ApplicationError, ValidationError } = utils.errors;
2017-12-04 15:35:45 +01:00
const sanitizeOutput = (user, ctx) => {
const schema = strapi.getModel('plugin::users-permissions.user');
const { auth } = ctx.state;
return sanitize.contentAPI.output(user, schema, { auth });
};
module.exports = {
/**
* Create a/an user record.
* @return {Object}
*/
async create(ctx) {
const advanced = await strapi
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
.get();
await validateCreateUserBody(ctx.request.body);
const { email, username, role } = ctx.request.body;
const userWithSameUsername = await strapi
.query('plugin::users-permissions.user')
.findOne({ where: { username } });
2017-11-14 11:11:22 +01:00
if (userWithSameUsername) {
if (!email) throw new ApplicationError('Username already taken');
}
if (advanced.unique_email) {
const userWithSameEmail = await strapi
.query('plugin::users-permissions.user')
.findOne({ where: { email: email.toLowerCase() } });
if (userWithSameEmail) {
throw new ApplicationError('Email already taken');
}
}
const user = {
...ctx.request.body,
provider: 'local',
};
user.email = _.toLower(user.email);
if (!role) {
const defaultRole = await strapi
.query('plugin::users-permissions.role')
.findOne({ where: { type: advanced.default_role } });
user.role = defaultRole.id;
}
try {
const data = await getService('user').add(user);
const sanitizedData = await sanitizeOutput(data, ctx);
ctx.created(sanitizedData);
} catch (error) {
throw new ApplicationError(error.message);
}
},
/**
* Update a/an user record.
* @return {Object}
*/
async update(ctx) {
const advancedConfigs = await strapi
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
.get();
const { id } = ctx.params;
const { email, username, password } = ctx.request.body;
const user = await getService('user').fetch({ id });
await validateUpdateUserBody(ctx.request.body);
if (user.provider === 'local' && _.has(ctx.request.body, 'password') && !password) {
throw new ValidationError('password.notNull');
}
if (_.has(ctx.request.body, 'username')) {
const userWithSameUsername = await strapi
.query('plugin::users-permissions.user')
.findOne({ where: { username } });
if (userWithSameUsername && userWithSameUsername.id != id) {
throw new ApplicationError('Username already taken');
}
}
if (_.has(ctx.request.body, 'email') && advancedConfigs.unique_email) {
const userWithSameEmail = await strapi
.query('plugin::users-permissions.user')
.findOne({ where: { email: email.toLowerCase() } });
if (userWithSameEmail && userWithSameEmail.id != id) {
throw new ApplicationError('Email already taken');
}
ctx.request.body.email = ctx.request.body.email.toLowerCase();
}
let updateData = {
...ctx.request.body,
};
const data = await getService('user').edit({ id }, updateData);
const sanitizedData = await sanitizeOutput(data, ctx);
ctx.send(sanitizedData);
},
2017-11-14 11:11:22 +01:00
/**
* Retrieve user records.
* @return {Object|Array}
*/
async find(ctx, next, { populate } = {}) {
2021-07-28 21:03:32 +02:00
const users = await getService('user').fetchAll(ctx.query, populate);
2019-05-21 16:18:18 +02:00
ctx.body = await Promise.all(users.map(user => sanitizeOutput(user, ctx)));
},
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 = await sanitizeOutput(data, ctx);
}
2017-11-14 11:11:22 +01:00
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) {
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 });
const sanitizedUser = await sanitizeOutput(data, ctx);
2021-07-08 18:15:32 +02:00
ctx.send(sanitizedUser);
},
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) {
2021-10-20 17:30:05 +02:00
return ctx.unauthorized();
}
ctx.body = await sanitizeOutput(user, ctx);
},
2017-11-14 11:11:22 +01:00
};