From 291b3aea4797e1ad1b2d67c4f3e8e1d1f7b8a492 Mon Sep 17 00:00:00 2001 From: Birm Date: Fri, 21 Jun 2024 13:24:45 -0400 Subject: [PATCH] simplify method --- caracal.js | 4 +--- handlers/customHandlers.js | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/caracal.js b/caracal.js index 850e6d6..9b7ba96 100644 --- a/caracal.js +++ b/caracal.js @@ -80,9 +80,7 @@ var HANDLERS = { "requestResetPassword": customHandlers.requestResetPassword, "getOwnUser": customHandlers.getOwnUser, "editOwnUser": customHandlers.editOwnUser, - "impersonate": function() { - return customHandlers.impersonate(userFunction); - }, + "impersonate": customHandlers.impersonate, "dataEnforce": auth.dataEnforce, "monitorCheck": monitor.check, "mongoFind": dataHandlers.General.find, diff --git a/handlers/customHandlers.js b/handlers/customHandlers.js index ccfd4af..deb3386 100644 --- a/handlers/customHandlers.js +++ b/handlers/customHandlers.js @@ -3,6 +3,8 @@ const auth = require('./authHandlers.js'); const sgMail = require('@sendgrid/mail'); const mongoDB = require("../service/database"); +var dataHandlers = require('./dataHandlers.js'); + sgMail.setApiKey(process.env.SENDGRID_API_KEY); let defaultAddresses = '["rbirmin@emory.edu", "brandon.gallas@fda.hhs.gov", "Emma.Gardecki@fda.hhs.gov"]'; let adminAddressRaw = process.env.ADMIN_EMAILS || defaultAddresses; @@ -139,13 +141,19 @@ function editOwnUser(db, collection) { }; } -function impersonate(userFunction) { +function impersonate() { return function(req, res, next) { - let fakeToken = {'email': req.body.email}; - let user = userFunction(fakeToken); - let token = auth.makeJwt(user, auth.PRIKEY, auth.EXPIRY); - req.data = token; - next(); + dataHandlers.User.forLogin(req.body.email).then((x)=>{ + const newToken = {}; + newToken.userType = x[0].userType || 'Null'; + newToken.userFilter = x[0].userFilter || ['Public']; + newToken.sub = req.body.email; + newToken.email = req.body.email; + newToken.name = req.body.email; + newToken.data = x[0]; + req.data = newToken; + next() + }); }; }