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 {string|string[]} [fieldsToSelect] - The fields to include or exclude in the returned document.
* @returns {Promise<Object|null>} A plain object representing the group document, or `null` if no group is found.
* @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 documents.
* @returns {Promise<Object[]>} An array of plain objects representing the group documents.
*/
const findGroup = (searchCriteria, fieldsToSelect = null) => {
const query = Group.findOne(searchCriteria);
const query = Group.find(searchCriteria);
if (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 { getStrategyFunctions } = require('~/server/services/Files/strategies');
const { findUser, createUser, updateUser } = require('~/models/userMethods');
const { findGroup } = require('~/models/groupMethods');
const { hashToken } = require('~/server/utils/crypto');
const { isEnabled } = require('~/server/utils');
const { logger } = require('~/config');
const Group = require('~/models');
let crypto;
try {
@ -193,30 +193,28 @@ async function setupOpenId() {
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.
const currentOpenIdGroups = await Group.find({
_id: { $in: userGroupIds },
const currentOpenIdGroups = await findGroup({
_id: { $in: user.groups },
provider: 'openid',
});
const currentOpenIdGroupIds = new Set(currentOpenIdGroups.map(g => g._id.toString()));
user.groups = user.groups.filter(id => !currentOpenIdGroupIds.has(id.toString()));
// Look up groups matching the roles.
const matchingGroups = await Group.find({
// Look up groups in the Group collection matching the roles.
const matchingGroups = await findGroup({
provider: 'openid',
externalId: { $in: roles },
});
const userGroupSet = new Set(user.groups.map(id => id.toString()));
for (const group of matchingGroups) {
const groupIdStr = group._id.toString();
if (!userGroupSet.has(groupIdStr)) {
matchingGroups.forEach(group => {
if (!user.groups.some(id => id.toString() === group._id.toString())) {
user.groups.push(group._id);
userGroupSet.add(groupIdStr);
}
}
});
}
let username = '';