Generate user list and user pages
[gruntmaster-handlers.git] / lib / Gruntmaster / Handlers.pm
CommitLineData
9f02ff25
MG
1package Gruntmaster::Handlers;
2
3use 5.014000;
4use strict;
5use warnings;
6our $VERSION = '0.001';
7
9f02ff25 8use Apache2::Access;
b746d002 9use Apache2::Authen::Passphrase qw/pwcheck pwset USER_REGEX/;
9f02ff25
MG
10use Apache2::AuthzCaps qw/hascaps/;
11use Apache2::RequestRec;
12use Apache2::RequestIO;
13use Apache2::Request;
14use Apache2::Const qw/OK DECLINED/;
15use Apache2::Log;
16use Apache2::Upload;
17
18use Cwd qw/cwd/;
19use File::Basename qw/fileparse/;
20use File::Temp qw/tempdir/;
21use File::Copy qw/move/;
b746d002 22use Gruntmaster::Data qw/contest_start contest_end push_job set_job_inmeta insert_user PUBLISH/;
5855b3bd 23use Gruntmaster::Page::Log;
9f02ff25 24
b746d002
MG
25sub aputs{
26 my ($r, $str) = @_;
27 $r->set_content_length(length $str);
28 $r->puts($str);
29 $r->content_type('text/plain');
30 OK
31}
32
9f02ff25
MG
33sub submit{
34 my $r = shift;
9f02ff25 35 my $req = Apache2::Request->new($r);
5855b3bd
MG
36 my ($problem, $format, $contest, $private) = map {scalar $req->param($_)} 'problem', 'prog_format', 'contest', 'private';
37 my $prog;
38 $req->upload('prog')->slurp($prog);
39 die if defined $contest && $contest !~ /^\w+$/ ;
40 die if defined $contest && (time < contest_start $contest || time > contest_end $contest);
41
42 my $job = push_job (
43 date => time,
44 problem => $problem,
45 user => $r->user,
46 defined $private ? (private => $private) : (),
47 defined $contest ? (contest => $contest, private => 1) : (),
48 filesize => length $prog,
49 extension => Gruntmaster::Page::Log::FORMAT_EXTENSION->{$format},
50 );
51
52 set_job_inmeta $job, {
53 files => {
54 prog => {
55 format => $format,
56 name => 'prog.' . Gruntmaster::Page::Log::FORMAT_EXTENSION->{$format},
57 content => $prog,
58 }
9f02ff25 59 }
9f02ff25
MG
60 };
61
b16e6cc2 62 PUBLISH 'jobs', $job;
9f02ff25
MG
63 $r->print("Job submitted");
64 OK
65}
66
b746d002
MG
67sub register{
68 my $r = shift;
69 my $req = Apache2::Request->new($r);
f1df5335 70 my ($username, $password, $confirm_password, $name, $email, $phone, $town, $university, $level) = map { die if length > 200; $_ } map {scalar $req->param($_)} qw/username password confirm_password name email phone town university level/;
b746d002
MG
71
72 local $Apache2::Authen::Passphrase::rootdir = $r->dir_config('AuthenPassphraseRootdir');
73 return aputs $r, 'Bad username. Allowed characters are letters, digits and underscores, and the username must be between 2 and 20 characters long.' unless $username =~ USER_REGEX;
74 return aputs $r, 'Username already in use' if -e "$Apache2::Authen::Passphrase::rootdir/$username.yml";
75 return aputs $r, 'The two passwords do not match' unless $password eq $confirm_password;
f1df5335 76 return aputs $r, 'All fields are required' if grep { !length } $username, $password, $confirm_password, $name, $email, $phone, $town, $university, $level;
b746d002
MG
77 pwset $username, $password;
78
f1df5335 79 insert_user $username, name => $name, email => $email, phone => $phone, town => $town, university => $university, level => $level;
b746d002 80
35504454
MG
81 PUBLISH genpage => "us/index.html";
82 PUBLISH genpage => "us/$username.html";
b746d002
MG
83 aputs $r, 'Registered successfully';
84}
85
86sub passwd{
87 my $r = shift;
88 my $req = Apache2::Request->new($r);
89 my ($oldpass, $newpass, $confirm) = map {scalar $req->param($_)} 'password', 'new_password', 'confirm_new_password';
90
91 local $Apache2::Authen::Passphrase::rootdir = $r->dir_config('AuthenPassphraseRootdir');
92 return aputs $r, 'Incorrect password' unless eval { pwcheck $r->user, $oldpass; 1 };
93 return aputs $r, 'The two passwords do not match' unless $newpass eq $confirm;
94
95 pwset $r->user, $newpass;
96 aputs $r, 'Password changed successfully';
97}
98
5855b3bd
MG
99=begin comment
100
9f02ff25
MG
101sub private{
102 my $r = shift;
103 my $dir = (fileparse $r->uri)[1];
104 my $user = $r->user;
105 chdir $r->dir_config('root') . $dir;
106
107 for my $requirement (map { $_->{requirement} } @{$r->requires}) {
108 my ($command, @args) = split ' ', $requirement;
109
110 given ($command){
111 when('admin-if-private'){
112 my $meta = LoadFile 'meta.yml';
113 return OK if !$meta->{private} || ($r->user && hascaps $r->user, 'gmadm')
114 }
115
116 }
117 }
118
119 DECLINED
120}
121
5855b3bd
MG
122=end comment
123
124=cut
125
9f02ff25
MG
1261;
127__END__
128# Below is stub documentation for your module. You'd better edit it!
129
130=head1 NAME
131
132Gruntmaster::Handlers - Perl extension for blah blah blah
133
134=head1 SYNOPSIS
135
136 use Gruntmaster::Handlers;
137 blah blah blah
138
139=head1 DESCRIPTION
140
141Stub documentation for Gruntmaster::Handlers, created by h2xs. It looks like the
142author of the extension was negligent enough to leave the stub
143unedited.
144
145Blah blah blah.
146
147
148=head1 SEE ALSO
149
150Mention other useful documentation such as the documentation of
151related modules or operating system documentation (such as man pages
152in UNIX), or any relevant external documentation such as RFCs or
153standards.
154
155If you have a mailing list set up for your module, mention it here.
156
157If you have a web site set up for your module, mention it here.
158
159=head1 AUTHOR
160
161Marius Gavrilescu, E<lt>marius@E<gt>
162
163=head1 COPYRIGHT AND LICENSE
164
165Copyright (C) 2013 by Marius Gavrilescu
166
167This library is free software; you can redistribute it and/or modify
168it under the same terms as Perl itself, either Perl version 5.18.1 or,
169at your option, any later version of Perl 5 you may have available.
170
171
172=cut
This page took 0.019609 seconds and 4 git commands to generate.