From dd762f7223946f11be87a9a0c2f2776c791cdb58 Mon Sep 17 00:00:00 2001 From: Ruben Talstra Date: Sat, 22 Feb 2025 11:00:15 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Refactor=20Group=20Assignme?= =?UTF-8?q?nt=20and=20Creation=20Logic=20with=20Enhanced=20Input=20Handlin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/assign-group.js | 17 +++++++++---- config/create-group.js | 58 ++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/config/assign-group.js b/config/assign-group.js index d6f407f944..56181bca15 100644 --- a/config/assign-group.js +++ b/config/assign-group.js @@ -13,8 +13,8 @@ const connect = require('./connect'); console.purple('---------------------------------------'); // Read arguments from CLI or prompt the user - let userEmail = process.argv[2] || (await askQuestion('User email: ')); - let groupName = process.argv[3] || (await askQuestion('Group name to assign: ')); + const userEmail = process.argv[2] || (await askQuestion('User email: ')); + const groupName = process.argv[3] || (await askQuestion('Group name to assign: ')); // Validate email format if (!userEmail.includes('@')) { @@ -39,19 +39,26 @@ const connect = require('./connect'); // Assign the group to the user if not already assigned try { - if (!user.groups) { + if (!Array.isArray(user.groups)) { user.groups = []; } - if (!user.groups.includes(group._id)) { + + // Convert both user group IDs and the target group ID to strings for comparison + const groupIdStr = group._id.toString(); + const userGroupIds = user.groups.map(id => id.toString()); + + if (!userGroupIds.includes(groupIdStr)) { user.groups.push(group._id); await user.save(); + console.green(`User ${user.email} successfully assigned to group ${group.name}!`); + } else { + console.yellow(`User ${user.email} is already assigned to group ${group.name}.`); } } catch (error) { console.red('Error assigning group to user: ' + error.message); silentExit(1); } - console.green(`User ${user.email} successfully assigned to group ${group.name}!`); silentExit(0); })(); diff --git a/config/create-group.js b/config/create-group.js index 5a70fb08bc..6d7ff8d802 100644 --- a/config/create-group.js +++ b/config/create-group.js @@ -11,40 +11,42 @@ const connect = require('./connect'); console.purple('Create a New Group'); console.purple('---------------------------------------'); - // Read arguments from CLI or prompt the user - let groupName = process.argv[2] || (await askQuestion('Group name: ')); - let groupDescription = + // Prompt for basic group info. + const groupName = process.argv[2] || (await askQuestion('Group name: ')); + const groupDescription = process.argv[3] || (await askQuestion('Group description (optional): ')); - let allowedEndpointsInput = - process.argv[4] || - (await askQuestion( - 'Allowed endpoints (comma separated, e.g., "assistants,agents", or enter "*" for all): ', - )); - let allowedModelsInput = - process.argv[5] || - (await askQuestion( - 'Allowed models (comma separated, e.g., "gpt-4,chatgpt-4o-latest", or enter "*" for all): ', - )); - // Process the comma-separated inputs into arrays - const allowedEndpoints = allowedEndpointsInput - .split(',') - .map((s) => s.trim()) - .filter((s) => s); - const allowedModels = allowedModelsInput - .split(',') - .map((s) => s.trim()) - .filter((s) => s); + // Ask for the group type (local or openid; defaults to local) + let groupType = + process.argv[4] || + (await askQuestion('Group type (local/openid, default is local): ')); + groupType = groupType.trim().toLowerCase() || 'local'; + + let groupData; + if (groupType === 'openid') { + // For OpenID groups, prompt for an external ID. + const externalId = + process.argv[5] || + (await askQuestion('External ID for OpenID group: ')); + groupData = { + name: groupName, + description: groupDescription, + provider: 'openid', + externalId: externalId.trim(), + }; + } else { + // For local groups, we only need name and description. + groupData = { + name: groupName, + description: groupDescription, + provider: 'local', + }; + } // Create the group document let group; try { - group = await Group.create({ - name: groupName, - description: groupDescription, - allowedEndpoints, - allowedModels, - }); + group = await Group.create(groupData); } catch (error) { console.red('Error creating group: ' + error.message); silentExit(1);