Path: blob/master/src/packages/next/lib/api/assert-trusted.ts
1448 views
import { db } from "@cocalc/database";1import { is_admin } from "@cocalc/database/postgres/account-queries";2import { getServerSettings } from "@cocalc/database/settings";3import getMinBalance from "@cocalc/server/purchases/get-min-balance";4import { KUCALC_COCALC_COM } from "@cocalc/util/db-schema/site-defaults";5import { currency } from "@cocalc/util/misc";67const THRESH = -100;8export const TRUST_ERROR_MESSAGE = `Please contact support and request a minimum balance that is under ${currency(9THRESH,10)} to access this API endpoint.`;1112export default async function assertTrusted(account_id: string): Promise<void> {13const { kucalc } = await getServerSettings();1415if (kucalc === KUCALC_COCALC_COM) {16// on cocalc.com, we check if users have gained trust by giving them a lower min balance17if ((await getMinBalance(account_id)) > THRESH) {18throw new Error(TRUST_ERROR_MESSAGE);19}20} else {21// for on-prem instances, only admins are allowed to create accounts22if (!(await is_admin(db(), account_id))) {23throw new Error(24"Only users in the group 'admin' are allowed to create new users.",25);26}27}28}293031