feat: Enhance group search functionality to support multiple group retrieval

This commit is contained in:
Ruben Talstra 2025-02-22 12:02:09 +01:00
parent 24fc57fd01
commit d3764fd9fe
No known key found for this signature in database
GPG key ID: 2A5A7174A60F3BEA
2 changed files with 16 additions and 18 deletions

View file

@ -17,14 +17,14 @@ const getGroupById = (groupId, fieldsToSelect = null) => {
}; };
/** /**
* Search for a single group based on partial data and return a matching group document as a plain object. * Search for a single group or multiple groups based on partial data and return them as plain objects.
* *
* @param {Partial<Object>} searchCriteria - The partial data to use for searching the group. * @param {Partial<Object>} searchCriteria - The partial data to use for searching groups.
* @param {string|string[]} [fieldsToSelect] - The fields to include or exclude in the returned document. * @param {string|string[]} [fieldsToSelect] - The fields to include or exclude in the returned documents.
* @returns {Promise<Object|null>} A plain object representing the group document, or `null` if no group is found. * @returns {Promise<Object[]>} An array of plain objects representing the group documents.
*/ */
const findGroup = (searchCriteria, fieldsToSelect = null) => { const findGroup = (searchCriteria, fieldsToSelect = null) => {
const query = Group.findOne(searchCriteria); const query = Group.find(searchCriteria);
if (fieldsToSelect) { if (fieldsToSelect) {
query.select(fieldsToSelect); query.select(fieldsToSelect);
} }

View file

@ -5,10 +5,10 @@ const { HttpsProxyAgent } = require('https-proxy-agent');
const { Issuer, Strategy: OpenIDStrategy, custom } = require('openid-client'); const { Issuer, Strategy: OpenIDStrategy, custom } = require('openid-client');
const { getStrategyFunctions } = require('~/server/services/Files/strategies'); const { getStrategyFunctions } = require('~/server/services/Files/strategies');
const { findUser, createUser, updateUser } = require('~/models/userMethods'); const { findUser, createUser, updateUser } = require('~/models/userMethods');
const { findGroup } = require('~/models/groupMethods');
const { hashToken } = require('~/server/utils/crypto'); const { hashToken } = require('~/server/utils/crypto');
const { isEnabled } = require('~/server/utils'); const { isEnabled } = require('~/server/utils');
const { logger } = require('~/config'); const { logger } = require('~/config');
const Group = require('~/models');
let crypto; let crypto;
try { try {
@ -193,30 +193,28 @@ async function setupOpenId() {
message: `You must have the "${requiredRole}" role to log in.`, message: `You must have the "${requiredRole}" role to log in.`,
}); });
} }
// Synchronize the user's groups for OpenID.
const userGroupIds = user.groups.map(id => id.toString());
if (!user.groups) {
user.groups = [];
}
// Remove existing OpenID group references. // Remove existing OpenID group references.
const currentOpenIdGroups = await Group.find({ const currentOpenIdGroups = await findGroup({
_id: { $in: userGroupIds }, _id: { $in: user.groups },
provider: 'openid', provider: 'openid',
}); });
const currentOpenIdGroupIds = new Set(currentOpenIdGroups.map(g => g._id.toString())); const currentOpenIdGroupIds = new Set(currentOpenIdGroups.map(g => g._id.toString()));
user.groups = user.groups.filter(id => !currentOpenIdGroupIds.has(id.toString())); user.groups = user.groups.filter(id => !currentOpenIdGroupIds.has(id.toString()));
// Look up groups matching the roles. // Look up groups in the Group collection matching the roles.
const matchingGroups = await Group.find({ const matchingGroups = await findGroup({
provider: 'openid', provider: 'openid',
externalId: { $in: roles }, externalId: { $in: roles },
}); });
const userGroupSet = new Set(user.groups.map(id => id.toString())); matchingGroups.forEach(group => {
for (const group of matchingGroups) { if (!user.groups.some(id => id.toString() === group._id.toString())) {
const groupIdStr = group._id.toString();
if (!userGroupSet.has(groupIdStr)) {
user.groups.push(group._id); user.groups.push(group._id);
userGroupSet.add(groupIdStr);
} }
} });
} }
let username = ''; let username = '';