RBAC ADDED AND LOG FILTERING

This commit is contained in:
BIG2EYEZ
2024-01-16 17:54:33 +08:00
parent 1fbfa68f1a
commit 35d8c74990
4 changed files with 242 additions and 138 deletions

View File

@ -10,6 +10,7 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="/user-creation.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
@ -92,7 +93,7 @@
<span class="details">Job Title</span>
<select name="jobTitle" id="jobTitle">
<option value="admin">Admin</option>
<option value="dataAnalyst">Data Analyst</option>
<option value="user">User</option>
</select>
</div>
</div>
@ -147,9 +148,12 @@
<div id="logsContainer" style="display: none;">
<!-- Content for logs will be added here -->
</div>
<script>
const allUsers = <%- JSON.stringify(allUsers) %>;
const currentUsername = '<%= currentUsername %>';
</script>
@ -159,6 +163,7 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/exceljs/4.2.1/exceljs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.js"></script>
<script src="inusers.js"></script>
</div>

View File

@ -59,81 +59,7 @@ $('#logsLink').on('click', function () {
fetchLogs();
});
function fetchLogs() {
// Make a fetch request to your server endpoint for logs
fetch('/api/getLogs')
.then(response => response.json())
.then(logs => {
// Process and display logs in the logs container
displayLogs(logs);
})
.catch(error => {
console.error('Error fetching logs:', error);
// Handle errors, e.g., display an alert
});
}
// Update the displayLogs function to generate a table
function displayLogs(logs) {
const logsContainer = $('#logsContainer');
// Clear previous logs
logsContainer.empty();
if (logs && logs.length > 0) {
// Create the table and header row
const table = $('<table>').addClass('logs-table');
const headerRow = '<tr><th>ID</th><th>Username</th><th>Activity</th><th>Timestamp</th></tr>';
table.append(headerRow);
// Add each log as a row in the table
logs.forEach(log => {
const row = `<tr><td>${log.id}</td><td>${log.username}</td><td>${log.activity}</td><td>${log.timestamp}</td></tr>`;
table.append(row);
});
// Append the table to the logsContainer
logsContainer.append(table);
// Add a download button at the top with the current date and time in the file name
const currentDate = new Date();
const formattedDate = currentDate.toISOString().split('T')[0];
const formattedTime = currentDate.toTimeString().split(' ')[0].replace(/:/g, '-');
const downloadButton = $('<button>').text('Download Log').on('click', function () {
downloadLogs(logs, `log_${formattedDate}_${formattedTime}.csv`);
});
// Prepend the download button to the logsContainer
logsContainer.prepend(downloadButton);
} else {
// Display a message if no logs are available
logsContainer.html('<p>No logs available.</p>');
}
}
function downloadLogs(logs, filename) {
if (logs && logs.length > 0) {
const csvContent = 'data:text/csv;charset=utf-8,';
const header = 'ID,Username,Activity,Timestamp\n';
const rows = logs.map(log => `${log.id},${log.username},${log.activity},"${log.timestamp}"`).join('\n');
const data = header + rows;
const encodedData = encodeURI(csvContent + data);
// Create a hidden anchor element to trigger the download
const link = document.createElement('a');
link.setAttribute('href', encodedData);
link.setAttribute('download', 'logs.csv');
document.body.appendChild(link);
// Trigger the download
link.click();
// Remove the link from the DOM
document.body.removeChild(link);
} else {
console.error('No logs available for download.');
}
}
});
function searchUser(username) {
@ -321,7 +247,7 @@ function resetFormFields() {
}),
})
.then(response => {
if (response.status === 201) {
if (response.ok) {
// Status 201 indicates successful creation
return response.json();
} else {
@ -436,6 +362,158 @@ $('#resetPasswordForm').on('submit', function (e) {
});
});
// Declare a variable to store fetched logs
let logs = [];
// Function to fetch logs from the server
function fetchLogs() {
// Make a fetch request to your server endpoint for logs
fetch('/api/getLogs')
.then(response => response.json())
.then(data => {
// Assign the logs to the variable
logs = data;
// Process and display logs in the logs container
displayLogs(logs);
})
.catch(error => {
console.error('Error fetching logs:', error);
// Handle errors, e.g., display an alert
});
}
// Update the displayLogs function to generate a table
function displayLogs(logs) {
const logsContainer = $('#logsContainer');
// Clear previous logs and date filter elements
logsContainer.empty();
if (logs && logs.length > 0) {
// Add date filter elements
logsContainer.append(`
<label for="datePicker">Filter by Date:</label>
<input type="text" id="datePicker">
<button onclick="applyDateFilter()">Apply Filter</button>
`);
// Create the table and header row
const table = $('<table>').addClass('logs-table');
const headerRow = '<tr><th>ID</th><th>Username</th><th>Activity</th><th>Timestamp</th></tr>';
table.append(headerRow);
// Add each log as a row in the table
logs.forEach(log => {
const row = `<tr><td>${log.id}</td><td>${log.username}</td><td>${log.activity}</td><td>${log.timestamp}</td></tr>`;
table.append(row);
});
// Append the table to the logsContainer
logsContainer.append(table);
// Add a download button at the top with the current date and time in the file name
const currentDate = new Date();
const formattedDate = currentDate.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
});
const formattedTime = currentDate.toTimeString().split(' ')[0].replace(/:/g, '-');
const downloadButton = $('<button>').text('Download Log').on('click', function () {
downloadLogs(logs, `log_${formattedDate}_${formattedTime}.csv`);
});
// Prepend the download button to the logsContainer
logsContainer.prepend(downloadButton);
} else {
// Display a message if no logs are available
logsContainer.html('<p>No logs available.</p>');
}
// Initialize Flatpickr for the date picker
flatpickr("#datePicker", {
dateFormat: "m/d/Y, h:i:S K", // Adjust the format to match your logs timestamp format
});
}
// Function to apply date filter
function applyDateFilter() {
const selectedDate = $("#datePicker").val();
const formattedSelectedDate = new Date(selectedDate).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
});
const filteredLogs = logs.filter(log => {
const formattedLogDate = new Date(log.timestamp).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
});
return formattedLogDate === formattedSelectedDate;
});
displayLogs(filteredLogs);
}
function downloadLogs(logs, filename) {
if (logs && logs.length > 0) {
const csvContent = 'data:text/csv;charset=utf-8,';
const header = 'ID,Username,Activity,Timestamp\n';
const rows = logs.map(log => `${log.id},${log.username},${log.activity},"${log.timestamp}"`).join('\n');
const data = header + rows;
const encodedData = encodeURI(csvContent + data);
// Create a hidden anchor element to trigger the download
const link = document.createElement('a');
link.setAttribute('href', encodedData);
link.setAttribute('download', 'logs.csv');
document.body.appendChild(link);
// Trigger the download
link.click();
// Remove the link from the DOM
document.body.removeChild(link);
} else {
console.error('No logs available for download.');
}
}
fetchLogs();
// Assuming EJS is properly configured to evaluate expressions
// Assuming allUsers is an array containing user information
const user = allUsers.find(user => user.username === currentUsername);
const userRole = user?.jobTitle;
console.log('All Users:', allUsers);
console.log('Current Username:', currentUsername);
// Log the user role to the console
console.log('User Role:', userRole);
// Function to enable/disable actions based on user role
function handleUserRoleAccess() {
// Disable user creation, deletion, and password reset for non-admin users
if (userRole !== 'admin') {
document.getElementById('addUserLink').style.display = 'none';
document.getElementById('deleteUserLink').style.display = 'none';
document.getElementById('resetPasswordLink').style.display = 'none';
}
// Allow admin users to view logs
if (userRole === 'admin') {
document.getElementById('logsLink').classList.remove('hidden');
}
}
// Call the function to handle user role access when the page loads
handleUserRoleAccess();