forked from ubc/webwork2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathww_purge_old_nonces
executable file
·92 lines (66 loc) · 2.19 KB
/
ww_purge_old_nonces
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
#!/usr/bin/env perl
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright © 2000-2012 The WeBWorK Project, http://webwork.maa.org
# $CVSHeader: webwork2/bin/ww_purge_old_nonces ,v 1.0 2012/05/10 wheeler Exp $
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of either: (a) the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version, or (b) the "Artistic License" which comes with this package.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
# Artistic License for more details.
################################################################################
=head1 NAME
ww_purge_old_nonces Delete nonce records from Key table for timestamps
that are older than ten seconds.
=head1 SYNOPSIS
ww_purge_old_nonces course
=head1 DESCRIPTION
Deletes nonce records from the Key table if their timestamps
are more than 10 seconds old.
=head1 OPTIONS
=item course
Course for which old nonces should be deleted.
=back
=cut
use strict;
use warnings;
BEGIN {
die "WEBWORK_ROOT not found in environment.\n"
unless exists $ENV{WEBWORK_ROOT};
}
use lib "$ENV{WEBWORK_ROOT}/lib";
use WeBWorK::CourseEnvironment;
use WeBWorK::DB;
sub usage {
print STDERR "usage: $0 course \n";
exit 1;
}
my ($course) = @ARGV;
usage() unless $course ;
my $ce = WeBWorK::CourseEnvironment->new({
webwork_dir => $ENV{WEBWORK_ROOT},
courseName => $course,
});
my $db = WeBWorK::DB->new($ce->{dbLayout});
my @errors;
my @listKeys = $db -> listKeys();
foreach my $user_id (@listKeys) {
my $Key;
eval { $Key = $db -> getKey($user_id);};
if ($@) { push @errors, "$user_id: ". $@ ;}
else {
if ($Key -> key eq "nonce" && $Key -> timestamp +10 < time()) {
eval {$db -> deleteKey($user_id);};
if ($@) { push @errors, "$user_id: ". $@ ;}
}
}
}
if (@errors) {
warn "The following errors occured:\n", map { "* $_\n" } @errors;
exit 1;
}