Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored controllers to require <tag>Service #1106

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions api/source/controllers/Asset.js

Large diffs are not rendered by default.

120 changes: 60 additions & 60 deletions api/source/controllers/Collection.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions api/source/controllers/Metrics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const config = require('../utils/config')
const MetricsSvc = require(`../service/${config.database.type}/MetricsService`)
const MetricsService = require(`../service/${config.database.type}/MetricsService`)
const Collection = require('./Collection')
const Security = require('../utils/accessLevels')
const SmError = require('../utils/error')
Expand All @@ -17,7 +17,7 @@ async function getCollectionMetrics (req, res, next, {style, aggregation, firstR
assetIds: req.query.assetId,
benchmarkIds: req.query.benchmarkId,
}
const rows = await MetricsSvc.queryMetrics({
const rows = await MetricsService.queryMetrics({
inPredicates,
userId: req.userObject.userId,
style,
Expand Down
8 changes: 4 additions & 4 deletions api/source/controllers/Operation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const writer = require('../utils/writer.js')
const config = require('../utils/config')
const Operation = require(`../service/${config.database.type}/OperationService`)
const OperationService = require(`../service/${config.database.type}/OperationService`)
const Asset = require(`./Asset`)
const Collection = require(`./Collection`)
const User = require(`./User`)
Expand All @@ -11,7 +11,7 @@ const SmError = require('../utils/error.js')

module.exports.getConfiguration = async function getConfiguration (req, res, next) {
try {
let dbConfigs = await Operation.getConfiguration()
let dbConfigs = await OperationService.getConfiguration()
let version = {version: config.version}
let commit = {commit: config.commit}
let response = { ...version, ...commit, ...dbConfigs }
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports.replaceAppData = async function replaceAppData (req, res, next) {
appdata = req.body
}
let options = []
let response = await Operation.replaceAppData(options, appdata, req.userObject, res )
let response = await OperationService.replaceAppData(options, appdata, req.userObject, res )
}
else {
throw new SmError.PrivilegeError()
Expand Down Expand Up @@ -133,7 +133,7 @@ module.exports.getDetails = async function getDetails (req, res, next) {
try {
let elevate = req.query.elevate
if ( elevate ) {
const response = await Operation.getDetails()
const response = await OperationService.getDetails()
res.json(response)
}
else {
Expand Down
38 changes: 19 additions & 19 deletions api/source/controllers/STIG.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const config = require('../utils/config');
const SmError = require('../utils/error');
const parsers = require('../utils/parsers.js')
const STIG = require(`../service/${config.database.type}/STIGService`)
const STIGService = require(`../service/${config.database.type}/STIGService`)

module.exports.importBenchmark = async function importManualBenchmark (req, res, next) {
try {
Expand All @@ -22,7 +22,7 @@ module.exports.importBenchmark = async function importManualBenchmark (req, res,
if (benchmark.scap) {
throw new SmError.UnprocessableError('SCAP Benchmarks are not imported.')
}
const revision = await STIG.insertManualBenchmark(benchmark, clobber, res.svcStatus)
const revision = await STIGService.insertManualBenchmark(benchmark, clobber, res.svcStatus)
res.json(revision)
}
catch(err) {
Expand All @@ -37,20 +37,20 @@ module.exports.deleteRevisionByString = async function deleteRevisionByString (r
const revisionStr = req.params.revisionStr
const force = req.query.force
try {
const response = await STIG.getRevisionByString(benchmarkId, revisionStr, req.userObject, true)
const response = await STIGService.getRevisionByString(benchmarkId, revisionStr, req.userObject, true)
if(response === undefined) {
throw new SmError.NotFoundError('No matching revisionStr found.')
}
const existingRevisions = await STIG.getRevisionsByBenchmarkId(benchmarkId, req.userObject)
const stigAssigned = await STIG.getStigById(benchmarkId, req.userObject, true)
const existingRevisions = await STIGService.getRevisionsByBenchmarkId(benchmarkId, req.userObject)
const stigAssigned = await STIGService.getStigById(benchmarkId, req.userObject, true)
if (stigAssigned.collectionIds.length && existingRevisions.length == 1 && !force) {
throw new SmError.UnprocessableError("The revisionStr is the last remaining revision for this benchmark, which is assigned to one or more Collections. Set force=true to force the delete")
}
if (response.collectionIds.length && !force) {
throw new SmError.UnprocessableError("The revisionStr is pinned to one or more Collections. Set force=true to force the delete")
}
else {
await STIG.deleteRevisionByString(benchmarkId, revisionStr, res.svcStatus)
await STIGService.deleteRevisionByString(benchmarkId, revisionStr, res.svcStatus)
res.json(response)
}
}
Expand All @@ -68,14 +68,14 @@ module.exports.deleteStigById = async function deleteStigById (req, res, next) {
try {
const benchmarkId = req.params.benchmarkId
const force = req.query.force
const response = await STIG.getStigById(benchmarkId, req.userObject, true)
const response = await STIGService.getStigById(benchmarkId, req.userObject, true)
if(response === undefined) {
throw new SmError.NotFoundError('No matching benchmarkId found.')
}
if (response.collectionIds.length && !force) {
throw new SmError.UnprocessableError("The benchmarkId is assigned to one or more Collections. Set force=true to force the delete")
}
await STIG.deleteStigById(benchmarkId, res.svcStatus)
await STIGService.deleteStigById(benchmarkId, res.svcStatus)
res.json(response)
}
catch (err) {
Expand All @@ -91,7 +91,7 @@ module.exports.getCci = async function getCci (req, res, next) {
let cci = req.params.cci
let projection = req.query.projection
try {
let response = await STIG.getCci(cci, projection, req.userObject)
let response = await STIGService.getCci(cci, projection, req.userObject)
res.json(response)
}
catch(err) {
Expand All @@ -103,7 +103,7 @@ module.exports.getCcisByRevision = async function getCcisByRevision (req, res, n
let benchmarkId = req.params.benchmarkId
let revisionStr = req.params.revisionStr
try {
let response = await STIG.getCcisByRevision(benchmarkId, revisionStr, req.userObject)
let response = await STIGService.getCcisByRevision(benchmarkId, revisionStr, req.userObject)
res.json(response)
}
catch(err) {
Expand All @@ -117,7 +117,7 @@ module.exports.getGroupByRevision = async function getGroupByRevision (req, res,
let revisionStr = req.params.revisionStr
let groupId = req.params.groupId
try {
let response = await STIG.getGroupByRevision(benchmarkId, revisionStr, groupId, projection, req.userObject)
let response = await STIGService.getGroupByRevision(benchmarkId, revisionStr, groupId, projection, req.userObject)
res.json(response)
}
catch(err) {
Expand All @@ -130,7 +130,7 @@ module.exports.getGroupsByRevision = async function getGroupsByRevision (req, re
let benchmarkId = req.params.benchmarkId
let revisionStr = req.params.revisionStr
try {
let response = await STIG.getGroupsByRevision(benchmarkId, revisionStr, projection, req.userObject)
let response = await STIGService.getGroupsByRevision(benchmarkId, revisionStr, projection, req.userObject)
res.json(response)
}
catch(err) {
Expand All @@ -143,7 +143,7 @@ module.exports.getRevisionByString = async function getRevisionByString (req, re
const revisionStr = req.params.revisionStr
const elevate = req.query.elevate
try {
const response = await STIG.getRevisionByString(benchmarkId, revisionStr, req.userObject, elevate)
const response = await STIGService.getRevisionByString(benchmarkId, revisionStr, req.userObject, elevate)
res.json(response)
}
catch(err) {
Expand All @@ -155,7 +155,7 @@ module.exports.getRevisionsByBenchmarkId = async function getRevisionsByBenchmar
const benchmarkId = req.params.benchmarkId
const elevate = req.query.elevate
try {
const response = await STIG.getRevisionsByBenchmarkId(benchmarkId, req.userObject, elevate)
const response = await STIGService.getRevisionsByBenchmarkId(benchmarkId, req.userObject, elevate)
res.json(response)
}
catch(err) {
Expand All @@ -167,7 +167,7 @@ module.exports.getRuleByRuleId = async function getRuleByRuleId (req, res, next)
let projection = req.query.projection
let ruleId = req.params.ruleId
try {
let response = await STIG.getRuleByRuleId(ruleId, projection, req.userObject)
let response = await STIGService.getRuleByRuleId(ruleId, projection, req.userObject)
res.json(response)
}
catch(err) {
Expand All @@ -181,7 +181,7 @@ module.exports.getRuleByRevision = async function getRulesByRevision (req, res,
let revisionStr = req.params.revisionStr
let ruleId = req.params.ruleId
try {
let response = await STIG.getRuleByRevision(benchmarkId, revisionStr, ruleId, projection, req.userObject)
let response = await STIGService.getRuleByRevision(benchmarkId, revisionStr, ruleId, projection, req.userObject)
res.json(response)
}
catch(err) {
Expand All @@ -194,7 +194,7 @@ module.exports.getRulesByRevision = async function getRulesByRevision (req, res,
let benchmarkId = req.params.benchmarkId
let revisionStr = req.params.revisionStr
try {
let response = await STIG.getRulesByRevision(benchmarkId, revisionStr, projection, req.userObject)
let response = await STIGService.getRulesByRevision(benchmarkId, revisionStr, projection, req.userObject)
res.json(response)
}
catch(err) {
Expand All @@ -207,7 +207,7 @@ module.exports.getSTIGs = async function getSTIGs (req, res, next) {
const elevate = req.query.elevate
const projection = req.query.projection || []
try {
let response = await STIG.getSTIGs(title, projection, req.userObject, elevate)
let response = await STIGService.getSTIGs(title, projection, req.userObject, elevate)
res.json(response)
}
catch(err) {
Expand All @@ -219,7 +219,7 @@ module.exports.getStigById = async function getStigById (req, res, next) {
let benchmarkId = req.params.benchmarkId
const elevate = req.query.elevate
try {
let response = await STIG.getStigById(benchmarkId, req.userObject, elevate)
let response = await STIGService.getStigById(benchmarkId, req.userObject, elevate)
res.json(response)
}
catch(err) {
Expand Down
34 changes: 17 additions & 17 deletions api/source/controllers/User.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const config = require('../utils/config')
const User = require(`../service/${config.database.type}/UserService`)
const Asset = require(`../service/${config.database.type}/AssetService`)
const Collection = require(`../service/${config.database.type}/CollectionService`)
const UserService = require(`../service/${config.database.type}/UserService`)
const AssetService = require(`../service/${config.database.type}/AssetService`)
const CollectionService = require(`../service/${config.database.type}/CollectionService`)
const SmError = require('../utils/error')

/* */
module.exports.createUser = async function createUser (req, res, next) {
try {
let elevate = req.query.elevate
Expand All @@ -16,14 +16,14 @@ module.exports.createUser = async function createUser (req, res, next) {
if (body.hasOwnProperty('collectionGrants') ) {
// Verify each grant for a valid collectionId
let requestedIds = body.collectionGrants.map( g => g.collectionId )
let availableCollections = await Collection.getCollections({}, [], elevate, req.userObject)
let availableCollections = await CollectionService.getCollections({}, [], elevate, req.userObject)
let availableIds = availableCollections.map( c => c.collectionId)
if (! requestedIds.every( id => availableIds.includes(id) ) ) {
throw new SmError.UnprocessableError('One or more collectionIds are invalid.')
}
}
try {
let response = await User.createUser(body, projection, elevate, req.userObject, res.svcStatus)
let response = await UserService.createUser(body, projection, elevate, req.userObject, res.svcStatus)
res.status(201).json(response)
}
catch (err) {
Expand Down Expand Up @@ -51,12 +51,12 @@ module.exports.deleteUser = async function deleteUser (req, res, next) {
if (elevate) {
let userId = req.params.userId
let projection = req.query.projection
let userData = await User.getUserByUserId(userId, ['statistics'], elevate, req.userObject)
let userData = await UserService.getUserByUserId(userId, ['statistics'], elevate, req.userObject)
if (userData?.statistics?.lastAccess) {
// User has accessed the system, so we need to reject the request
throw new SmError.UnprocessableError('User has accessed the system. Use PATCH to remove collection grants or configure Authentication provider to reject user entirely.')
}
let response = await User.deleteUser(userId, projection, elevate, req.userObject)
let response = await UserService.deleteUser(userId, projection, elevate, req.userObject)
res.json(response)
}
else {
Expand All @@ -70,7 +70,7 @@ module.exports.deleteUser = async function deleteUser (req, res, next) {

module.exports.exportUsers = async function exportUsers (projection, elevate, userObject) {
if (elevate) {
return await User.getUsers(null, null, projection, elevate, userObject )
return await UserService.getUsers(null, null, projection, elevate, userObject )
}
else {
throw new SmError.PrivilegeError()
Expand All @@ -92,7 +92,7 @@ module.exports.getUserByUserId = async function getUserByUserId (req, res, next)
if ( elevate ) {
let userId = req.params.userId
let projection = req.query.projection
let response = await User.getUserByUserId(userId, projection, elevate, req.userObject)
let response = await UserService.getUserByUserId(userId, projection, elevate, req.userObject)
res.json(response)
}
else {
Expand All @@ -113,7 +113,7 @@ module.exports.getUsers = async function getUsers (req, res, next) {
if ( !elevate && projection && projection.length > 0) {
throw new SmError.PrivilegeError()
}
let response = await User.getUsers( username, usernameMatch, projection, elevate, req.userObject)
let response = await UserService.getUsers( username, usernameMatch, projection, elevate, req.userObject)
res.json(response)
}
catch(err) {
Expand All @@ -132,14 +132,14 @@ module.exports.replaceUser = async function replaceUser (req, res, next) {
if (body.hasOwnProperty('collectionGrants') ) {
// Verify each grant for a valid collectionId
let requestedIds = body.collectionGrants.map( g => g.collectionId )
let availableCollections = await Collection.getCollections({}, [], elevate, req.userObject)
let availableCollections = await CollectionService.getCollections({}, [], elevate, req.userObject)
let availableIds = availableCollections.map( c => c.collectionId)
if (! requestedIds.every( id => availableIds.includes(id) ) ) {
throw new SmError.UnprocessableError('One or more collectionIds are invalid.')
}
}

let response = await User.replaceUser(userId, body, projection, elevate, req.userObject, res.svcStatus)
let response = await UserService.replaceUser(userId, body, projection, elevate, req.userObject, res.svcStatus)
res.json(response)
}
else {
Expand All @@ -162,14 +162,14 @@ module.exports.updateUser = async function updateUser (req, res, next) {
if (body.hasOwnProperty('collectionGrants') ) {
// Verify each grant for a valid collectionId
let requestedIds = body.collectionGrants.map( g => g.collectionId )
let availableCollections = await Collection.getCollections({}, [], elevate, req.userObject)
let availableCollections = await CollectionService.getCollections({}, [], elevate, req.userObject)
let availableIds = availableCollections.map( c => c.collectionId)
if (! requestedIds.every( id => availableIds.includes(id) ) ) {
throw new SmError.UnprocessableError('One or more collectionIds are invalid.')
}
}

let response = await User.replaceUser(userId, body, projection, elevate, req.userObject, res.svcStatus)
let response = await UserService.replaceUser(userId, body, projection, elevate, req.userObject, res.svcStatus)
res.json(response)
}
else {
Expand All @@ -184,8 +184,8 @@ module.exports.updateUser = async function updateUser (req, res, next) {
/* c8 ignore start */
module.exports.setUserData = async function setUserData (username, fields) {
try {
await User.setUserData(username, fields)
return await User.getUserByUsername(username)
await UserService.setUserData(username, fields)
return await UserService.getUserByUsername(username)
}
catch (e) {
next(err)
Expand Down