Initial release
[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 constant FORMAT_EXTENSION => {
9 CPP => 'cpp',
10 };
11
12 use Apache2::Access;
13 use Apache2::AuthzCaps qw/hascaps/;
14 use Apache2::RequestRec;
15 use Apache2::RequestIO;
16 use Apache2::Request;
17 use Apache2::Const qw/OK DECLINED/;
18 use Apache2::Log;
19 use Apache2::Upload;
20
21 use Cwd qw/cwd/;
22 use File::Basename qw/fileparse/;
23 use File::Temp qw/tempdir/;
24 use File::Copy qw/move/;
25 use YAML::Any qw/LoadFile DumpFile/;
26
27 sub 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
56 sub 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
77 1;
78 __END__
79 # Below is stub documentation for your module. You'd better edit it!
80
81 =head1 NAME
82
83 Gruntmaster::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
92 Stub documentation for Gruntmaster::Handlers, created by h2xs. It looks like the
93 author of the extension was negligent enough to leave the stub
94 unedited.
95
96 Blah blah blah.
97
98
99 =head1 SEE ALSO
100
101 Mention other useful documentation such as the documentation of
102 related modules or operating system documentation (such as man pages
103 in UNIX), or any relevant external documentation such as RFCs or
104 standards.
105
106 If you have a mailing list set up for your module, mention it here.
107
108 If you have a web site set up for your module, mention it here.
109
110 =head1 AUTHOR
111
112 Marius Gavrilescu, E<lt>marius@E<gt>
113
114 =head1 COPYRIGHT AND LICENSE
115
116 Copyright (C) 2013 by Marius Gavrilescu
117
118 This library is free software; you can redistribute it and/or modify
119 it under the same terms as Perl itself, either Perl version 5.18.1 or,
120 at your option, any later version of Perl 5 you may have available.
121
122
123 =cut
This page took 0.025639 seconds and 4 git commands to generate.