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');
|
|
|
|
|
2019-07-16 15:31:15 +02:00
|
|
|
const sanitizeUser = user => _.omit(user, ['password', 'resetPasswordToken']);
|
2019-08-21 12:10:23 +02:00
|
|
|
const formatError = error => [
|
|
|
|
{ messages: [{ id: error.id, message: error.message, field: error.field }] },
|
2019-07-16 15:31:15 +02:00
|
|
|
];
|
2017-11-14 11:11:22 +01:00
|
|
|
|
2019-07-15 23:16:50 +02:00
|
|
|
module.exports = {
|
2017-11-14 11:11:22 +01:00
|
|
|
/**
|
|
|
|
* Retrieve user records.
|
|
|
|
* @return {Object|Array}
|
|
|
|
*/
|
2019-07-15 23:16:50 +02:00
|
|
|
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
|
|
|
|
users = await strapi
|
|
|
|
.query('user', 'users-permissions')
|
|
|
|
.search(ctx.query, populate);
|
|
|
|
} else {
|
2019-07-15 23:16:50 +02:00
|
|
|
users = await strapi.plugins['users-permissions'].services.user.fetchAll(
|
|
|
|
ctx.query,
|
|
|
|
populate
|
|
|
|
);
|
2019-05-21 16:18:18 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 23:16:50 +02:00
|
|
|
const data = users.map(sanitizeUser);
|
2017-11-14 11:11:22 +01:00
|
|
|
ctx.send(data);
|
|
|
|
},
|
|
|
|
|
2018-01-10 20:29:34 +01:00
|
|
|
/**
|
|
|
|
* Retrieve authenticated user.
|
|
|
|
* @return {Object|Array}
|
|
|
|
*/
|
2019-07-15 23:16:50 +02:00
|
|
|
async me(ctx) {
|
2018-01-10 20:29:34 +01:00
|
|
|
const user = ctx.state.user;
|
2018-01-11 16:24:16 +01:00
|
|
|
|
|
|
|
if (!user) {
|
2019-07-15 23:16:50 +02:00
|
|
|
return ctx.badRequest(null, [
|
|
|
|
{ messages: [{ id: 'No authorization header was found' }] },
|
|
|
|
]);
|
2018-01-11 16:24:16 +01:00
|
|
|
}
|
2018-01-18 14:10:26 +01:00
|
|
|
|
2019-07-15 23:16:50 +02:00
|
|
|
const data = sanitizeUser(user);
|
2018-01-10 20:29:34 +01:00
|
|
|
ctx.send(data);
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
let data = await strapi.plugins['users-permissions'].services.user.fetch({
|
|
|
|
id,
|
|
|
|
});
|
2017-12-06 14:15:27 +01:00
|
|
|
|
|
|
|
if (data) {
|
2019-07-15 23:16:50 +02:00
|
|
|
data = sanitizeUser(data);
|
2017-12-06 14:15:27 +01:00
|
|
|
}
|
2017-11-14 11:11:22 +01:00
|
|
|
|
|
|
|
// Send 200 `ok`
|
|
|
|
ctx.send(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a/an user record.
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2019-07-15 23:16:50 +02:00
|
|
|
async create(ctx) {
|
|
|
|
const advanced = await strapi
|
|
|
|
.store({
|
|
|
|
environment: '',
|
|
|
|
type: 'plugin',
|
|
|
|
name: 'users-permissions',
|
|
|
|
key: 'advanced',
|
|
|
|
})
|
|
|
|
.get();
|
2018-06-12 19:19:10 +02:00
|
|
|
|
2019-07-16 15:31:15 +02:00
|
|
|
const { email, username, password, role } = ctx.request.body;
|
|
|
|
|
|
|
|
if (!email) return ctx.badRequest('missing.email');
|
|
|
|
if (!username) return ctx.badRequest('missing.username');
|
|
|
|
if (!password) return ctx.badRequest('missing.password');
|
|
|
|
|
2019-07-16 16:26:53 +02:00
|
|
|
const userWithSameUsername = await strapi
|
2019-07-16 15:31:15 +02:00
|
|
|
.query('user', 'users-permissions')
|
|
|
|
.findOne({ username });
|
|
|
|
|
2019-07-16 16:26:53 +02:00
|
|
|
if (userWithSameUsername) {
|
2019-07-16 15:31:15 +02:00
|
|
|
return ctx.badRequest(
|
|
|
|
null,
|
2019-08-21 12:10:23 +02:00
|
|
|
formatError({
|
|
|
|
id: 'Auth.form.error.username.taken',
|
|
|
|
message: 'Username already taken.',
|
|
|
|
field: ['username'],
|
|
|
|
})
|
2019-07-16 15:31:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (advanced.unique_email) {
|
2019-07-16 16:26:53 +02:00
|
|
|
const userWithSameEmail = await strapi
|
2019-07-15 23:16:50 +02:00
|
|
|
.query('user', 'users-permissions')
|
2019-07-16 15:31:15 +02:00
|
|
|
.findOne({ email });
|
2018-01-26 09:37:24 +01:00
|
|
|
|
2019-07-16 16:26:53 +02:00
|
|
|
if (userWithSameEmail) {
|
2019-07-15 23:16:50 +02:00
|
|
|
return ctx.badRequest(
|
|
|
|
null,
|
2019-08-21 12:10:23 +02:00
|
|
|
|
|
|
|
formatError({
|
|
|
|
id: 'Auth.form.error.email.taken',
|
|
|
|
message: 'Email already taken.',
|
|
|
|
field: ['email'],
|
|
|
|
})
|
2019-07-15 23:16:50 +02:00
|
|
|
);
|
2018-01-26 09:37:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 15:31:15 +02:00
|
|
|
const user = {
|
2019-07-16 16:26:53 +02:00
|
|
|
...ctx.request.body,
|
2019-07-16 15:31:15 +02:00
|
|
|
provider: 'local',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!role) {
|
2019-07-15 23:16:50 +02:00
|
|
|
const defaultRole = await strapi
|
|
|
|
.query('role', 'users-permissions')
|
|
|
|
.findOne({ type: advanced.default_role }, []);
|
2018-06-12 19:19:10 +02:00
|
|
|
|
2019-07-16 15:31:15 +02:00
|
|
|
user.role = defaultRole.id;
|
2018-06-12 19:19:10 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 15:11:55 +01:00
|
|
|
try {
|
2019-07-15 23:16:50 +02:00
|
|
|
const data = await strapi.plugins['users-permissions'].services.user.add(
|
2019-07-16 15:31:15 +02:00
|
|
|
user
|
2019-07-15 23:16:50 +02:00
|
|
|
);
|
2018-11-27 14:59:28 +01:00
|
|
|
|
2017-12-06 15:11:55 +01:00
|
|
|
ctx.created(data);
|
2019-07-15 23:16:50 +02:00
|
|
|
} catch (error) {
|
2019-08-21 12:10:23 +02:00
|
|
|
ctx.badRequest(null, formatError(error));
|
2017-12-06 15:11:55 +01:00
|
|
|
}
|
2017-11-14 11:11:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a/an user record.
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2019-07-15 23:16:50 +02:00
|
|
|
async update(ctx) {
|
2019-07-16 16:26:53 +02:00
|
|
|
const advancedConfigs = await strapi
|
|
|
|
.store({
|
|
|
|
environment: '',
|
|
|
|
type: 'plugin',
|
|
|
|
name: 'users-permissions',
|
|
|
|
key: 'advanced',
|
|
|
|
})
|
|
|
|
.get();
|
2017-12-04 15:35:45 +01:00
|
|
|
|
2019-07-16 16:26:53 +02:00
|
|
|
const { id } = ctx.params;
|
|
|
|
const { email, username, password } = ctx.request.body;
|
2017-12-04 15:35:45 +01:00
|
|
|
|
2019-07-16 16:26:53 +02:00
|
|
|
if (!email) return ctx.badRequest('missing.email');
|
|
|
|
if (!username) return ctx.badRequest('missing.username');
|
|
|
|
if (!password) return ctx.badRequest('missing.password');
|
2018-01-18 14:10:26 +01:00
|
|
|
|
2019-07-16 16:26:53 +02:00
|
|
|
const userWithSameUsername = await strapi
|
|
|
|
.query('user', 'users-permissions')
|
|
|
|
.findOne({ username });
|
2017-11-14 11:11:22 +01:00
|
|
|
|
2019-07-16 16:26:53 +02:00
|
|
|
if (userWithSameUsername && userWithSameUsername.id != id) {
|
|
|
|
return ctx.badRequest(
|
2019-07-15 23:16:50 +02:00
|
|
|
null,
|
2019-08-21 12:10:23 +02:00
|
|
|
formatError({
|
|
|
|
id: 'Auth.form.error.username.taken',
|
|
|
|
message: 'username.alreadyTaken.',
|
|
|
|
field: ['username'],
|
|
|
|
})
|
2019-07-15 23:16:50 +02:00
|
|
|
);
|
2017-12-06 15:11:55 +01:00
|
|
|
}
|
2019-07-16 16:26:53 +02:00
|
|
|
|
|
|
|
if (advancedConfigs.unique_email) {
|
|
|
|
const userWithSameEmail = await strapi
|
|
|
|
.query('user', 'users-permissions')
|
|
|
|
.findOne({ email });
|
|
|
|
|
|
|
|
if (userWithSameEmail && userWithSameEmail.id != id) {
|
|
|
|
return ctx.badRequest(
|
|
|
|
null,
|
2019-08-21 12:10:23 +02:00
|
|
|
formatError({
|
|
|
|
id: 'Auth.form.error.email.taken',
|
|
|
|
message: 'Eamil already taken',
|
|
|
|
field: ['email'],
|
|
|
|
})
|
2019-07-16 16:26:53 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await strapi.plugins['users-permissions'].services.user.fetch({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
let updateData = {
|
|
|
|
...ctx.request.body,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (password === user.password) {
|
2019-08-05 10:31:18 +02:00
|
|
|
delete updateData.password;
|
2019-07-16 16:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const data = await strapi.plugins['users-permissions'].services.user.edit(
|
|
|
|
{ id },
|
|
|
|
updateData
|
|
|
|
);
|
|
|
|
|
|
|
|
ctx.send(data);
|
2017-11-14 11:11:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a/an user record.
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2019-07-15 23:16:50 +02:00
|
|
|
async destroy(ctx) {
|
2019-07-16 16:49:36 +02:00
|
|
|
const { id } = ctx.params;
|
2019-07-15 23:16:50 +02:00
|
|
|
const data = await strapi.plugins['users-permissions'].services.user.remove(
|
2019-07-16 16:49:36 +02:00
|
|
|
{ id }
|
2019-07-15 23:16:50 +02:00
|
|
|
);
|
|
|
|
|
2018-06-06 16:20:52 +02:00
|
|
|
ctx.send(data);
|
|
|
|
},
|
|
|
|
|
2019-07-15 23:16:50 +02:00
|
|
|
async destroyAll(ctx) {
|
|
|
|
const data = await strapi.plugins[
|
|
|
|
'users-permissions'
|
|
|
|
].services.user.removeAll(ctx.params, ctx.request.query);
|
2018-06-06 16:20:52 +02:00
|
|
|
|
2017-11-14 11:11:22 +01:00
|
|
|
ctx.send(data);
|
2019-07-15 23:16:50 +02:00
|
|
|
},
|
2017-11-14 11:11:22 +01:00
|
|
|
};
|