Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tenant): tenant name can't exceed db limit #1776

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/api/controller/rootAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import jwt from 'koa-jwt';
import { auth } from 'config';
import { ObjectId } from 'mongodb';
import { createWorkerQueue, deleteWorkerQueue } from '../workers';
import { ROOT_ROLE, checkForbiddenNames } from '../../common/tools/tenantTools';

import {
ROOT_ROLE,
checkForbiddenNames,
checkNameTooLong,
} from '../../common/tools/tenantTools';
import bullBoard from '../bullBoard';
import { insertConfigTenant } from '../services/configTenant';

Expand Down Expand Up @@ -51,6 +54,9 @@ const postTenant = async ctx => {
if (tenantExists || checkForbiddenNames(name)) {
ctx.status = 403;
ctx.body = { error: `Invalid name: "${name}"` };
} else if (checkNameTooLong(name)) {
ctx.status = 403;
ctx.body = { error: `Tenant name: "${name}" too long` };
} else {
await ctx.tenantCollection.create({
name,
Expand Down
9 changes: 7 additions & 2 deletions src/app/js/root-admin/CreateTenantDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import NameField from './NameField';
import {
checkForbiddenNames,
forbiddenNamesMessage,
getTenantMaxSize,
MAX_DB_NAME_SIZE,
} from '../../../common/tools/tenantTools';

const CreateTenantDialog = ({ isOpen, handleClose, createAction }) => {
Expand Down Expand Up @@ -43,8 +45,11 @@ const CreateTenantDialog = ({ isOpen, handleClose, createAction }) => {
>
Le nom ne peut pas être {forbiddenNamesMessage}. Les
majuscules ne sont pas autorisées. Seules les lettres,
chiffres et le tiret “-” sont autorisés. 50 caractères
maximum sont autorisés
chiffres et le tiret “-” sont autorisés.{' '}
{getTenantMaxSize(window.__DBNAME__)} caractères maximum
sont autorisés. (Le terme{' '}
{'"{non du container}_{nom de l\'instance}"'} ne doit
pas dépasser {MAX_DB_NAME_SIZE} caractères).
</FormHelperText>
</FormControl>

Expand Down
5 changes: 4 additions & 1 deletion src/app/js/root-admin/NameField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { forwardRef } from 'react';
import PropTypes from 'prop-types';
import { OutlinedInput } from '@mui/material';
import { IMaskInput } from 'react-imask';
import { getTenantMaxSize } from '../../../common/tools/tenantTools';

const definitions = {
'#': /[0-9ux*]/i,
Expand All @@ -14,7 +15,9 @@ const TextMaskCustom = forwardRef(function TextMaskCustom(props, ref) {
{...other}
mask={[
{
mask: /^[a-z0-9-]{1,50}$/,
mask: new RegExp(
`^[a-z0-9-]{1,${getTenantMaxSize(window.__DBNAME__)}}$`,
),
definitions,
},
]}
Expand Down
26 changes: 22 additions & 4 deletions src/app/js/root-admin/Tenants.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,29 @@ const Tenants = ({ handleLogout }) => {
// Define the columns for the datagrid
const columns = [
{ field: '_id', headerName: 'ID', width: 200 },
{ field: 'name', headerName: 'Nom', flex: 1 },
{
field: 'name',
headerName: 'Nom',
flex: 4,
renderCell: params => {
return (
<Typography
sx={{
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
}}
title={params.value}
>
{params.value}
</Typography>
);
},
},
{
field: 'description',
headerName: 'Description',
flex: 1,
flex: 4,
valueFormatter: params => {
return formatValue(params.value);
},
Expand All @@ -261,15 +279,15 @@ const Tenants = ({ handleLogout }) => {
{
field: 'author',
headerName: 'Auteur',
flex: 1,
flex: 2,
valueFormatter: params => {
return formatValue(params.value);
},
},
{
field: 'createdAt',
headerName: 'Créée le',
flex: 1,
flex: 2,
valueFormatter: params => {
if (params.value == null) {
return '-';
Expand Down
12 changes: 10 additions & 2 deletions src/common/tools/tenantTools.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import config from 'config';
export const DEFAULT_TENANT = 'default';
export const ROOT_ROLE = 'root';
export const ADMIN_ROLE = 'admin';
export const MAX_DB_NAME_SIZE = 63;

export const INVALID_NAMES = [ADMIN_ROLE, ROOT_ROLE, DEFAULT_TENANT, ''];
export const checkForbiddenNames = value =>
INVALID_NAMES.includes(value) || value.length > 50;

export const getTenantMaxSize = (dbName = config?.mongo?.dbName) =>
MAX_DB_NAME_SIZE - dbName?.length - 1;

export const checkForbiddenNames = value => INVALID_NAMES.includes(value);

export const checkNameTooLong = value => value.length > getTenantMaxSize();

export const forbiddenNamesMessage = INVALID_NAMES.filter(name => !!name)
.map((name, index, list) =>
index === list.length - 1 ? `or "${name}"` : `"${name}"`,
Expand Down