mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
* chore: update package version to 0.7.416 * chore: Update Role.js imports order * refactor: move updateTagsInConvo to tags route, add RBAC for tags * refactor: add updateTagsInConvoOptions * fix: loading state for bookmark form * refactor: update primaryText class in TitleButton component * refactor: remove duplicate bookmarks and theming * refactor: update EditIcon component to use React.forwardRef * refactor: add _id field to tConversationTagSchema * refactor: remove promises * refactor: move mutation logic from BookmarkForm -> BookmarkEditDialog * refactor: update button class in BookmarkForm component * fix: conversation mutations and add better logging to useConversationTagMutation * refactor: update logger message in BookmarkEditDialog component * refactor: improve UI consistency in BookmarkNav and NewChat components * refactor: update logger message in BookmarkEditDialog component * refactor: Add tags prop to BookmarkForm component * refactor: Update BookmarkForm to avoid tag mutation if the tag already exists; also close dialog on submission programmatically * refactor: general role helper function to support updating access permissions for different permission types * refactor: Update getLatestText function to handle undefined values in message.content * refactor: Update useHasAccess hook to handle null role values for authenticated users * feat: toggle bookmarks access * refactor: Update PromptsCommand to handle access permissions for prompts * feat: updateConversationSelector * refactor: rename `vars` to `tagToDelete` for clarity * fix: prevent recreation of deleted tags in BookmarkMenu on Item Click * ci: mock updateBookmarksAccess function * ci: mock updateBookmarksAccess function
143 lines
4.3 KiB
JavaScript
143 lines
4.3 KiB
JavaScript
const {
|
|
CacheKeys,
|
|
SystemRoles,
|
|
roleDefaults,
|
|
PermissionTypes,
|
|
removeNullishValues,
|
|
promptPermissionsSchema,
|
|
bookmarkPermissionsSchema,
|
|
} = require('librechat-data-provider');
|
|
const getLogStores = require('~/cache/getLogStores');
|
|
const Role = require('~/models/schema/roleSchema');
|
|
const { logger } = require('~/config');
|
|
|
|
/**
|
|
* Retrieve a role by name and convert the found role document to a plain object.
|
|
* If the role with the given name doesn't exist and the name is a system defined role, create it and return the lean version.
|
|
*
|
|
* @param {string} roleName - The name of the role to find or create.
|
|
* @param {string|string[]} [fieldsToSelect] - The fields to include or exclude in the returned document.
|
|
* @returns {Promise<Object>} A plain object representing the role document.
|
|
*/
|
|
const getRoleByName = async function (roleName, fieldsToSelect = null) {
|
|
try {
|
|
const cache = getLogStores(CacheKeys.ROLES);
|
|
const cachedRole = await cache.get(roleName);
|
|
if (cachedRole) {
|
|
return cachedRole;
|
|
}
|
|
let query = Role.findOne({ name: roleName });
|
|
if (fieldsToSelect) {
|
|
query = query.select(fieldsToSelect);
|
|
}
|
|
let role = await query.lean().exec();
|
|
|
|
if (!role && SystemRoles[roleName]) {
|
|
role = roleDefaults[roleName];
|
|
role = await new Role(role).save();
|
|
await cache.set(roleName, role);
|
|
return role.toObject();
|
|
}
|
|
await cache.set(roleName, role);
|
|
return role;
|
|
} catch (error) {
|
|
throw new Error(`Failed to retrieve or create role: ${error.message}`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Update role values by name.
|
|
*
|
|
* @param {string} roleName - The name of the role to update.
|
|
* @param {Partial<TRole>} updates - The fields to update.
|
|
* @returns {Promise<TRole>} Updated role document.
|
|
*/
|
|
const updateRoleByName = async function (roleName, updates) {
|
|
try {
|
|
const cache = getLogStores(CacheKeys.ROLES);
|
|
const role = await Role.findOneAndUpdate(
|
|
{ name: roleName },
|
|
{ $set: updates },
|
|
{ new: true, lean: true },
|
|
)
|
|
.select('-__v')
|
|
.lean()
|
|
.exec();
|
|
await cache.set(roleName, role);
|
|
return role;
|
|
} catch (error) {
|
|
throw new Error(`Failed to update role: ${error.message}`);
|
|
}
|
|
};
|
|
|
|
const permissionSchemas = {
|
|
[PermissionTypes.PROMPTS]: promptPermissionsSchema,
|
|
[PermissionTypes.BOOKMARKS]: bookmarkPermissionsSchema,
|
|
};
|
|
|
|
/**
|
|
* Updates access permissions for a specific role and permission type.
|
|
* @param {SystemRoles} roleName - The role to update.
|
|
* @param {PermissionTypes} permissionType - The type of permission to update.
|
|
* @param {Object.<Permissions, boolean>} permissions - Permissions to update and their values.
|
|
*/
|
|
async function updateAccessPermissions(roleName, permissionType, _permissions) {
|
|
const permissions = removeNullishValues(_permissions);
|
|
if (Object.keys(permissions).length === 0) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const role = await getRoleByName(roleName);
|
|
if (!role || !permissionSchemas[permissionType]) {
|
|
return;
|
|
}
|
|
|
|
await updateRoleByName(roleName, {
|
|
[permissionType]: {
|
|
...role[permissionType],
|
|
...permissionSchemas[permissionType].partial().parse(permissions),
|
|
},
|
|
});
|
|
|
|
Object.entries(permissions).forEach(([permission, value]) =>
|
|
logger.info(
|
|
`Updated '${roleName}' role ${permissionType} '${permission}' permission to: ${value}`,
|
|
),
|
|
);
|
|
} catch (error) {
|
|
logger.error(`Failed to update ${roleName} role ${permissionType} permissions:`, error);
|
|
}
|
|
}
|
|
|
|
const updatePromptsAccess = (roleName, permissions) =>
|
|
updateAccessPermissions(roleName, PermissionTypes.PROMPTS, permissions);
|
|
|
|
const updateBookmarksAccess = (roleName, permissions) =>
|
|
updateAccessPermissions(roleName, PermissionTypes.BOOKMARKS, permissions);
|
|
|
|
/**
|
|
* Initialize default roles in the system.
|
|
* Creates the default roles (ADMIN, USER) if they don't exist in the database.
|
|
*
|
|
* @returns {Promise<void>}
|
|
*/
|
|
const initializeRoles = async function () {
|
|
const defaultRoles = [SystemRoles.ADMIN, SystemRoles.USER];
|
|
|
|
for (const roleName of defaultRoles) {
|
|
let role = await Role.findOne({ name: roleName }).select('name').lean();
|
|
if (!role) {
|
|
role = new Role(roleDefaults[roleName]);
|
|
await role.save();
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
getRoleByName,
|
|
initializeRoles,
|
|
updateRoleByName,
|
|
updatePromptsAccess,
|
|
updateBookmarksAccess,
|
|
};
|