UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

317 lines 15.6 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.add_collaborators_to_projects = void 0; var misc_1 = require("smc-util/misc"); var async_utils_1 = require("smc-util/async-utils"); var GROUPS = ["owner", "collaborator"]; function add_collaborators_to_projects(db, account_id, accounts, projects, // can be empty strings if tokens specified (since they determine project_id) tokens // must be all specified or none ) { return __awaiter(this, void 0, void 0, function () { var err_1, _a, _b, _i, i, project_id, account_id_1, token_id; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 2, , 6]); // In case of project tokens, this mutates the projects array. return [4 /*yield*/, verify_write_access_to_projects(db, account_id, projects, tokens)]; case 1: // In case of project tokens, this mutates the projects array. _c.sent(); return [3 /*break*/, 6]; case 2: err_1 = _c.sent(); if (!(accounts.length == 1 && account_id == accounts[0] && tokens == null)) return [3 /*break*/, 4]; return [4 /*yield*/, verify_course_access_to_project(db, account_id, projects[0])]; case 3: _c.sent(); return [3 /*break*/, 5]; case 4: throw err_1; case 5: return [3 /*break*/, 6]; case 6: /* Right now this function is called from outside typescript (e.g., api from user), so we have to do extra type checking. Also, the input is uuid's, which typescript can't check. */ verify_types(account_id, accounts, projects); _a = []; for (_b in projects) _a.push(_b); _i = 0; _c.label = 7; case 7: if (!(_i < _a.length)) return [3 /*break*/, 12]; i = _a[_i]; project_id = projects[i]; account_id_1 = accounts[i]; token_id = tokens === null || tokens === void 0 ? void 0 : tokens[i]; return [4 /*yield*/, async_utils_1.callback2(db.user_is_collaborator, { project_id: project_id, account_id: account_id_1 })]; case 8: if (_c.sent()) { // nothing to do since user is already on the given project -- won't use up token. return [3 /*break*/, 11]; } return [4 /*yield*/, async_utils_1.callback2(db.add_user_to_project, { project_id: project_id, account_id: account_id_1, })]; case 9: _c.sent(); if (!(token_id != null)) return [3 /*break*/, 11]; return [4 /*yield*/, increment_project_invite_token_counter(db, token_id)]; case 10: _c.sent(); _c.label = 11; case 11: _i++; return [3 /*break*/, 7]; case 12: return [2 /*return*/]; } }); }); } exports.add_collaborators_to_projects = add_collaborators_to_projects; function verify_write_access_to_projects(db, account_id, projects, tokens) { return __awaiter(this, void 0, void 0, function () { var i, _a, project_id, error, _b, _c, project_id, e_1_1; var e_1, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: if (!(tokens != null)) return [3 /*break*/, 5]; i = 0; _e.label = 1; case 1: if (!(i < projects.length)) return [3 /*break*/, 4]; if (tokens[i] == null) { throw Error("If tokens are specified, they must all be non-null."); } return [4 /*yield*/, project_invite_token_project_id(db, tokens[i])]; case 2: _a = _e.sent(), project_id = _a.project_id, error = _a.error; if (error || !project_id) { throw Error("Project invite token is not valid - " + error); } projects[i] = project_id; _e.label = 3; case 3: i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; case 5: _e.trys.push([5, 10, 11, 12]); _b = __values(new Set(projects)), _c = _b.next(); _e.label = 6; case 6: if (!!_c.done) return [3 /*break*/, 9]; project_id = _c.value; return [4 /*yield*/, async_utils_1.callback2(db.user_is_in_project_group, { project_id: project_id, account_id: account_id, groups: GROUPS, })]; case 7: if (!(_e.sent())) { throw Error("user " + account_id + " does not have write access to project " + project_id); } _e.label = 8; case 8: _c = _b.next(); return [3 /*break*/, 6]; case 9: return [3 /*break*/, 12]; case 10: e_1_1 = _e.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 12]; case 11: try { if (_c && !_c.done && (_d = _b.return)) _d.call(_b); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 12: return [2 /*return*/]; } }); }); } function verify_types(account_id, accounts, projects) { var e_2, _a, e_3, _b; if (!misc_1.is_valid_uuid_string(account_id)) throw Error("account_id (=\"" + account_id + "\") must be a valid uuid string (type=" + typeof account_id + ")"); if (!misc_1.is_array(accounts)) { throw Error("accounts must be an array"); } if (!misc_1.is_array(projects)) { throw Error("projects must be an array"); } if (accounts.length != projects.length) { throw Error("accounts (of length " + accounts.length + ") and projects (of length " + projects.length + ") must be arrays of the same length"); } try { for (var accounts_1 = __values(accounts), accounts_1_1 = accounts_1.next(); !accounts_1_1.done; accounts_1_1 = accounts_1.next()) { var x = accounts_1_1.value; if (!misc_1.is_valid_uuid_string(x)) throw Error("all account id's must be valid uuid's, but \"" + x + "\" is not"); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (accounts_1_1 && !accounts_1_1.done && (_a = accounts_1.return)) _a.call(accounts_1); } finally { if (e_2) throw e_2.error; } } try { for (var projects_1 = __values(projects), projects_1_1 = projects_1.next(); !projects_1_1.done; projects_1_1 = projects_1.next()) { var x = projects_1_1.value; if (x != "" && !misc_1.is_valid_uuid_string(x)) throw Error("all project id's must be valid uuid's (or empty), but \"" + x + "\" is not"); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (projects_1_1 && !projects_1_1.done && (_b = projects_1.return)) _b.call(projects_1); } finally { if (e_3) throw e_3.error; } } } // Returns {error:"..."} if token is not valid. // Returns {project_id:"...."} with project_id of the project if the token is valid. function project_invite_token_project_id(db, token) { return __awaiter(this, void 0, void 0, function () { var v, err_2, _a, expires, counter, usage_limit, project_id; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); return [4 /*yield*/, db.async_query({ table: "project_invite_tokens", select: ["expires", "counter", "usage_limit", "project_id"], where: { token: token }, })]; case 1: v = _b.sent(); return [3 /*break*/, 3]; case 2: err_2 = _b.sent(); return [2 /*return*/, { error: "problem querying the database -- " + err_2 }]; case 3: if (v.rows.length == 0) return [2 /*return*/, { error: "no such token" }]; _a = v.rows[0], expires = _a.expires, counter = _a.counter, usage_limit = _a.usage_limit, project_id = _a.project_id; if (expires != null && expires <= new Date()) { return [2 /*return*/, { error: "the token already expired" }]; } if (usage_limit != null && counter >= usage_limit) { return [2 /*return*/, { error: "the token can only be used " + usage_limit + " times" }]; } return [2 /*return*/, { project_id: project_id }]; } }); }); } function increment_project_invite_token_counter(db, token) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, db.async_query({ query: "UPDATE project_invite_tokens SET counter=coalesce(counter, 0)+1 WHERE token=$1", params: [token], })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); } function verify_course_access_to_project(db, account_id, project_id) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var v, course_id, w, group; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, db.async_query({ query: "SELECT course FROM projects WHERE project_id=$1", params: [project_id], })]; case 1: v = _c.sent(); if (v.rows.length == 0) { throw Error("no project with id \"" + project_id + "\""); } course_id = (_a = v.rows[0].course) === null || _a === void 0 ? void 0 : _a.project_id; if (!misc_1.is_valid_uuid_string(course_id)) { throw Error("cannot add self to \"" + project_id + "\" -- must be an admin"); } if (!misc_1.is_valid_uuid_string(account_id)) { // be extra careful since we directly put account_id in the query string. throw Error("account_id " + account_id + " must be a valid uuid"); } return [4 /*yield*/, db.async_query({ query: "SELECT users#>'{" + account_id + ",group}' AS group FROM projects WHERE project_id=$1", params: [course_id], })]; case 2: w = _c.sent(); group = (_b = w.rows[0]) === null || _b === void 0 ? void 0 : _b.group; if (group != "owner" && group != "collaborator") { throw Error("cannot add self to \"" + project_id + "\" -- must be owner or collaborator on course project"); } return [2 /*return*/]; } }); }); } //# sourceMappingURL=collab.js.map