-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_system.cpp
More file actions
162 lines (127 loc) · 4.13 KB
/
access_system.cpp
File metadata and controls
162 lines (127 loc) · 4.13 KB
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
#include <iostream>
#include "access_system.h"
//Implementing singleton
AccessSystem AccessSystem::sys = AccessSystem();
//There follows implementation of AccessSystem class
AccessSystem::AccessSystem() : logEnabled(true), emergencyStatus(false) {}
//The following two function enable and disable log to admin's console
void AccessSystem::enableLog() {
logEnabled = true;
}
void AccessSystem::disableLog() {
logEnabled = false;
}
void AccessSystem::activateEmergency() {
emergencyStatus = true;
if (logEnabled) {
std::cout << "Attention! Emergency situation! Please, leave the building immediately using emergency exits.\n";
}
}
void AccessSystem::deactivateEmergency() {
emergencyStatus = false;
if (logEnabled) {
std::cout << "Emergency is cancelled.\n";
}
}
bool AccessSystem::checkAccess(User* user, Room* room) {
if (emergencyStatus) {
if (logEnabled) {
std::cout << "Emergency! " << user->getName() + " got temporary access to room " + room->getName() << '\n';
}
return true;
}
bool resp = false;
//First check processes access levels
if (room->getAccessLevel() == AccessLevel::NO_LEVEL) {
resp = room->isAllowed(user);
} else {
resp = room->getAccessLevel() <= user->getAccessibleLevel();
}
//Second check is in ban list or in white list
if (resp) {
resp = !room->isBanned(user);
} else {
resp = room->isAllowed(user);
}
//Doing log if it's needed
if (logEnabled) {
if (resp) {
std::cout << user->getName() + " got access to room " + room->getName() << '\n';
} else {
std::cout << user->getName() + " didn't get access to room " + room->getName() << '\n';
}
}
return resp;
}
bool AccessSystem::grantCustomAccess(User* user, Room* room) {
//Disabling log temporary for internal access check
disableLog();
bool resp = checkAccess(user, room);
enableLog();
//Granting access only if needed
if (!resp) {
room->unbanUser(user);
room->addUser(user);
}
if (logEnabled) {
if (!resp) {
std::cout << user->getName() + " was granted a custom access to room " + room->getName() << '\n';
}
else {
std::cout << user->getName() + " already has an access to room " + room->getName() << '\n';
}
}
return !resp;
}
bool AccessSystem::withdrawAccess(User* user, Room* room) {
//Disabling log temporary for internal access check
disableLog();
bool resp = checkAccess(user, room);
enableLog();
//Withdrawing access only if it wasn't done yet
if (resp) {
room->removeUser(user);
room->banUser(user);
}
if (logEnabled) {
if (resp) {
std::cout << user->getName() + " was banned from room " + room->getName() << '\n';
}
else {
std::cout << user->getName() + " already doesn't have an access to room " + room->getName() << '\n';
}
}
return resp;
}
//Creates a user account directly from the admin console
//These accounts will not be written into the database (therefore they will be deleted after reboot of the Access System)
//Those are typically used for temporary pass cards that are to be used by guests or until the proper card is made
int AccessSystem::createUserAccount() {
std::cout << "Sign up\n";
std::cout << "Note: Access System can only register guest account.\n";
std::cout << "To sign up as student or professor, please contact IT department.\n\n";
std::cout << "Full name: ";
std::string name;
int age = 0, id;
std::getline(std::cin, name);
while (name.size() < 5 || name.find(" ") == -1) {
std::cout << "Full name should consist at least of given name and family name. Please, try again.\n";
std::cout << "Full name: ";
std::getline(std::cin, name);
}
std::cout << "Age: ";
std::cin >> age;
while (age < 5 || age > 130) {
std::cin.clear();
std::cin.ignore(INT32_MAX, '\n');
std::cout << "Please, enter proper age.\n";
std::cout << "Age: ";
std::cin >> age;
}
std::cin.clear();
std::cin.ignore(INT32_MAX, '\n');
Users::database.push_back(new Guest(name, age));
id = Users::database.size() - 1;
std::cout << "\nHello " << name << "! Your ID in system is " << id << '\n';
return id;
}