Fixing some issues with the ctx.user, this was previously filled in by the old auth middleware.

This commit is contained in:
mike12345567 2021-04-13 15:27:47 +01:00
parent 02c9f69143
commit 56b4b1583c
5 changed files with 15 additions and 5 deletions

View File

@ -159,7 +159,6 @@ exports.create = async function(ctx) {
const url = await getAppUrlIfNotInUse(ctx)
const appId = instance._id
const version = packageJson.version
const newApplication = {
_id: appId,
type: "app",

View File

@ -71,7 +71,8 @@ exports.authenticate = async ctx => {
}
exports.fetchSelf = async ctx => {
const { userId, appId } = ctx.user
const appId = ctx.appId
const { userId } = ctx.user
/* istanbul ignore next */
if (!userId) {
ctx.body = {}

View File

@ -106,15 +106,18 @@ exports.serveComponentLibrary = async function(ctx) {
)
return send(ctx, "/awsDeploy.js", { root: componentLibraryPath })
}
const db = new CouchDB(appId)
const appInfo = await db.get(appId)
let componentLib = "componentlibrary"
if (ctx.user.version) {
componentLib += `-${ctx.user.version}`
if (appInfo && appInfo.version) {
componentLib += `-${appInfo.version}`
} else {
componentLib += `-${COMP_LIB_BASE_APP_VERSION}`
}
const S3_URL = encodeURI(
join(
objectStoreUrl(appId),
objectStoreUrl(),
componentLib,
ctx.query.library,
"dist",

View File

@ -1,5 +1,6 @@
const { getAppId, setCookie, getCookie, Cookies } = require("@budibase/auth")
const { getRole } = require("../utilities/security/roles")
const { generateUserMetadataID } = require("../db/utils")
const { getGlobalUsers } = require("../utilities/workerRequests")
const { BUILTIN_ROLE_IDS } = require("../utilities/security/roles")
@ -35,9 +36,14 @@ module.exports = async (ctx, next) => {
if (appId) {
ctx.appId = appId
if (roleId) {
const userId = ctx.user
? generateUserMetadataID(ctx.user.email)
: undefined
ctx.roleId = roleId
ctx.user = {
...ctx.user,
_id: userId,
userId,
role: await getRole(appId, roleId),
}
}

View File

@ -109,6 +109,7 @@ describe("Current app middleware", () => {
expect(cookieFn).not.toHaveBeenCalled()
}
expect(config.ctx.roleId).toEqual("BASIC")
expect(config.ctx.user.role._id).toEqual("BASIC")
expect(config.ctx.appId).toEqual("app_test")
expect(config.next).toHaveBeenCalled()
}