Skip to content

Commit 27a1cad

Browse files
authored
Merge pull request #1508 from CVEProject/dr_1495_orgWithPartnerRole_middleware
Closes #1495 - Fixes onlyOrgwithPartner role, and an issue in the migrate / populate…
2 parents 115e7ee + 3223d97 commit 27a1cad

File tree

3 files changed

+48
-65
lines changed

3 files changed

+48
-65
lines changed

src/middleware/middleware.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ async function onlyOrgWithPartnerRole (req, res, next) {
310310
if (org === null) {
311311
logger.info({ uuid: req.ctx.uuid, message: shortName + ' does NOT exist ' })
312312
return res.status(404).json(error.orgDoesNotExist(shortName))
313-
} else if ((org.authority.length === 1 && org.authority[0] === 'BULK_DOWNLOAD')) {
313+
} else if ((org.authority.length === 1 && org.authority[0] === 'BULK_DOWNLOAD') || (org.authority?.active_roles?.length === 1 && org.authority.active_roles[0] === 'BULK_DOWNLOAD')) {
314314
logger.info({ uuid: req.ctx.uuid, message: org.short_name + 'only has BULK_DOWNLOAD role ' })
315315
return res.status(403).json(error.orgHasNoPartnerRole(shortName))
316-
} else if (org.authority.length > 0) {
316+
} else if (org.authority.length > 0 || org.authority?.active_roles.length > 0) {
317317
logger.info({ uuid: req.ctx.uuid, message: org.short_name + ' has a role ' })
318318
next()
319319
} else {

src/scripts/populate.js

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -80,79 +80,62 @@ db.once('open', async () => {
8080

8181
for (const name in populateTheseCollections) {
8282
if (names.includes(name)) {
83-
logger.info(`Dropping ${name} collection indexes!!!`)
84-
await db.collections[name].dropIndexes()
85-
logger.info(`Dropping ${name} collection !!!`)
86-
await db.dropCollection(name)
83+
if (db.collections[name]) {
84+
logger.info(`Dropping ${name} collection indexes!!!`)
85+
await db.collections[name].dropIndexes()
86+
logger.info(`Dropping ${name} collection !!!`)
87+
await db.dropCollection(name)
88+
}
8789
}
8890
}
8991

90-
if (!names.includes('Cve-Id-Range') && !names.includes('Cve-Id') && !names.includes('Cve') && !names.includes('Org') && !names.includes('User') && !names.includes('BaseOrg') && !names.includes('BaseUser')) {
91-
// Org
92-
await dataUtils.populateCollection(
93-
'./datadump/pre-population/orgs.json',
94-
Org, dataUtils.newOrgTransform
95-
)
96-
97-
// await dataUtils.populateCollection(
98-
// './datadump/pre-population/registry-orgs.json',
99-
// RegistryOrg
100-
// )
101-
102-
// User, depends on Org
103-
const hash = await dataUtils.preprocessUserSecrets()
104-
await dataUtils.populateCollection(
105-
'./datadump/pre-population/users.json',
106-
User, dataUtils.newUserTransform, hash
107-
)
108-
109-
// const registryUserHash = await dataUtils.preprocessUserSecrets()
110-
// await dataUtils.populateCollection(
111-
// './datadump/pre-population/registry-users.json',
112-
// RegistryUser, dataUtils.newRegistryUserTransform, registryUserHash
113-
// )
114-
115-
const populatePromises = []
116-
117-
// CVE ID Range
92+
// Org
93+
await dataUtils.populateCollection(
94+
'./datadump/pre-population/orgs.json',
95+
Org, dataUtils.newOrgTransform
96+
)
97+
98+
// User, depends on Org
99+
const hash = await dataUtils.preprocessUserSecrets()
100+
await dataUtils.populateCollection(
101+
'./datadump/pre-population/users.json',
102+
User, dataUtils.newUserTransform, hash
103+
)
104+
105+
const populatePromises = []
106+
107+
// CVE ID Range
108+
populatePromises.push(dataUtils.populateCollection(
109+
'./datadump/pre-population/cve-ids-range.json',
110+
CveIdRange
111+
))
112+
113+
// CVE
114+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
118115
populatePromises.push(dataUtils.populateCollection(
119-
'./datadump/pre-population/cve-ids-range.json',
120-
CveIdRange
116+
'./datadump/pre-population/cves.json',
117+
Cve, dataUtils.newCveTransform
121118
))
119+
}
122120

123-
// CVE
124-
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
125-
populatePromises.push(dataUtils.populateCollection(
126-
'./datadump/pre-population/cves.json',
127-
Cve, dataUtils.newCveTransform
128-
))
129-
}
121+
// CVE ID, depends on User and Org
122+
populatePromises.push(dataUtils.populateCollection(
123+
'./datadump/pre-population/cve-ids.json',
124+
CveId, dataUtils.newCveIdTransform
125+
))
130126

131-
// CVE ID, depends on User and Org
132-
populatePromises.push(dataUtils.populateCollection(
133-
'./datadump/pre-population/cve-ids.json',
134-
CveId, dataUtils.newCveIdTransform
135-
))
127+
// don't close database connection until all remaining populate
128+
// promises are resolved
129+
Promise.all(populatePromises).then(function () {
130+
logger.info('Successfully populated the database!')
136131

137-
// don't close database connection until all remaining populate
138-
// promises are resolved
139-
Promise.all(populatePromises).then(function () {
140-
logger.info('Successfully populated the database!')
141-
142-
Object.keys(indexesToCreate).forEach(col => {
143-
indexesToCreate[col].forEach(index => {
144-
db.collections[col].createIndex(index)
145-
})
132+
Object.keys(indexesToCreate).forEach(col => {
133+
indexesToCreate[col].forEach(index => {
134+
db.collections[col].createIndex(index)
146135
})
147-
mongoose.connection.close()
148136
})
149-
} else {
150-
logger.error(
151-
'The database was not populated because ' +
152-
'some of the collections were not deleted.'
153-
)
154137
mongoose.connection.close()
155-
}
138+
})
156139
} else {
157140
mongoose.connection.close()
158141
}

test/unit-tests/middleware/onlyOrgWithPartnerRoleTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const stubSecretariat = {
6565
}
6666
}
6767

68-
describe.skip('Testing onlyOrgWithPartnerRole middleware', () => {
68+
describe('Testing onlyOrgWithPartnerRole middleware', () => {
6969
let status, json, res, next, getOrgRepository, baseUserRepo, baseOrgRepo, getBaseOrgRepository, getBaseUserRepository, orgRepo
7070
beforeEach(() => {
7171
status = sinon.stub()

0 commit comments

Comments
 (0)