Password reset for budibase users

This commit is contained in:
Rory Powell 2021-09-17 13:41:22 +01:00
parent 3576ca87be
commit 91b9a98247
7 changed files with 10 additions and 22 deletions

View File

@ -63,6 +63,7 @@ exports.tryAddTenant = async (tenantId, userId, email) => {
} }
if (emailDoc) { if (emailDoc) {
emailDoc.tenantId = tenantId emailDoc.tenantId = tenantId
emailDoc.userId = userId
promises.push(db.put(emailDoc)) promises.push(db.put(emailDoc))
} }
if (tenants.tenantIds.indexOf(tenantId) === -1) { if (tenants.tenantIds.indexOf(tenantId) === -1) {

View File

@ -12,7 +12,7 @@
} }
// redirect to account portal for authentication in the cloud // redirect to account portal for authentication in the cloud
if ($admin.cloud && $admin.accountPortalUrl) { if (!$auth.user && $admin.cloud && $admin.accountPortalUrl) {
window.location.href = $admin.accountPortalUrl window.location.href = $admin.accountPortalUrl
} }
}) })

View File

@ -96,7 +96,7 @@ exports.reset = async ctx => {
exports.resetUpdate = async ctx => { exports.resetUpdate = async ctx => {
const { resetCode, password } = ctx.request.body const { resetCode, password } = ctx.request.body
try { try {
const userId = await checkResetPasswordCode(resetCode) const { userId } = await checkResetPasswordCode(resetCode)
const db = getGlobalDB() const db = getGlobalDB()
const user = await db.get(userId) const user = await db.get(userId)
user.password = await hash(password) user.password = await hash(password)

View File

@ -6,13 +6,11 @@ const {
} = require("@budibase/auth/db") } = require("@budibase/auth/db")
const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils
const { UserStatus, EmailTemplatePurpose } = require("../../../constants") const { UserStatus, EmailTemplatePurpose } = require("../../../constants")
const { DEFAULT_TENANT_ID } = require("@budibase/auth/constants")
const { checkInviteCode } = require("../../../utilities/redis") const { checkInviteCode } = require("../../../utilities/redis")
const { sendEmail } = require("../../../utilities/email") const { sendEmail } = require("../../../utilities/email")
const { user: userCache } = require("@budibase/auth/cache") const { user: userCache } = require("@budibase/auth/cache")
const { invalidateSessions } = require("@budibase/auth/sessions") const { invalidateSessions } = require("@budibase/auth/sessions")
const CouchDB = require("../../../db") const CouchDB = require("../../../db")
const env = require("../../../environment")
const { const {
getGlobalDB, getGlobalDB,
getTenantId, getTenantId,
@ -251,25 +249,14 @@ exports.find = async ctx => {
ctx.body = user ctx.body = user
} }
exports.tenantLookup = async ctx => { exports.tenantUserLookup = async ctx => {
const id = ctx.params.id const id = ctx.params.id
// lookup, could be email or userId, either will return a doc // lookup, could be email or userId, either will return a doc
const db = new CouchDB(PLATFORM_INFO_DB) const db = new CouchDB(PLATFORM_INFO_DB)
let tenantId = null
try { try {
const doc = await db.get(id) ctx.body = await db.get(id)
if (doc && doc.tenantId) {
tenantId = doc.tenantId
}
} catch (err) { } catch (err) {
if (!env.MULTI_TENANCY) { ctx.throw(400, "No tenant user found.")
tenantId = DEFAULT_TENANT_ID
} else {
ctx.throw(400, "No tenant found.")
}
}
ctx.body = {
tenantId,
} }
} }

View File

@ -94,7 +94,7 @@ router
controller.adminUser controller.adminUser
) )
.get("/api/global/users/self", controller.getSelf) .get("/api/global/users/self", controller.getSelf)
.get("/api/global/users/tenant/:id", controller.tenantLookup) .get("/api/global/users/tenant/:id", controller.tenantUserLookup)
// global endpoint but needs to come at end (blocks other endpoints otherwise) // global endpoint but needs to come at end (blocks other endpoints otherwise)
.get("/api/global/users/:id", adminOnly, controller.find) .get("/api/global/users/:id", adminOnly, controller.find)

View File

@ -51,7 +51,7 @@ function createSMTPTransport(config) {
async function getLinkCode(purpose, email, user, info = null) { async function getLinkCode(purpose, email, user, info = null) {
switch (purpose) { switch (purpose) {
case EmailTemplatePurpose.PASSWORD_RECOVERY: case EmailTemplatePurpose.PASSWORD_RECOVERY:
return getResetPasswordCode(user._id) return getResetPasswordCode(user._id, info)
case EmailTemplatePurpose.INVITATION: case EmailTemplatePurpose.INVITATION:
return getInviteCode(email, info) return getInviteCode(email, info)
default: default:

View File

@ -63,8 +63,8 @@ exports.shutdown = async () => {
* @param {string} userId the ID of the user which is to be reset. * @param {string} userId the ID of the user which is to be reset.
* @return {Promise<string>} returns the code that was stored to redis. * @return {Promise<string>} returns the code that was stored to redis.
*/ */
exports.getResetPasswordCode = async userId => { exports.getResetPasswordCode = async (userId, info) => {
return writeACode(utils.Databases.PW_RESETS, userId) return writeACode(utils.Databases.PW_RESETS, { userId, info })
} }
/** /**