1
0
Fork 0
mirror of synced 2024-06-01 18:20:18 +12:00

Updating per review comments.

This commit is contained in:
mike12345567 2021-11-10 12:00:29 +00:00
parent a0ebe7537d
commit c8e02a20a0
4 changed files with 41 additions and 18 deletions

View file

@ -259,6 +259,24 @@ exports.getAllApps = async (CouchDB, { dev, all, idsOnly } = {}) => {
}
}
/**
* Utility function for getAllApps but filters to production apps only.
*/
exports.getDeployedAppIDs = async CouchDB => {
return (await exports.getAllApps(CouchDB, { idsOnly: true })).filter(
id => !exports.isDevAppID(id)
)
}
/**
* Utility function for the inverse of above.
*/
exports.getDevAppIDs = async CouchDB => {
return (await exports.getAllApps(CouchDB, { idsOnly: true })).filter(id =>
exports.isDevAppID(id)
)
}
exports.dbExists = async (CouchDB, dbName) => {
let exists = false
try {

View file

@ -8,11 +8,7 @@ const { getGlobalUsers, getRawGlobalUser } = require("../../utilities/global")
const { getFullUser } = require("../../utilities/users")
const { isEqual } = require("lodash")
const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
const {
getDevelopmentAppID,
getAllApps,
isDevAppID,
} = require("@budibase/auth/db")
const { getDevelopmentAppID, getDeployedAppIDs } = require("@budibase/auth/db")
const { doesDatabaseExist } = require("../../utilities")
const { UserStatus } = require("@budibase/auth/constants")
@ -78,8 +74,12 @@ exports.syncUser = async function (ctx) {
try {
user = await getRawGlobalUser(userId)
} catch (err) {
user = {}
deleting = true
if (err && err.status === 404) {
user = {}
deleting = true
} else {
throw err
}
}
const roles = user.roles
// remove props which aren't useful to metadata
@ -90,9 +90,7 @@ exports.syncUser = async function (ctx) {
let prodAppIds
// if they are a builder then get all production app IDs
if ((user.builder && user.builder.global) || deleting) {
prodAppIds = (await getAllApps(CouchDB, { idsOnly: true })).filter(
id => !isDevAppID(id)
)
prodAppIds = await getDeployedAppIDs(CouchDB)
} else {
prodAppIds = Object.entries(roles)
.filter(entry => entry[1] !== BUILTIN_ROLE_IDS.PUBLIC)

View file

@ -35,7 +35,7 @@ router
controller.destroyMetadata
)
.post(
"/api/users/sync/:id",
"/api/users/metadata/sync/:id",
authorized(PermissionTypes.USER, PermissionLevels.WRITE),
controller.syncUser
)

View file

@ -4,7 +4,7 @@ const { getTenantId, isTenantIdSet } = require("@budibase/auth/tenancy")
const { checkSlashesInUrl } = require("../utilities")
const env = require("../environment")
exports.syncUserInApps = async userId => {
async function makeAppRequest(url, method, body) {
if (env.isTest()) {
return
}
@ -13,12 +13,19 @@ exports.syncUserInApps = async userId => {
if (isTenantIdSet()) {
request.headers[Headers.TENANT_ID] = getTenantId()
}
request.headers["Content-Type"] = "application/json"
request.body = JSON.stringify({})
request.method = "POST"
const response = await fetch(
checkSlashesInUrl(env.APPS_URL + `/api/users/sync/${userId}`),
request
if (body) {
request.headers["Content-Type"] = "application/json"
request.body = JSON.stringify(body)
}
request.method = method
return fetch(checkSlashesInUrl(env.APPS_URL + url), request)
}
exports.syncUserInApps = async userId => {
const response = await makeAppRequest(
`/api/users/metadata/sync/${userId}`,
"POST",
{}
)
if (response.status !== 200) {
throw "Unable to sync user."