Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/accounts/get-email-address.ts
Views: 687
/*1Get the email address, if there is one, associated to an account_id.23SECURITY: This is only available to admins and partners, i.e.,4highly privileged accounts, since we don't want anybody to be able5to dump our email addresses and spam people.6*/78import getAccountId from "lib/account/get-account";9import getEmailAddress from "@cocalc/server/accounts/get-email-address";10import getParams from "lib/api/get-params";11import userIsInGroup from "@cocalc/server/accounts/is-in-group";1213import { apiRoute, apiRouteOperation } from "lib/api";14import {15GetAccountEmailAddressInputSchema,16GetAccountEmailAddressOutputSchema,17} from "lib/api/schema/accounts/get-email-address";1819async function handle(req, res) {20const { account_id } = getParams(req);21const user_account_id = await getAccountId(req);22try {23res.json({ email_address: await getAddress(user_account_id, account_id) });24} catch (err) {25res.json({ error: err.message });26}27}2829async function getAddress(30user_account_id: string | undefined,31account_id: string | undefined,32): Promise<string | undefined> {33if (account_id == null) return undefined;34// check that user_account_id is admin or partner35if (36user_account_id == null ||37(!(await userIsInGroup(user_account_id, "partner")) &&38!(await userIsInGroup(user_account_id, "admin")))39) {40throw Error(41"you must be an admin or partner to get the email address of any account_id",42);43}4445// get the address46return await getEmailAddress(account_id);47}4849export default apiRoute({50getEmailAddress: apiRouteOperation({51method: "POST",52openApiOperation: {53tags: ["Accounts", "Admin"],54},55})56.input({57contentType: "application/json",58body: GetAccountEmailAddressInputSchema,59})60.outputs([61{62status: 200,63contentType: "application/json",64body: GetAccountEmailAddressOutputSchema,65},66])67.handler(handle),68});697071