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-11-09 18:38:20 +01:00
|
|
|
const utils = require('@strapi/utils');
|
2021-07-08 18:15:32 +02:00
|
|
|
const { getService } = require('../utils');
|
2021-11-09 18:38:20 +01:00
|
|
|
const { validateCreateUserBody, validateUpdateUserBody } = require('./validation/user');
|
2019-09-12 10:50:52 +02:00
|
|
|
|
2022-01-14 14:23:04 +08:00
|
|
|
const { ApplicationError, ValidationError, NotFoundError } = utils.errors;
|
2017-12-04 15:35:45 +01:00
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
const sanitizeOutput = async (user, ctx) => {
|
2021-11-09 18:38:20 +01:00
|
|
|
const schema = strapi.getModel('plugin::users-permissions.user');
|
2021-11-04 15:47:53 +01:00
|
|
|
const { auth } = ctx.state;
|
|
|
|
|
2024-03-21 20:07:54 +01:00
|
|
|
return strapi.contentAPI.sanitize.output(user, schema, { auth });
|
2021-11-04 15:47:53 +01:00
|
|
|
};
|
2020-07-06 16:25:25 +02:00
|
|
|
|
2023-08-10 15:24:35 +02:00
|
|
|
const validateQuery = async (query, ctx) => {
|
2023-02-09 11:35:50 +01:00
|
|
|
const schema = strapi.getModel('plugin::users-permissions.user');
|
|
|
|
const { auth } = ctx.state;
|
|
|
|
|
2024-03-21 20:07:54 +01:00
|
|
|
return strapi.contentAPI.validate.query(query, schema, { auth });
|
2023-02-09 11:35:50 +01:00
|
|
|
};
|
|
|
|
|
2023-08-11 13:13:44 +02:00
|
|
|
const sanitizeQuery = async (query, ctx) => {
|
|
|
|
const schema = strapi.getModel('plugin::users-permissions.user');
|
|
|
|
const { auth } = ctx.state;
|
|
|
|
|
2024-03-21 20:07:54 +01:00
|
|
|
return strapi.contentAPI.sanitize.query(query, schema, { auth });
|
2023-08-11 13:13:44 +02:00
|
|
|
};
|
|
|
|
|
2021-11-04 15:18:09 +01:00
|
|
|
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();
|
2020-07-06 16:25:25 +02:00
|
|
|
|
2021-11-09 18:38:20 +01:00
|
|
|
await validateCreateUserBody(ctx.request.body);
|
2020-07-06 16:25:25 +02:00
|
|
|
|
2021-11-09 18:38:20 +01:00
|
|
|
const { email, username, role } = ctx.request.body;
|
2020-07-06 16:25:25 +02:00
|
|
|
|
2024-03-13 15:40:30 +01:00
|
|
|
const userWithSameUsername = await strapi.db
|
2021-11-04 15:18:09 +01:00
|
|
|
.query('plugin::users-permissions.user')
|
|
|
|
.findOne({ where: { username } });
|
2017-11-14 11:11:22 +01:00
|
|
|
|
2021-11-04 15:18:09 +01:00
|
|
|
if (userWithSameUsername) {
|
2021-11-09 18:38:20 +01:00
|
|
|
if (!email) throw new ApplicationError('Username already taken');
|
2021-11-04 15:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (advanced.unique_email) {
|
2024-03-13 15:40:30 +01:00
|
|
|
const userWithSameEmail = await strapi.db
|
2021-11-04 15:18:09 +01:00
|
|
|
.query('plugin::users-permissions.user')
|
|
|
|
.findOne({ where: { email: email.toLowerCase() } });
|
|
|
|
|
|
|
|
if (userWithSameEmail) {
|
2021-11-09 18:38:20 +01:00
|
|
|
throw new ApplicationError('Email already taken');
|
2021-11-04 15:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = {
|
|
|
|
...ctx.request.body,
|
2022-06-04 09:48:31 +02:00
|
|
|
email: email.toLowerCase(),
|
2021-11-04 15:18:09 +01:00
|
|
|
provider: 'local',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!role) {
|
2024-03-13 15:40:30 +01:00
|
|
|
const defaultRole = await strapi.db
|
2021-11-04 15:18:09 +01:00
|
|
|
.query('plugin::users-permissions.role')
|
|
|
|
.findOne({ where: { type: advanced.default_role } });
|
|
|
|
|
|
|
|
user.role = defaultRole.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const data = await getService('user').add(user);
|
2021-11-09 18:38:20 +01:00
|
|
|
const sanitizedData = await sanitizeOutput(data, ctx);
|
2021-11-04 15:18:09 +01:00
|
|
|
|
2021-11-09 18:38:20 +01:00
|
|
|
ctx.created(sanitizedData);
|
2021-11-04 15:18:09 +01:00
|
|
|
} catch (error) {
|
2021-11-09 18:38:20 +01:00
|
|
|
throw new ApplicationError(error.message);
|
2021-11-04 15:18:09 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2022-03-03 23:59:32 +09:00
|
|
|
const user = await getService('user').fetch(id);
|
2022-01-14 14:23:04 +08:00
|
|
|
if (!user) {
|
2022-05-19 22:03:36 +02:00
|
|
|
throw new NotFoundError(`User not found`);
|
2022-01-14 14:23:04 +08:00
|
|
|
}
|
2021-11-04 15:18:09 +01:00
|
|
|
|
2021-11-09 18:38:20 +01:00
|
|
|
await validateUpdateUserBody(ctx.request.body);
|
2021-11-04 15:18:09 +01:00
|
|
|
|
2021-11-09 18:38:20 +01:00
|
|
|
if (user.provider === 'local' && _.has(ctx.request.body, 'password') && !password) {
|
|
|
|
throw new ValidationError('password.notNull');
|
2021-11-04 15:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.has(ctx.request.body, 'username')) {
|
2024-03-13 15:40:30 +01:00
|
|
|
const userWithSameUsername = await strapi.db
|
2021-11-04 15:18:09 +01:00
|
|
|
.query('plugin::users-permissions.user')
|
|
|
|
.findOne({ where: { username } });
|
|
|
|
|
2022-09-05 15:18:24 +02:00
|
|
|
if (userWithSameUsername && _.toString(userWithSameUsername.id) !== _.toString(id)) {
|
2021-11-09 18:38:20 +01:00
|
|
|
throw new ApplicationError('Username already taken');
|
2021-11-04 15:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.has(ctx.request.body, 'email') && advancedConfigs.unique_email) {
|
2024-03-13 15:40:30 +01:00
|
|
|
const userWithSameEmail = await strapi.db
|
2021-11-04 15:18:09 +01:00
|
|
|
.query('plugin::users-permissions.user')
|
|
|
|
.findOne({ where: { email: email.toLowerCase() } });
|
|
|
|
|
2022-09-05 15:18:24 +02:00
|
|
|
if (userWithSameEmail && _.toString(userWithSameEmail.id) !== _.toString(id)) {
|
2021-11-09 18:38:20 +01:00
|
|
|
throw new ApplicationError('Email already taken');
|
2021-11-04 15:18:09 +01:00
|
|
|
}
|
|
|
|
ctx.request.body.email = ctx.request.body.email.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2022-08-08 15:50:34 +02:00
|
|
|
const updateData = {
|
2021-11-04 15:18:09 +01:00
|
|
|
...ctx.request.body,
|
|
|
|
};
|
|
|
|
|
2022-01-05 23:54:58 +09:00
|
|
|
const data = await getService('user').edit(user.id, updateData);
|
2021-11-09 18:38:20 +01:00
|
|
|
const sanitizedData = await sanitizeOutput(data, ctx);
|
2021-11-04 15:18:09 +01:00
|
|
|
|
2021-11-09 18:38:20 +01:00
|
|
|
ctx.send(sanitizedData);
|
2021-11-04 15:18:09 +01:00
|
|
|
},
|
2020-07-06 16:25:25 +02:00
|
|
|
|
2017-11-14 11:11:22 +01:00
|
|
|
/**
|
|
|
|
* Retrieve user records.
|
|
|
|
* @return {Object|Array}
|
|
|
|
*/
|
2021-12-21 10:43:36 +09:00
|
|
|
async find(ctx) {
|
2023-08-11 13:13:44 +02:00
|
|
|
await validateQuery(ctx.query, ctx);
|
|
|
|
const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
|
2023-02-09 11:35:50 +01:00
|
|
|
const users = await getService('user').fetchAll(sanitizedQuery);
|
2019-05-21 16:18:18 +02:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
ctx.body = await Promise.all(users.map((user) => sanitizeOutput(user, ctx)));
|
2020-04-17 17:33:21 +02:00
|
|
|
},
|
|
|
|
|
2017-11-14 11:11:22 +01:00
|
|
|
/**
|
|
|
|
* Retrieve a user record.
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2019-07-15 23:16:50 +02:00
|
|
|
async findOne(ctx) {
|
2019-07-16 16:26:53 +02:00
|
|
|
const { id } = ctx.params;
|
2023-08-11 13:13:44 +02:00
|
|
|
await validateQuery(ctx.query, ctx);
|
|
|
|
const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
|
2022-03-03 22:56:58 +09:00
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
let data = await getService('user').fetch(id, sanitizedQuery);
|
2017-12-06 14:15:27 +01:00
|
|
|
|
|
|
|
if (data) {
|
2021-11-09 18:38:20 +01:00
|
|
|
data = await sanitizeOutput(data, ctx);
|
2017-12-06 14:15:27 +01:00
|
|
|
}
|
2017-11-14 11:11:22 +01:00
|
|
|
|
2020-07-06 16:25:25 +02:00
|
|
|
ctx.body = data;
|
2017-11-14 11:11:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-07-06 16:25:25 +02:00
|
|
|
* Retrieve user count.
|
|
|
|
* @return {Number}
|
2017-11-14 11:11:22 +01:00
|
|
|
*/
|
2020-07-06 16:25:25 +02:00
|
|
|
async count(ctx) {
|
2023-08-11 13:13:44 +02:00
|
|
|
await validateQuery(ctx.query, ctx);
|
|
|
|
const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
|
2023-02-09 11:35:50 +01:00
|
|
|
|
|
|
|
ctx.body = await getService('user').count(sanitizedQuery);
|
2017-11-14 11:11:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-07-06 16:25:25 +02:00
|
|
|
* Destroy a/an user record.
|
2017-11-14 11:11:22 +01:00
|
|
|
* @return {Object}
|
|
|
|
*/
|
2020-07-06 16:25:25 +02:00
|
|
|
async destroy(ctx) {
|
2019-07-16 16:26:53 +02:00
|
|
|
const { id } = ctx.params;
|
2021-07-08 18:15:32 +02:00
|
|
|
|
|
|
|
const data = await getService('user').remove({ id });
|
2021-11-09 18:38:20 +01:00
|
|
|
const sanitizedUser = await sanitizeOutput(data, ctx);
|
2021-07-08 18:15:32 +02:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.send(sanitizedUser);
|
2020-07-06 16:25:25 +02:00
|
|
|
},
|
2019-07-16 16:26:53 +02:00
|
|
|
|
2017-11-14 11:11:22 +01:00
|
|
|
/**
|
2020-07-06 16:25:25 +02:00
|
|
|
* Retrieve authenticated user.
|
|
|
|
* @return {Object|Array}
|
2017-11-14 11:11:22 +01:00
|
|
|
*/
|
2020-07-06 16:25:25 +02:00
|
|
|
async me(ctx) {
|
2022-06-01 19:11:18 +02:00
|
|
|
const authUser = ctx.state.user;
|
|
|
|
const { query } = ctx;
|
2018-06-06 16:20:52 +02:00
|
|
|
|
2022-06-01 19:11:18 +02:00
|
|
|
if (!authUser) {
|
2021-10-20 17:30:05 +02:00
|
|
|
return ctx.unauthorized();
|
2020-07-06 16:25:25 +02:00
|
|
|
}
|
2018-06-06 16:20:52 +02:00
|
|
|
|
2023-08-11 13:13:44 +02:00
|
|
|
await validateQuery(query, ctx);
|
|
|
|
const sanitizedQuery = await sanitizeQuery(query, ctx);
|
2023-02-09 11:35:50 +01:00
|
|
|
const user = await getService('user').fetch(authUser.id, sanitizedQuery);
|
2022-06-01 19:11:18 +02:00
|
|
|
|
2021-11-09 18:38:20 +01:00
|
|
|
ctx.body = await sanitizeOutput(user, ctx);
|
2019-07-15 23:16:50 +02:00
|
|
|
},
|
2017-11-14 11:11:22 +01:00
|
|
|
};
|