modified some titles and fixed reset database I
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
<link href="https://fonts.googleapis.com/css2?family=Pridi:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">
|
||||
|
||||
<script src="../js/helpers.js"></script>
|
||||
<title>Welcome</title>
|
||||
<title>Restore Backup</title>
|
||||
</head>
|
||||
<body onload="fadeIn()">
|
||||
<div class="container">
|
||||
@@ -23,7 +23,7 @@
|
||||
<img src="../assets/refresh-data.webp" alt="Backup Fetcher" class="backup-img">
|
||||
<div class="loading-section">
|
||||
<div class="spinner"></div>
|
||||
<p>Backup is not searched, please wait...</p>
|
||||
<p>Backup is now searched, please wait...</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
<script src="../js/helpers.js"></script>
|
||||
<script src="../js/reset_database.js"></script>
|
||||
<title>UC Not Found</title>
|
||||
<title>Reset Database</title>
|
||||
</head>
|
||||
<body onload="fadeIn()">
|
||||
<div class="container">
|
||||
@@ -23,7 +23,7 @@
|
||||
<img src="../assets/2581896.png" style="margin-left: 20px" alt="Connecting to server" class="server-img">
|
||||
<div class="loading-section">
|
||||
<div class="spinner"></div>
|
||||
<p>Please wait while we reset the database.</p>
|
||||
<p>Please wait while we reset the database...</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@@ -11,21 +11,27 @@ document.addEventListener('DOMContentLoaded', async function () {
|
||||
// Open a TCP socket to the stored IP
|
||||
if (!await window.networkAPI.openUcSocket()) {
|
||||
await window.uiAPI.showAlert('Internal error of the application. Unable to open socket.');
|
||||
await window.uiAPI.changeContent('main_menu');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!await window.networkAPI.sendUcMessage(codeResetDatabase)) {
|
||||
await window.uiAPI.showAlert('Failed to send login request.');
|
||||
await window.uiAPI.changeContent('main_menu');
|
||||
await window.networkAPI.closeUcSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await waitForResponse();
|
||||
if (response && response.operationCode !== codeOk) {
|
||||
await window.uiAPI.showAlert('Database reset failed.');
|
||||
await window.uiAPI.changeContent('main_menu');
|
||||
await window.networkAPI.closeUcSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
await window.networkAPI.closeUcSocket();
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
fadeOut('login');
|
||||
|
||||
await window.databaseAPI.resetInternalDatabase();
|
||||
await window.uiAPI.changeContent('welcome');
|
||||
});
|
||||
@@ -13,7 +13,7 @@
|
||||
<link href="https://fonts.googleapis.com/css2?family=Pridi:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">
|
||||
|
||||
<script src="../js/helpers.js"></script>
|
||||
<title>Welcome</title>
|
||||
<title>Restore Backup</title>
|
||||
</head>
|
||||
<body onload="fadeIn()">
|
||||
<div class="container">
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
|
||||
<script src="../js/helpers.js"></script>
|
||||
<script src="../js/reset_database.js"></script>
|
||||
<title>Welcome</title>
|
||||
<title>Reset Database</title>
|
||||
</head>
|
||||
<body onload="fadeIn()">
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1 class="title">Welcome to TeamVault</h1>
|
||||
<h1 class="title">TeamVault</h1>
|
||||
</div>
|
||||
<img src="../assets/2581896.png" style="margin-left: 20px" alt="Connecting to server" class="server-img">
|
||||
<div class="loading-section">
|
||||
|
||||
@@ -15,7 +15,7 @@ export class DepartmentDatabase {
|
||||
}
|
||||
|
||||
// Create the table if it doesn't exist
|
||||
private createTableIfNotExists() {
|
||||
public createTableIfNotExists() {
|
||||
const createTableQuery = `
|
||||
CREATE TABLE IF NOT EXISTS departments (
|
||||
id TEXT PRIMARY KEY,
|
||||
@@ -25,7 +25,6 @@ export class DepartmentDatabase {
|
||||
this.db.exec(createTableQuery);
|
||||
|
||||
const ceoDepartment = this.db.prepare('SELECT COUNT(*) as count FROM departments WHERE name = ?').get('CEO').count;
|
||||
const defaultDepartment = this.db.prepare('SELECT COUNT(*) as count FROM departments WHERE name = ?').get('Worker').count;
|
||||
|
||||
// Create CEO department if it doesn't exist
|
||||
if (ceoDepartment === 0) {
|
||||
@@ -35,13 +34,6 @@ export class DepartmentDatabase {
|
||||
console.log('CEO department already exists.');
|
||||
}
|
||||
|
||||
if (defaultDepartment === 0) {
|
||||
this.createDepartment('Worker');
|
||||
console.log('Created Worker department.');
|
||||
} else {
|
||||
console.log('Worker department already exists.');
|
||||
}
|
||||
|
||||
console.log('Departments table checked/created.');
|
||||
}
|
||||
|
||||
@@ -92,25 +84,15 @@ export class DepartmentDatabase {
|
||||
return stmt.all();
|
||||
}
|
||||
|
||||
public cleanTable(): { success: boolean; message: string } {
|
||||
public dropTable(): { success: boolean; message: string } {
|
||||
try {
|
||||
// Fetch the CEO department ID
|
||||
const ceoDepartmentRow = this.db.prepare(`SELECT id FROM departments WHERE LOWER(name) = 'ceo'`).get();
|
||||
if (!ceoDepartmentRow) {
|
||||
console.error('CEO department not found in the database.');
|
||||
return { success: false, message: 'Failed to find CEO department.' };
|
||||
}
|
||||
|
||||
const ceoDepartmentId = ceoDepartmentRow.id;
|
||||
|
||||
// Delete all departments except the CEO department
|
||||
const deleteDepartmentsStmt = this.db.prepare(`DELETE FROM departments WHERE id != ?`);
|
||||
deleteDepartmentsStmt.run(ceoDepartmentId);
|
||||
|
||||
return { success: true, message: 'All non-CEO departments have been deleted.' };
|
||||
this.db.exec('DROP TABLE IF EXISTS departments;');
|
||||
console.log('Departments table dropped successfully.');
|
||||
return { success: true, message: 'Departments table dropped successfully.' };
|
||||
} catch (error) {
|
||||
console.error('Error while cleaning the departments table:', error);
|
||||
return { success: false, message: 'Failed to clean departments table.' };
|
||||
console.error('Error while dropping the departments table:', error);
|
||||
return { success: false, message: 'Failed to drop departments table.' };
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,19 +14,16 @@ export class KeyDatabase {
|
||||
private db: any;
|
||||
|
||||
constructor() {
|
||||
// Get the singleton instance of the database
|
||||
this.db = SQLiteDatabase.getInstance();
|
||||
|
||||
// Create the keys table if it doesn't exist
|
||||
this.createTableIfNotExists();
|
||||
}
|
||||
|
||||
// Create the table if it doesn't exist
|
||||
private createTableIfNotExists() {
|
||||
public createTableIfNotExists() {
|
||||
const createTableQuery = `
|
||||
CREATE TABLE IF NOT EXISTS user_keys (
|
||||
id TEXT PRIMARY KEY,
|
||||
userId TEXT UNIQUE NOT NULL, -- Ensure one key per user
|
||||
userId TEXT UNIQUE NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
iv TEXT NOT NULL,
|
||||
FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
|
||||
@@ -134,31 +131,15 @@ export class KeyDatabase {
|
||||
return stmt.all();
|
||||
}
|
||||
|
||||
// Clean the table but keep CEO keys
|
||||
public cleanTable(): { success: boolean; message: string } {
|
||||
public dropTable(): { success: boolean; message: string } {
|
||||
try {
|
||||
const departments = departmentDatabase.getAllDepartments();
|
||||
const ceoDepartment = departments.find(dept => dept.name.toLowerCase() === 'ceo');
|
||||
|
||||
if (!ceoDepartment) {
|
||||
console.error('CEO department not found in the database.');
|
||||
return { success: false, message: 'Failed to find CEO department.' };
|
||||
}
|
||||
|
||||
// Fetch all users in the CEO department
|
||||
const ceoUsers = userDatabase.findByDepartmentId(ceoDepartment.id);
|
||||
const ceoUserIds = ceoUsers.map(user => user.id);
|
||||
|
||||
// Delete all keys except for the CEO users
|
||||
const stmt = this.db.prepare(`
|
||||
DELETE FROM user_keys WHERE userId NOT IN (${ceoUserIds.map(() => '?').join(', ')})
|
||||
`);
|
||||
stmt.run(...ceoUserIds);
|
||||
|
||||
return { success: true, message: 'All non-CEO keys have been deleted.' };
|
||||
this.db.exec('DROP TABLE IF EXISTS user_keys;');
|
||||
console.log('user_keys table dropped successfully.');
|
||||
return { success: true, message: 'user_keys table dropped successfully.' };
|
||||
} catch (error) {
|
||||
console.error('Error while cleaning the keys table:', error);
|
||||
return { success: false, message: 'Failed to clean keys table.' };
|
||||
console.error('Error while dropping the user_keys table:', error);
|
||||
return { success: false, message: 'Failed to drop user_keys table.' };
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ export class UserDatabase {
|
||||
}
|
||||
|
||||
// Create the table if it doesn't exist
|
||||
private createTableIfNotExists() {
|
||||
public createTableIfNotExists() {
|
||||
const createTableQuery = `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY,
|
||||
@@ -209,28 +209,14 @@ export class UserDatabase {
|
||||
return pbkdf2Sync(password, salt, 1000, 64, 'sha256').toString('hex');
|
||||
}
|
||||
|
||||
public cleanTable(): { success: boolean; message: string } {
|
||||
public dropTable(): { success: boolean; message: string } {
|
||||
try {
|
||||
// Fetch departments
|
||||
const departments = departmentDatabase.getAllDepartments();
|
||||
|
||||
const ceoDepartment = departments.find(dept => dept.name.toLowerCase() === 'ceo');
|
||||
|
||||
if (!ceoDepartment) {
|
||||
console.error('Admin or CEO department not found in the database.');
|
||||
return { success: false, message: 'Failed to find Admin or CEO department.' };
|
||||
}
|
||||
|
||||
// Delete users from the table except for those in the Admin and CEO departments
|
||||
const stmt = this.db.prepare(`
|
||||
DELETE FROM users WHERE departmentId NOT IN (?, ?)
|
||||
`);
|
||||
stmt.run(ceoDepartment.id);
|
||||
|
||||
return { success: true, message: 'All users except Admin and CEO have been deleted.' };
|
||||
this.db.exec('DROP TABLE IF EXISTS users;');
|
||||
console.log('Users table dropped successfully.');
|
||||
return { success: true, message: 'Users table dropped successfully.' };
|
||||
} catch (error) {
|
||||
console.error('Error while cleaning the users table:', error);
|
||||
return { success: false, message: 'Failed to clean users table.' };
|
||||
console.error('Error while dropping the users table:', error);
|
||||
return { success: false, message: 'Failed to drop users table.' };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,13 @@ export class CeoOperations implements OperationPlugin {
|
||||
|
||||
public static async handleResetDatabase(): Promise<ParsedMessage> {
|
||||
console.log('Resetting the database...');
|
||||
departmentDatabase.cleanTable();
|
||||
userDatabase.cleanTable();
|
||||
keyDatabase.cleanTable();
|
||||
departmentDatabase.createDepartment('Worker');
|
||||
keyDatabase.dropTable();
|
||||
userDatabase.dropTable();
|
||||
departmentDatabase.dropTable();
|
||||
|
||||
departmentDatabase.createTableIfNotExists();
|
||||
userDatabase.createTableIfNotExists();
|
||||
keyDatabase.createTableIfNotExists();
|
||||
|
||||
return {
|
||||
operationCode: operationCodes.OK,
|
||||
|
||||
Reference in New Issue
Block a user