-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_control.ruleset
308 lines (288 loc) · 7.94 KB
/
access_control.ruleset
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
#include "gaia/logger.hpp"
#include "actions.hpp"
#include "enums.hpp"
#include "helpers.hpp"
using namespace gaia::access_control;
// Here we define our declarative rules.
ruleset access_control_ruleset
{
// A new scan is added.
//
// Reacts to:
// A new row in the scan table.
// Changes:
// scan.badge_scan
// scan.vehicle_entering
// scan.vehicle_departing
// scan.joining_wifi
// scan.leaving_wifi
// scan.face_scan
// scan.leaving
// person.face_signature
// person.stranger
// vehicle.license
// vehicle.vehicle_owner
// Creates:
// person
// vehicle
//
on_insert(S:scan)
{
auto scan_row = scan_t::get(S.gaia_id());
if (!scan_row.seen_who_person() && !scan_row.seen_license_vehicle())
{
auto stranger_row = helpers::insert_stranger(scan.face_signature);
if (scan.scan_type == enums::scan_table::e_scan_type::vehicle_entering)
{
helpers::insert_stranger_vehicle(stranger_row, scan.license);
}
actions::stranger_detected();
}
else
{
using namespace enums::scan_table;
switch (scan.scan_type)
{
case e_scan_type::badge:
{
scan.badge_scan = true;
break;
}
case e_scan_type::vehicle_entering:
{
scan.vehicle_entering = true;
break;
}
case e_scan_type::vehicle_departing:
{
scan.vehicle_departing = true;
break;
}
case e_scan_type::joining_wifi:
{
scan.joining_wifi = true;
break;
}
case e_scan_type::leaving_wifi:
{
scan.leaving_wifi = true;
break;
}
case e_scan_type::face:
{
scan.face_scan = true;
break;
}
case e_scan_type::leaving:
{
scan.leaving = true;
break;
}
}
}
}
// Handles when a row is updated and that update must be sent to the UI.
//
// Reacts to:
// An updated row in the scan table.
//
on_update(S:scan)
{
auto scan_row = scan_t::get(S.gaia_id());
if (scan_row.seen_who_person())
{
helpers::send_updated_scan(scan_row.seen_who_person(), S.scan_type);
}
else if (scan_row.seen_license_vehicle())
{
helpers::send_updated_scan(scan_row.seen_license_vehicle().owner(), S.scan_type);
}
}
// Handles when someone swipes their badge.
//
// Reacts to:
// scan.badge_scan
// Changes:
// person.badged
//
{
if (@badge_scan && !stranger)
{
badged = true;
}
}
// Handles when someone is joining a Wifi network.
//
// Reacts to:
// scan.joining_wifi
// Changes:
// person.on_wifi
//
{
if (@joining_wifi)
{
person.on_wifi = true;
}
}
// Handles when someone leaves a Wifi network.
//
// Reacts to:
// scan.leaving_wifi
// Changes:
// person.on_wifi
//
{
if (@leaving_wifi)
{
person.on_wifi = false;
}
}
// Handles an incoming license plate scan to the parking lot.
//
// Reacts to:
// scan.vehicle_entering
// Changes:
// person.parked
//
{
if (@vehicle_entering)
{
person.parked = true;
}
}
// Handles an outgoing license plate scan from the parking lot.
//
// Reacts to:
// scan.vehicle_departing
// Changes:
// person.parked
//
{
if (@scan.vehicle_departing)
{
person.parked = false;
}
}
// Handles when a person scans their face but lacks proper credentials.
//
// Reacts to:
// scan.face_scan
//
{
if (@scan.face_scan && !person.credentialed)
{
actions::base_credentials_required(person.person_id);
}
}
// Allow a person in depending on how they were scanned
// and if they entered during their allowed time.
//
// Reacts to:
// person.badged
// person.parked
// person.on_wifi
// Changes:
// person.credentialed
// person.admissible
//
{
if (@badged || @parked || @on_wifi)
{
person.credentialed = true;
if (helpers::time_is_between(helpers::get_time_now(),
entry_time, leave_time))
{
person.admissible = true;
}
}
}
// Handles face scans for employees who are currently admissible to a building or room.
//
// Reacts to:
// scan.face_scan
// Changes:
// person.entered
// person.inside
//
{
if (@scan.face_scan && employee && credentialed && admissible)
{
helpers::let_them_in(person.gaia_id(), scan.gaia_id());
}
}
// Handles face scans for visitors who have ongoing events in a room/building.
//
// Reacts to:
// scan.face_scan
// Changes:
// person.entered
// person.inside
//
{
if (@scan.face_scan && visitor && credentialed && admissible)
{
// Check to see if the scanned visitor has a scheuled event before
// letting them enter a room or the building.
if (helpers::person_has_event_now(person.gaia_id(),
scan_t::get(scan.gaia_id()).seen_in_room()))
{
helpers::let_them_in(person.gaia_id(), scan.gaia_id());
return;
}
if (scan_t::get(scan.gaia_id()).seen_in_room())
{
actions::not_this_room(person.person_id, scan->room.name, scan->room->building.name);
}
else
{
actions::not_this_building(person.person_id, scan->building.name);
}
}
}
// Handles face scans for people who are credentialed but are currently inadmissible.
//
// Reacts to:
// scan.face_scan
//
{
if (@scan.face_scan && person.credentialed && !person.admissible)
{
if (scan_t::get(scan.gaia_id()).seen_in_room())
{
actions::no_entry_right_now(person.person_id, scan->room.name, scan->room->building.name);
}
else
{
actions::not_this_building(person.person_id, scan->building.name);
}
}
}
// Handles people leaving rooms/buildings.
//
// Reacts to:
// scan.leaving
// Changes:
// person.entered
// person.inside
//
on_change(S:scan.leaving)
{
if (S.leaving)
{
auto seen_person = scan_t::get(S.gaia_id()).seen_who_person();
if (seen_person.inside_room())
{
// Explicitly remove the relationship between a person the room they are in.
helpers::disconnect_person_from_room(seen_person.gaia_id());
}
else
{
person.badged = false;
person.credentialed = false;
person.admissible = false;
// Explicitly remove the relationship between a person the building they are in.
helpers::disconnect_person_from_building(seen_person.gaia_id());
}
}
}
}