Skip to content

Commit

Permalink
Merge pull request ToolJet#9833 from ToolJet/main
Browse files Browse the repository at this point in the history
Main to develop
  • Loading branch information
gsmithun4 authored May 23, 2024
2 parents dc267aa + 7c35c5f commit 0d35857
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.43.0
2.44.0
2 changes: 1 addition & 1 deletion frontend/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.43.0
2.44.0
12 changes: 4 additions & 8 deletions frontend/src/Editor/CodeBuilder/CodeHinter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ export function CodeHinter({
}) {
const context = useContext(CodeHinterContext);

const hiddenWorkspaceConstantText = 'Workspace constant values are hidden';

const darkMode = localStorage.getItem('darkMode') === 'true';
const options = {
lineNumbers: lineNumbers ?? false,
Expand Down Expand Up @@ -137,8 +135,6 @@ export function CodeHinter({
const isWorkspaceVariable =
typeof currentValue === 'string' && (currentValue.includes('%%client') || currentValue.includes('%%server'));

const isWorkspaceConstant = typeof currentValue === 'string' && currentValue.includes('constants.');

const constantRegex = /{{constants\.([a-zA-Z0-9_]+)}}/g;

const slideInStyles = useSpring({
Expand Down Expand Up @@ -269,7 +265,7 @@ export function CodeHinter({
globalPreviewCopy = preview;
globalErrorCopy = null;
setResolvingError(null);
setResolvedValue(isWorkspaceConstant ? hiddenWorkspaceConstantText : preview);
setResolvedValue(preview);
}

return [globalPreviewCopy, globalErrorCopy];
Expand All @@ -281,7 +277,7 @@ export function CodeHinter({
return () => {
if (enablePreview) {
setPrevCurrentValue(null);
setResolvedValue(isWorkspaceConstant ? hiddenWorkspaceConstantText : globalPreviewCopy);
setResolvedValue(globalPreviewCopy);
setResolvingError(globalErrorCopy);
}
};
Expand Down Expand Up @@ -377,9 +373,9 @@ export function CodeHinter({
<div>
<div className="d-flex my-1">
<div className="flex-grow-1" style={{ fontWeight: 700, textTransform: 'capitalize' }}>
{!isWorkspaceConstant && previewType}
{previewType}
</div>
{isFocused && !isWorkspaceConstant && (
{isFocused && (
<div className="preview-icons position-relative">
<CodeHinter.PopupIcon callback={() => copyToClipboard(content)} icon="copy" tip="Copy to clipboard" />
</div>
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/Editor/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ enablePatches();

const decimalToHex = (alpha) => (alpha === 0 ? '00' : Math.round(255 * alpha).toString(16));

const maskedWorkspaceConstantStr = '**************';

const EditorComponent = (props) => {
const { socket } = createWebsocketConnection(props?.params?.id);
const mounted = useMounted();
Expand Down Expand Up @@ -374,7 +372,8 @@ const EditorComponent = (props) => {
orgEnvironmentConstantService.getAll().then(({ constants }) => {
const orgConstants = {};
constants.map((constant) => {
orgConstants[constant.name] = maskedWorkspaceConstantStr;
const constantValue = constant.values.find((value) => value.environmentName === 'production')['value'];
orgConstants[constant.name] = constantValue;
});

useCurrentStateStore.getState().actions.setCurrentState({
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/Editor/Viewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import DesktopHeader from './Viewer/DesktopHeader';
import './Viewer/viewer.scss';
import useAppDarkMode from '@/_hooks/useAppDarkMode';

const maskedWorkspaceConstantStr = '**************';
class ViewerComponent extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -246,7 +245,7 @@ class ViewerComponent extends React.Component {
if (variablesResult && Array.isArray(variablesResult)) {
variablesResult.map((constant) => {
const constantValue = constant.values.find((value) => value.environmentName === 'production')['value'];
orgConstants[constant.name] = maskedWorkspaceConstantStr;
orgConstants[constant.name] = constantValue;
});
return {
constants: orgConstants,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,7 @@ const verifyConstant = (value, definedConstants) => {
};

const ResolvedValue = ({ value, isFocused, state = {}, type }) => {
const isConstant = type === 'Workspace Constant';
const hiddenWorkspaceConstantText = 'Workspace constant values are hidden';
const invalidConstants = verifyConstant(value, state.constants);
let preview;
let error;
if (invalidConstants?.length) {
[preview, error] = [value, `Undefined constants: ${invalidConstants}`];
} else {
[preview, error] = resolveReferences(value, state, null, {}, true, true);
if (isConstant) {
preview = hiddenWorkspaceConstantText;
}
}
const [preview, error] = resolveReferences(value, state, null, {}, true, true);

const previewType = typeof preview;

Expand All @@ -84,7 +72,7 @@ const ResolvedValue = ({ value, isFocused, state = {}, type }) => {
: error?.toString();
const isValidError = error && errorMessage !== 'HiddenEnvironmentVariable';

if (error && (!isValidError || error?.toString().includes('Undefined constants:'))) {
if (error && !isValidError) {
resolvedValue = errorMessage;
}

Expand All @@ -109,6 +97,8 @@ const ResolvedValue = ({ value, isFocused, state = {}, type }) => {
}
};

const isConstant = type === 'Workspace Constant';

const [heightRef, currentHeight] = useHeight();

const slideInStyles = useSpring({
Expand All @@ -130,7 +120,7 @@ const ResolvedValue = ({ value, isFocused, state = {}, type }) => {
<div className="alert-banner-type-text">
<div className="d-flex my-1">
<div className="flex-grow-1" style={{ fontWeight: 800, textTransform: 'capitalize' }}>
{isValidError ? 'Error' : isConstant ? null : ` ${type} - ${previewType}`}
{isValidError ? 'Error' : ` ${type} - ${previewType}`}
</div>
</div>
{getPreviewContent(resolvedValue, previewType)}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/_stores/currentStateStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const initialState = {
variables: {},
},
succededQuery: {},
constants: {},
};

export const useCurrentStateStore = create(
Expand Down
1 change: 0 additions & 1 deletion plugins/packages/restapi/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export default class RestapiQueryService implements QueryService {
...this.fetchHttpsCertsForCustomCA(sourceOptions),
headers: sanitizeHeaders(sourceOptions, queryOptions, hasDataSource),
searchParams,
...(isUrlEncoded ? { form: json } : { json }),
};

const hasFiles = (json) => {
Expand Down
2 changes: 1 addition & 1 deletion server/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.43.0
2.44.0
26 changes: 5 additions & 21 deletions server/src/controllers/organization_constants.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { OrganizationConstantsAbilityFactory } from 'src/modules/casl/abilities/
import { AppDecorator as App } from 'src/decorators/app.decorator';
import { OrgEnvironmentVariablesAbilityFactory } from 'src/modules/casl/abilities/org-environment-variables-ability.factory';
import { OrgEnvironmentVariable } from 'src/entities/org_envirnoment_variable.entity';
import { OrganizationConstant } from 'src/entities/organization_constants.entity';

@Controller('organization-constants')
export class OrganizationConstantController {
Expand All @@ -32,39 +31,24 @@ export class OrganizationConstantController {

@UseGuards(JwtAuthGuard)
@Get()
async get(@User() user, @Query('decryptValue') decryptValue) {
const ability = await this.organizationConstantsAbilityFactory.organizationConstantActions(user, null);
const decrypt =
decryptValue === 'true' &&
(ability.can('createOrganizationConstant', OrganizationConstant) ||
ability.can('deleteOrganizationConstant', OrganizationConstant));
const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId, decrypt);
async get(@User() user) {
const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId);
return { constants: result };
}

@UseGuards(IsPublicGuard)
@Get(':app_slug')
async getConstantsFromApp(@App() app, @User() user) {
const result = await this.organizationConstantsService.allEnvironmentConstants(app.organizationId, false);
const result = await this.organizationConstantsService.allEnvironmentConstants(app.organizationId);
return { constants: result };
}

@UseGuards(JwtAuthGuard)
@Get('/environment/:environmentId')
async getConstantsFromEnvironment(
@User() user,
@Param('environmentId') environmentId,
@Query('decryptValue') decryptValue
) {
const ability = await this.organizationConstantsAbilityFactory.organizationConstantActions(user, null);
const decrypt =
decryptValue === 'true' &&
(ability.can('createOrganizationConstant', OrganizationConstant) ||
ability.can('deleteOrganizationConstant', OrganizationConstant));
async getConstantsFromEnvironment(@User() user, @Param('environmentId') environmentId) {
const result = await this.organizationConstantsService.getConstantsForEnvironment(
user.organizationId,
environmentId,
decrypt
environmentId
);
return { constants: result };
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/dto/app-authentication.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsEmail, IsNotEmpty, IsOptional, IsString, IsUUID, MinLength } from 'class-validator';
import { IsEmail, IsNotEmpty, IsOptional, IsString, IsUUID, MinLength, MaxLength } from 'class-validator';
import { lowercaseString } from 'src/helpers/utils.helper';
import { Transform } from 'class-transformer';

Expand Down Expand Up @@ -31,6 +31,7 @@ export class AppSignupDto {
@IsString()
@IsNotEmpty()
@MinLength(5, { message: 'Password should contain more than 5 letters' })
@MaxLength(100, { message: 'Password length should not be more than 100 ' })
password: string;

@IsOptional()
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/data_queries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ export class DataQueriesService {

if (variables?.length > 0) {
for (const variable of variables) {
object = object.replace(variable, await this.resolveConstants(variable, organization_id, environmentId));
object = object.replace(variable, options[variable]);
}
}
return object;
Expand Down
38 changes: 8 additions & 30 deletions server/src/services/organization_constants.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class OrganizationConstantsService {
private appEnvironmentService: AppEnvironmentService
) {}

async allEnvironmentConstants(organizationId: string, decryptValue: boolean): Promise<OrganizationConstant[]> {
async allEnvironmentConstants(organizationId: string): Promise<OrganizationConstant[]> {
return await dbTransactionWrap(async (manager: EntityManager) => {
const query = manager
.createQueryBuilder(OrganizationConstant, 'organization_constants')
Expand All @@ -33,21 +33,11 @@ export class OrganizationConstantsService {
appEnvironments.map(async (env) => {
const value = constant.orgEnvironmentConstantValues.find((value) => value.environmentId === env.id);

const valueResult = {
return {
environmentName: env.name,
id: value ? value.environmentId : undefined, // Safeguard for undefined 'value'
value: value && value.value.length > 0 ? await this.decryptSecret(organizationId, value.value) : '',
id: value.environmentId,
};

if (value && value.value.length > 0) {
const decryptedOrRawValue = decryptValue
? await this.decryptSecret(organizationId, value.value)
: value.value;

if (decryptValue) {
valueResult['value'] = decryptedOrRawValue;
}
}
return valueResult;
})
);

Expand All @@ -64,11 +54,7 @@ export class OrganizationConstantsService {
});
}

async getConstantsForEnvironment(
organizationId: string,
environmentId: string,
decryptValue: boolean
): Promise<OrganizationConstant[]> {
async getConstantsForEnvironment(organizationId: string, environmentId: string): Promise<OrganizationConstant[]> {
return await dbTransactionWrap(async (manager: EntityManager) => {
const query = manager
.createQueryBuilder(OrganizationConstant, 'organization_constants')
Expand All @@ -78,20 +64,12 @@ export class OrganizationConstantsService {
const result = await query.getMany();

const constantsWithValues = result.map(async (constant) => {
const constantResult = {
const decryptedValue = await this.decryptSecret(organizationId, constant.orgEnvironmentConstantValues[0].value);
return {
id: constant.id,
name: constant.constantName,
value: decryptedValue,
};

if (decryptValue && constant.orgEnvironmentConstantValues.length > 0) {
const decryptedValue = await this.decryptSecret(
organizationId,
constant.orgEnvironmentConstantValues[0].value
);
constantResult['value'] = decryptedValue;
}

return constantResult;
});

return Promise.all(constantsWithValues);
Expand Down
9 changes: 5 additions & 4 deletions server/src/services/tooljet_db_operations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { QueryService, QueryResult } from '@tooljet/plugins/dist/packages/common
import { TooljetDbService } from './tooljet_db.service';
import { isEmpty } from 'lodash';
import { PostgrestProxyService } from './postgrest_proxy.service';
import { maybeSetSubPath } from 'src/helpers/utils.helper';

@Injectable()
export class TooljetDbOperationsService implements QueryService {
Expand Down Expand Up @@ -74,7 +75,7 @@ export class TooljetDbOperationsService implements QueryService {
!isEmpty(offset) && query.push(`offset=${offset}`);
}
const headers = { 'data-query-id': queryOptions.id, 'tj-workspace-id': queryOptions.organization_id };
const url = `/api/tooljet-db/proxy/${tableId}` + `?${query}`;
const url = maybeSetSubPath(`/api/tooljet-db/proxy/${tableId}` + `?${query}`);

return await this.proxyPostgrest(url, 'GET', headers);
}
Expand All @@ -87,7 +88,7 @@ export class TooljetDbOperationsService implements QueryService {

const headers = { 'data-query-id': queryOptions.id, 'tj-workspace-id': queryOptions.organization_id };

const url = `/api/tooljet-db/proxy/${queryOptions.table_id}`;
const url = maybeSetSubPath(`/api/tooljet-db/proxy/${queryOptions.table_id}`);
return await this.proxyPostgrest(url, 'POST', headers, columns);
}

Expand All @@ -112,7 +113,7 @@ export class TooljetDbOperationsService implements QueryService {
!isEmpty(whereQuery) && query.push(whereQuery);

const headers = { 'data-query-id': queryOptions.id, 'tj-workspace-id': queryOptions.organization_id };
const url = `/api/tooljet-db/proxy/${tableId}?` + query.join('&') + '&order=id';
const url = maybeSetSubPath(`/api/tooljet-db/proxy/${tableId}?` + query.join('&') + '&order=id');
return await this.proxyPostgrest(url, 'PATCH', headers, body);
}

Expand Down Expand Up @@ -149,7 +150,7 @@ export class TooljetDbOperationsService implements QueryService {
limit && limit !== '' && query.push(`limit=${limit}&order=id`);

const headers = { 'data-query-id': queryOptions.id, 'tj-workspace-id': queryOptions.organization_id };
const url = `/api/tooljet-db/proxy/${tableId}?` + query.join('&');
const url = maybeSetSubPath(`/api/tooljet-db/proxy/${tableId}?` + query.join('&'));
return await this.proxyPostgrest(url, 'DELETE', headers);
}

Expand Down

0 comments on commit 0d35857

Please sign in to comment.