Updating fetch self to get the global user as well as local metadata.

This commit is contained in:
mike12345567 2021-04-12 15:54:14 +01:00
parent 22a3f1691f
commit 698c983056
4 changed files with 30 additions and 23 deletions

View File

@ -8,7 +8,7 @@ const { setCookie } = require("../../utilities")
const { outputProcessing } = require("../../utilities/rowProcessor") const { outputProcessing } = require("../../utilities/rowProcessor")
const { InternalTables } = require("../../db/utils") const { InternalTables } = require("../../db/utils")
const { UserStatus } = require("@budibase/auth") const { UserStatus } = require("@budibase/auth")
const setBuilderToken = require("../../utilities/builder/setBuilderToken") const { getFullUser } = require("../../utilities/users")
const INVALID_ERR = "Invalid Credentials" const INVALID_ERR = "Invalid Credentials"
@ -70,11 +70,6 @@ exports.authenticate = async ctx => {
} }
} }
// exports.builderLogin = async ctx => {
// await setBuilderToken(ctx)
// ctx.status = 200
// }
exports.fetchSelf = async ctx => { exports.fetchSelf = async ctx => {
const { userId, appId } = ctx.user const { userId, appId } = ctx.user
/* istanbul ignore next */ /* istanbul ignore next */
@ -83,7 +78,7 @@ exports.fetchSelf = async ctx => {
return return
} }
const db = new CouchDB(appId) const db = new CouchDB(appId)
const user = await db.get(userId) const user = await getFullUser({ ctx, userId: userId })
const userTable = await db.get(InternalTables.USER_METADATA) const userTable = await db.get(InternalTables.USER_METADATA)
if (user) { if (user) {
delete user.password delete user.password

View File

@ -11,6 +11,7 @@ const {
saveGlobalUser, saveGlobalUser,
deleteGlobalUser, deleteGlobalUser,
} = require("../../utilities/workerRequests") } = require("../../utilities/workerRequests")
const { getFullUser } = require("../../utilities/users")
exports.fetchMetadata = async function(ctx) { exports.fetchMetadata = async function(ctx) {
const database = new CouchDB(ctx.appId) const database = new CouchDB(ctx.appId)
@ -95,15 +96,9 @@ exports.destroyMetadata = async function(ctx) {
} }
exports.findMetadata = async function(ctx) { exports.findMetadata = async function(ctx) {
const database = new CouchDB(ctx.appId) ctx.body = await getFullUser({
const email = ctx,
ctx.params.email || getEmailFromUserMetadataID(ctx.params.userId) email: ctx.params.email,
const global = await getGlobalUsers(ctx, ctx.appId, email) userId: ctx.params.userId,
const user = await database.get(generateUserMetadataID(email)) })
ctx.body = {
...global,
...user,
// make sure the ID is always a local ID, not a global one
_id: generateUserMetadataID(email),
}
} }

View File

@ -1,14 +1,10 @@
const Router = require("@koa/router") const Router = require("@koa/router")
const controller = require("../controllers/auth") const controller = require("../controllers/auth")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("../../utilities/security/permissions")
const router = Router() const router = Router()
// TODO: needs removed
router.post("/api/authenticate", controller.authenticate) router.post("/api/authenticate", controller.authenticate)
// TODO: this is a hack simply to make sure builder has a cookie until auth reworked
// router.post("/api/builder/login", authorized(BUILDER), controller.builderLogin)
// doesn't need authorization as can only fetch info about self
router.get("/api/self", controller.fetchSelf) router.get("/api/self", controller.fetchSelf)
module.exports = router module.exports = router

View File

@ -0,0 +1,21 @@
const CouchDB = require("../../db")
const {
generateUserMetadataID,
getEmailFromUserMetadataID,
} = require("../db/utils")
const { getGlobalUsers } = require("../../utilities/workerRequests")
exports.getFullUser = async ({ ctx, email, userId }) => {
if (!email) {
email = getEmailFromUserMetadataID(userId)
}
const db = new CouchDB(ctx.appId)
const global = await getGlobalUsers(ctx, ctx.appId, email)
const user = await db.get(generateUserMetadataID(email))
return {
...global,
...user,
// make sure the ID is always a local ID, not a global one
_id: generateUserMetadataID(email),
}
}