-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathutils.js
151 lines (130 loc) · 3.97 KB
/
utils.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
const Org = require('../model/org')
const User = require('../model/user')
const getConstants = require('../constants').getConstants
const validateDate = require('validate-date')
async function getOrgUUID (shortName) {
const org = await Org.findOne().byShortName(shortName)
let result = null
if (org) {
result = org.UUID
}
return result
}
async function getUserUUID (userName, orgUUID) {
const user = await User.findOne().byUserNameAndOrgUUID(userName, orgUUID)
let result = null
if (user) {
result = user.UUID
}
return result
}
async function isSecretariat (shortName) {
let result = false
const CONSTANTS = getConstants()
const orgUUID = await getOrgUUID(shortName) // may be null if org does not exists
const secretariats = await Org.find({ 'authority.active_roles': { $in: [CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT] } })
if (orgUUID) {
secretariats.forEach((obj) => {
if (obj.UUID === orgUUID) {
result = true // org is secretariat
}
})
}
return result // org is not secretariat
}
async function isSecretariatUUID (orgUUID) {
let result = false
const CONSTANTS = getConstants()
const secretariats = await Org.find({ 'authority.active_roles': { $in: [CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT] } })
if (orgUUID) {
secretariats.forEach((obj) => {
if (obj.UUID === orgUUID) {
result = true // org is secretariat
}
})
}
return result // org is not secretariat
}
async function isAdmin (requesterUsername, requesterShortName) {
let result = false
const CONSTANTS = getConstants()
const requesterOrgUUID = await getOrgUUID(requesterShortName) // may be null if org does not exists
if (requesterOrgUUID) {
const user = await User.findOne().byUserNameAndOrgUUID(requesterUsername, requesterOrgUUID)
if (user) {
result = user.authority.active_roles.includes(CONSTANTS.USER_ROLE_ENUM.ADMIN)
}
}
return result // org is not secretariat
}
async function isAdminUUID (requesterUsername, requesterOrgUUID) {
let result = false
const CONSTANTS = getConstants()
if (requesterOrgUUID) {
const user = await User.findOne().byUserNameAndOrgUUID(requesterUsername, requesterOrgUUID)
if (user) {
result = user.authority.active_roles.includes(CONSTANTS.USER_ROLE_ENUM.ADMIN)
}
}
return result // org is not secretariat
}
function reqCtxMapping (req, keyType, keys) {
if (!(keyType in req.ctx)) {
req.ctx[keyType] = {}
}
// request body gets mapped to request context
// while query parameters or headers are mapped individually
if (keyType === 'body') {
if (req[keyType]) {
req.ctx[keyType] = req[keyType]
}
} else {
keys.forEach(k => {
if (k in req[keyType]) {
req.ctx[keyType][k] = req[keyType][k]
}
})
}
}
// Return true if boolean is 0, true, or yes, with any mix of casing
function booleanIsTrue (val) {
if ((val.toString() === '1') ||
(val.toString().toLowerCase() === 'true') ||
(val.toString().toLowerCase() === 'yes')) {
return true
} else { return false }
}
// Sanitizer for dates
function toDate (val) {
val = val.toUpperCase()
let value = val.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(|Z|((-|\+|\s)\d{2}:\d{2}))$/)
let result = null
if (value) {
value[0] = value[0].replace(' ', '+') // Re-add literal '+' which was stripped
const dateStr = value[0]
// Make sure that the string passed is a valid date
// eslint doesn't like that responseType is not defined, but it is needed as is
/* eslint-disable-next-line */
const valid = validateDate(dateStr.toString().substring(0, 10), responseType = 'boolean')
if (valid) {
result = new Date(dateStr)
}
} else {
value = val.match(/^\d{4}-\d{2}-\d{2}$/)
if (value) {
result = new Date(`${value[0]}T00:00:00.000+00:00`)
}
}
return result
}
module.exports = {
isSecretariat,
isAdmin,
isAdminUUID,
isSecretariatUUID,
getOrgUUID,
getUserUUID,
reqCtxMapping,
booleanIsTrue,
toDate
}