Add admin directory management

This commit is contained in:
Christopher Cookman 2024-12-15 06:13:09 -07:00
parent 6d8411acdb
commit c99ecbf8d3
3 changed files with 93 additions and 17 deletions

View file

@ -27,7 +27,7 @@ db.get("SELECT * FROM users WHERE id = 1", [], (err, row) => {
console.error('Error checking for admin user:', err);
return;
}
if (!row || process.env.RESET_ADMIN == true) {
if (!row || process.env.RESET_ADMIN == "true") {
// Destroy all sessions
sessionStore.clear((err) => {
if (err) {
@ -289,6 +289,26 @@ app.delete('/api/v1/admin/route/:id', (req, res) => { // Delete a route
});
});
app.delete('/api/v1/admin/directory/:number', (req, res) => { // Delete a directory entry
if (!req.session.adminAuthenticated) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const number = Number(req.params.number);
if (!number) {
res.status(400).json({ error: 'Bad Request' });
return;
}
db.run('DELETE FROM directory WHERE number = ?', [number], (err) => {
if (err) {
console.error('Error deleting directory entry:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
res.status(200).json({ message: 'Deleted' });
});
});
// == END ADMIN ROUTES ==
// == User routes == // allows someone to log in with their API key and add entries to the Directory (as long as the number is within their block range)

View file

@ -0,0 +1,30 @@
function getDirectoryEntries() {
fetch('/api/v1/directory')
.then(response => response.json())
.then(data => {
const table = document.getElementById('directoryList');
data.forEach(entry => {
console.log(entry)
const row = document.createElement('tr');
row.innerHTML = `<td>${entry.number}</td><td>${entry.name}</td><td><button class="btn btn-danger" onclick="deleteDirectoryEntry(${entry.number})">Delete</button></td>`;
table.appendChild(row);
});
})
.catch(error => console.error('Error fetching directory:', error));
}
document.addEventListener('DOMContentLoaded', function () {
getDirectoryEntries();
});
function deleteDirectoryEntry(number) {
fetch(`/api/v1/admin/directory/${number}`, {
method: 'DELETE'
})
.then(response => {
if (response.ok) {
getDirectoryEntries();
}
})
.catch(error => console.error('Error deleting directory entry:', error));
}

View file

@ -22,22 +22,48 @@
<h2 class="m-0">Admin Dashboard</h2>
<a href="/admin/create" class="btn btn-primary">Create New Server</a>
</div>
<table class="table table-striped table-dark" id="adminTable">
<thead>
<tr>
<th>ID</th>
<th>Hostname:Port</th>
<th>IAX Username/Context</th>
<th>IAX2 Secret</th>
<th>Number Block</th>
<th>API Key</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Data will be dynamically populated by adminMain.js -->
</tbody>
</table>
<div class="d-flex">
<div class="flex-grow-1">
<table class="table table-striped table-dark" id="adminTable">
<thead>
<tr>
<th>ID</th>
<th>Hostname:Port</th>
<th>IAX Username/Context</th>
<th>IAX2 Secret</th>
<th>Number Block</th>
<th>API Key</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Data will be dynamically populated by adminMain.js -->
</tbody>
</table>
</div>
<div class="d-flex">
<div class="flex-grow-1">
<!-- Original table is already in place above -->
</div>
<div class="ms-3 flex-grow-1">
<div class="d-flex align-items-center gap-3 mb-3">
<h2 class="m-0">Directory Management</h2>
</div>
<table class="table table-striped table-dark" id="directoryTable">
<thead>
<tr>
<th>Number</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="directoryList">
<!-- Data will be dynamically populated by adminMain.js -->
</tbody>
</table>
</div>
</div>
<script src="/assets/js/adminDirectory.js"></script>
<script src="/assets/js/adminMain.js"></script>
<script src="/assets/js/bootstrap.min.js"></script>
<script src="/assets/js/bootstrap.bundle.min.js"></script>