forked from deic-dk-retired/ddosapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
300 lines (287 loc) · 7.75 KB
/
users.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// connect to db and then get the
// query handler method
var db = require('./db')
const chalk = require('chalk')
// uses json api conventions
function authenticate (req, res, next) {
var sqlUserAccess = db.miniQuery('.sql/users/userAccess.sql')
// console.log(chalk.hex('#039BE5')(sqlUserAccess))
db.foddb.any(sqlUserAccess, {username: req.params.username, password: req.params.password})
.then(function (data) {
console.log(chalk.hex('#039BE5')(data))
res.status(200)
.json({
type: 'users',
id: parseInt(data.id),
attributes: data
})
})
.catch(function (err) {
return next(err.message)
})
}
function auth (req, res, next) {
var sqlUserAccess = db.miniQuery('.sql/users/userAccess.sql')
db.foddb.any(sqlUserAccess, {username: req.params.username, password: req.params.password})
.then(function (data) {
res.status(200)
.json({
type: 'users',
id: data.id,
attributes: data
})
})
.catch(function (err) {
return next(err.message)
})
}
// uses json api conventions
function getAllUsers (req, res, next) {
var reldata = null
var sqlUserNetworks = db.miniQuery('.sql/customers/userNetworks.sql')
// funtion to return networks for each user
function userNetworks (usrid) {
return usrid
}
var sqlAllUsers = db.miniQuery('.sql/users/allUsers.sql')
db.foddb.any(sqlAllUsers)
.then(function (data) {
// create json api array
var jsonarr = []
var jsonobj
data.map(function (e) {
jsonobj = {
type: 'users',
id: parseInt(e.administratorid),
links: {
self: 'http://10.33.1.97:4242/api/users/' + e.administratorid
},
relationships: {
networks: {
links: {
self: 'http://10.33.1.97:4242/api/users/' + e.administratorid + '/relationships/networks',
related: 'http://10.33.1.97:4242/api/users/' + e.administratorid + '/networks'
},
data: userNetworks(e.administratorid)
}
}
}
// remove duplicate id property from attributes
delete e.administratorid
jsonobj.attributes = e
jsonarr.push(jsonobj)
})
// show jsonapi
res.status(200)
.json({
data: jsonarr,
meta: {
total: data.length
}
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
// uses json api conventions
function getOneUser (req, res, next) {
var reldata = null
// fetch user networks
var sqlUserNetworks = db.miniQuery('.sql/customers/userNetworks.sql')
db.foddb.any(sqlUserNetworks, {userid: req.params.userid})
.then(function (data) {
// create json api array
var prarr
if (data.length > 1) {
prarr = []
data.map(function (e) {
prarr.push({
type: 'networks',
id: parseInt(e.customernetworkid)
})
})
reldata = prarr
}
if (data.length === 1) {
reldata = {
type: 'networks',
id: parseInt(data[0].customernetworkid)
}
}
if (data.length === 0) {
// null and not []
reldata = null
}
return reldata
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
// fetch user data
var sqlOneUser = db.miniQuery('.sql/users/oneUser.sql')
db.foddb.one(sqlOneUser, {userid: req.params.userid})
.then(function (data) {
var jsonobj = {
type: 'users',
id: parseInt(data.administratorid),
links: {
self: 'http://10.33.1.97:4242/api/users/' + data.administratorid
},
relationships: {
networks: {
links: {
self: 'http://10.33.1.97:4242/api/users/' + data.administratorid + '/relationships/networks',
related: 'http://10.33.1.97:4242/api/users/' + data.administratorid + '/networks'
},
data: reldata
}
}
}
// remove duplicate id property from attributes
delete data.administratorid
jsonobj.attributes = data
// show jsonapi
res.status(200)
.json({
data: jsonobj
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
function getUserNetworks (req, res, next) {
var sqlUserNetworks = db.miniQuery('.sql/customers/userNetworks.sql')
db.foddb.any(sqlUserNetworks, {userid: req.params.userid})
.then(function (data) {
// create pr api array
var prarr
var probj
if (data.length > 1) {
prarr = []
data.map(function (e) {
probj = {
type: 'networks',
id: parseInt(e.customernetworkid)
}
// remove duplicate id property from attributes
delete e.customernetworkid
probj.attributes = e
prarr.push(probj)
})
}
if (data.length === 1) {
console.log(data)
probj = {
type: 'networks',
id: parseInt(data[0].customernetworkid)
}
// remove duplicate id property from attributes
delete data[0].customernetworkid
probj.attributes = data[0]
prarr = probj
}
if (data.length === 0) {
prarr = []
}
// show prapi
res.status(200)
.json({
links: {
self: 'http://10.33.1.97:4242/api/networks/' + req.params.userid
},
data: prarr
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
function updateUser (req, res, next) {
// fetch user details before updating
var sqlUpdateUser = db.miniQuery('.sql/users/updateUser.sql')
db.foddb.any(sqlUpdateUser,
{ customerid: parseInt(req.body.customerid),
kind: req.body.kind,
name: req.body.name,
phone: req.body.phone,
email: req.body.email,
username: req.body.username,
password: req.body.password,
userid: parseInt(req.params.userid)
})
.then(function () {
res.status(200)
.json({
meta: {
status: 'success',
message: 'User ' + req.body.username + ' modified'
}
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
function createUser (req, res, next) {
var sqlCreateUser = db.miniQuery('.sql/users/createUser.sql')
db.foddb.none(sqlCreateUser,
{ customerid: parseInt(req.body.customerid),
kind: req.body.kind,
name: req.body.name,
phone: req.body.phone,
email: req.body.email,
username: req.body.username,
password: req.body.password
})
.then(function () {
res.status(200)
.json({
meta: {
status: 'successfully created user',
message: {
user: req.body.name,
username: req.body.username,
access: req.body.kind
}
}
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
function removeUser (req, res, next) {
var sqlDeleteUser = db.miniQuery('.sql/users/deleteUser.sql')
db.foddb.result(sqlDeleteUser, {username: req.params.username})
.then(function (result) {
res.status(200)
.json({
meta: {
status: 'success',
message: 'Removed user: ' + req.params.username
}
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
module.exports = {
authenticate: authenticate,
auth: auth,
getAllUsers: getAllUsers,
getOneUser: getOneUser,
getUserNetworks: getUserNetworks,
createUser: createUser,
updateUser: updateUser,
removeUser: removeUser
}