const fs = require('fs'); module.exports = function(app, checkApiKey) { // Route pour supprimer un utilisateur ou un groupe d'un dépôt app.post('/action/removepermrepo', checkApiKey, async (req, res) => { try { const { repoName, userOrGroup } = req.body; if (!repoName || !userOrGroup) { return res.status(400).json({ error: "Missing required parameters" }); } await removeAuthzEntry(repoName, userOrGroup); res.json({ message: `Permissions removed successfully for ${userOrGroup} on ${repoName}` }); } catch (error) { console.error('Error in removepermrepo:', error); res.status(500).json({ error: error.message }); } }); }; // Fonction pour supprimer une entrée dans le fichier authz async function removeAuthzEntry(repoName, userOrGroup) { const authzFilePath = '/etc/apache2/dav_svn.authz'; try { if (!fs.existsSync(authzFilePath) || !fs.lstatSync(authzFilePath).isFile()) { throw new Error(`${authzFilePath} is not a valid file.`); } let data = fs.readFileSync(authzFilePath, 'utf8'); const repoSection = `[/${repoName}]`; if (!data.includes(repoSection)) { throw new Error(`Repository ${repoName} not found in authz file.`); } // Split le contenu du fichier en lignes const lines = data.split('\n'); let repoSectionIndex = lines.findIndex(line => line.trim() === repoSection); let sectionEnd = lines.slice(repoSectionIndex + 1).findIndex(line => line.startsWith('[')) + repoSectionIndex + 1; if (sectionEnd <= repoSectionIndex) sectionEnd = lines.length; // Format de l'entrée utilisateur/groupe const entryPrefix = userOrGroup.startsWith('@') ? '' : ''; const entryToRemove = `${entryPrefix}${userOrGroup} =`; // Trouve et supprime l'entrée utilisateur/groupe const entryIndex = lines.findIndex((line, index) => { return index > repoSectionIndex && index < sectionEnd && line.trim().startsWith(entryToRemove); }); if (entryIndex !== -1) { lines.splice(entryIndex, 1); // Écrit les modifications dans le fichier fs.writeFileSync(authzFilePath, lines.join('\n')); console.log(`Permissions removed for ${userOrGroup} on repository ${repoName}`); } else { throw new Error(`User or group ${userOrGroup} not found in repository ${repoName}.`); } } catch (error) { console.error('Error removing authz entry:', error); throw error; } }