CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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