Checkpoint

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 705f2157-ef97-4fbd-89e4-8c7f2ecaea90
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7ed01c5f-a82d-405a-b728-b2e3d127c60c/e50e1752-9703-4966-afe2-754edcd5518a.jpg
This commit is contained in:
alphaeusmote
2025-04-11 02:02:28 +00:00
parent 89ade33591
commit 4233ac7b80
@@ -111,7 +111,9 @@ const defaultGroup: Condition = {
// Non-recursive function to generate LDAP filter syntax from conditions // Non-recursive function to generate LDAP filter syntax from conditions
const generateLdapFilter = (conditions: Condition[]): string => { const generateLdapFilter = (conditions: Condition[]): string => {
if (conditions.length === 0) return '(objectClass=*)'; if (!conditions || conditions.length === 0) return '(objectClass=*)';
console.log("Generating filter from conditions:", JSON.stringify(conditions));
// Helper function to generate filter for a regular condition (not a group) // Helper function to generate filter for a regular condition (not a group)
const generateRegularFilter = (condition: Condition): string => { const generateRegularFilter = (condition: Condition): string => {
@@ -143,49 +145,57 @@ const generateLdapFilter = (conditions: Condition[]): string => {
} }
}; };
// Build a map of parent IDs to their child conditions // Copy the conditions array to avoid mutation issues
const workingConditions = [...conditions];
// First, build a map of parent IDs to their child conditions
const childrenByParentId = new Map<number | null | undefined, Condition[]>(); const childrenByParentId = new Map<number | null | undefined, Condition[]>();
// Organize conditions by their parent // Organize conditions by their parent
for (const condition of conditions) { for (const condition of workingConditions) {
const parentId = condition.parentId; const parentId = condition.parentId;
if (!childrenByParentId.has(parentId)) { if (!childrenByParentId.has(parentId)) {
childrenByParentId.set(parentId, []); childrenByParentId.set(parentId, []);
} }
childrenByParentId.get(parentId)!.push(condition); childrenByParentId.get(parentId)!.push({...condition}); // Copy to avoid mutation issues
} }
// Generate filter for a group using an iterative approach // Generate filter for a group using a more robust approach
const generateGroupFilter = (parentId: number | null | undefined): string => { const generateGroupFilter = (parentId: number | null | undefined): string => {
const childConditions = childrenByParentId.get(parentId) || []; const childConditions = childrenByParentId.get(parentId) || [];
if (childConditions.length === 0) return '(objectClass=*)'; if (childConditions.length === 0) return '(objectClass=*)';
// Find the logical operator for this group // Find the group condition or use default
const logicalOp = parentId === null || parentId === undefined let groupCondition: Condition | undefined;
? 'AND' // Default for root level if (parentId !== null && parentId !== undefined) {
: conditions.find(c => c.id === parentId)?.logicalOperator || 'AND'; groupCondition = workingConditions.find(c => c.id === parentId);
}
// Get logical operator for the group
const logicalOp = groupCondition?.logicalOperator || 'AND';
const operator = logicalOp === 'AND' ? '&' : '|'; const operator = logicalOp === 'AND' ? '&' : '|';
// Initialize group filter
let groupFilter = `(${operator}`; let groupFilter = `(${operator}`;
// Track if we've added any conditions to this group // Keep track of added conditions to handle edge cases
let hasAddedCondition = false; let hasAddedCondition = false;
// Process regular conditions in this group // First process all regular conditions in this group
for (const condition of childConditions) { const regularConditions = childConditions.filter(c => !c.isGroup);
if (!condition.isGroup) { for (const condition of regularConditions) {
const filter = generateRegularFilter(condition); const filter = generateRegularFilter(condition);
if (filter) { if (filter) {
groupFilter += filter; groupFilter += filter;
hasAddedCondition = true; hasAddedCondition = true;
}
} }
} }
// Process subgroups in this group // Then process all subgroups in this group
for (const condition of childConditions) { const subgroups = childConditions.filter(c => c.isGroup && c.id !== undefined);
if (condition.isGroup && condition.id !== undefined) { for (const subgroup of subgroups) {
const subGroupFilter = generateGroupFilter(condition.id); if (subgroup.id !== undefined) {
const subGroupFilter = generateGroupFilter(subgroup.id);
if (subGroupFilter !== '(objectClass=*)') { if (subGroupFilter !== '(objectClass=*)') {
groupFilter += subGroupFilter; groupFilter += subGroupFilter;
hasAddedCondition = true; hasAddedCondition = true;
@@ -195,7 +205,7 @@ const generateLdapFilter = (conditions: Condition[]): string => {
groupFilter += ')'; groupFilter += ')';
// If there are no actual conditions, return a default // If no conditions were added, return a default filter
if (!hasAddedCondition) { if (!hasAddedCondition) {
return '(objectClass=*)'; return '(objectClass=*)';
} }
@@ -204,7 +214,9 @@ const generateLdapFilter = (conditions: Condition[]): string => {
}; };
// Start with the root level conditions // Start with the root level conditions
return generateGroupFilter(null); const result = generateGroupFilter(null);
console.log("Generated LDAP filter:", result);
return result;
}; };
// Main component (completely rewritten) // Main component (completely rewritten)
@@ -241,10 +253,13 @@ const FixedConditionBuilder: React.FC<ConditionBuilderProps> = ({
} }
}, [attributesData]); }, [attributesData]);
// Update LDAP filter when conditions change // Update LDAP filter when conditions change - use deep comparison
useEffect(() => { useEffect(() => {
setLdapFilter(generateLdapFilter(conditions)); const newFilter = generateLdapFilter(conditions);
}, [conditions]); setLdapFilter(newFilter);
// Log for debugging
console.log("Filter updated:", newFilter);
}, [JSON.stringify(conditions)]); // Use JSON.stringify for deep comparison
// Handle adding a new condition // Handle adding a new condition
const addCondition = (parentId?: number | null) => { const addCondition = (parentId?: number | null) => {