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/database/postgres/site-license/manager.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { PostgreSQL } from "../types";
7
import { query } from "../query";
8
import { is_valid_uuid_string } from "@cocalc/util/misc";
9
10
export async function site_license_is_manager(
11
db: PostgreSQL,
12
account_id: string,
13
license_id: string
14
): Promise<boolean> {
15
return (
16
(
17
await query({
18
db,
19
query:
20
"SELECT COUNT(*)::INT FROM site_licenses WHERE id=$1 AND $2 = ANY(managers)",
21
one: true,
22
params: [license_id, account_id],
23
})
24
).count > 0
25
);
26
}
27
28
export async function site_license_manager_set(
29
db: PostgreSQL,
30
account_id: string,
31
info: {
32
id: string;
33
title?: string;
34
description?: string;
35
managers?: string[];
36
}
37
): Promise<void> {
38
// First make sure they really are a manager
39
if (!(await site_license_is_manager(db, account_id, info.id))) {
40
throw Error("user must be a manager of the license to change it");
41
}
42
const set: { title?: string; description?: string; managers?: string[] } = {};
43
// Set files and do some sanity checks to avoid having bad data in the database.
44
if (info.title != null) {
45
set.title = info.title;
46
if (typeof set.title != "string") {
47
throw Error("title must be a string");
48
}
49
}
50
if (info.description != null) {
51
set.description = info.description;
52
if (typeof set.description != "string") {
53
throw Error("description must be a string");
54
}
55
}
56
if (info.managers != null) {
57
set.managers = info.managers;
58
for (const manager of set.managers) {
59
if (!is_valid_uuid_string(manager)) {
60
throw Error("managers must be an array of valid uuid's");
61
}
62
}
63
}
64
65
// Now do the query
66
await db.async_query({
67
query: "UPDATE site_licenses",
68
set,
69
where: { id: info.id },
70
});
71
}
72
73