🧹 chore: Move direct model usage from PermissionsController to data-schemas
Some checks are pending
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Waiting to run
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Waiting to run

This commit is contained in:
Danny Avila 2026-03-21 15:20:15 -04:00
parent a78865b5e0
commit 04e65bb21a
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
3 changed files with 47 additions and 80 deletions

View file

@ -15,19 +15,11 @@ const {
ensurePrincipalExists,
getAvailableRoles,
} = require('~/server/services/PermissionService');
const {
searchPrincipals: searchLocalPrincipals,
sortPrincipalsByRelevance,
calculateRelevanceScore,
findRoleByIdentifier,
aggregateAclEntries,
bulkWriteAclEntries,
} = require('~/models');
const {
entraIdPrincipalFeatureEnabled,
searchEntraIdPrincipals,
} = require('~/server/services/GraphApiService');
const { Agent, AclEntry, AccessRole, User } = require('~/db/models');
const db = require('~/models');
/**
* Generic controller for resource permission endpoints
@ -46,28 +38,6 @@ const validateResourceType = (resourceType) => {
}
};
/**
* Removes an agent from the favorites of specified users (fire-and-forget).
* Both AGENT and REMOTE_AGENT resource types share the Agent collection.
* @param {string} resourceId - The agent's MongoDB ObjectId hex string
* @param {string[]} userIds - User ObjectId strings whose favorites should be cleaned
*/
const removeRevokedAgentFromFavorites = (resourceId, userIds) =>
Agent.findOne({ _id: resourceId }, { id: 1 })
.lean()
.then((agent) => {
if (!agent) {
return;
}
return User.updateMany(
{ _id: { $in: userIds }, 'favorites.agentId': agent.id },
{ $pull: { favorites: { agentId: agent.id } } },
);
})
.catch((err) => {
logger.error('[removeRevokedAgentFromFavorites] Error cleaning up favorites', err);
});
/**
* Bulk update permissions for a resource (grant, update, remove)
* @route PUT /api/{resourceType}/{resourceId}/permissions
@ -187,7 +157,9 @@ const updateResourcePermissions = async (req, res) => {
.map((p) => p.id);
if (isAgentResource && revokedUserIds.length > 0) {
removeRevokedAgentFromFavorites(resourceId, revokedUserIds);
db.removeAgentFromUserFavorites(resourceId, revokedUserIds).catch((err) => {
logger.error('[removeRevokedAgentFromFavorites] Error cleaning up favorites', err);
});
}
/** @type {TUpdateResourcePermissionsResponse} */
@ -220,7 +192,7 @@ const getResourcePermissions = async (req, res) => {
const { resourceType, resourceId } = req.params;
validateResourceType(resourceType);
const results = await aggregateAclEntries([
const results = await db.aggregateAclEntries([
// Match ACL entries for this resource
{
$match: {
@ -317,9 +289,9 @@ const getResourcePermissions = async (req, res) => {
if (resourceType === ResourceType.REMOTE_AGENT) {
const enricherDeps = {
aggregateAclEntries,
bulkWriteAclEntries,
findRoleByIdentifier,
aggregateAclEntries: db.aggregateAclEntries,
bulkWriteAclEntries: db.bulkWriteAclEntries,
findRoleByIdentifier: db.findRoleByIdentifier,
logger,
};
const enrichResult = await enrichRemoteAgentPrincipals(enricherDeps, resourceId, principals);
@ -438,7 +410,7 @@ const searchPrincipals = async (req, res) => {
typeFilters = validTypes.length > 0 ? validTypes : null;
}
const localResults = await searchLocalPrincipals(query.trim(), searchLimit, typeFilters);
const localResults = await db.searchPrincipals(query.trim(), searchLimit, typeFilters);
let allPrincipals = [...localResults];
const useEntraId = entraIdPrincipalFeatureEnabled(req.user);
@ -494,10 +466,11 @@ const searchPrincipals = async (req, res) => {
}
const scoredResults = allPrincipals.map((item) => ({
...item,
_searchScore: calculateRelevanceScore(item, query.trim()),
_searchScore: db.calculateRelevanceScore(item, query.trim()),
}));
const finalResults = sortPrincipalsByRelevance(scoredResults)
const finalResults = db
.sortPrincipalsByRelevance(scoredResults)
.slice(0, searchLimit)
.map((result) => {
const { _searchScore, ...resultWithoutScore } = result;