From b2f1ea01d398f3f8fa94159af761c13b151bcbd1 Mon Sep 17 00:00:00 2001 From: andrei-mihnea-cerbu Date: Fri, 14 Feb 2025 12:55:04 +0200 Subject: [PATCH] modified some titles and fixed reset database I --- .../render/ceo/html/backup_retrieve.html | 4 +- .../render/ceo/html/reset_database.html | 4 +- Desktop App/render/ceo/js/reset_database.js | 10 ++++- .../render/client/html/backup_retrieve.html | 2 +- .../render/client/html/reset_database.html | 4 +- UC/src/db_managers/department_database.ts | 34 ++++------------- UC/src/db_managers/key_database.ts | 37 +++++-------------- UC/src/db_managers/user_database.ts | 28 ++++---------- .../operations_custom/ceo_operations.ts | 11 ++++-- 9 files changed, 46 insertions(+), 88 deletions(-) diff --git a/Desktop App/render/ceo/html/backup_retrieve.html b/Desktop App/render/ceo/html/backup_retrieve.html index 4d346ef..3b99ba3 100644 --- a/Desktop App/render/ceo/html/backup_retrieve.html +++ b/Desktop App/render/ceo/html/backup_retrieve.html @@ -13,7 +13,7 @@ - Welcome + Restore Backup
@@ -23,7 +23,7 @@ Backup Fetcher
-

Backup is not searched, please wait...

+

Backup is now searched, please wait...

diff --git a/Desktop App/render/ceo/html/reset_database.html b/Desktop App/render/ceo/html/reset_database.html index 059ba2c..35cd258 100644 --- a/Desktop App/render/ceo/html/reset_database.html +++ b/Desktop App/render/ceo/html/reset_database.html @@ -13,7 +13,7 @@ - UC Not Found + Reset Database
@@ -23,7 +23,7 @@ Connecting to server
-

Please wait while we reset the database.

+

Please wait while we reset the database...

diff --git a/Desktop App/render/ceo/js/reset_database.js b/Desktop App/render/ceo/js/reset_database.js index 9fdba1b..e2efaa9 100644 --- a/Desktop App/render/ceo/js/reset_database.js +++ b/Desktop App/render/ceo/js/reset_database.js @@ -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'); }); \ No newline at end of file diff --git a/Desktop App/render/client/html/backup_retrieve.html b/Desktop App/render/client/html/backup_retrieve.html index e3ae2e9..3b99ba3 100644 --- a/Desktop App/render/client/html/backup_retrieve.html +++ b/Desktop App/render/client/html/backup_retrieve.html @@ -13,7 +13,7 @@ - Welcome + Restore Backup
diff --git a/Desktop App/render/client/html/reset_database.html b/Desktop App/render/client/html/reset_database.html index db6d68f..18c94f9 100644 --- a/Desktop App/render/client/html/reset_database.html +++ b/Desktop App/render/client/html/reset_database.html @@ -14,12 +14,12 @@ - Welcome + Reset Database
-

Welcome to TeamVault

+

TeamVault

Connecting to server
diff --git a/UC/src/db_managers/department_database.ts b/UC/src/db_managers/department_database.ts index fb3474c..31f5f9f 100644 --- a/UC/src/db_managers/department_database.ts +++ b/UC/src/db_managers/department_database.ts @@ -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.' }; } } + } diff --git a/UC/src/db_managers/key_database.ts b/UC/src/db_managers/key_database.ts index ceeeba8..bee1516 100644 --- a/UC/src/db_managers/key_database.ts +++ b/UC/src/db_managers/key_database.ts @@ -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.' }; } } + } diff --git a/UC/src/db_managers/user_database.ts b/UC/src/db_managers/user_database.ts index 7e9299a..6bdfa85 100644 --- a/UC/src/db_managers/user_database.ts +++ b/UC/src/db_managers/user_database.ts @@ -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.' }; } } } diff --git a/UC/src/network/operations_custom/ceo_operations.ts b/UC/src/network/operations_custom/ceo_operations.ts index 1f46c20..f50729b 100644 --- a/UC/src/network/operations_custom/ceo_operations.ts +++ b/UC/src/network/operations_custom/ceo_operations.ts @@ -11,10 +11,13 @@ export class CeoOperations implements OperationPlugin { public static async handleResetDatabase(): Promise { 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,