snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
This commit is contained in:
27
apps/meteor/server/lib/ldap/operations/executeOperation.ts
Normal file
27
apps/meteor/server/lib/ldap/operations/executeOperation.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { ILDAPEntry } from '@rocket.chat/core-typings';
|
||||
|
||||
import { executeFallback, type LDAPVariableFallback } from './fallback';
|
||||
import { executeMatch, type LDAPVariableMatch } from './match';
|
||||
import { executeReplace, type LDAPVariableReplace } from './replace';
|
||||
import { executeSplit, type LDAPVariableSplit } from './split';
|
||||
import { executeSubstring, type LDAPVariableSubString } from './substring';
|
||||
|
||||
export type LDAPVariableOperation =
|
||||
LDAPVariableReplace | LDAPVariableMatch | LDAPVariableSubString | LDAPVariableFallback | LDAPVariableSplit;
|
||||
|
||||
export function executeOperation(ldapUser: ILDAPEntry, input: string, operation?: LDAPVariableOperation): string | undefined {
|
||||
switch (operation?.operation) {
|
||||
case 'replace':
|
||||
return executeReplace(input, operation);
|
||||
case 'match':
|
||||
return executeMatch(input, operation);
|
||||
case 'substring':
|
||||
return executeSubstring(input, operation);
|
||||
case 'fallback':
|
||||
return executeFallback(ldapUser, input, operation);
|
||||
case 'split':
|
||||
return executeSplit(input, operation);
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
61
apps/meteor/server/lib/ldap/operations/fallback.spec.ts
Normal file
61
apps/meteor/server/lib/ldap/operations/fallback.spec.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import type { ILDAPEntry } from '@rocket.chat/core-typings';
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import type { LDAPVariableFallback } from './fallback';
|
||||
import { executeFallback } from './fallback';
|
||||
|
||||
describe('executeFallback function', () => {
|
||||
const mockUser: ILDAPEntry = {
|
||||
_raw: {},
|
||||
defaultFallback: 'defaultFallbackValue',
|
||||
};
|
||||
|
||||
it('should return the input value when it is valid and no minLength is provided', () => {
|
||||
const input = 'validInput';
|
||||
const operation: LDAPVariableFallback = { operation: 'fallback', fallback: 'defaultFallback' };
|
||||
const result = executeFallback(mockUser, input, operation);
|
||||
expect(result).to.equal(input);
|
||||
});
|
||||
|
||||
it('should return the input value when it is valid and meets minLength requirement', () => {
|
||||
const input = 'validInput';
|
||||
const operation: LDAPVariableFallback = { operation: 'fallback', fallback: 'defaultFallback', minLength: 5 };
|
||||
const result = executeFallback(mockUser, input, operation);
|
||||
expect(result).to.equal(input);
|
||||
});
|
||||
|
||||
it('should return fallback when input is invalid', () => {
|
||||
const input = '';
|
||||
const operation: LDAPVariableFallback = { operation: 'fallback', fallback: 'defaultFallback' };
|
||||
const result = executeFallback(mockUser, input, operation);
|
||||
expect(result).to.equal('defaultFallbackValue');
|
||||
});
|
||||
|
||||
it('should return fallback when input is too short', () => {
|
||||
const input = 'short';
|
||||
const operation: LDAPVariableFallback = { operation: 'fallback', fallback: 'defaultFallback', minLength: 10 };
|
||||
const result = executeFallback(mockUser, input, operation);
|
||||
expect(result).to.equal('defaultFallbackValue');
|
||||
});
|
||||
|
||||
it('should return fallback when input is undefined', () => {
|
||||
const operation: LDAPVariableFallback = { operation: 'fallback', fallback: 'defaultFallback' };
|
||||
const result = executeFallback(mockUser, undefined as any, operation);
|
||||
expect(result).to.equal('defaultFallbackValue');
|
||||
});
|
||||
|
||||
it('should return fallback when input is an empty string and minLength is zero', () => {
|
||||
const input = '';
|
||||
const operation: LDAPVariableFallback = { operation: 'fallback', fallback: 'defaultFallback', minLength: 0 };
|
||||
const result = executeFallback(mockUser, input, operation);
|
||||
expect(result).to.equal('defaultFallbackValue');
|
||||
});
|
||||
|
||||
it('should return fallback when input is an empty string and minLength is undefined', () => {
|
||||
const input = '';
|
||||
const operation: LDAPVariableFallback = { operation: 'fallback', fallback: 'defaultFallback', minLength: undefined };
|
||||
const result = executeFallback(mockUser, input, operation);
|
||||
expect(result).to.equal('defaultFallbackValue');
|
||||
});
|
||||
});
|
||||
24
apps/meteor/server/lib/ldap/operations/fallback.ts
Normal file
24
apps/meteor/server/lib/ldap/operations/fallback.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { ILDAPEntry } from '@rocket.chat/core-typings';
|
||||
|
||||
import { getLdapDynamicValue } from '../getLdapDynamicValue';
|
||||
|
||||
export type LDAPVariableFallback = {
|
||||
operation: 'fallback';
|
||||
fallback: string;
|
||||
|
||||
minLength?: number;
|
||||
};
|
||||
|
||||
export function executeFallback(ldapUser: ILDAPEntry, input: string, operation: LDAPVariableFallback): string | undefined {
|
||||
let valid = Boolean(input);
|
||||
|
||||
if (valid && typeof operation.minLength === 'number') {
|
||||
valid = input.length >= operation.minLength;
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
return input;
|
||||
}
|
||||
|
||||
return getLdapDynamicValue(ldapUser, operation.fallback);
|
||||
}
|
||||
106
apps/meteor/server/lib/ldap/operations/match.spec.ts
Normal file
106
apps/meteor/server/lib/ldap/operations/match.spec.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import type { LDAPVariableMatch } from './match';
|
||||
import { executeMatch } from './match';
|
||||
|
||||
describe('executeMatch function', () => {
|
||||
describe('Validation', () => {
|
||||
it('throws an error when pattern is missing', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: '',
|
||||
regex: true,
|
||||
};
|
||||
expect(() => executeMatch('input', operation)).to.throw('Invalid MATCH operation.');
|
||||
});
|
||||
|
||||
it('throws an error when neither valueIfTrue nor indexToUse is provided', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: 'pattern',
|
||||
};
|
||||
expect(() => executeMatch('input', operation)).to.throw('Invalid MATCH operation.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Non-Regex Matching', () => {
|
||||
it('returns valueIfTrue when input matches pattern', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: 'hello',
|
||||
valueIfTrue: 'matched',
|
||||
valueIfFalse: 'not matched',
|
||||
};
|
||||
expect(executeMatch('hello', operation)).to.be.equal('matched');
|
||||
});
|
||||
|
||||
it('returns valueIfFalse when input does not match pattern', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: 'hello',
|
||||
valueIfTrue: 'matched',
|
||||
valueIfFalse: 'not matched',
|
||||
};
|
||||
expect(executeMatch('world', operation)).to.be.equal('not matched');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Regex Matching', () => {
|
||||
it('returns valueIfTrue when input matches regex pattern', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: '^hello$',
|
||||
regex: true,
|
||||
valueIfTrue: 'matched',
|
||||
valueIfFalse: 'not matched',
|
||||
};
|
||||
expect(executeMatch('hello', operation)).to.be.equal('matched');
|
||||
});
|
||||
|
||||
it('returns valueIfFalse when input does not match regex pattern', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: '^hello$',
|
||||
regex: true,
|
||||
valueIfTrue: 'matched',
|
||||
valueIfFalse: 'not matched',
|
||||
};
|
||||
expect(executeMatch('world', operation)).to.be.equal('not matched');
|
||||
});
|
||||
|
||||
it('uses flags when provided', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: '^HELLO$',
|
||||
regex: true,
|
||||
flags: 'i',
|
||||
valueIfTrue: 'matched',
|
||||
valueIfFalse: 'not matched',
|
||||
};
|
||||
expect(executeMatch('hello', operation)).to.be.equal('matched');
|
||||
});
|
||||
});
|
||||
|
||||
describe('IndexToUse', () => {
|
||||
it('returns the matched group at indexToUse', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: '(hello) (world)',
|
||||
regex: true,
|
||||
indexToUse: 1,
|
||||
};
|
||||
expect(executeMatch('hello world', operation)).to.be.equal('hello');
|
||||
});
|
||||
|
||||
it('returns undefined when indexToUse is out of range', () => {
|
||||
const operation: LDAPVariableMatch = {
|
||||
operation: 'match',
|
||||
pattern: '(hello)',
|
||||
regex: true,
|
||||
indexToUse: 2,
|
||||
};
|
||||
expect(executeMatch('hello', operation)).to.be.undefined;
|
||||
});
|
||||
});
|
||||
});
|
||||
28
apps/meteor/server/lib/ldap/operations/match.ts
Normal file
28
apps/meteor/server/lib/ldap/operations/match.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
export type LDAPVariableMatch = {
|
||||
operation: 'match';
|
||||
pattern: string;
|
||||
regex?: boolean;
|
||||
flags?: string;
|
||||
indexToUse?: number;
|
||||
valueIfTrue?: string;
|
||||
valueIfFalse?: string;
|
||||
};
|
||||
|
||||
export function executeMatch(input: string, operation: LDAPVariableMatch): string | undefined {
|
||||
if (!operation.pattern || (typeof operation.valueIfTrue !== 'string' && typeof operation.indexToUse !== 'number')) {
|
||||
throw new Error('Invalid MATCH operation.');
|
||||
}
|
||||
|
||||
const pattern = operation.regex ? new RegExp(operation.pattern, operation.flags) : operation.pattern;
|
||||
|
||||
const result = input.match(pattern);
|
||||
if (!result) {
|
||||
return operation.valueIfFalse;
|
||||
}
|
||||
|
||||
if (typeof operation.indexToUse === 'number' && result.length > operation.indexToUse) {
|
||||
return result[operation.indexToUse];
|
||||
}
|
||||
|
||||
return operation.valueIfTrue;
|
||||
}
|
||||
105
apps/meteor/server/lib/ldap/operations/replace.spec.ts
Normal file
105
apps/meteor/server/lib/ldap/operations/replace.spec.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import type { LDAPVariableReplace } from './replace';
|
||||
import { executeReplace } from './replace';
|
||||
|
||||
describe('executeReplace', () => {
|
||||
describe('Validation', () => {
|
||||
it('throws an error when pattern is missing', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: '',
|
||||
replacement: 'new-value',
|
||||
};
|
||||
expect(() => executeReplace('input', operation)).to.throw('Invalid REPLACE operation.');
|
||||
});
|
||||
|
||||
it('throws an error when replacement is not a string', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: 'old-value',
|
||||
replacement: 123 as any,
|
||||
};
|
||||
expect(() => executeReplace('input', operation)).to.throw('Invalid REPLACE operation.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('String Replacement', () => {
|
||||
it('replaces the first occurrence of the pattern', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: 'old',
|
||||
replacement: 'new',
|
||||
};
|
||||
expect(executeReplace('old-value old-value', operation)).to.be.equal('new-value old-value');
|
||||
});
|
||||
|
||||
it('replaces all occurrences of the pattern when `all` is true', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: 'old',
|
||||
replacement: 'new',
|
||||
all: true,
|
||||
};
|
||||
expect(executeReplace('old-value old-value', operation)).to.be.equal('new-value new-value');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Regex Replacement', () => {
|
||||
it('replaces the first occurrence of the pattern', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: 'old',
|
||||
replacement: 'new',
|
||||
regex: true,
|
||||
};
|
||||
expect(executeReplace('old-value old-value', operation)).to.be.equal('new-value old-value');
|
||||
});
|
||||
|
||||
it('replaces all occurrences of the pattern when `regex` and `all` are true', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: 'old',
|
||||
replacement: 'new',
|
||||
regex: true,
|
||||
all: true,
|
||||
};
|
||||
expect(executeReplace('old-value old-value', operation)).to.be.equal('new-value new-value');
|
||||
});
|
||||
|
||||
it('uses the provided flags', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: 'OLD',
|
||||
replacement: 'new',
|
||||
regex: true,
|
||||
flags: 'i',
|
||||
};
|
||||
expect(executeReplace('OLD-value old-value', operation)).to.be.equal('new-value old-value');
|
||||
});
|
||||
|
||||
it('adds the `g` flag when `all` is true', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: 'old',
|
||||
replacement: 'new',
|
||||
regex: true,
|
||||
all: true,
|
||||
};
|
||||
expect(executeReplace('old-value old-value', operation)).to.be.equal('new-value new-value');
|
||||
});
|
||||
|
||||
it('does not duplicate the `g` flag when already present', () => {
|
||||
const operation: LDAPVariableReplace = {
|
||||
operation: 'replace',
|
||||
pattern: 'old',
|
||||
replacement: 'new',
|
||||
regex: true,
|
||||
all: true,
|
||||
flags: 'g',
|
||||
};
|
||||
expect(executeReplace('old-value old-value', operation)).to.be.equal('new-value new-value');
|
||||
});
|
||||
});
|
||||
});
|
||||
23
apps/meteor/server/lib/ldap/operations/replace.ts
Normal file
23
apps/meteor/server/lib/ldap/operations/replace.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
export type LDAPVariableReplace = {
|
||||
operation: 'replace';
|
||||
pattern: string;
|
||||
regex?: boolean;
|
||||
flags?: string;
|
||||
all?: boolean;
|
||||
replacement: string;
|
||||
};
|
||||
|
||||
export function executeReplace(input: string, operation: LDAPVariableReplace): string {
|
||||
if (!operation.pattern || typeof operation.replacement !== 'string') {
|
||||
throw new Error('Invalid REPLACE operation.');
|
||||
}
|
||||
|
||||
const flags = operation.regex && operation.all ? `${operation.flags || ''}${operation.flags?.includes('g') ? '' : 'g'}` : operation.flags;
|
||||
const pattern = operation.regex ? new RegExp(operation.pattern, flags) : operation.pattern;
|
||||
|
||||
if (operation.all) {
|
||||
return input.replaceAll(pattern, operation.replacement);
|
||||
}
|
||||
|
||||
return input.replace(pattern, operation.replacement);
|
||||
}
|
||||
48
apps/meteor/server/lib/ldap/operations/split.spec.ts
Normal file
48
apps/meteor/server/lib/ldap/operations/split.spec.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import type { LDAPVariableSplit } from './split';
|
||||
import { executeSplit } from './split';
|
||||
|
||||
describe('executeSplit function', () => {
|
||||
it('should throw an error if the pattern is empty', () => {
|
||||
const operation: LDAPVariableSplit = { operation: 'split', pattern: '' };
|
||||
expect(() => executeSplit('input', operation)).to.throw('Invalid SPLIT operation.');
|
||||
});
|
||||
|
||||
it('should split the input string by the pattern and return the first element by default', () => {
|
||||
const operation: LDAPVariableSplit = { operation: 'split', pattern: ',' };
|
||||
const result = executeSplit('hello,world', operation);
|
||||
expect(result).to.be.equal('hello');
|
||||
});
|
||||
|
||||
it('should split the input string by the pattern and return the element at the specified index', () => {
|
||||
const operation: LDAPVariableSplit = { operation: 'split', pattern: ',', indexToUse: 1 };
|
||||
const result = executeSplit('hello,world', operation);
|
||||
expect(result).to.be.equal('world');
|
||||
});
|
||||
|
||||
it('should return undefined if the index is out of range', () => {
|
||||
const operation: LDAPVariableSplit = { operation: 'split', pattern: ',', indexToUse: 2 };
|
||||
const result = executeSplit('hello,world', operation);
|
||||
expect(result).to.be.undefined;
|
||||
});
|
||||
|
||||
it('should return undefined if the input string does not contain the pattern', () => {
|
||||
const operation: LDAPVariableSplit = { operation: 'split', pattern: ',' };
|
||||
const result = executeSplit('helloworld', operation);
|
||||
expect(result).to.be.equal('helloworld');
|
||||
});
|
||||
|
||||
it('should return the first element if the input string is empty', () => {
|
||||
const operation: LDAPVariableSplit = { operation: 'split', pattern: ',' };
|
||||
const result = executeSplit('', operation);
|
||||
expect(result).to.be.equal('');
|
||||
});
|
||||
|
||||
it('should return undefined if the input string is undefined', () => {
|
||||
const operation: LDAPVariableSplit = { operation: 'split', pattern: ',' };
|
||||
const result = executeSplit(undefined as any, operation);
|
||||
expect(result).to.be.undefined;
|
||||
});
|
||||
});
|
||||
30
apps/meteor/server/lib/ldap/operations/split.ts
Normal file
30
apps/meteor/server/lib/ldap/operations/split.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export type LDAPVariableSplit = {
|
||||
operation: 'split';
|
||||
pattern: string;
|
||||
indexToUse?: number;
|
||||
};
|
||||
|
||||
export function executeSplit(input: string, operation: LDAPVariableSplit): string | undefined {
|
||||
if (!operation.pattern) {
|
||||
throw new Error('Invalid SPLIT operation.');
|
||||
}
|
||||
|
||||
if (!input) {
|
||||
return input;
|
||||
}
|
||||
|
||||
const result = input.split(operation.pattern);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof operation.indexToUse === 'number') {
|
||||
if (result.length > operation.indexToUse) {
|
||||
return result[operation.indexToUse];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return result.shift();
|
||||
}
|
||||
48
apps/meteor/server/lib/ldap/operations/substring.spec.ts
Normal file
48
apps/meteor/server/lib/ldap/operations/substring.spec.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import type { LDAPVariableSubString } from './substring';
|
||||
import { executeSubstring } from './substring';
|
||||
|
||||
describe('executeSubstring function', () => {
|
||||
it('should throw an error if the start is missing', () => {
|
||||
const operation: LDAPVariableSubString = { operation: 'substring' } as unknown as LDAPVariableSubString;
|
||||
expect(() => executeSubstring('input', operation)).to.throw('Invalid SUBSTRING operation.');
|
||||
});
|
||||
|
||||
it('should throw an error if the start is invalid', () => {
|
||||
const operation: LDAPVariableSubString = { operation: 'substring', start: 0, end: null } as unknown as LDAPVariableSubString;
|
||||
expect(() => executeSubstring('input', operation)).to.throw('Invalid SUBSTRING operation.');
|
||||
});
|
||||
|
||||
it('should get the substring of the input, using the start param', () => {
|
||||
const result = executeSubstring('hello world', { operation: 'substring', start: 6 });
|
||||
expect(result).to.be.equal('world');
|
||||
});
|
||||
|
||||
it('should get the whole string when the start is zero', () => {
|
||||
const result = executeSubstring('hello world', { operation: 'substring', start: 0 });
|
||||
expect(result).to.be.equal('hello world');
|
||||
});
|
||||
|
||||
it('should get the substring of the input, using the start and end param', () => {
|
||||
const result = executeSubstring('hello world', { operation: 'substring', start: 6, end: 8 });
|
||||
expect(result).to.be.equal('wo');
|
||||
});
|
||||
|
||||
it('should work backwards if end is smaller than start', () => {
|
||||
const result = executeSubstring('hello world', { operation: 'substring', start: 5, end: 0 });
|
||||
expect(result).to.be.equal('hello');
|
||||
});
|
||||
|
||||
it('should get an empty string if start and end are the same', () => {
|
||||
expect(executeSubstring('hello world', { operation: 'substring', start: 0, end: 0 })).to.be.equal('');
|
||||
expect(executeSubstring('hello world', { operation: 'substring', start: 5, end: 5 })).to.be.equal('');
|
||||
});
|
||||
|
||||
it('should treat negative values as zero', () => {
|
||||
expect(executeSubstring('hello world', { operation: 'substring', start: -4, end: 5 })).to.be.equal('hello');
|
||||
expect(executeSubstring('hello world', { operation: 'substring', start: 5, end: -4 })).to.be.equal('hello');
|
||||
expect(executeSubstring('hello world', { operation: 'substring', start: -5, end: -4 })).to.be.equal('');
|
||||
});
|
||||
});
|
||||
13
apps/meteor/server/lib/ldap/operations/substring.ts
Normal file
13
apps/meteor/server/lib/ldap/operations/substring.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export type LDAPVariableSubString = {
|
||||
operation: 'substring';
|
||||
start: number;
|
||||
end?: number;
|
||||
};
|
||||
|
||||
export function executeSubstring(input: string, operation: LDAPVariableSubString): string | undefined {
|
||||
if (typeof operation.start !== 'number' || (operation.end !== undefined && typeof operation.end !== 'number')) {
|
||||
throw new Error('Invalid SUBSTRING operation.');
|
||||
}
|
||||
|
||||
return input.substring(operation.start, operation.end);
|
||||
}
|
||||
Reference in New Issue
Block a user