/
user.js
220 lines (172 loc) 路 5.78 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'use strict';
/**
* User.js controller
*
* @description: A set of functions called "actions" for managing `User`.
*/
const _ = require('lodash');
const utils = require('@strapi/utils');
const { getService } = require('../utils');
const { validateCreateUserBody, validateUpdateUserBody } = require('./validation/user');
const { sanitize, validate } = utils;
const { ApplicationError, ValidationError, NotFoundError } = utils.errors;
const sanitizeOutput = async (user, ctx) => {
const schema = strapi.getModel('plugin::users-permissions.user');
const { auth } = ctx.state;
return sanitize.contentAPI.output(user, schema, { auth });
};
const validateQuery = async (query, ctx) => {
const schema = strapi.getModel('plugin::users-permissions.user');
const { auth } = ctx.state;
return validate.contentAPI.query(query, schema, { auth });
};
const sanitizeQuery = async (query, ctx) => {
const schema = strapi.getModel('plugin::users-permissions.user');
const { auth } = ctx.state;
return sanitize.contentAPI.query(query, 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 } });
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,
email: email.toLowerCase(),
provider: 'local',
};
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);
if (!user) {
throw new NotFoundError(`User not found`);
}
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 && _.toString(userWithSameUsername.id) !== _.toString(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 && _.toString(userWithSameEmail.id) !== _.toString(id)) {
throw new ApplicationError('Email already taken');
}
ctx.request.body.email = ctx.request.body.email.toLowerCase();
}
const updateData = {
...ctx.request.body,
};
const data = await getService('user').edit(user.id, updateData);
const sanitizedData = await sanitizeOutput(data, ctx);
ctx.send(sanitizedData);
},
/**
* Retrieve user records.
* @return {Object|Array}
*/
async find(ctx) {
await validateQuery(ctx.query, ctx);
const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
const users = await getService('user').fetchAll(sanitizedQuery);
ctx.body = await Promise.all(users.map((user) => sanitizeOutput(user, ctx)));
},
/**
* Retrieve a user record.
* @return {Object}
*/
async findOne(ctx) {
const { id } = ctx.params;
await validateQuery(ctx.query, ctx);
const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
let data = await getService('user').fetch(id, sanitizedQuery);
if (data) {
data = await sanitizeOutput(data, ctx);
}
ctx.body = data;
},
/**
* Retrieve user count.
* @return {Number}
*/
async count(ctx) {
await validateQuery(ctx.query, ctx);
const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
ctx.body = await getService('user').count(sanitizedQuery);
},
/**
* Destroy a/an user record.
* @return {Object}
*/
async destroy(ctx) {
const { id } = ctx.params;
const data = await getService('user').remove({ id });
const sanitizedUser = await sanitizeOutput(data, ctx);
ctx.send(sanitizedUser);
},
/**
* Retrieve authenticated user.
* @return {Object|Array}
*/
async me(ctx) {
const authUser = ctx.state.user;
const { query } = ctx;
if (!authUser) {
return ctx.unauthorized();
}
await validateQuery(query, ctx);
const sanitizedQuery = await sanitizeQuery(query, ctx);
const user = await getService('user').fetch(authUser.id, sanitizedQuery);
ctx.body = await sanitizeOutput(user, ctx);
},
};