generated from sfuad001/Project-CS180
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_layer_records.cpp
150 lines (129 loc) · 4.56 KB
/
data_layer_records.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "data_layer_records.h"
#include "json.hpp"
#include <algorithm>
#include <bitset>
#include <cctype>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using json = nlohmann::json;
using namespace std;
// Modify File Output to prevent modification
string generateSignatureForRecord(const Record &record) {
string str = to_string(record.id) + record.tableName + record.data +
record.creator + record.timestamp + record.last_modified +
record.last_read + record.encryptionType + record.signature +
to_string(record.state);
hash<string> hasher;
size_t hash = hasher(str);
return to_string(hash);
}
vector<Record> loadRecords() {
vector<Record> records;
ifstream ifs("records.json");
if (ifs.is_open()) {
json j_records;
ifs >> j_records;
for (const auto &j_record : j_records) {
Record record;
record.id = j_record["id"];
record.tableID = j_record["tableID"];
record.tableName = j_record["tableName"];
record.data = j_record["data"];
record.creator = j_record["creator"];
record.timestamp = j_record["timestamp"];
record.last_modified = j_record["last_modified"];
record.last_read = j_record["last_read"];
record.encryptionType = j_record["encryptionType"];
record.signature = j_record["signature"];
record.jsonSignature = j_record["jsonSignature"];
record.state = j_record["state"].get<int>();
// Recalculate the signature
string recalculatedSignature = generateSignatureForRecord(record);
if (recalculatedSignature != record.jsonSignature) {
cerr << "Warning: Data integrity issue detected for record id "
<< record.id << "\n";
record.state = 2;
}
records.push_back(record);
}
ifs.close();
}
return records;
}
void saveRecords(const vector<Record> &records) {
json j_records = json::array();
for (const Record &record : records) {
string jsonSignature = generateSignatureForRecord(record);
json j_record = {{"id", record.id},
{"tableID", record.tableID},
{"tableName", record.tableName},
{"data", record.data},
{"creator", record.creator},
{"timestamp", record.timestamp},
{"last_modified", record.last_modified},
{"last_read", record.last_read},
{"encryptionType", record.encryptionType},
{"signature", record.signature},
{"jsonSignature", jsonSignature},
{"state", record.state}};
j_records.push_back(j_record);
}
ofstream ofs("records.json");
ofs << j_records.dump(4) << endl;
ofs.close();
}
int getNextId(const vector<Record> &records) {
int maxId = 0;
for (const auto &record : records) {
if (record.id > maxId) {
maxId = record.id;
}
}
return maxId + 1;
}
string currentDateTime() {
time_t now = time(0);
tm *localtm = localtime(&now);
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtm);
return string(buffer);
}
// EXPORT FUNCTION (The only way user download their data back, currently only
// table wise)
void outputTableToJson(const vector<Record> &records, const string &tableName,
const User ¤tUser, int tableID) {
vector<Record> tableRecords = filterByTableID(records, tableID);
if (tableRecords.empty()) {
cout << "No records found for table with ID: " << tableID << endl;
return;
}
if (tableRecords[0].creator != currentUser.username &&
!currentUser.isManager) {
cout << "You do not have the right to export this table. Only the owner can." << endl;
return;
}
json j_records = json::array();
for (const Record &record : tableRecords) {
json j_record = {{"id", record.id},
{"tableID", record.tableID},
{"tableName", record.tableName},
{"data", record.data},
{"creator", record.creator},
{"timestamp", record.timestamp},
{"last_modified", record.last_modified},
{"last_read", record.last_read},
{"encryptionType", record.encryptionType},
{"signature", record.signature}};
j_records.push_back(j_record);
}
ofstream ofs(tableName + ".json");
ofs << j_records.dump(4) << endl;
ofs.close();
cout << "File output completed." << endl;
}