hydra/src/Hydra/lib/Hydra/Helper/CatalystUtils.pm

60 lines
1.2 KiB
Perl
Raw Normal View History

package Hydra::Helper::CatalystUtils;
use strict;
use Exporter;
2009-03-02 10:23:40 +00:00
use Readonly;
our @ISA = qw(Exporter);
2009-03-02 10:23:40 +00:00
our @EXPORT = qw(
getBuild error notFound
requireLogin requireProjectOwner
2009-03-02 10:23:40 +00:00
$pathCompRE $relPathRE
);
sub getBuild {
my ($c, $id) = @_;
my $build = $c->model('DB::Builds')->find($id);
return $build;
}
sub error {
my ($c, $msg) = @_;
$c->error($msg);
$c->detach; # doesn't return
}
2009-02-25 14:34:29 +00:00
sub notFound {
my ($c, $msg) = @_;
$c->response->status(404);
error($c, $msg);
}
sub requireLogin {
my ($c) = @_;
$c->flash->{afterLogin} = $c->request->uri;
$c->response->redirect($c->uri_for('/login'));
$c->detach; # doesn't return
}
sub requireProjectOwner {
my ($c, $project) = @_;
requireLogin($c) if !$c->user_exists;
error($c, "Only the project owner or the administrator can perform this operation.")
unless $c->check_user_roles('admin') || $c->user->username eq $project->owner->username;
}
2009-03-02 10:23:40 +00:00
# Security checking of filenames.
Readonly::Scalar our $pathCompRE => "(?:[A-Za-z0-9-\+][A-Za-z0-9-\+\._]*)";
Readonly::Scalar our $relPathRE => "(?:$pathCompRE(?:\/$pathCompRE)*)";
1;