forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
245 lines (211 loc) · 7.26 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const express = require('express');
const { restrict } = require('../lib/auth');
const { getId, clearSessionValue } = require('../lib/common');
const colors = require('colors');
const bcrypt = require('bcryptjs');
const { validateJson } = require('../lib/schema');
const router = express.Router();
router.get('/admin/users', restrict, async (req, res) => {
const db = req.app.db;
const users = await db.users.find({}, { projection: { userPassword: 0 } }).toArray();
if(req.apiAuthenticated){
res.status(200).json(users);
return;
}
res.render('users', {
title: 'Users',
users: users,
admin: true,
config: req.app.config,
isAdmin: req.session.isAdmin,
helpers: req.handlebars.helpers,
session: req.session,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType')
});
});
// edit user
router.get('/admin/user/edit/:id', restrict, async (req, res) => {
const db = req.app.db;
const user = await db.users.findOne({ _id: getId(req.params.id) });
// Check user is found
if(!user){
if(req.apiAuthenticated){
res.status(400).json({ message: 'User not found' });
return;
}
req.session.message = 'User not found';
req.session.messageType = 'danger';
res.redirect('/admin/users');
return;
}
// if the user we want to edit is not the current logged in user and the current user is not
// an admin we render an access denied message
if(user.userEmail !== req.session.user && req.session.isAdmin === false){
if(req.apiAuthenticated){
res.status(400).json({ message: 'Access denied' });
return;
}
req.session.message = 'Access denied';
req.session.messageType = 'danger';
res.redirect('/admin/users');
return;
}
res.render('user-edit', {
title: 'User edit',
user: user,
admin: true,
session: req.session,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers,
config: req.app.config
});
});
// users new
router.get('/admin/user/new', restrict, (req, res) => {
res.render('user-new', {
title: 'User - New',
admin: true,
session: req.session,
helpers: req.handlebars.helpers,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
config: req.app.config
});
});
// delete a user
router.post('/admin/user/delete', restrict, async (req, res) => {
const db = req.app.db;
// userId
if(req.session.isAdmin !== true){
res.status(400).json({ message: 'Access denied' });
return;
}
// Cannot delete your own account
if(req.session.userId === req.body.userId){
res.status(400).json({ message: 'Unable to delete own user account' });
return;
}
const user = await db.users.findOne({ _id: getId(req.body.userId) });
// If user is not found
if(!user){
res.status(400).json({ message: 'User not found.' });
return;
}
// Cannot delete the original user/owner
if(user.isOwner){
res.status(400).json({ message: 'Access denied.' });
return;
}
try{
await db.users.deleteOne({ _id: getId(req.body.userId) }, {});
res.status(200).json({ message: 'User deleted.' });
return;
}catch(ex){
console.log('Failed to delete user', ex);
res.status(200).json({ message: 'Cannot delete user' });
return;
};
});
// update a user
router.post('/admin/user/update', restrict, async (req, res) => {
const db = req.app.db;
let isAdmin = req.body.userAdmin === 'on';
// get the user we want to update
const user = await db.users.findOne({ _id: getId(req.body.userId) });
// If user not found
if(!user){
res.status(400).json({ message: 'User not found' });
return;
}
// If the current user changing own account ensure isAdmin retains existing
if(user.userEmail === req.session.user){
isAdmin = user.isAdmin;
}
// if the user we want to edit is not the current logged in user and the current user is not
// an admin we render an access denied message
if(user.userEmail !== req.session.user && req.session.isAdmin === false){
res.status(400).json({ message: 'Access denied' });
return;
}
// create the update doc
const updateDoc = {};
updateDoc.isAdmin = isAdmin;
if(req.body.usersName){
updateDoc.usersName = req.body.usersName;
}
if(req.body.userEmail){
updateDoc.userEmail = req.body.userEmail;
}
if(req.body.userPassword){
updateDoc.userPassword = bcrypt.hashSync(req.body.userPassword);
}
// Validate update user
const schemaResult = validateJson('editUser', updateDoc);
if(!schemaResult.result){
res.status(400).json({
message: 'Failed to create user. Check inputs.',
error: schemaResult.errors
});
return;
}
try{
const updatedUser = await db.users.findOneAndUpdate(
{ _id: getId(req.body.userId) },
{
$set: updateDoc
}, { multi: false, returnOriginal: false }
);
const returnUser = updatedUser.value;
delete returnUser.userPassword;
delete returnUser.apiKey;
res.status(200).json({ message: 'User account updated', user: updatedUser.value });
return;
}catch(ex){
console.error(colors.red(`Failed updating user: ${ex}`));
res.status(400).json({ message: 'Failed to update user' });
}
});
// insert a user
router.post('/admin/user/insert', restrict, async (req, res) => {
const db = req.app.db;
// Check number of users
const userCount = await db.users.countDocuments({});
let isAdmin = false;
// if no users, setup user as admin
if(userCount === 0){
isAdmin = true;
}
const userObj = {
usersName: req.body.usersName,
userEmail: req.body.userEmail,
userPassword: bcrypt.hashSync(req.body.userPassword, 10),
isAdmin: isAdmin
};
// Validate new user
const schemaResult = validateJson('newUser', userObj);
if(!schemaResult.result){
res.status(400).json({ message: 'Failed to create user. Check inputs.', error: schemaResult.errors });
return;
}
// check for existing user
const user = await db.users.findOne({ userEmail: req.body.userEmail });
if(user){
console.error(colors.red('Failed to insert user, possibly already exists'));
res.status(400).json({ message: 'A user with that email address already exists' });
return;
}
// email is ok to be used.
try{
const newUser = await db.users.insertOne(userObj);
res.status(200).json({
message: 'User account inserted',
userId: newUser.insertedId
});
}catch(ex){
console.error(colors.red(`Failed to insert user: ${ex}`));
res.status(400).json({ message: 'New user creation failed' });
}
});
module.exports = router;