From d8e6bd0ab9e34cdae5482ad64bafac46f8ff703f Mon Sep 17 00:00:00 2001 From: kevmodrome Date: Thu, 2 Jul 2020 17:53:09 +0200 Subject: [PATCH] sets up dummy back-end --- .../tabs/{API.svelte => APIKeys.svelte} | 5 ++- .../src/components/settings/tabs/index.js | 2 +- .../server/src/api/controllers/apikeys.js | 45 +++++++++++++++++++ packages/server/src/api/index.js | 4 ++ packages/server/src/api/routes/apikeys.js | 12 +++++ packages/server/src/api/routes/index.js | 2 + 6 files changed, 68 insertions(+), 2 deletions(-) rename packages/builder/src/components/settings/tabs/{API.svelte => APIKeys.svelte} (85%) create mode 100644 packages/server/src/api/controllers/apikeys.js create mode 100644 packages/server/src/api/routes/apikeys.js diff --git a/packages/builder/src/components/settings/tabs/API.svelte b/packages/builder/src/components/settings/tabs/APIKeys.svelte similarity index 85% rename from packages/builder/src/components/settings/tabs/API.svelte rename to packages/builder/src/components/settings/tabs/APIKeys.svelte index fe3fe142ce..03aa2c86fc 100644 --- a/packages/builder/src/components/settings/tabs/API.svelte +++ b/packages/builder/src/components/settings/tabs/APIKeys.svelte @@ -9,12 +9,15 @@ async function updateKey(e) { console.log("Event Key: ", e.detail) // Send to endpoint when it exists - // const response = await api.put(`/api/${$store.appId}`, data) + const response = await api.put(`/api/${$store.appId}`, data) // const res = await response.json() } // Get Keys async function fetchKeys() { + const response = await api.get(`/api/keys/`) + const res = await response.json() + console.log("Res: ", res) // fetch keys from endpoint // return keys } diff --git a/packages/builder/src/components/settings/tabs/index.js b/packages/builder/src/components/settings/tabs/index.js index 67f79b536c..6e34141d09 100644 --- a/packages/builder/src/components/settings/tabs/index.js +++ b/packages/builder/src/components/settings/tabs/index.js @@ -2,5 +2,5 @@ export { default as General } from "./General.svelte" export { default as Integrations } from "./Integrations.svelte" export { default as Permissions } from "./Permissions.svelte" export { default as Users } from "./Users.svelte" -export { default as APIKeys } from "./Api.svelte" +export { default as APIKeys } from "./APIKeys.svelte" export { default as DangerZone } from "./DangerZone.svelte" diff --git a/packages/server/src/api/controllers/apikeys.js b/packages/server/src/api/controllers/apikeys.js new file mode 100644 index 0000000000..11dd0ac787 --- /dev/null +++ b/packages/server/src/api/controllers/apikeys.js @@ -0,0 +1,45 @@ +const CouchDB = require("../../db") +const clientDb = require("../../db/clientDb") +const bcrypt = require("../../utilities/bcrypt") +const getUserId = userName => `user_${userName}` +const { + POWERUSER_LEVEL_ID, + ADMIN_LEVEL_ID, +} = require("../../utilities/accessLevels") + +exports.fetch = async function (ctx) { + // Temporary while "real" infrastructure to store keys is created + ctx.status = 200 + ctx.message = "API Keys" + ctx.body = { + budibase: 'testFromBackEnd', + sendgrid: 'testFromBackEnd' + } +} + +exports.update = async function (ctx) { + ctx.status = 200 + ctx.message = `Updated ${ctx.params.key} succesfully.` + ctx.body = { + [ctx.params.key]: "somethingsomethingsomething" + } + + ctx.status = 200 + ctx.message = `User ${ctx.request.body.username} updated successfully.` + ctx.body = response +} + +const checkAccessLevel = async (db, accessLevelId) => { + if (!accessLevelId) return + if ( + accessLevelId === POWERUSER_LEVEL_ID || + accessLevelId === ADMIN_LEVEL_ID + ) { + return { + _id: accessLevelId, + name: accessLevelId, + permissions: [], + } + } + return await db.get(accessLevelId) +} diff --git a/packages/server/src/api/index.js b/packages/server/src/api/index.js index e6143d6725..09849422da 100644 --- a/packages/server/src/api/index.js +++ b/packages/server/src/api/index.js @@ -17,6 +17,7 @@ const { componentRoutes, workflowRoutes, accesslevelRoutes, + apiKeysRoutes } = require("./routes") const router = new Router() @@ -98,6 +99,9 @@ router.use(clientRoutes.allowedMethods()) router.use(accesslevelRoutes.routes()) router.use(accesslevelRoutes.allowedMethods()) +router.use(apiKeysRoutes.routes()) +router.use(apiKeysRoutes.allowedMethods()) + router.use(staticRoutes.routes()) router.use(staticRoutes.allowedMethods()) diff --git a/packages/server/src/api/routes/apikeys.js b/packages/server/src/api/routes/apikeys.js new file mode 100644 index 0000000000..66e1cd2d68 --- /dev/null +++ b/packages/server/src/api/routes/apikeys.js @@ -0,0 +1,12 @@ +const Router = require("@koa/router") +const controller = require("../controllers/apikeys") +const authorized = require("../../middleware/authorized") +const { BUILDER } = require("../../utilities/accessLevels") + +const router = Router() + +router + .get("/api/keys", authorized(BUILDER), controller.fetch) + .put("/api/keys/:key", authorized(BUILDER), controller.update) + +module.exports = router diff --git a/packages/server/src/api/routes/index.js b/packages/server/src/api/routes/index.js index b50fee788a..2e8662242f 100644 --- a/packages/server/src/api/routes/index.js +++ b/packages/server/src/api/routes/index.js @@ -11,6 +11,7 @@ const staticRoutes = require("./static") const componentRoutes = require("./component") const workflowRoutes = require("./workflow") const accesslevelRoutes = require("./accesslevel") +const apiKeysRoutes = require("./apikeys") module.exports = { authRoutes, @@ -26,4 +27,5 @@ module.exports = { componentRoutes, workflowRoutes, accesslevelRoutes, + apiKeysRoutes }