Allow server admins to grant server admin access to other users

This commit is contained in:
carlosmonastyrski
2025-03-04 12:38:27 -03:00
parent e5bca5b5df
commit be4adc2759
5 changed files with 134 additions and 24 deletions
@@ -211,6 +211,27 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
}
});
server.route({
method: "PATCH",
url: "/user-management/users/grant-admin-access",
config: {
rateLimit: writeLimit
},
schema: {
body: z.object({
userId: z.string()
})
},
onRequest: (req, res, done) => {
verifyAuth([AuthMode.JWT])(req, res, () => {
verifySuperAdmin(req, res, done);
});
},
handler: async (req) => {
await server.services.superAdmin.grantServerAdminAccessToUser(req.body.userId);
}
});
server.route({
method: "GET",
url: "/encryption-strategies",
@@ -291,6 +291,18 @@ export const superAdminServiceFactory = ({
return user;
};
const grantServerAdminAccessToUser = async (userId: string) => {
if (!licenseService.onPremFeatures?.instanceUserManagement) {
throw new BadRequestError({
message: "Failed to grant server admin access to user due to plan restriction. Upgrade to Infisical's Pro plan."
});
}
await userDAL.transaction(async (tx) => {
await userDAL.updateById(userId, { superAdmin: true }, tx);
});
};
const getAdminSlackConfig = async () => {
const serverCfg = await serverCfgDAL.findById(ADMIN_CONFIG_DB_UUID);
@@ -381,6 +393,7 @@ export const superAdminServiceFactory = ({
deleteUser,
getAdminSlackConfig,
updateRootEncryptionStrategy,
getConfiguredEncryptionStrategies
getConfiguredEncryptionStrategies,
grantServerAdminAccessToUser
};
};