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