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/next/pages/api/v2/accounts/set-name.ts
Views: 687
/*1Set account {user/first/last} name.2*/34import userQuery from "@cocalc/database/user-query";5import userIsInGroup from "@cocalc/server/accounts/is-in-group";67import getAccountId from "lib/account/get-account";8import getParams from "lib/api/get-params";910import { apiRoute, apiRouteOperation } from "lib/api";11import { SuccessStatus } from "lib/api/status";12import {13SetAccountNameInputSchema,14SetAccountNameOutputSchema,15} from "lib/api/schema/accounts/set-name";1617async function handle(req, res) {18try {19await get(req);20res.json(SuccessStatus);21} catch (err) {22res.json({ error: `${err.message ? err.message : err}` });23return;24}25}2627async function get(req) {28const client_account_id = await getAccountId(req);2930if (client_account_id == null) {31throw Error("Must be signed in to edit account name.");32}3334const { username, first_name, last_name, account_id } = getParams(req);3536// This user MUST be an admin:37if (account_id && !(await userIsInGroup(client_account_id, "admin"))) {38throw Error(39"The `account_id` field may only be specified by account administrators.",40);41}4243return userQuery({44account_id: account_id || client_account_id,45query: {46accounts: {47// Any provided values must be non-empty in order for userQuery to SET values48// instead of fetching them.49//50...(username && { name: username }),51...(first_name && { first_name }),52...(last_name && { last_name }),53},54},55});56}5758export default apiRoute({59setName: apiRouteOperation({60method: "POST",61openApiOperation: {62tags: ["Accounts", "Admin"],63},64})65.input({66contentType: "application/json",67body: SetAccountNameInputSchema,68})69.outputs([70{71status: 200,72contentType: "application/json",73body: SetAccountNameOutputSchema,74},75])76.handler(handle),77});787980