expressa 2.0.13 → 2.0.15
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/controllers/users.js +52 -52
- package/middleware/permissions.js +4 -10
- package/package.json +1 -1
- package/util.js +19 -0
package/controllers/users.js
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
const auth = require('../auth')
|
|
2
|
-
const util = require('../util')
|
|
3
|
-
const collectionsApi = require('./collections')
|
|
4
|
-
const permissions = require('../middleware/permissions')
|
|
5
|
-
|
|
6
|
-
exports.login = async (req, collection) => {
|
|
7
|
-
const password = req.body.password
|
|
8
|
-
|
|
9
|
-
if (typeof req.body.email !== 'string') {
|
|
10
|
-
throw new util.ApiError(400, 'email must be a string')
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
req.body.email = req.body.email.toLowerCase()
|
|
14
|
-
|
|
15
|
-
// check if user exists
|
|
16
|
-
const result = await req.db[collection].find({
|
|
17
|
-
email: req.body.email
|
|
18
|
-
})
|
|
19
|
-
if (result.length === 0) {
|
|
20
|
-
throw new util.ApiError(400, 'No ' + collection + ' found with this email.')
|
|
21
|
-
}
|
|
22
|
-
const user = result[0]
|
|
23
|
-
if (!auth.isValidPassword(password, user.password)) {
|
|
24
|
-
throw new util.ApiError(401, 'Incorrect password')
|
|
25
|
-
}
|
|
26
|
-
const jwt_options = req.settings.jwt_expires_in ? { expiresIn: req.settings.jwt_expires_in } : {}
|
|
27
|
-
const payload = auth.doLogin({
|
|
28
|
-
id: user._id,
|
|
29
|
-
collection,
|
|
30
|
-
timestamp: user.meta.password_last_updated_at,
|
|
31
|
-
jwt_secret: req.getSetting('jwt_secret'),
|
|
32
|
-
jwt_options
|
|
33
|
-
})
|
|
34
|
-
req.uid = user._id
|
|
35
|
-
req.ucollection = collection
|
|
36
|
-
req.user = user
|
|
37
|
-
await permissions.addRolePermissionsAsync(req)
|
|
38
|
-
payload.canUseAdmin = req.hasPermission('login to admin')
|
|
39
|
-
return payload
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
exports.register = function (req, collection) {
|
|
43
|
-
req.url = `/${collection}`
|
|
44
|
-
req.params.collection = collection
|
|
45
|
-
return collectionsApi.insert(req)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
exports.getMe = function (req, collection) {
|
|
49
|
-
req.params.collection = collection
|
|
50
|
-
req.params.id = req.uid
|
|
51
|
-
return collectionsApi.getById(req)
|
|
52
|
-
}
|
|
1
|
+
const auth = require('../auth')
|
|
2
|
+
const util = require('../util')
|
|
3
|
+
const collectionsApi = require('./collections')
|
|
4
|
+
const permissions = require('../middleware/permissions')
|
|
5
|
+
|
|
6
|
+
exports.login = async (req, collection) => {
|
|
7
|
+
const password = req.body.password
|
|
8
|
+
|
|
9
|
+
if (typeof req.body.email !== 'string') {
|
|
10
|
+
throw new util.ApiError(400, 'email must be a string')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
req.body.email = req.body.email.toLowerCase()
|
|
14
|
+
|
|
15
|
+
// check if user exists
|
|
16
|
+
const result = await req.db[collection].find({
|
|
17
|
+
email: req.body.email
|
|
18
|
+
})
|
|
19
|
+
if (result.length === 0) {
|
|
20
|
+
throw new util.ApiError(400, 'No ' + collection + ' found with this email.')
|
|
21
|
+
}
|
|
22
|
+
const user = result[0]
|
|
23
|
+
if (!auth.isValidPassword(password, user.password)) {
|
|
24
|
+
throw new util.ApiError(401, 'Incorrect password')
|
|
25
|
+
}
|
|
26
|
+
const jwt_options = req.settings.jwt_expires_in ? { expiresIn: req.settings.jwt_expires_in } : {}
|
|
27
|
+
const payload = auth.doLogin({
|
|
28
|
+
id: user._id,
|
|
29
|
+
collection,
|
|
30
|
+
timestamp: user.meta.password_last_updated_at,
|
|
31
|
+
jwt_secret: req.getSetting('jwt_secret'),
|
|
32
|
+
jwt_options
|
|
33
|
+
})
|
|
34
|
+
req.uid = user._id
|
|
35
|
+
req.ucollection = collection
|
|
36
|
+
req.user = user
|
|
37
|
+
await permissions.addRolePermissionsAsync(req)
|
|
38
|
+
payload.canUseAdmin = req.hasPermission('login to admin')
|
|
39
|
+
return payload
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exports.register = function (req, collection) {
|
|
43
|
+
req.url = `/${collection}`
|
|
44
|
+
req.params.collection = collection
|
|
45
|
+
return collectionsApi.insert(req)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
exports.getMe = function (req, collection) {
|
|
49
|
+
req.params.collection = collection
|
|
50
|
+
req.params.id = req.uid
|
|
51
|
+
return collectionsApi.getById(req)
|
|
52
|
+
}
|
|
@@ -2,15 +2,9 @@ const util = require('../util')
|
|
|
2
2
|
let authenticatedRoleExists
|
|
3
3
|
|
|
4
4
|
async function addRolePermissions (req, roles) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
for (const permission in roleDoc.permissions) {
|
|
9
|
-
if (roleDoc.permissions[permission]) {
|
|
10
|
-
req.permissions[permission] = true
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
})
|
|
5
|
+
const rolePermissions = await util.getEffectivePermissionsForRoles(roles, req.db)
|
|
6
|
+
req.permissions ??= {}
|
|
7
|
+
req.permissions = { ...req.permissions, ...rolePermissions }
|
|
14
8
|
}
|
|
15
9
|
|
|
16
10
|
async function doesAuthenticatedRoleExist(req) {
|
|
@@ -31,7 +25,7 @@ module.exports.addRolePermissionsAsync = async function addRolePermissionsMiddle
|
|
|
31
25
|
req.hasPermission = () => true
|
|
32
26
|
return
|
|
33
27
|
}
|
|
34
|
-
req.hasPermission = (permission) => req.permissions
|
|
28
|
+
req.hasPermission = (permission) => util.hasPermission(req.permissions, permission)
|
|
35
29
|
let roles = ['Anonymous']
|
|
36
30
|
const isAuthenticatedRole = await doesAuthenticatedRoleExist(req)
|
|
37
31
|
if (req.uid) {
|
package/package.json
CHANGED
package/util.js
CHANGED
|
@@ -75,6 +75,8 @@ exports.getSchemaProperty = function(collection, path) {
|
|
|
75
75
|
for (const part of parts) {
|
|
76
76
|
if (obj.properties && obj.properties[part]) {
|
|
77
77
|
obj = obj.properties[part]
|
|
78
|
+
} else if (obj.items && obj.items.properties && obj.items.properties[part]) {
|
|
79
|
+
obj = obj.items.properties[part]
|
|
78
80
|
} else {
|
|
79
81
|
return
|
|
80
82
|
}
|
|
@@ -497,6 +499,23 @@ exports.getDatabaseOffset = function getDatabaseOffset(page, itemsPerPage) {
|
|
|
497
499
|
return (page - 1) * itemsPerPage
|
|
498
500
|
}
|
|
499
501
|
|
|
502
|
+
exports.hasPermission = function hasPermission(permissions, permission) {
|
|
503
|
+
return permissions && permissions[permission]
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
exports.getEffectivePermissionsForRoles = async function getEffectivePermissionsForRoles(roles, db) {
|
|
507
|
+
const permissions = {}
|
|
508
|
+
const roleDocs = await Promise.all(roles.map((name) => db.role.get(name)))
|
|
509
|
+
roleDocs.forEach((roleDoc) => {
|
|
510
|
+
for (const permission in roleDoc.permissions) {
|
|
511
|
+
if (roleDoc.permissions[permission]) {
|
|
512
|
+
permissions[permission] = true
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
})
|
|
516
|
+
return permissions
|
|
517
|
+
}
|
|
518
|
+
|
|
500
519
|
exports.createHash = auth.createHash
|
|
501
520
|
exports.isHashed = auth.isHashed
|
|
502
521
|
exports.doLogin = auth.doLogin
|