|
| 1 | +'use strict' |
| 2 | + |
| 3 | +import logger from 'winston' |
| 4 | + |
| 5 | +import * as authorisation from './authorisation' |
| 6 | +import {AppModelAPI} from '../model/apps' |
| 7 | + |
| 8 | +/* |
| 9 | + Checks admin permission for create, update and delete operations. |
| 10 | + Throws error if user does not have admin access |
| 11 | +*/ |
| 12 | +const checkUserPermission = (ctx, operation) => { |
| 13 | + if (!authorisation.inGroup('admin', ctx.authenticated)) { |
| 14 | + ctx.statusCode = 403 |
| 15 | + throw Error( |
| 16 | + `User ${ctx.authenticated.email} is not an admin, API access to ${operation} an app denied.` |
| 17 | + ) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/* |
| 22 | + Returns app if it exists, if not it throws an error |
| 23 | +*/ |
| 24 | +const checkAppExists = async (ctx, appId) => { |
| 25 | + const app = await AppModelAPI.findById(appId) |
| 26 | + |
| 27 | + if (!app) { |
| 28 | + ctx.statusCode = 404 |
| 29 | + throw Error(`App with id ${appId} does not exist`) |
| 30 | + } |
| 31 | + |
| 32 | + return app |
| 33 | +} |
| 34 | + |
| 35 | +// Creates error response for operations create, read, update and delete |
| 36 | +const createErrorResponse = (ctx, operation, error) => { |
| 37 | + logger.error(`Could not ${operation} an app via the API: ${error.message}`) |
| 38 | + |
| 39 | + ctx.body = { |
| 40 | + error: error.message |
| 41 | + } |
| 42 | + ctx.status = ctx.statusCode ? ctx.statusCode : 500 |
| 43 | +} |
| 44 | + |
| 45 | +const validateId = (ctx, id) => { |
| 46 | + if (!id.match(/^[0-9a-fA-F]{24}$/)) { |
| 47 | + ctx.statusCode = 400 |
| 48 | + throw Error(`App id "${id}" is invalid. ObjectId should contain 24 characters`) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export async function addApp(ctx) { |
| 53 | + try { |
| 54 | + checkUserPermission(ctx, 'add') |
| 55 | + |
| 56 | + const app = new AppModelAPI(ctx.request.body) |
| 57 | + |
| 58 | + await app |
| 59 | + .save() |
| 60 | + .then(app => { |
| 61 | + logger.info(`User ${ctx.request.email} created app ${app.name}`) |
| 62 | + |
| 63 | + ctx.status = 201 |
| 64 | + ctx.body = app |
| 65 | + }) |
| 66 | + .catch(e => { |
| 67 | + ctx.statusCode = 400 |
| 68 | + throw e |
| 69 | + }) |
| 70 | + } catch (e) { |
| 71 | + createErrorResponse(ctx, 'add', e) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export async function updateApp(ctx, appId) { |
| 76 | + try { |
| 77 | + checkUserPermission(ctx, 'update') |
| 78 | + |
| 79 | + validateId(ctx, appId) |
| 80 | + |
| 81 | + await checkAppExists(ctx, appId) |
| 82 | + |
| 83 | + const update = ctx.request.body |
| 84 | + |
| 85 | + await AppModelAPI.findOneAndUpdate({_id: appId}, update, { |
| 86 | + new: true, |
| 87 | + runValidators: true |
| 88 | + }) |
| 89 | + .then(app => { |
| 90 | + logger.info(`User ${ctx.authenticated.email} updated app ${app.name}`) |
| 91 | + |
| 92 | + ctx.body = app |
| 93 | + ctx.status = 200 |
| 94 | + }) |
| 95 | + .catch(e => { |
| 96 | + ctx.statusCode = 400 |
| 97 | + throw e |
| 98 | + }) |
| 99 | + } catch (e) { |
| 100 | + createErrorResponse(ctx, 'update', e) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export async function getApps(ctx) { |
| 105 | + try { |
| 106 | + const apps = await AppModelAPI.find(ctx.request.query) |
| 107 | + |
| 108 | + logger.info(`User ${ctx.authenticated.email} fetched ${apps.length} apps`) |
| 109 | + |
| 110 | + ctx.body = apps |
| 111 | + ctx.status = 200 |
| 112 | + } catch (e) { |
| 113 | + createErrorResponse(ctx, 'retrieve', e) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export async function getApp(ctx, appId) { |
| 118 | + try { |
| 119 | + validateId(ctx, appId) |
| 120 | + |
| 121 | + const app = await checkAppExists(ctx, appId) |
| 122 | + |
| 123 | + logger.info(`User ${ctx.authenticated.email} app fetched ${appId}`) |
| 124 | + |
| 125 | + ctx.body = app |
| 126 | + ctx.status = 200 |
| 127 | + } catch (e) { |
| 128 | + createErrorResponse(ctx, 'retrieve', e) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export async function deleteApp(ctx, appId) { |
| 133 | + try { |
| 134 | + checkUserPermission(ctx, 'delete') |
| 135 | + |
| 136 | + validateId(ctx, appId) |
| 137 | + |
| 138 | + await checkAppExists(ctx, appId) |
| 139 | + |
| 140 | + await AppModelAPI.deleteOne({_id: appId}).then(() => { |
| 141 | + logger.info(`User ${ctx.authenticated.email} deleted app ${appId}`) |
| 142 | + |
| 143 | + ctx.status = 200 |
| 144 | + ctx.body = { |
| 145 | + success: true |
| 146 | + } |
| 147 | + }) |
| 148 | + } catch (e) { |
| 149 | + createErrorResponse(ctx, 'delete', e) |
| 150 | + } |
| 151 | +} |
0 commit comments