Initial release
[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
8use constant FORMAT_EXTENSION => {
9 CPP => 'cpp',
10};
11
12use Apache2::Access;
13use Apache2::AuthzCaps qw/hascaps/;
14use Apache2::RequestRec;
15use Apache2::RequestIO;
16use Apache2::Request;
17use Apache2::Const qw/OK DECLINED/;
18use Apache2::Log;
19use Apache2::Upload;
20
21use Cwd qw/cwd/;
22use File::Basename qw/fileparse/;
23use File::Temp qw/tempdir/;
24use File::Copy qw/move/;
25use YAML::Any qw/LoadFile DumpFile/;
26
27sub submit{
28 my $r = shift;
29 chdir $r->dir_config('root');
30 my $req = Apache2::Request->new($r);
31 my ($problem, $format) = ($req->param('problem'), $req->param('prog_format'));
32 my $ext = FORMAT_EXTENSION->{$format};
33 my $prog = $req->upload('prog');
34
35 my $dir = tempdir;
36 $prog->link("$dir/prog.$ext");
37 DumpFile "$dir/meta.yml", {
38 files => {
39 prog => {
40 format => $format,
41 name => 'prog.cpp',
42 }
43 },
44 problem => $problem,
45 user => $r->user,
46 };
47
48 my $jobname = int rand 1_000_000_000;
49 $jobname = int rand 1_000_000_000 while -d "jobs/$jobname";
50 move $dir, "jobs/$jobname" or die $!;
51
52 $r->print("Job submitted");
53 OK
54}
55
56sub private{
57 my $r = shift;
58 my $dir = (fileparse $r->uri)[1];
59 my $user = $r->user;
60 chdir $r->dir_config('root') . $dir;
61
62 for my $requirement (map { $_->{requirement} } @{$r->requires}) {
63 my ($command, @args) = split ' ', $requirement;
64
65 given ($command){
66 when('admin-if-private'){
67 my $meta = LoadFile 'meta.yml';
68 return OK if !$meta->{private} || ($r->user && hascaps $r->user, 'gmadm')
69 }
70
71 }
72 }
73
74 DECLINED
75}
76
771;
78__END__
79# Below is stub documentation for your module. You'd better edit it!
80
81=head1 NAME
82
83Gruntmaster::Handlers - Perl extension for blah blah blah
84
85=head1 SYNOPSIS
86
87 use Gruntmaster::Handlers;
88 blah blah blah
89
90=head1 DESCRIPTION
91
92Stub documentation for Gruntmaster::Handlers, created by h2xs. It looks like the
93author of the extension was negligent enough to leave the stub
94unedited.
95
96Blah blah blah.
97
98
99=head1 SEE ALSO
100
101Mention other useful documentation such as the documentation of
102related modules or operating system documentation (such as man pages
103in UNIX), or any relevant external documentation such as RFCs or
104standards.
105
106If you have a mailing list set up for your module, mention it here.
107
108If you have a web site set up for your module, mention it here.
109
110=head1 AUTHOR
111
112Marius Gavrilescu, E<lt>marius@E<gt>
113
114=head1 COPYRIGHT AND LICENSE
115
116Copyright (C) 2013 by Marius Gavrilescu
117
118This library is free software; you can redistribute it and/or modify
119it under the same terms as Perl itself, either Perl version 5.18.1 or,
120at your option, any later version of Perl 5 you may have available.
121
122
123=cut
This page took 0.014999 seconds and 4 git commands to generate.