-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsUser.h
381 lines (285 loc) · 6.43 KB
/
clsUser.h
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#pragma once
#include <iostream>
#include <string>
#include "clsPerson.h"
#include "clsString.h"
#include <vector>
#include <fstream>
#include "clsDate.h"
using namespace std;
class clsUser : public clsPerson
{
private:
enum enMode { EmptyMode = 0, UpdateMode = 1, AddNewMode = 2 };
enMode _Mode;
string _UserName;
string _Password;
int _Permissions;
bool _MarkedForDelete = false;
static clsUser _ConvertLinetoUserObject(string Line, string Seperator = "#//#")
{
vector<string> vUserData;
vUserData = clsString::SplitString(Line, Seperator);
return clsUser(enMode::UpdateMode, vUserData[0], vUserData[1], vUserData[2],
vUserData[3], vUserData[4], vUserData[5], stoi(vUserData[6]));
}
static string _ConverUserObjectToLine(clsUser User, string Seperator = "#//#")
{
string UserRecord = "";
UserRecord += User.FirstName + Seperator;
UserRecord += User.LastName + Seperator;
UserRecord += User.Email + Seperator;
UserRecord += User.Phone + Seperator;
UserRecord += User.UserName + Seperator;
UserRecord += User.Password + Seperator;
UserRecord += to_string(User.Permissions);
return UserRecord;
}
static vector <clsUser> _LoadUsersDataFromFile()
{
vector <clsUser> vUsers;
fstream MyFile;
MyFile.open("Users.txt", ios::in);//read Mode
if (MyFile.is_open())
{
string Line;
while (getline(MyFile, Line))
{
clsUser User = _ConvertLinetoUserObject(Line);
vUsers.push_back(User);
}
MyFile.close();
}
return vUsers;
}
static void _SaveUsersDataToFile(vector <clsUser> vUsers)
{
fstream MyFile;
MyFile.open("Users.txt", ios::out);//overwrite
string DataLine;
if (MyFile.is_open())
{
for (clsUser U : vUsers)
{
if (U.MarkedForDeleted() == false)
{
//we only write records that are not marked for delete.
DataLine = _ConverUserObjectToLine(U);
MyFile << DataLine << endl;
}
}
MyFile.close();
}
}
void _Update()
{
vector <clsUser> _vUsers;
_vUsers = _LoadUsersDataFromFile();
for (clsUser& U : _vUsers)
{
if (U.UserName == UserName)
{
U = *this;
break;
}
}
_SaveUsersDataToFile(_vUsers);
}
void _AddNew()
{
_AddDataLineToFile(_ConverUserObjectToLine(*this));
}
void _AddDataLineToFile(string stDataLine)
{
fstream MyFile;
MyFile.open("Users.txt", ios::out | ios::app);
if (MyFile.is_open())
{
MyFile << stDataLine << endl;
MyFile.close();
}
}
static clsUser _GetEmptyUserObject()
{
return clsUser(enMode::EmptyMode, "", "", "", "", "", "", 0);
}
string _GetUserData()
{
return "#//#" + UserName + "#//#" + Password + "#//#" + to_string(Permissions);
}
public:
enum enPermissions {
eAll = -1, pListClients = 1, pAddNewClient = 2, pDeleteClient = 4,
pUpdateClients = 8, pFindClient = 16, pTranactions = 32, pManageUsers = 64, pShowLogRegister = 128,
pCurrencyExchange = 256
};
clsUser(enMode Mode, string FirstName, string LastName,
string Email, string Phone, string UserName, string Password,
int Permissions) :
clsPerson(FirstName, LastName, Email, Phone)
{
_Mode = Mode;
_UserName = UserName;
_Password = Password;
_Permissions = Permissions;
}
bool IsEmpty()
{
return (_Mode == enMode::EmptyMode);
}
bool MarkedForDeleted()
{
return _MarkedForDelete;
}
string GetUserName()
{
return _UserName;
}
void SetUserName(string UserName)
{
_UserName = UserName;
}
__declspec(property(get = GetUserName, put = SetUserName)) string UserName;
void SetPassword(string Password)
{
_Password = Password;
}
string GetPassword()
{
return _Password;
}
__declspec(property(get = GetPassword, put = SetPassword)) string Password;
void SetPermissions(int Permissions)
{
_Permissions = Permissions;
}
int GetPermissions()
{
return _Permissions;
}
__declspec(property(get = GetPermissions, put = SetPermissions)) int Permissions;
static clsUser Find(string UserName)
{
fstream MyFile;
MyFile.open("Users.txt", ios::in);//read Mode
if (MyFile.is_open())
{
string Line;
while (getline(MyFile, Line))
{
clsUser User = _ConvertLinetoUserObject(Line);
if (User.UserName == UserName)
{
MyFile.close();
return User;
}
}
MyFile.close();
}
return _GetEmptyUserObject();
}
static clsUser Find(string UserName, string Password)
{
fstream MyFile;
MyFile.open("Users.txt", ios::in);//read Mode
if (MyFile.is_open())
{
string Line;
while (getline(MyFile, Line))
{
clsUser User = _ConvertLinetoUserObject(Line);
if (User.UserName == UserName && User.Password == Password)
{
MyFile.close();
return User;
}
}
MyFile.close();
}
return _GetEmptyUserObject();
}
enum enSaveResults { svFaildEmptyObject = 0, svSucceeded = 1, svFaildUserExists = 2 };
enSaveResults Save()
{
switch (_Mode)
{
case enMode::EmptyMode:
{
if (IsEmpty())
{
return enSaveResults::svFaildEmptyObject;
}
}
case enMode::UpdateMode:
{
_Update();
return enSaveResults::svSucceeded;
break;
}
case enMode::AddNewMode:
{
//This will add new record to file or database
if (clsUser::IsUserExist(_UserName))
{
return enSaveResults::svFaildUserExists;
}
else
{
_AddNew();
//We need to set the mode to update after add new
_Mode = enMode::UpdateMode;
return enSaveResults::svSucceeded;
}
break;
}
}
}
static bool IsUserExist(string UserName)
{
clsUser User = clsUser::Find(UserName);
return (!User.IsEmpty());
}
bool Delete()
{
vector <clsUser> _vUsers;
_vUsers = _LoadUsersDataFromFile();
for (clsUser& U : _vUsers)
{
if (U.UserName == _UserName)
{
U._MarkedForDelete = true;
break;
}
}
_SaveUsersDataToFile(_vUsers);
*this = _GetEmptyUserObject();
return true;
}
static clsUser GetAddNewUserObject(string UserName)
{
return clsUser(enMode::AddNewMode, "", "", "", "", UserName, "", 0);
}
static vector <clsUser> GetUsersList()
{
return _LoadUsersDataFromFile();
}
bool CheckAccessPermission(enPermissions Permission)
{
if (this->Permissions == enPermissions::eAll)
return true;
if ((Permission & this->Permissions) == Permission)
return true;
else
return false;
}
void CreateRegisterLogins()
{
fstream MyFile;
MyFile.open("LogFile.txt", ios::app | ios::out);
if (MyFile.is_open())
{
MyFile << clsDate::GetDateString() << " - " << clsDate::GetTimeNowString() << _GetUserData() << endl;
}
MyFile.close();
}
};