Add register and passwd
[gruntmaster-handlers.git] / lib / Gruntmaster / Handlers.pm
1 package Gruntmaster::Handlers;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 our $VERSION = '0.001';
7
8 use Apache2::Access;
9 use Apache2::Authen::Passphrase qw/pwcheck pwset USER_REGEX/;
10 use Apache2::AuthzCaps qw/hascaps/;
11 use Apache2::RequestRec;
12 use Apache2::RequestIO;
13 use Apache2::Request;
14 use Apache2::Const qw/OK DECLINED/;
15 use Apache2::Log;
16 use Apache2::Upload;
17
18 use Cwd qw/cwd/;
19 use File::Basename qw/fileparse/;
20 use File::Temp qw/tempdir/;
21 use File::Copy qw/move/;
22 use Gruntmaster::Data qw/contest_start contest_end push_job set_job_inmeta insert_user PUBLISH/;
23 use Gruntmaster::Page::Log;
24
25 sub 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
33 sub submit{
34 my $r = shift;
35 my $req = Apache2::Request->new($r);
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 }
59 }
60 };
61
62 PUBLISH 'jobs', $job;
63 $r->print("Job submitted");
64 OK
65 }
66
67 sub register{
68 my $r = shift;
69 my $req = Apache2::Request->new($r);
70 my ($username, $password, $confirm_password, $name, $email, $town, $university) = map {scalar $req->param($_)} 'username', 'password', 'confirm_password', 'name', 'email', 'town', 'university';
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;
76 pwset $username, $password;
77
78 insert_user $username, name => $name, email => $email, town => $town, university => $university;
79
80 aputs $r, 'Registered successfully';
81 }
82
83 sub passwd{
84 my $r = shift;
85 my $req = Apache2::Request->new($r);
86 my ($oldpass, $newpass, $confirm) = map {scalar $req->param($_)} 'password', 'new_password', 'confirm_new_password';
87
88 local $Apache2::Authen::Passphrase::rootdir = $r->dir_config('AuthenPassphraseRootdir');
89 return aputs $r, 'Incorrect password' unless eval { pwcheck $r->user, $oldpass; 1 };
90 return aputs $r, 'The two passwords do not match' unless $newpass eq $confirm;
91
92 pwset $r->user, $newpass;
93 aputs $r, 'Password changed successfully';
94 }
95
96 =begin comment
97
98 sub private{
99 my $r = shift;
100 my $dir = (fileparse $r->uri)[1];
101 my $user = $r->user;
102 chdir $r->dir_config('root') . $dir;
103
104 for my $requirement (map { $_->{requirement} } @{$r->requires}) {
105 my ($command, @args) = split ' ', $requirement;
106
107 given ($command){
108 when('admin-if-private'){
109 my $meta = LoadFile 'meta.yml';
110 return OK if !$meta->{private} || ($r->user && hascaps $r->user, 'gmadm')
111 }
112
113 }
114 }
115
116 DECLINED
117 }
118
119 =end comment
120
121 =cut
122
123 1;
124 __END__
125 # Below is stub documentation for your module. You'd better edit it!
126
127 =head1 NAME
128
129 Gruntmaster::Handlers - Perl extension for blah blah blah
130
131 =head1 SYNOPSIS
132
133 use Gruntmaster::Handlers;
134 blah blah blah
135
136 =head1 DESCRIPTION
137
138 Stub documentation for Gruntmaster::Handlers, created by h2xs. It looks like the
139 author of the extension was negligent enough to leave the stub
140 unedited.
141
142 Blah blah blah.
143
144
145 =head1 SEE ALSO
146
147 Mention other useful documentation such as the documentation of
148 related modules or operating system documentation (such as man pages
149 in UNIX), or any relevant external documentation such as RFCs or
150 standards.
151
152 If you have a mailing list set up for your module, mention it here.
153
154 If you have a web site set up for your module, mention it here.
155
156 =head1 AUTHOR
157
158 Marius Gavrilescu, E<lt>marius@E<gt>
159
160 =head1 COPYRIGHT AND LICENSE
161
162 Copyright (C) 2013 by Marius Gavrilescu
163
164 This library is free software; you can redistribute it and/or modify
165 it under the same terms as Perl itself, either Perl version 5.18.1 or,
166 at your option, any later version of Perl 5 you may have available.
167
168
169 =cut
This page took 0.028951 seconds and 4 git commands to generate.