@websy/websy-designs 1.9.14 → 1.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/helpers/v1/authHelper.js +11 -11
- package/dist/server/helpers/v1/pgHelper.js +78 -7
- package/dist/server/routes/v1/api.js +26 -10
- package/dist/websy-designs-es6.debug.js +270 -110
- package/dist/websy-designs-es6.js +562 -368
- package/dist/websy-designs-es6.min.js +1 -1
- package/dist/websy-designs.debug.js +280 -115
- package/dist/websy-designs.js +568 -373
- package/dist/websy-designs.min.js +1 -1
- package/package.json +1 -1
|
@@ -8,7 +8,7 @@ class AuthHelper {
|
|
|
8
8
|
loginType: 'email'
|
|
9
9
|
}
|
|
10
10
|
this.options = Object.assign({}, DEFAULTS, options)
|
|
11
|
-
console.log(this.options)
|
|
11
|
+
// console.log(this.options)
|
|
12
12
|
}
|
|
13
13
|
login (req, res) {
|
|
14
14
|
return new Promise((resolve, reject) => {
|
|
@@ -16,25 +16,25 @@ class AuthHelper {
|
|
|
16
16
|
return (md5(md5(password) + user.salt)) === user.pepper
|
|
17
17
|
}
|
|
18
18
|
// check in mongo if a user with username exists or not
|
|
19
|
-
console.log(req.body)
|
|
19
|
+
// console.log(req.body)
|
|
20
20
|
const email = req.body[this.options.loginType].toLowerCase()
|
|
21
21
|
const userQuery = `
|
|
22
22
|
SELECT *
|
|
23
23
|
FROM users
|
|
24
24
|
WHERE ${this.options.loginType} = '${email}'
|
|
25
25
|
`
|
|
26
|
-
console.log(userQuery)
|
|
26
|
+
// console.log(userQuery)
|
|
27
27
|
this.dbHelper.execute(userQuery).then(result => {
|
|
28
28
|
// Username does not exist, log the error and redirect back
|
|
29
29
|
if (result.rows.length === 0) {
|
|
30
|
-
console.log('No User')
|
|
30
|
+
// console.log('No User')
|
|
31
31
|
reject(`User not found with ${this.options.loginType} ${email}`)
|
|
32
32
|
return
|
|
33
33
|
}
|
|
34
34
|
const user = result.rows[0]
|
|
35
35
|
// User exists but wrong password, log the error
|
|
36
36
|
if (!isValidPassword(user, req.body.password)) {
|
|
37
|
-
console.log('Invalid Password')
|
|
37
|
+
// console.log('Invalid Password')
|
|
38
38
|
reject('Invalid Password')
|
|
39
39
|
return
|
|
40
40
|
}
|
|
@@ -46,10 +46,10 @@ class AuthHelper {
|
|
|
46
46
|
WHERE id = ${user.id}
|
|
47
47
|
`
|
|
48
48
|
this.dbHelper.execute(userUpdateQuery).then(result => {
|
|
49
|
-
console.log('yes')
|
|
49
|
+
// console.log('yes')
|
|
50
50
|
resolve(user)
|
|
51
51
|
}, err => {
|
|
52
|
-
console.log('no')
|
|
52
|
+
// console.log('no')
|
|
53
53
|
reject(err)
|
|
54
54
|
})
|
|
55
55
|
// LogonHistory.create({
|
|
@@ -121,18 +121,18 @@ class AuthHelper {
|
|
|
121
121
|
})
|
|
122
122
|
}
|
|
123
123
|
isLoggedIn (req, res, next) {
|
|
124
|
-
console.log(req.session)
|
|
124
|
+
// console.log(req.session)
|
|
125
125
|
if (req.session && req.session.user && req.session.user.isAnonymous !== true) {
|
|
126
|
-
console.log('in condition D')
|
|
126
|
+
// console.log('in condition D')
|
|
127
127
|
next()
|
|
128
128
|
}
|
|
129
129
|
else {
|
|
130
|
-
console.log('in condition E')
|
|
130
|
+
// console.log('in condition E')
|
|
131
131
|
res.redirect('/login')
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
checkPermissions (req, res, next) {
|
|
135
|
-
console.log('hello')
|
|
135
|
+
// console.log('hello')
|
|
136
136
|
if (process.env.USE_PERMISSIONS === 'true' || process.env.USE_PERMISSIONS === true) {
|
|
137
137
|
const permissions = {
|
|
138
138
|
entities: {
|
|
@@ -117,11 +117,27 @@ class PGHelper {
|
|
|
117
117
|
if (process.env.EDIT_DATE_FIELD) {
|
|
118
118
|
data[process.env.EDIT_DATE_FIELD] = (new Date()).toISOString()
|
|
119
119
|
}
|
|
120
|
-
|
|
120
|
+
let sql = `
|
|
121
121
|
INSERT INTO ${entity} (${Object.keys(data).join(',')})
|
|
122
|
-
VALUES (
|
|
122
|
+
VALUES (
|
|
123
|
+
`
|
|
124
|
+
sql += Object.values(data).map(d => {
|
|
125
|
+
if (d === null) {
|
|
126
|
+
return d
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
if (typeof d === 'string') {
|
|
130
|
+
d = d.replace(/'/g, `''`)
|
|
131
|
+
}
|
|
132
|
+
return `'${d}'`
|
|
133
|
+
// (d === null ? `${d}` : `'${d.replace(/'/)}'`)
|
|
134
|
+
}
|
|
135
|
+
}).join(',')
|
|
136
|
+
sql += `
|
|
137
|
+
)
|
|
123
138
|
RETURNING ${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'}
|
|
124
139
|
`
|
|
140
|
+
return sql
|
|
125
141
|
}
|
|
126
142
|
buildOrderBy (query) {
|
|
127
143
|
return `
|
|
@@ -178,7 +194,7 @@ class PGHelper {
|
|
|
178
194
|
`
|
|
179
195
|
return sql
|
|
180
196
|
}
|
|
181
|
-
buildUpdate (entity, where, data) {
|
|
197
|
+
buildUpdate (entity, where, data, user) {
|
|
182
198
|
let updates = []
|
|
183
199
|
for (let key in data) {
|
|
184
200
|
if (this.updateIgnores.indexOf(key) === -1) {
|
|
@@ -186,7 +202,14 @@ class PGHelper {
|
|
|
186
202
|
data[key] = data[key].replace(/''/gm, `'`).replace(/'/gm, `''`).replace(/\\\\/gm, '\\')
|
|
187
203
|
}
|
|
188
204
|
updates.push(`${key} = ${(data[key] === null ? data[key] : `'${data[key]}'`)}`)
|
|
189
|
-
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (process.env.CREATE_USER_FIELD && process.env.USERID_FIELD && user) {
|
|
208
|
+
let userId = user[process.env.USERID_FIELD || 'id']
|
|
209
|
+
updates.push(`${process.env.EDIT_USER_FIELD} = '${userId}'`)
|
|
210
|
+
}
|
|
211
|
+
if (process.env.EDIT_DATE_FIELD) {
|
|
212
|
+
updates.push(`${process.env.EDIT_DATE_FIELD} = '${(new Date()).toISOString()}'`)
|
|
190
213
|
}
|
|
191
214
|
return `
|
|
192
215
|
UPDATE ${entity}
|
|
@@ -194,19 +217,67 @@ class PGHelper {
|
|
|
194
217
|
WHERE ${this.buildWhere(where)}
|
|
195
218
|
`
|
|
196
219
|
}
|
|
197
|
-
buildUpdateWithId (entity, id, data) {
|
|
220
|
+
buildUpdateWithId (entity, id, data, user) {
|
|
198
221
|
let updates = []
|
|
199
222
|
for (let key in data) {
|
|
200
223
|
if (this.updateIgnores.indexOf(key) === -1) {
|
|
201
224
|
updates.push(`${key} = ${(data[key] === null ? data[key] : `'${data[key]}'`)}`)
|
|
202
225
|
}
|
|
203
226
|
}
|
|
227
|
+
if (process.env.CREATE_USER_FIELD && process.env.USERID_FIELD && user) {
|
|
228
|
+
let userId = user[process.env.USERID_FIELD || 'id']
|
|
229
|
+
updates.push(`${process.env.EDIT_USER_FIELD} = '${userId}'`)
|
|
230
|
+
}
|
|
231
|
+
if (process.env.EDIT_DATE_FIELD) {
|
|
232
|
+
updates.push(`${process.env.EDIT_DATE_FIELD} = '${(new Date()).toISOString()}'`)
|
|
233
|
+
}
|
|
204
234
|
return `
|
|
205
235
|
UPDATE ${entity}
|
|
206
236
|
SET ${updates.join(',')}
|
|
207
237
|
WHERE ${this.buildWhereWithId(entity, id)}
|
|
208
238
|
`
|
|
209
239
|
}
|
|
240
|
+
buildUpsert (entity, data, user) {
|
|
241
|
+
// delete data[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
|
|
242
|
+
// data.create_user = user
|
|
243
|
+
let sql = ``
|
|
244
|
+
data.forEach(row => {
|
|
245
|
+
if (process.env.CREATE_USER_FIELD && process.env.USERID_FIELD && user) {
|
|
246
|
+
row[process.env.CREATE_USER_FIELD] = user[process.env.USERID_FIELD || 'id']
|
|
247
|
+
row[process.env.EDIT_USER_FIELD] = user[process.env.USERID_FIELD || 'id']
|
|
248
|
+
}
|
|
249
|
+
if (process.env.EDIT_DATE_FIELD) {
|
|
250
|
+
row[process.env.EDIT_DATE_FIELD] = (new Date()).toISOString()
|
|
251
|
+
}
|
|
252
|
+
if (typeof row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'] !== 'undefined') {
|
|
253
|
+
let id = row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
|
|
254
|
+
delete row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
|
|
255
|
+
let updates = []
|
|
256
|
+
for (let key in row) {
|
|
257
|
+
if (this.updateIgnores.indexOf(key) === -1) {
|
|
258
|
+
if (typeof row[key] === 'string') {
|
|
259
|
+
row[key] = row[key].replace(/''/gm, `'`).replace(/'/gm, `''`).replace(/\\\\/gm, '\\')
|
|
260
|
+
}
|
|
261
|
+
updates.push(`${key} = ${(row[key] === null ? row[key] : `'${row[key]}'`)}`)
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
sql += `
|
|
265
|
+
UPDATE ${entity}
|
|
266
|
+
SET ${updates.join(',')}
|
|
267
|
+
WHERE ${this.buildWhereWithId(entity, id)};
|
|
268
|
+
`
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
delete row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
|
|
272
|
+
sql += `
|
|
273
|
+
INSERT INTO ${entity} (${Object.keys(row).join(',')})
|
|
274
|
+
VALUES (${Object.values(row).map(d => (d === null ? `${d}` : `'${d}'`)).join(',')})
|
|
275
|
+
RETURNING ${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'};
|
|
276
|
+
`
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
return sql
|
|
280
|
+
}
|
|
210
281
|
buildWhere (input, entity) {
|
|
211
282
|
if (typeof input === 'undefined') {
|
|
212
283
|
return '1=1'
|
|
@@ -218,8 +289,8 @@ class PGHelper {
|
|
|
218
289
|
catch (error) {
|
|
219
290
|
// console.log(error)
|
|
220
291
|
}
|
|
221
|
-
console.log('where input', input)
|
|
222
|
-
console.log('splitter is', this.options.fieldValueSeparator)
|
|
292
|
+
// console.log('where input', input)
|
|
293
|
+
// console.log('splitter is', this.options.fieldValueSeparator)
|
|
223
294
|
let list = input.split(';').map(d => {
|
|
224
295
|
let parts = d.split(this.options.fieldValueSeparator)
|
|
225
296
|
if (parts.length === 2) {
|
|
@@ -43,29 +43,45 @@ function APIRoutes (dbHelper, authHelper) {
|
|
|
43
43
|
}
|
|
44
44
|
}, err => res.json(err))
|
|
45
45
|
})
|
|
46
|
+
router.post('/:entity/upsert', authHelper.checkPermissions, (req, res) => {
|
|
47
|
+
let user
|
|
48
|
+
if (req.session && req.session.user) {
|
|
49
|
+
user = req.session.user
|
|
50
|
+
}
|
|
51
|
+
const sql = dbHelper.buildUpsert(req.params.entity, req.body, user)
|
|
52
|
+
console.log('upsert sql', sql)
|
|
53
|
+
dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
|
|
54
|
+
})
|
|
46
55
|
router.post('/:entity', authHelper.checkPermissions, (req, res) => {
|
|
47
56
|
// const sql = dbHelper.buildInsert(req.params.entity, req.body, req.session.passport.user.id)
|
|
48
|
-
console.log(req.body)
|
|
49
|
-
console.log(req.session)
|
|
57
|
+
// console.log(req.body)
|
|
58
|
+
// console.log(req.session)
|
|
50
59
|
let user
|
|
51
60
|
if (req.session && req.session.user) {
|
|
52
61
|
user = req.session.user
|
|
53
62
|
}
|
|
54
63
|
const sql = dbHelper.buildInsert(req.params.entity, req.body, user)
|
|
55
|
-
console.log(sql)
|
|
64
|
+
// console.log(sql)
|
|
56
65
|
dbHelper.execute(sql).then(response => res.json(response), err => {
|
|
57
66
|
res.statusCode = 404
|
|
58
67
|
res.json({err})
|
|
59
68
|
})
|
|
60
|
-
})
|
|
61
|
-
console.log('defining put endpoint for /:entity/:id')
|
|
62
|
-
router.put('/:entity/:id', (req, res) => {
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
})
|
|
70
|
+
// console.log('defining put endpoint for /:entity/:id')
|
|
71
|
+
router.put('/:entity/:id', authHelper.checkPermissions, (req, res) => {
|
|
72
|
+
let user
|
|
73
|
+
if (req.session && req.session.user) {
|
|
74
|
+
user = req.session.user
|
|
75
|
+
}
|
|
76
|
+
const sql = dbHelper.buildUpdateWithId(req.params.entity, req.params.id, req.body, user)
|
|
65
77
|
dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
|
|
66
78
|
})
|
|
67
|
-
router.put('/:entity', (req, res) => {
|
|
68
|
-
|
|
79
|
+
router.put('/:entity', authHelper.checkPermissions, (req, res) => {
|
|
80
|
+
let user
|
|
81
|
+
if (req.session && req.session.user) {
|
|
82
|
+
user = req.session.user
|
|
83
|
+
}
|
|
84
|
+
const sql = dbHelper.buildUpdate(req.params.entity, req.query.where, req.body, user)
|
|
69
85
|
dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
|
|
70
86
|
})
|
|
71
87
|
return router
|