Skip to content

Commit

Permalink
policy: improve time ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
dufkan committed May 3, 2024
1 parent 1eb1e93 commit d6be731
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions meesign_core/bin/policy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:meta/meta.dart';

extension Range<T> on Comparable<T> {
bool within(T a, T b) => compareTo(a) >= 0 && compareTo(b) <= 0;
bool outside(T a, T b) => compareTo(a) <= 0 || compareTo(b) >= 0;
}

@immutable
Expand Down Expand Up @@ -140,20 +141,22 @@ bool? evalPolicy<T>(Map<String, dynamic> basePolicy, Map<String, dynamic> extPol
result = false;
}

if ((policy["from"] ?? '').isNotEmpty && (policy["to"] ?? '').isNotEmpty) {
try {
final Time from = Time.parse(policy['from']);
final Time to = Time.parse(policy['to']);
result = result && Time.now().within(from, to);
} on Exception catch (e) {
print("Error parsing time: $e");
try {
final after = Time.parse(policy["after"] ?? "00:00");
final before = Time.parse(policy["before"] ?? "23:59");
if (after.compareTo(before) > 0) {
result = result && Time.now().outside(before, after);
} else {
result = result && Time.now().within(after, before);
}
} on Exception catch (e) {
print("Error parsing after: $e");
}

if (result) {
return true;
}
if (policy["deny"] ?? false) {
if (policy["decline"] ?? false) {
return false;
}
return null;
Expand Down Expand Up @@ -246,13 +249,13 @@ void main(List<String> args) async {

groupRepository.approveAll(device.id);

Timer.periodic(Duration(seconds: 5), (_) {
Timer.periodic(Duration(seconds: 1), (_) {
fileRepository.decide(device.id, policyData);
});
Timer.periodic(Duration(seconds: 5), (_) {
Timer.periodic(Duration(seconds: 1), (_) {
challengeRepository.decide(device.id, policyData);
});
Timer.periodic(Duration(seconds: 5), (_) {
Timer.periodic(Duration(seconds: 1), (_) {
decryptRepository.decide(device.id, policyData);
});

Expand Down

0 comments on commit d6be731

Please sign in to comment.