-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4715 from cloud-gov/feat-add-api-endoints-for-fil…
…e-storage-2082 Feat add api endoints for file storage 2082
- Loading branch information
Showing
55 changed files
with
5,028 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const EventCreator = require('../../services/EventCreator'); | ||
const { canAdminCreateSiteFileStorage } = require('../../authorizers/file-storage'); | ||
const { serializeFileStorageService } = require('../../serializers/file-storage'); | ||
const { wrapHandlers } = require('../../utils'); | ||
const { Event } = require('../../models'); | ||
const { adminCreateSiteFileStorage } = require('../../services/file-storage'); | ||
|
||
module.exports = wrapHandlers({ | ||
async createSiteFileStorage(req, res) { | ||
const { params, user } = req; | ||
|
||
const siteId = parseInt(params.id, 10); | ||
const { site } = await canAdminCreateSiteFileStorage(siteId); | ||
|
||
const fss = await adminCreateSiteFileStorage(site); | ||
|
||
EventCreator.audit( | ||
Event.labels.ADMIN, | ||
user, | ||
'Site File Storage Service Created', | ||
fss, | ||
); | ||
|
||
const data = serializeFileStorageService(fss); | ||
return res.send(data); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const siteErrors = require('../responses/siteErrors'); | ||
const { FileStorageService, Site } = require('../models'); | ||
const { isSiteOrgManager, isOrgManager, isOrgUser } = require('./utils'); | ||
|
||
const canCreateSiteStorage = async (userId, siteId) => { | ||
const { site, organization } = await isSiteOrgManager(userId, siteId); | ||
|
||
const fss = await FileStorageService.findOne({ where: { siteId } }); | ||
|
||
if (fss) { | ||
throw { | ||
status: 403, | ||
message: siteErrors.SITE_FILE_STORAGE_EXISTS, | ||
}; | ||
} | ||
|
||
return { site, organization }; | ||
}; | ||
|
||
const canAdminCreateSiteFileStorage = async (siteId) => { | ||
const site = await Site.findByPk(siteId); | ||
|
||
if (!site) { | ||
throw { | ||
status: 403, | ||
message: siteErrors.SITE_DOES_NOT_EXIST, | ||
}; | ||
} | ||
|
||
const fss = await FileStorageService.findOne({ where: { siteId } }); | ||
|
||
if (fss) { | ||
throw { | ||
status: 403, | ||
message: siteErrors.SITE_FILE_STORAGE_EXISTS, | ||
}; | ||
} | ||
|
||
return { site }; | ||
}; | ||
|
||
async function hasFileStorage(fssId) { | ||
const fileStorageService = await FileStorageService.findByPk(fssId); | ||
|
||
if (!fileStorageService) { | ||
throw { | ||
status: 404, | ||
message: siteErrors.NOT_FOUND, | ||
}; | ||
} | ||
|
||
return { fileStorageService }; | ||
} | ||
|
||
async function isFileStorageManager(userId, fssId) { | ||
const { fileStorageService } = await hasFileStorage(fssId); | ||
|
||
const { organization } = await isOrgManager(userId, fileStorageService.organizationId); | ||
|
||
return { organization, fileStorageService }; | ||
} | ||
|
||
async function isFileStorageUser(userId, fssId) { | ||
const { fileStorageService } = await hasFileStorage(fssId); | ||
|
||
const { organization } = await isOrgUser(userId, fileStorageService.organizationId); | ||
|
||
return { fileStorageService, organization }; | ||
} | ||
|
||
module.exports = { | ||
canAdminCreateSiteFileStorage, | ||
canCreateSiteStorage, | ||
isFileStorageManager, | ||
isFileStorageUser, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const siteErrors = require('../responses/siteErrors'); | ||
const { Organization, Site } = require('../models'); | ||
const { fetchModelById } = require('../utils/queryDatabase'); | ||
|
||
const authorize = async (userId, siteId) => { | ||
const site = await fetchModelById( | ||
siteId, | ||
Site.forUser({ | ||
id: userId, | ||
}), | ||
); | ||
|
||
if (!site) { | ||
throw { | ||
status: 404, | ||
message: siteErrors.NOT_FOUND, | ||
}; | ||
} | ||
|
||
if (!site.isActive) { | ||
// if site is not active | ||
throw { | ||
status: 403, | ||
message: siteErrors.ORGANIZATION_INACTIVE, | ||
}; | ||
} | ||
|
||
if (site.organizationId) { | ||
// if site exists in an org | ||
const org = await site.getOrganization(); | ||
if (!org.isActive) { | ||
throw { | ||
status: 403, | ||
message: siteErrors.ORGANIZATION_INACTIVE, | ||
}; | ||
} | ||
} | ||
|
||
return site; | ||
}; | ||
|
||
const isSiteOrgManager = async (userId, siteId) => { | ||
const site = await authorize(userId, siteId); | ||
const organization = await fetchModelById( | ||
site.organizationId, | ||
Organization.forManagerRole({ id: userId }), | ||
); | ||
|
||
if (!organization) { | ||
throw { | ||
status: 403, | ||
message: siteErrors.ORGANIZATION_MANAGER_ACCESS, | ||
}; | ||
} | ||
|
||
return { site, organization }; | ||
}; | ||
|
||
const isOrgManager = async (userId, orgId) => { | ||
const organization = await fetchModelById( | ||
orgId, | ||
Organization.forManagerRole({ id: userId }), | ||
); | ||
|
||
if (!organization) { | ||
throw { | ||
status: 403, | ||
message: siteErrors.ORGANIZATION_MANAGER_ACCESS, | ||
}; | ||
} | ||
|
||
return { organization }; | ||
}; | ||
|
||
const isOrgUser = async (userId, orgId) => { | ||
const organization = await fetchModelById(orgId, Organization.forUser({ id: userId })); | ||
|
||
if (!organization) { | ||
throw { | ||
status: 403, | ||
message: siteErrors.ORGANIZATION_USER_ACCESS, | ||
}; | ||
} | ||
|
||
return { organization }; | ||
}; | ||
|
||
const isSiteUser = authorize; | ||
|
||
module.exports = { authorize, isOrgManager, isOrgUser, isSiteOrgManager, isSiteUser }; |
Oops, something went wrong.